1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
6 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <netinet/ip.h>
10 #include <netinet/ip6.h>
11 #include <netinet/udp.h>
18 static const char *pex_peer_id_str(const uint8_t *key
)
23 for (i
= 0; i
< 8; i
++)
24 sprintf(str
+ i
* 2, "%02x", key
[i
]);
29 static struct pex_hdr
*
30 pex_msg_init(struct network
*net
, uint8_t opcode
)
32 return __pex_msg_init(net
->config
.pubkey
, opcode
);
35 static struct pex_hdr
*
36 pex_msg_init_ext(struct network
*net
, uint8_t opcode
, bool ext
)
38 return __pex_msg_init_ext(net
->config
.pubkey
, net
->config
.auth_key
, opcode
, ext
);
41 static struct network_peer
*
42 pex_msg_peer(struct network
*net
, const uint8_t *id
)
44 struct network_peer
*peer
;
45 uint8_t key
[WG_KEY_LEN
] = {};
47 memcpy(key
, id
, PEX_ID_LEN
);
48 peer
= avl_find_ge_element(&net
->peers
.avl
, key
, peer
, node
.avl
);
49 if (!peer
|| memcmp(peer
->key
, key
, PEX_ID_LEN
) != 0) {
50 D_NET(net
, "can't find peer %s", pex_peer_id_str(id
));
58 pex_get_peer_addr(struct sockaddr_in6
*sin6
, struct network
*net
,
59 struct network_peer
*peer
)
61 *sin6
= (struct sockaddr_in6
){
62 .sin6_family
= AF_INET6
,
63 .sin6_addr
= peer
->local_addr
.in6
,
64 .sin6_port
= htons(peer
->pex_port
),
68 static void pex_msg_send(struct network
*net
, struct network_peer
*peer
)
70 struct sockaddr_in6 sin6
= {};
72 if (!peer
|| peer
== &net
->net_config
.local_host
->peer
||
76 pex_get_peer_addr(&sin6
, net
, peer
);
77 if (__pex_msg_send(net
->pex
.fd
.fd
, &sin6
, NULL
, 0) < 0)
78 D_PEER(net
, peer
, "pex_msg_send failed: %s", strerror(errno
));
81 static void pex_msg_send_ext(struct network
*net
, struct network_peer
*peer
,
82 struct sockaddr_in6
*addr
)
84 char addrbuf
[INET6_ADDRSTRLEN
];
87 return pex_msg_send(net
, peer
);
89 if (__pex_msg_send(-1, addr
, NULL
, 0) < 0)
90 D_NET(net
, "pex_msg_send_ext(%s) failed: %s",
91 inet_ntop(addr
->sin6_family
, (const void *)&addr
->sin6_addr
, addrbuf
,
97 pex_send_hello(struct network
*net
, struct network_peer
*peer
)
99 struct pex_hello
*data
;
101 pex_msg_init(net
, PEX_MSG_HELLO
);
102 data
= pex_msg_append(sizeof(*data
));
103 if (peer
->state
.endpoint
.sa
.sa_family
== AF_INET6
)
104 data
->flags
|= htons(PEER_EP_F_IPV6
);
105 if (network_get_local_addr(&data
->local_addr
, &peer
->state
.endpoint
))
108 pex_msg_send(net
, peer
);
112 pex_msg_add_peer_endpoint(struct network
*net
, struct network_peer
*peer
,
113 struct network_peer
*receiver
)
115 struct pex_peer_endpoint
*data
;
121 addr
= network_endpoint_addr(&peer
->state
.endpoint
, &len
);
122 port
= peer
->state
.endpoint
.in
.sin_port
;
124 flags
|= PEER_EP_F_IPV6
;
125 if (network_endpoint_addr_equal(&peer
->state
.endpoint
,
126 &receiver
->state
.endpoint
)) {
127 if (!peer
->state
.has_local_ep_addr
) {
128 D_PEER(net
, peer
, "can't send peer to %s, missing local address",
129 network_peer_name(receiver
));
133 addr
= &peer
->state
.local_ep_addr
;
134 port
= htons(peer
->port
);
135 flags
|= PEER_EP_F_LOCAL
;
138 data
= pex_msg_append(sizeof(*data
));
142 memcpy(data
->peer_id
, peer
->key
, sizeof(data
->peer_id
));
143 memcpy(data
->addr
, addr
, len
);
145 data
->flags
= htons(flags
);
146 D_PEER(net
, peer
, "send endpoint to %s", network_peer_name(receiver
));
152 network_pex_handle_endpoint_change(struct network
*net
, struct network_peer
*peer
)
154 struct network_peer
*cur
;
156 vlist_for_each_element(&net
->peers
, cur
, node
) {
157 if (cur
== peer
|| !cur
->state
.connected
)
160 pex_msg_init(net
, PEX_MSG_NOTIFY_PEERS
);
161 if (pex_msg_add_peer_endpoint(net
, peer
, cur
))
164 pex_msg_send(net
, cur
);
169 network_pex_host_send_endpoint_notify(struct network
*net
, struct network_pex_host
*host
)
182 union network_endpoint dest_ep
;
183 union network_addr local_addr
= {};
186 pex_msg_init_ext(net
, PEX_MSG_ENDPOINT_NOTIFY
, true);
188 memcpy(&dest_ep
, &host
->endpoint
, sizeof(dest_ep
));
190 /* work around issue with local address lookup for local broadcast */
191 if (host
->endpoint
.sa
.sa_family
== AF_INET
) {
192 uint8_t *data
= (uint8_t *)&dest_ep
.in
.sin_addr
;
197 network_get_local_addr(&local_addr
, &dest_ep
);
199 memset(&dest_ep
, 0, sizeof(dest_ep
));
200 dest_ep
.sa
.sa_family
= host
->endpoint
.sa
.sa_family
;
201 if (host
->endpoint
.sa
.sa_family
== AF_INET
) {
202 packet
.ipv4
.ip
= (struct ip
){
207 .ip_src
= local_addr
.in
,
208 .ip_dst
= host
->endpoint
.in
.sin_addr
,
210 dest_ep
.in
.sin_addr
= host
->endpoint
.in
.sin_addr
;
211 udp
= &packet
.ipv4
.udp
;
212 len
= sizeof(packet
.ipv4
);
214 packet
.ipv6
.ip
= (struct ip6_hdr
){
215 .ip6_flow
= htonl(6 << 28),
217 .ip6_nxt
= IPPROTO_UDP
,
218 .ip6_src
= local_addr
.in6
,
219 .ip6_dst
= host
->endpoint
.in6
.sin6_addr
,
221 dest_ep
.in6
.sin6_addr
= host
->endpoint
.in6
.sin6_addr
;
222 udp
= &packet
.ipv6
.udp
;
223 len
= sizeof(packet
.ipv6
);
226 udp
->uh_sport
= htons(net
->net_config
.local_host
->peer
.port
);
227 udp
->uh_dport
= host
->endpoint
.in6
.sin6_port
;
229 if (__pex_msg_send(-1, &dest_ep
, &packet
, len
) < 0)
230 D_NET(net
, "pex_msg_send_raw failed: %s", strerror(errno
));
235 network_pex_host_send_port_notify(struct network
*net
, struct network_pex_host
*host
)
237 struct pex_endpoint_port_notify
*data
;
239 if (!net
->stun
.port_ext
)
242 pex_msg_init_ext(net
, PEX_MSG_ENDPOINT_PORT_NOTIFY
, true);
244 data
= pex_msg_append(sizeof(*data
));
245 data
->port
= htons(net
->stun
.port_ext
);
247 __pex_msg_send(-1, &host
->endpoint
, NULL
, 0);
251 network_pex_host_request_update(struct network
*net
, struct network_pex_host
*host
)
253 char addrstr
[INET6_ADDRSTRLEN
];
254 uint64_t version
= 0;
256 host
->last_ping
= unet_gettime();
258 if (net
->net_data_len
)
259 version
= net
->net_data_version
;
261 D("request network data from host %s",
262 inet_ntop(host
->endpoint
.sa
.sa_family
,
263 (host
->endpoint
.sa
.sa_family
== AF_INET6
?
264 (const void *)&host
->endpoint
.in6
.sin6_addr
:
265 (const void *)&host
->endpoint
.in
.sin_addr
),
266 addrstr
, sizeof(addrstr
)));
268 if (!pex_msg_update_request_init(net
->config
.pubkey
, net
->config
.key
,
269 net
->config
.auth_key
, &host
->endpoint
,
273 __pex_msg_send(-1, &host
->endpoint
, NULL
, 0);
275 if (!net
->net_config
.local_host
)
278 network_pex_host_send_port_notify(net
, host
);
279 network_pex_host_send_endpoint_notify(net
, host
);
283 network_pex_free_host(struct network
*net
, struct network_pex_host
*host
)
285 struct network_pex
*pex
= &net
->pex
;
288 list_del(&host
->list
);
293 network_pex_request_update_cb(struct uloop_timeout
*t
)
295 struct network
*net
= container_of(t
, struct network
, pex
.request_update_timer
);
296 struct network_pex
*pex
= &net
->pex
;
297 struct network_pex_host
*host
, *tmp
;
298 uint64_t now
= unet_gettime();
300 uloop_timeout_set(t
, 500);
302 if (list_empty(&pex
->hosts
))
305 list_for_each_entry_safe(host
, tmp
, &pex
->hosts
, list
) {
306 if (host
->timeout
&& host
->timeout
< now
) {
307 network_pex_free_host(net
, host
);
311 if (host
->last_ping
+ 10 >= now
)
314 list_move_tail(&host
->list
, &pex
->hosts
);
315 network_pex_host_request_update(net
, host
);
319 void network_pex_init(struct network
*net
)
321 struct network_pex
*pex
= &net
->pex
;
323 memset(pex
, 0, sizeof(*pex
));
325 INIT_LIST_HEAD(&pex
->hosts
);
326 pex
->request_update_timer
.cb
= network_pex_request_update_cb
;
330 network_pex_query_hosts(struct network
*net
)
332 struct network_host
*host
;
337 pex_msg_init(net
, PEX_MSG_QUERY
);
339 avl_for_each_element(&net
->hosts
, host
, node
) {
340 struct network_peer
*peer
= &host
->peer
;
343 if ((net
->stun
.port_ext
&& host
== net
->net_config
.local_host
) ||
344 peer
->state
.connected
|| peer
->endpoint
)
347 id
= pex_msg_append(PEX_ID_LEN
);
351 memcpy(id
, peer
->key
, PEX_ID_LEN
);
358 rv
%= net
->hosts
.count
;
359 for (i
= 0; i
< 2; i
++) {
360 avl_for_each_element(&net
->hosts
, host
, node
) {
361 struct network_peer
*peer
= &host
->peer
;
368 if (host
== net
->net_config
.local_host
)
371 if (!peer
->state
.connected
)
374 D_PEER(net
, peer
, "send query for %d hosts", hosts
);
375 pex_msg_send(net
, peer
);
383 network_pex_send_ping(struct network
*net
, struct network_peer
*peer
)
385 if (peer
->state
.pinged
|| !peer
->state
.endpoint
.sa
.sa_family
)
388 pex_msg_init(net
, PEX_MSG_PING
);
389 pex_msg_send(net
, peer
);
390 peer
->state
.pinged
= true;
394 network_pex_send_update_request(struct network
*net
, struct network_peer
*peer
,
395 struct sockaddr_in6
*addr
)
397 union network_endpoint ep
= {};
398 uint64_t version
= 0;
401 memcpy(&ep
.in6
, addr
, sizeof(ep
.in6
));
403 pex_get_peer_addr(&ep
.in6
, net
, peer
);
405 if (net
->net_data_len
)
406 version
= net
->net_data_version
;
408 if (!pex_msg_update_request_init(net
->config
.pubkey
, net
->config
.key
,
409 net
->config
.auth_key
, &ep
,
413 pex_msg_send_ext(net
, peer
, addr
);
416 void network_pex_event(struct network
*net
, struct network_peer
*peer
,
419 if (!network_pex_active(&net
->pex
))
423 case PEX_EV_HANDSHAKE
:
424 pex_send_hello(net
, peer
);
425 if (net
->config
.type
== NETWORK_TYPE_DYNAMIC
)
426 network_pex_send_update_request(net
, peer
, NULL
);
428 case PEX_EV_ENDPOINT_CHANGE
:
429 network_pex_handle_endpoint_change(net
, peer
);
432 network_pex_query_hosts(net
);
435 network_pex_send_ping(net
, peer
);
441 network_pex_recv_hello(struct network
*net
, struct network_peer
*peer
,
442 const struct pex_hello
*data
, size_t len
)
444 char addrstr
[INET6_ADDRSTRLEN
];
448 if (len
< sizeof(*data
))
451 if (peer
->state
.has_local_ep_addr
&&
452 !memcmp(&peer
->state
.local_ep_addr
, data
->local_addr
, sizeof(data
->local_addr
)))
455 flags
= ntohs(data
->flags
);
456 af
= (flags
& PEER_EP_F_IPV6
) ? AF_INET6
: AF_INET
;
457 D_PEER(net
, peer
, "set local endpoint address to %s",
458 inet_ntop(af
, data
->local_addr
, addrstr
, sizeof(addrstr
)));
459 peer
->state
.has_local_ep_addr
= true;
460 memcpy(&peer
->state
.local_ep_addr
, data
->local_addr
, sizeof(data
->local_addr
));
464 network_pex_recv_peers(struct network
*net
, struct network_peer
*peer
,
465 const struct pex_peer_endpoint
*data
, size_t len
)
467 struct network_peer
*local
= &net
->net_config
.local_host
->peer
;
468 struct network_peer
*cur
;
470 for (; len
>= sizeof(*data
); len
-= sizeof(*data
), data
++) {
471 union network_endpoint
*ep
;
476 if (!memcmp(data
->peer_id
, &local
->key
, PEX_ID_LEN
)) {
477 network_stun_update_port(net
, false, ntohs(data
->port
));
481 cur
= pex_msg_peer(net
, data
->peer_id
);
482 if (!cur
|| cur
== peer
)
485 D_PEER(net
, peer
, "received peer address for %s",
486 network_peer_name(cur
));
487 flags
= ntohs(data
->flags
);
488 ep
= &cur
->state
.next_endpoint
;
489 ep
->sa
.sa_family
= (flags
& PEER_EP_F_IPV6
) ? AF_INET6
: AF_INET
;
490 addr
= network_endpoint_addr(ep
, &len
);
491 memcpy(addr
, data
->addr
, len
);
492 ep
->in
.sin_port
= data
->port
;
497 network_pex_recv_query(struct network
*net
, struct network_peer
*peer
,
498 const uint8_t *data
, size_t len
)
500 struct network_peer
*cur
;
503 pex_msg_init(net
, PEX_MSG_NOTIFY_PEERS
);
504 for (; len
>= 8; data
+= 8, len
-= 8) {
505 cur
= pex_msg_peer(net
, data
);
506 if (!cur
|| !cur
->state
.connected
)
509 if (!pex_msg_add_peer_endpoint(net
, cur
, peer
))
516 D_PEER(net
, peer
, "send query response with %d hosts", resp
);
517 pex_msg_send(net
, peer
);
521 network_pex_recv_ping(struct network
*net
, struct network_peer
*peer
)
523 time_t now
= time(NULL
);
525 if (peer
->state
.last_request
== now
)
528 peer
->state
.last_request
= now
;
529 pex_msg_init(net
, PEX_MSG_PONG
);
530 pex_msg_send(net
, peer
);
534 network_pex_recv_update_request(struct network
*net
, struct network_peer
*peer
,
535 const uint8_t *data
, size_t len
,
536 struct sockaddr_in6
*addr
)
538 struct pex_update_request
*req
= (struct pex_update_request
*)data
;
539 struct pex_msg_update_send_ctx ctx
= {};
540 uint64_t req_version
= be64_to_cpu(req
->cur_version
);
544 if (len
< sizeof(struct pex_update_request
))
547 if (net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
551 query_count
= &peer
->state
.num_net_queries
;
553 query_count
= &net
->num_net_queries
;
555 if (++*query_count
> 10)
558 D("receive update request, local version=%"PRIu64
", remote version=%"PRIu64
, net
->net_data_version
, req_version
);
560 if (req_version
>= net
->net_data_version
) {
561 struct pex_update_response_no_data
*res
;
563 pex_msg_init_ext(net
, PEX_MSG_UPDATE_RESPONSE_NO_DATA
, !!addr
);
564 res
= pex_msg_append(sizeof(*res
));
565 res
->req_id
= req
->req_id
;
566 res
->cur_version
= cpu_to_be64(net
->net_data_version
);
567 pex_msg_send_ext(net
, peer
, addr
);
570 if (req_version
> net
->net_data_version
)
571 network_pex_send_update_request(net
, peer
, addr
);
573 if (!peer
|| !net
->net_data_len
)
576 if (req_version
>= net
->net_data_version
)
579 pex_msg_update_response_init(&ctx
, net
->config
.pubkey
, net
->config
.auth_key
,
580 peer
->key
, !!addr
, (void *)data
,
581 net
->net_data
, net
->net_data_len
);
583 pex_msg_send_ext(net
, peer
, addr
);
584 done
= !pex_msg_update_response_continue(&ctx
);
589 network_pex_recv_update_response(struct network
*net
, const uint8_t *data
, size_t len
,
590 struct sockaddr_in6
*addr
, enum pex_opcode op
)
592 struct network_peer
*peer
;
594 int net_data_len
= 0;
595 uint64_t version
= 0;
596 bool no_prev_data
= !net
->net_data_len
;
598 if (net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
601 net_data
= pex_msg_update_response_recv(data
, len
, op
, &net_data_len
, &version
);
605 if (version
<= net
->net_data_version
) {
610 D_NET(net
, "received updated network data, len=%d", net_data_len
);
613 net
->net_data
= net_data
;
614 net
->net_data_len
= net_data_len
;
615 net
->net_data_version
= version
;
616 if (network_save_dynamic(net
) < 0)
619 uloop_timeout_set(&net
->reload_timer
, no_prev_data
? 1 : UNETD_DATA_UPDATE_DELAY
);
620 vlist_for_each_element(&net
->peers
, peer
, node
) {
621 if (!peer
->state
.connected
|| !peer
->pex_port
)
623 network_pex_send_update_request(net
, peer
, NULL
);
628 network_pex_recv(struct network
*net
, struct network_peer
*peer
, struct pex_hdr
*hdr
)
630 const void *data
= hdr
+ 1;
632 if (hdr
->version
!= 0)
635 D_PEER(net
, peer
, "PEX rx op=%d", hdr
->opcode
);
636 switch (hdr
->opcode
) {
638 network_pex_recv_hello(net
, peer
, data
, hdr
->len
);
640 case PEX_MSG_NOTIFY_PEERS
:
641 network_pex_recv_peers(net
, peer
, data
, hdr
->len
);
644 network_pex_recv_query(net
, peer
, data
, hdr
->len
);
647 network_pex_recv_ping(net
, peer
);
651 case PEX_MSG_UPDATE_REQUEST
:
652 network_pex_recv_update_request(net
, peer
, data
, hdr
->len
,
655 case PEX_MSG_UPDATE_RESPONSE
:
656 case PEX_MSG_UPDATE_RESPONSE_DATA
:
657 case PEX_MSG_UPDATE_RESPONSE_NO_DATA
:
658 network_pex_recv_update_response(net
, data
, hdr
->len
,
661 case PEX_MSG_ENDPOINT_NOTIFY
:
667 network_pex_fd_cb(struct uloop_fd
*fd
, unsigned int events
)
669 struct network
*net
= container_of(fd
, struct network
, pex
.fd
);
670 struct network_peer
*local
= &net
->net_config
.local_host
->peer
;
671 struct network_peer
*peer
;
672 struct sockaddr_in6 sin6
;
673 static char buf
[PEX_BUF_SIZE
];
674 struct pex_hdr
*hdr
= (struct pex_hdr
*)buf
;
678 socklen_t slen
= sizeof(sin6
);
680 len
= recvfrom(fd
->fd
, buf
, sizeof(buf
), 0, (struct sockaddr
*)&sin6
, &slen
);
688 D_NET(net
, "recvfrom failed: %s", strerror(errno
));
689 network_pex_close(net
);
696 hdr
= pex_rx_accept(buf
, len
, false);
700 peer
= pex_msg_peer(net
, hdr
->id
);
704 if (memcmp(&sin6
.sin6_addr
, &peer
->local_addr
.in6
, sizeof(sin6
.sin6_addr
)) != 0)
710 network_pex_recv(net
, peer
, hdr
);
714 void network_pex_create_host(struct network
*net
, union network_endpoint
*ep
,
715 unsigned int timeout
)
717 struct network_pex
*pex
= &net
->pex
;
718 struct network_pex_host
*host
;
719 uint64_t now
= unet_gettime();
720 bool new_host
= false;
722 list_for_each_entry(host
, &pex
->hosts
, list
) {
723 if (memcmp(&host
->endpoint
, ep
, sizeof(host
->endpoint
)) != 0)
726 if (host
->last_ping
+ 10 < now
) {
727 list_move_tail(&host
->list
, &pex
->hosts
);
728 network_pex_host_request_update(net
, host
);
733 host
= calloc(1, sizeof(*host
));
735 memcpy(&host
->endpoint
, ep
, sizeof(host
->endpoint
));
736 list_add_tail(&host
->list
, &pex
->hosts
);
740 if (timeout
&& (new_host
|| host
->timeout
))
741 host
->timeout
= timeout
+ unet_gettime();
745 network_pex_open_auth_connect(struct network
*net
)
747 struct network_pex
*pex
= &net
->pex
;
748 struct network_peer
*peer
;
749 struct blob_attr
*cur
;
752 if (net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
755 uloop_timeout_set(&pex
->request_update_timer
, 5000);
757 vlist_for_each_element(&net
->peers
, peer
, node
) {
758 union network_endpoint ep
= {};
760 if (!peer
->endpoint
|| peer
->dynamic
)
763 if (network_get_endpoint(&ep
, AF_UNSPEC
, peer
->endpoint
,
764 UNETD_GLOBAL_PEX_PORT
, 0) < 0)
767 ep
.in
.sin_port
= htons(UNETD_GLOBAL_PEX_PORT
);
768 network_pex_create_host(net
, &ep
, 0);
771 if (!net
->config
.auth_connect
)
774 blobmsg_for_each_attr(cur
, net
->config
.auth_connect
, rem
) {
775 union network_endpoint ep
= {};
777 if (network_get_endpoint(&ep
, AF_UNSPEC
, blobmsg_get_string(cur
),
778 UNETD_GLOBAL_PEX_PORT
, 0) < 0)
781 network_pex_create_host(net
, &ep
, 0);
786 int network_pex_open(struct network
*net
)
788 struct network_host
*local_host
= net
->net_config
.local_host
;
789 struct network_peer
*local
;
790 struct network_pex
*pex
= &net
->pex
;
791 struct sockaddr_in6 sin6
= {};
795 network_pex_open_auth_connect(net
);
797 if (!local_host
|| !local_host
->peer
.pex_port
)
800 local
= &local_host
->peer
;
801 fd
= socket(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
805 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
806 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
808 sin6
.sin6_family
= AF_INET6
;
809 memcpy(&sin6
.sin6_addr
, &local
->local_addr
.in6
,
810 sizeof(local
->local_addr
.in6
));
811 sin6
.sin6_port
= htons(local_host
->peer
.pex_port
);
813 if (bind(fd
, (struct sockaddr
*)&sin6
, sizeof(sin6
)) < 0) {
818 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
));
819 setsockopt(fd
, SOL_SOCKET
, SO_REUSEPORT
, &yes
, sizeof(yes
));
821 setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
822 network_name(net
), strlen(network_name(net
)));
826 pex
->fd
.cb
= network_pex_fd_cb
;
827 uloop_fd_add(&pex
->fd
, ULOOP_READ
);
836 void network_pex_close(struct network
*net
)
838 struct network_pex
*pex
= &net
->pex
;
839 struct network_pex_host
*host
, *tmp
;
840 uint64_t now
= unet_gettime();
842 uloop_timeout_cancel(&pex
->request_update_timer
);
843 list_for_each_entry_safe(host
, tmp
, &pex
->hosts
, list
) {
847 if (host
->last_active
+ UNETD_PEX_HOST_ACITVE_TIMEOUT
>= now
)
850 network_pex_free_host(net
, host
);
856 uloop_fd_delete(&pex
->fd
);
858 network_pex_init(net
);
861 void network_pex_free(struct network
*net
)
863 struct network_pex
*pex
= &net
->pex
;
864 struct network_pex_host
*host
, *tmp
;
866 list_for_each_entry_safe(host
, tmp
, &pex
->hosts
, list
)
867 network_pex_free_host(net
, host
);
870 static struct network
*
871 global_pex_find_network(const uint8_t *id
)
875 avl_for_each_element(&networks
, net
, node
) {
876 if (!memcmp(id
, net
->config
.auth_key
, PEX_ID_LEN
))
884 global_pex_set_active(struct network
*net
, struct sockaddr_in6
*addr
)
886 struct network_pex
*pex
= &net
->pex
;
887 struct network_pex_host
*host
;
889 list_for_each_entry(host
, &pex
->hosts
, list
) {
890 if (memcmp(&host
->endpoint
.in6
, addr
, sizeof(*addr
)) != 0)
893 host
->last_active
= unet_gettime();
898 global_pex_recv(void *msg
, size_t msg_len
, struct sockaddr_in6
*addr
)
901 struct pex_ext_hdr
*ehdr
;
902 struct network_peer
*peer
;
904 char buf
[INET6_ADDRSTRLEN
];
908 if (stun_msg_is_valid(msg
, msg_len
)) {
909 avl_for_each_element(&networks
, net
, node
)
910 network_stun_rx_packet(net
, msg
, msg_len
);
913 hdr
= pex_rx_accept(msg
, msg_len
, true);
917 ehdr
= (void *)(hdr
+ 1);
918 data
= (void *)(ehdr
+ 1);
920 if (hdr
->version
!= 0)
923 net
= global_pex_find_network(ehdr
->auth_id
);
924 if (!net
|| net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
927 *(uint64_t *)hdr
->id
^= pex_network_hash(net
->config
.auth_key
, ehdr
->nonce
);
929 global_pex_set_active(net
, addr
);
931 D("PEX global rx op=%d", hdr
->opcode
);
932 switch (hdr
->opcode
) {
934 case PEX_MSG_NOTIFY_PEERS
:
939 case PEX_MSG_UPDATE_REQUEST
:
940 peer
= pex_msg_peer(net
, hdr
->id
);
941 network_pex_recv_update_request(net
, peer
, data
, hdr
->len
,
944 case PEX_MSG_UPDATE_RESPONSE
:
945 case PEX_MSG_UPDATE_RESPONSE_DATA
:
946 case PEX_MSG_UPDATE_RESPONSE_NO_DATA
:
947 network_pex_recv_update_response(net
, data
, hdr
->len
, addr
, hdr
->opcode
);
949 case PEX_MSG_ENDPOINT_PORT_NOTIFY
:
950 if (hdr
->len
< sizeof(struct pex_endpoint_port_notify
))
952 case PEX_MSG_ENDPOINT_NOTIFY
:
953 peer
= pex_msg_peer(net
, hdr
->id
);
957 D_PEER(net
, peer
, "receive endpoint notification from %s",
958 inet_ntop(addr
->sin6_family
, network_endpoint_addr((void *)addr
, &addr_len
),
961 memcpy(&peer
->state
.next_endpoint
, addr
, sizeof(*addr
));
962 if (hdr
->opcode
== PEX_MSG_ENDPOINT_PORT_NOTIFY
) {
963 struct pex_endpoint_port_notify
*port
= data
;
964 union network_endpoint host_ep
= {
968 peer
->state
.next_endpoint
.in
.sin_port
= port
->port
;
969 if (net
->pex
.num_hosts
< NETWORK_PEX_HOSTS_LIMIT
)
970 network_pex_create_host(net
, &host_ep
, 120);
977 pex_recv_control(struct pex_msg_local_control
*msg
, int len
)
981 if (msg
->msg_type
!= 0)
984 net
= global_pex_find_network(msg
->auth_id
);
990 network_pex_create_host(net
, &msg
->ep
, msg
->timeout
);
993 int global_pex_open(const char *unix_path
)
995 struct sockaddr_in6 sin6
= {};
998 sin6
.sin6_family
= AF_INET6
;
999 sin6
.sin6_port
= htons(global_pex_port
);
1001 ret
= pex_open(&sin6
, sizeof(sin6
), global_pex_recv
, true);
1004 pex_unix_open(unix_path
, pex_recv_control
);