diff --git a/mud.c b/mud.c index 52b0e9b..03b6e7e 100644 --- a/mud.c +++ b/mud.c @@ -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 diff --git a/mud.h b/mud.h index 3156558..879afaf 100644 --- a/mud.h +++ b/mud.h @@ -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);