Add a global mud_set_conf()

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2020-01-16 17:34:31 +00:00
parent e3eb1f96a5
commit 3747aa7abd
2 changed files with 52 additions and 48 deletions

84
mud.c
View File

@@ -738,39 +738,11 @@ mud_set_key(struct mud *mud, unsigned char *key, size_t size)
return 0;
}
int
mud_set_tc(struct mud *mud, int tc)
{
if (tc != (tc & 255)) {
errno = EINVAL;
return -1;
}
mud->tc = tc;
return 0;
}
int
mud_set_loss_limit(struct mud *mud, unsigned loss)
{
if (loss > 100U) {
errno = EINVAL;
return -1;
}
mud->loss_limit = loss * 255U / 100U;
return 0;
}
static int
mud_set_msec(uint64_t *dst, unsigned long msec)
{
if (!msec) {
errno = EINVAL;
return -1;
}
if (!msec)
return 0;
const uint64_t x = msec * MUD_ONE_MSEC;
@@ -786,21 +758,49 @@ mud_set_msec(uint64_t *dst, unsigned long msec)
}
int
mud_set_keepalive(struct mud *mud, unsigned long msec)
mud_set_conf(struct mud *mud, struct mud_conf *conf)
{
return mud_set_msec(&mud->keepalive, msec);
}
uint64_t keepalive = mud->keepalive;
uint64_t timetolerance = mud->time_tolerance;
uint64_t kxtimeout = mud->keyx.timeout;
uint64_t losslimit = mud->loss_limit;
int tc = mud->tc;
int
mud_set_time_tolerance(struct mud *mud, unsigned long msec)
{
return mud_set_msec(&mud->time_tolerance, msec);
}
if (mud_set_msec(&keepalive, conf->keepalive))
return -1;
int
mud_set_keyx_timeout(struct mud *mud, unsigned long msec)
{
return mud_set_msec(&mud->keyx.timeout, msec);
if (mud_set_msec(&timetolerance, conf->timetolerance))
return -2;
if (mud_set_msec(&kxtimeout, conf->kxtimeout))
return -3;
if (conf->losslimit) {
if (conf->losslimit > 100) {
errno = ERANGE;
return -4;
}
losslimit = conf->losslimit * 255U / 100U;
}
if (conf->tc & 1) {
tc = conf->tc >> 1;
if (tc < 0 || tc > 255) {
errno = ERANGE;
return -5;
}
} else if (conf->tc) {
errno = EINVAL;
return -5;
}
mud->keepalive = keepalive;
mud->time_tolerance = timetolerance;
mud->keyx.timeout = kxtimeout;
mud->loss_limit = losslimit;
mud->tc = tc;
return 0;
}
size_t

16
mud.h
View File

@@ -22,6 +22,14 @@ struct mud_stat {
int setup;
};
struct mud_conf {
unsigned long keepalive;
unsigned long timetolerance;
unsigned long kxtimeout;
unsigned losslimit;
int tc;
};
struct mud_path {
enum mud_state state;
struct sockaddr_storage local_addr, addr, r_addr;
@@ -83,12 +91,8 @@ int mud_get_bad (struct mud *, struct mud_bad *);
int mud_set_key (struct mud *, unsigned char *, size_t);
int mud_get_key (struct mud *, unsigned char *, size_t *);
int mud_set_keepalive (struct mud *, unsigned long);
int mud_set_time_tolerance (struct mud *, unsigned long);
int mud_set_keyx_timeout (struct mud *, unsigned long);
int mud_set_loss_limit (struct mud *, unsigned);
int mud_set_tc (struct mud *, int);
int mud_set_aes (struct mud *);
int mud_set_aes (struct mud *);
int mud_set_conf (struct mud *, struct mud_conf *);
int mud_set_state (struct mud *, struct sockaddr *, enum mud_state,
unsigned long, unsigned long, unsigned long, unsigned char);