}
static void
-network_pex_host_request_update(struct network *net, struct network_pex_host *host)
+network_pex_host_send_endpoint_notify(struct network *net, struct network_pex_host *host)
{
union {
struct {
} ipv6;
} packet = {};
struct udphdr *udp;
- char addrstr[INET6_ADDRSTRLEN];
union network_endpoint dest_ep;
union network_addr local_addr = {};
- uint64_t version = 0;
int len;
- if (net->net_data_len)
- version = net->net_data_version;
-
- D("request network data from host %s",
- inet_ntop(host->endpoint.sa.sa_family,
- (host->endpoint.sa.sa_family == AF_INET6 ?
- (const void *)&host->endpoint.in6.sin6_addr :
- (const void *)&host->endpoint.in.sin_addr),
- addrstr, sizeof(addrstr)));
-
- if (!pex_msg_update_request_init(net->config.pubkey, net->config.key,
- net->config.auth_key, &host->endpoint,
- version, true))
- return;
-
- __pex_msg_send(-1, &host->endpoint, NULL, 0);
-
- if (!net->net_config.local_host)
- return;
-
pex_msg_init_ext(net, PEX_MSG_ENDPOINT_NOTIFY, true);
memcpy(&dest_ep, &host->endpoint, sizeof(dest_ep));
D_NET(net, "pex_msg_send_raw failed: %s", strerror(errno));
}
+
+static void
+network_pex_host_send_port_notify(struct network *net, struct network_pex_host *host)
+{
+ struct pex_endpoint_port_notify *data;
+
+ if (!net->stun.port_ext)
+ return;
+
+ pex_msg_init_ext(net, PEX_MSG_ENDPOINT_PORT_NOTIFY, true);
+
+ data = pex_msg_append(sizeof(*data));
+ data->port = htons(net->stun.port_ext);
+
+ __pex_msg_send(-1, &host->endpoint, NULL, 0);
+}
+
+static void
+network_pex_host_request_update(struct network *net, struct network_pex_host *host)
+{
+ char addrstr[INET6_ADDRSTRLEN];
+ uint64_t version = 0;
+
+ host->last_ping = unet_gettime();
+
+ if (net->net_data_len)
+ version = net->net_data_version;
+
+ D("request network data from host %s",
+ inet_ntop(host->endpoint.sa.sa_family,
+ (host->endpoint.sa.sa_family == AF_INET6 ?
+ (const void *)&host->endpoint.in6.sin6_addr :
+ (const void *)&host->endpoint.in.sin_addr),
+ addrstr, sizeof(addrstr)));
+
+ if (!pex_msg_update_request_init(net->config.pubkey, net->config.key,
+ net->config.auth_key, &host->endpoint,
+ version, true))
+ return;
+
+ __pex_msg_send(-1, &host->endpoint, NULL, 0);
+
+ if (!net->net_config.local_host)
+ return;
+
+ network_pex_host_send_port_notify(net, host);
+ network_pex_host_send_endpoint_notify(net, host);
+}
+
+static void
+network_pex_free_host(struct network *net, struct network_pex_host *host)
+{
+ struct network_pex *pex = &net->pex;
+
+ pex->num_hosts--;
+ list_del(&host->list);
+ free(host);
+}
+
static void
network_pex_request_update_cb(struct uloop_timeout *t)
{
struct network *net = container_of(t, struct network, pex.request_update_timer);
struct network_pex *pex = &net->pex;
- struct network_pex_host *host;
+ struct network_pex_host *host, *tmp;
+ uint64_t now = unet_gettime();
- uloop_timeout_set(t, 5000);
+ uloop_timeout_set(t, 500);
-retry:
if (list_empty(&pex->hosts))
return;
- host = list_first_entry(&pex->hosts, struct network_pex_host, list);
- if (host->timeout && host->timeout < unet_gettime()) {
- list_del(&host->list);
- free(host);
- goto retry;
- }
+ list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
+ if (host->timeout && host->timeout < now) {
+ network_pex_free_host(net, host);
+ continue;
+ }
+
+ if (host->last_ping + 10 >= now)
+ continue;
- list_move_tail(&host->list, &pex->hosts);
- network_pex_host_request_update(net, host);
+ list_move_tail(&host->list, &pex->hosts);
+ network_pex_host_request_update(net, host);
+ }
}
void network_pex_init(struct network *net)
network_pex_query_hosts(struct network *net)
{
struct network_host *host;
+ uint64_t now;
int rv = rand();
int hosts = 0;
int i;
struct network_peer *peer = &host->peer;
void *id;
- if (host == net->net_config.local_host ||
- peer->state.connected ||
- peer->endpoint)
+ if ((net->stun.port_ext && host == net->net_config.local_host) ||
+ peer->state.connected || peer->endpoint)
continue;
id = pex_msg_append(PEX_ID_LEN);
if (!hosts)
return;
+ now = unet_gettime();
rv %= net->hosts.count;
for (i = 0; i < 2; i++) {
avl_for_each_element(&net->hosts, host, node) {
if (host == net->net_config.local_host)
continue;
- if (!peer->state.connected)
+ if (!peer->state.connected ||
+ peer->state.last_query_sent + 15 >= now)
continue;
D_PEER(net, peer, "send query for %d hosts", hosts);
pex_msg_send(net, peer);
+ peer->state.last_query_sent = now;
return;
}
}
if (!network_pex_active(&net->pex))
return;
- if (peer)
- D_PEER(net, peer, "PEX event type=%d", ev);
- else
- D_NET(net, "PEX event type=%d", ev);
-
switch (ev) {
case PEX_EV_HANDSHAKE:
+ peer->state.last_query_sent = 0;
pex_send_hello(net, peer);
if (net->config.type == NETWORK_TYPE_DYNAMIC)
network_pex_send_update_request(net, peer, NULL);
void *addr;
int len;
- cur = pex_msg_peer(net, data->peer_id);
- if (!cur)
+ if (!memcmp(data->peer_id, &local->key, PEX_ID_LEN)) {
+ network_stun_update_port(net, false, ntohs(data->port));
continue;
+ }
- if (cur == peer || cur == local)
+ cur = pex_msg_peer(net, data->peer_id);
+ if (!cur || cur == peer)
continue;
D_PEER(net, peer, "received peer address for %s",
network_peer_name(cur));
flags = ntohs(data->flags);
- ep = &cur->state.next_endpoint;
+ ep = &cur->state.next_endpoint[ENDPOINT_TYPE_PEX];
ep->sa.sa_family = (flags & PEER_EP_F_IPV6) ? AF_INET6 : AF_INET;
addr = network_endpoint_addr(ep, &len);
memcpy(addr, data->addr, len);
if (!len)
continue;
- if (len < sizeof(*hdr))
- continue;
-
- hdr->len = ntohs(hdr->len);
- if (len - sizeof(hdr) < hdr->len)
+ hdr = pex_rx_accept(buf, len, false);
+ if (!hdr)
continue;
peer = pex_msg_peer(net, hdr->id);
{
struct network_pex *pex = &net->pex;
struct network_pex_host *host;
+ uint64_t now = unet_gettime();
bool new_host = false;
list_for_each_entry(host, &pex->hosts, list) {
if (memcmp(&host->endpoint, ep, sizeof(host->endpoint)) != 0)
continue;
- list_move_tail(&host->list, &pex->hosts);
+ if (host->last_ping + 10 < now) {
+ list_move_tail(&host->list, &pex->hosts);
+ network_pex_host_request_update(net, host);
+ }
goto out;
}
new_host = true;
memcpy(&host->endpoint, ep, sizeof(host->endpoint));
list_add_tail(&host->list, &pex->hosts);
+ pex->num_hosts++;
out:
if (timeout && (new_host || host->timeout))
host->timeout = timeout + unet_gettime();
- network_pex_host_request_update(net, host);
}
static void
if (!peer->endpoint || peer->dynamic)
continue;
- if (network_get_endpoint(&ep, peer->endpoint,
+ if (network_get_endpoint(&ep, AF_UNSPEC, peer->endpoint,
UNETD_GLOBAL_PEX_PORT, 0) < 0)
continue;
blobmsg_for_each_attr(cur, net->config.auth_connect, rem) {
union network_endpoint ep = {};
- if (network_get_endpoint(&ep, blobmsg_get_string(cur),
+ if (network_get_endpoint(&ep, AF_UNSPEC, blobmsg_get_string(cur),
UNETD_GLOBAL_PEX_PORT, 0) < 0)
continue;
{
struct network_pex *pex = &net->pex;
struct network_pex_host *host, *tmp;
+ uint64_t now = unet_gettime();
uloop_timeout_cancel(&pex->request_update_timer);
list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
if (host->timeout)
continue;
- list_del(&host->list);
- free(host);
+ if (host->last_active + UNETD_PEX_HOST_ACITVE_TIMEOUT >= now)
+ continue;
+
+ network_pex_free_host(net, host);
}
if (pex->fd.fd < 0)
struct network_pex *pex = &net->pex;
struct network_pex_host *host, *tmp;
- list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
- list_del(&host->list);
- free(host);
- }
+ list_for_each_entry_safe(host, tmp, &pex->hosts, list)
+ network_pex_free_host(net, host);
}
static struct network *
}
static void
-global_pex_recv(struct pex_hdr *hdr, struct sockaddr_in6 *addr)
+global_pex_set_active(struct network *net, struct sockaddr_in6 *addr)
{
- struct pex_ext_hdr *ehdr = (void *)(hdr + 1);
+ struct network_pex *pex = &net->pex;
+ struct network_pex_host *host;
+
+ list_for_each_entry(host, &pex->hosts, list) {
+ if (memcmp(&host->endpoint.in6, addr, sizeof(*addr)) != 0)
+ continue;
+
+ host->last_active = unet_gettime();
+ }
+}
+
+static void
+global_pex_recv(void *msg, size_t msg_len, struct sockaddr_in6 *addr)
+{
+ struct pex_hdr *hdr;
+ struct pex_ext_hdr *ehdr;
struct network_peer *peer;
struct network *net;
- void *data = (void *)(ehdr + 1);
char buf[INET6_ADDRSTRLEN];
+ void *data;
int addr_len;
+ int ep_idx = ENDPOINT_TYPE_ENDPOINT_NOTIFY;
+
+ if (stun_msg_is_valid(msg, msg_len)) {
+ avl_for_each_element(&networks, net, node)
+ network_stun_rx_packet(net, msg, msg_len);
+ }
+
+ hdr = pex_rx_accept(msg, msg_len, true);
+ if (!hdr)
+ return;
+
+ ehdr = (void *)(hdr + 1);
+ data = (void *)(ehdr + 1);
if (hdr->version != 0)
return;
*(uint64_t *)hdr->id ^= pex_network_hash(net->config.auth_key, ehdr->nonce);
+ global_pex_set_active(net, addr);
+
D("PEX global rx op=%d", hdr->opcode);
switch (hdr->opcode) {
case PEX_MSG_HELLO:
case PEX_MSG_UPDATE_RESPONSE_NO_DATA:
network_pex_recv_update_response(net, data, hdr->len, addr, hdr->opcode);
break;
+ case PEX_MSG_ENDPOINT_PORT_NOTIFY:
+ if (hdr->len < sizeof(struct pex_endpoint_port_notify))
+ break;
+
+ ep_idx = ENDPOINT_TYPE_ENDPOINT_PORT_NOTIFY;
+ fallthrough;
case PEX_MSG_ENDPOINT_NOTIFY:
peer = pex_msg_peer(net, hdr->id);
if (!peer)
break;
- if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
- struct sockaddr_in *sin = (struct sockaddr_in *)addr;
- struct in_addr in = *(struct in_addr *)&addr->sin6_addr.s6_addr[12];
- int port = addr->sin6_port;
-
- memset(addr, 0, sizeof(*addr));
- sin->sin_port = port;
- sin->sin_family = AF_INET;
- sin->sin_addr = in;
- }
-
D_PEER(net, peer, "receive endpoint notification from %s",
inet_ntop(addr->sin6_family, network_endpoint_addr((void *)addr, &addr_len),
buf, sizeof(buf)));
- memcpy(&peer->state.next_endpoint, addr, sizeof(*addr));
+ memcpy(&peer->state.next_endpoint[ep_idx], addr, sizeof(*addr));
+ if (hdr->opcode == PEX_MSG_ENDPOINT_PORT_NOTIFY) {
+ struct pex_endpoint_port_notify *port = data;
+ union network_endpoint host_ep = {
+ .in6 = *addr
+ };
+
+ peer->state.next_endpoint[ep_idx].in.sin_port = port->port;
+ if (net->pex.num_hosts < NETWORK_PEX_HOSTS_LIMIT)
+ network_pex_create_host(net, &host_ep, 120);
+ }
break;
}
}
-int global_pex_open(void)
+static void
+pex_recv_control(struct pex_msg_local_control *msg, int len)
+{
+ struct network *net;
+
+ if (msg->msg_type != 0)
+ return;
+
+ net = global_pex_find_network(msg->auth_id);
+ if (!net)
+ return;
+
+ if (!msg->timeout)
+ msg->timeout = 60;
+ network_pex_create_host(net, &msg->ep, msg->timeout);
+}
+
+int global_pex_open(const char *unix_path)
{
struct sockaddr_in6 sin6 = {};
+ int ret;
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(global_pex_port);
- return pex_open(&sin6, sizeof(sin6), global_pex_recv, true);
+ ret = pex_open(&sin6, sizeof(sin6), global_pex_recv, true);
+
+ if (unix_path)
+ pex_unix_open(unix_path, pex_recv_control);
+
+ return ret;
}