62
mud.c
62
mud.c
@@ -85,7 +85,6 @@ struct mud_ipaddr {
|
|||||||
|
|
||||||
struct mud_path {
|
struct mud_path {
|
||||||
struct {
|
struct {
|
||||||
unsigned active : 1;
|
|
||||||
unsigned backup : 1;
|
unsigned backup : 1;
|
||||||
} state;
|
} state;
|
||||||
struct mud_ipaddr local_addr;
|
struct mud_ipaddr local_addr;
|
||||||
@@ -542,30 +541,13 @@ mud_path(struct mud *mud, struct mud_ipaddr *local_addr,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
mud_peer(struct mud *mud, const char *name, const char *host, int port,
|
mud_peer(struct mud *mud, const char *host, int port)
|
||||||
int backup)
|
|
||||||
{
|
{
|
||||||
if (!host || !port) {
|
if (!host || !port) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
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;
|
struct sockaddr_storage addr;
|
||||||
|
|
||||||
if (mud_addrinfo(&addr, host, port))
|
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);
|
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)
|
if (!path)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
path->state.active = 1;
|
path->state.backup = !!backup;
|
||||||
|
|
||||||
if (name)
|
|
||||||
path->state.backup = !!backup;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1112,7 +1121,7 @@ mud_packet_recv(struct mud *mud, struct mud_path *path,
|
|||||||
memcpy(path->conf.kiss, packet->data.conf.kiss,
|
memcpy(path->conf.kiss, packet->data.conf.kiss,
|
||||||
sizeof(path->conf.kiss));
|
sizeof(path->conf.kiss));
|
||||||
path->conf.mtu.remote = mud_read48(packet->data.conf.mtu);
|
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,
|
if (!memcmp(mud->crypto.public.local,
|
||||||
packet->data.conf.public.remote, MUD_PUB_SIZE)) {
|
packet->data.conf.public.remote, MUD_PUB_SIZE)) {
|
||||||
mud_keyx(mud, packet->data.conf.public.local,
|
mud_keyx(mud, packet->data.conf.public.local,
|
||||||
@@ -1243,9 +1252,6 @@ mud_update(struct mud *mud)
|
|||||||
|
|
||||||
if (path) {
|
if (path) {
|
||||||
for (; path; path = path->next) {
|
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))
|
if (update_keyx || mud_timeout(now, path->recv_time, mud->send_timeout + MUD_ONE_SEC))
|
||||||
path->conf.remote = 0;
|
path->conf.remote = 0;
|
||||||
|
|
||||||
|
|||||||
4
mud.h
4
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_tc (struct mud *, int);
|
||||||
int mud_set_aes (struct mud *);
|
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_recv (struct mud *, void *, size_t);
|
||||||
int mud_send (struct mud *, const void *, size_t, int);
|
int mud_send (struct mud *, const void *, size_t, int);
|
||||||
|
|||||||
Reference in New Issue
Block a user