diff --git a/mud.c b/mud.c index 02e5c62..03abd72 100644 --- a/mud.c +++ b/mud.c @@ -85,7 +85,6 @@ struct mud_ipaddr { struct mud_path { struct { - unsigned active : 1; unsigned backup : 1; } state; struct mud_ipaddr local_addr; @@ -542,30 +541,13 @@ mud_path(struct mud *mud, struct mud_ipaddr *local_addr, } int -mud_peer(struct mud *mud, const char *name, const char *host, int port, - int backup) +mud_peer(struct mud *mud, const char *host, int port) { if (!host || !port) { errno = EINVAL; return -1; } - struct mud_ipaddr ipaddr, *local_addr = NULL; - - if (name) { - if (inet_pton(AF_INET, name, &ipaddr.ip.v4) == 1) { - ipaddr.family = AF_INET; - } else { - if (inet_pton(AF_INET6, name, &ipaddr.ip.v6) == 1) { - ipaddr.family = AF_INET6; - } else { - errno = EINVAL; - return -1; - } - } - local_addr = &ipaddr; - } - struct sockaddr_storage addr; if (mud_addrinfo(&addr, host, port)) @@ -573,15 +555,42 @@ mud_peer(struct mud *mud, const char *name, const char *host, int port, mud_unmapv4((struct sockaddr *)&addr); - struct mud_path *path = mud_path(mud, local_addr, (struct sockaddr *)&addr, 1); + if (mud->peer) + free(mud->peer); + + mud->peer = mud_path(mud, NULL, (struct sockaddr *)&addr, 1); + + return -!mud->peer; +} + +int +mud_add_path(struct mud *mud, const char *name, int backup) +{ + if (!name || !mud->peer) { + errno = EINVAL; + return -1; + } + + struct mud_ipaddr ipaddr; + + if (inet_pton(AF_INET, name, &ipaddr.ip.v4) == 1) { + ipaddr.family = AF_INET; + } else { + if (inet_pton(AF_INET6, name, &ipaddr.ip.v6) == 1) { + ipaddr.family = AF_INET6; + } else { + errno = EINVAL; + return -1; + } + } + + struct mud_path *path = mud_path(mud, &ipaddr, + (struct sockaddr *)&mud->peer->addr, 1); if (!path) return -1; - path->state.active = 1; - - if (name) - path->state.backup = !!backup; + path->state.backup = !!backup; return 0; } @@ -1112,7 +1121,7 @@ mud_packet_recv(struct mud *mud, struct mud_path *path, memcpy(path->conf.kiss, packet->data.conf.kiss, sizeof(path->conf.kiss)); path->conf.mtu.remote = mud_read48(packet->data.conf.mtu); - if (path->state.active || mud->peer) { + if (mud->peer) { if (!memcmp(mud->crypto.public.local, packet->data.conf.public.remote, MUD_PUB_SIZE)) { mud_keyx(mud, packet->data.conf.public.local, @@ -1243,9 +1252,6 @@ mud_update(struct mud *mud) if (path) { for (; path; path = path->next) { - if (!path->state.active) - continue; - if (update_keyx || mud_timeout(now, path->recv_time, mud->send_timeout + MUD_ONE_SEC)) path->conf.remote = 0; diff --git a/mud.h b/mud.h index a1c40e8..ff4ab7d 100644 --- a/mud.h +++ b/mud.h @@ -21,7 +21,9 @@ int mud_set_time_tolerance_sec (struct mud *, unsigned); int mud_set_tc (struct mud *, int); int mud_set_aes (struct mud *); -int mud_peer (struct mud *, const char *, const char *, int, int); +int mud_peer (struct mud *, const char *, int); + +int mud_add_path (struct mud *, const char *, int); int mud_recv (struct mud *, void *, size_t); int mud_send (struct mud *, const void *, size_t, int);