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_request_update(struct network
*net
, struct network_pex_host
*host
)
182 char addrstr
[INET6_ADDRSTRLEN
];
183 union network_endpoint dest_ep
;
184 union network_addr local_addr
= {};
185 uint64_t version
= 0;
188 if (net
->net_data_len
)
189 version
= net
->net_data_version
;
191 D("request network data from host %s",
192 inet_ntop(host
->endpoint
.sa
.sa_family
,
193 (host
->endpoint
.sa
.sa_family
== AF_INET6
?
194 (const void *)&host
->endpoint
.in6
.sin6_addr
:
195 (const void *)&host
->endpoint
.in
.sin_addr
),
196 addrstr
, sizeof(addrstr
)));
198 if (!pex_msg_update_request_init(net
->config
.pubkey
, net
->config
.key
,
199 net
->config
.auth_key
, &host
->endpoint
,
203 __pex_msg_send(-1, &host
->endpoint
, NULL
, 0);
205 if (!net
->net_config
.local_host
)
208 pex_msg_init_ext(net
, PEX_MSG_ENDPOINT_NOTIFY
, true);
210 memcpy(&dest_ep
, &host
->endpoint
, sizeof(dest_ep
));
212 /* work around issue with local address lookup for local broadcast */
213 if (host
->endpoint
.sa
.sa_family
== AF_INET
) {
214 uint8_t *data
= (uint8_t *)&dest_ep
.in
.sin_addr
;
219 network_get_local_addr(&local_addr
, &dest_ep
);
221 memset(&dest_ep
, 0, sizeof(dest_ep
));
222 dest_ep
.sa
.sa_family
= host
->endpoint
.sa
.sa_family
;
223 if (host
->endpoint
.sa
.sa_family
== AF_INET
) {
224 packet
.ipv4
.ip
= (struct ip
){
229 .ip_src
= local_addr
.in
,
230 .ip_dst
= host
->endpoint
.in
.sin_addr
,
232 dest_ep
.in
.sin_addr
= host
->endpoint
.in
.sin_addr
;
233 udp
= &packet
.ipv4
.udp
;
234 len
= sizeof(packet
.ipv4
);
236 packet
.ipv6
.ip
= (struct ip6_hdr
){
237 .ip6_flow
= htonl(6 << 28),
239 .ip6_nxt
= IPPROTO_UDP
,
240 .ip6_src
= local_addr
.in6
,
241 .ip6_dst
= host
->endpoint
.in6
.sin6_addr
,
243 dest_ep
.in6
.sin6_addr
= host
->endpoint
.in6
.sin6_addr
;
244 udp
= &packet
.ipv6
.udp
;
245 len
= sizeof(packet
.ipv6
);
248 udp
->uh_sport
= htons(net
->net_config
.local_host
->peer
.port
);
249 udp
->uh_dport
= host
->endpoint
.in6
.sin6_port
;
251 if (__pex_msg_send(-1, &dest_ep
, &packet
, len
) < 0)
252 D_NET(net
, "pex_msg_send_raw failed: %s", strerror(errno
));
256 network_pex_request_update_cb(struct uloop_timeout
*t
)
258 struct network
*net
= container_of(t
, struct network
, pex
.request_update_timer
);
259 struct network_pex
*pex
= &net
->pex
;
260 struct network_pex_host
*host
;
262 uloop_timeout_set(t
, 5000);
264 if (list_empty(&pex
->hosts
))
267 host
= list_first_entry(&pex
->hosts
, struct network_pex_host
, list
);
268 list_move_tail(&host
->list
, &pex
->hosts
);
269 network_pex_host_request_update(net
, host
);
272 void network_pex_init(struct network
*net
)
274 struct network_pex
*pex
= &net
->pex
;
276 memset(pex
, 0, sizeof(*pex
));
278 INIT_LIST_HEAD(&pex
->hosts
);
279 pex
->request_update_timer
.cb
= network_pex_request_update_cb
;
283 network_pex_query_hosts(struct network
*net
)
285 struct network_host
*host
;
290 pex_msg_init(net
, PEX_MSG_QUERY
);
292 avl_for_each_element(&net
->hosts
, host
, node
) {
293 struct network_peer
*peer
= &host
->peer
;
296 if (host
== net
->net_config
.local_host
||
297 peer
->state
.connected
||
301 id
= pex_msg_append(PEX_ID_LEN
);
305 memcpy(id
, peer
->key
, PEX_ID_LEN
);
312 rv
%= net
->hosts
.count
;
313 for (i
= 0; i
< 2; i
++) {
314 avl_for_each_element(&net
->hosts
, host
, node
) {
315 struct network_peer
*peer
= &host
->peer
;
322 if (host
== net
->net_config
.local_host
)
325 if (!peer
->state
.connected
)
328 D_PEER(net
, peer
, "send query for %d hosts", hosts
);
329 pex_msg_send(net
, peer
);
337 network_pex_send_ping(struct network
*net
, struct network_peer
*peer
)
339 pex_msg_init(net
, PEX_MSG_PING
);
340 pex_msg_send(net
, peer
);
344 network_pex_send_update_request(struct network
*net
, struct network_peer
*peer
,
345 struct sockaddr_in6
*addr
)
347 union network_endpoint ep
= {};
348 uint64_t version
= 0;
351 memcpy(&ep
.in6
, addr
, sizeof(ep
.in6
));
353 pex_get_peer_addr(&ep
.in6
, net
, peer
);
355 if (net
->net_data_len
)
356 version
= net
->net_data_version
;
358 if (!pex_msg_update_request_init(net
->config
.pubkey
, net
->config
.key
,
359 net
->config
.auth_key
, &ep
,
363 pex_msg_send_ext(net
, peer
, addr
);
366 void network_pex_event(struct network
*net
, struct network_peer
*peer
,
369 if (!network_pex_active(&net
->pex
))
373 D_PEER(net
, peer
, "PEX event type=%d", ev
);
375 D_NET(net
, "PEX event type=%d", ev
);
378 case PEX_EV_HANDSHAKE
:
379 pex_send_hello(net
, peer
);
380 if (net
->config
.type
== NETWORK_TYPE_DYNAMIC
)
381 network_pex_send_update_request(net
, peer
, NULL
);
383 case PEX_EV_ENDPOINT_CHANGE
:
384 network_pex_handle_endpoint_change(net
, peer
);
387 network_pex_query_hosts(net
);
390 network_pex_send_ping(net
, peer
);
396 network_pex_recv_hello(struct network
*net
, struct network_peer
*peer
,
397 const struct pex_hello
*data
, size_t len
)
399 char addrstr
[INET6_ADDRSTRLEN
];
403 if (len
< sizeof(*data
))
406 if (peer
->state
.has_local_ep_addr
&&
407 !memcmp(&peer
->state
.local_ep_addr
, data
->local_addr
, sizeof(data
->local_addr
)))
410 flags
= ntohs(data
->flags
);
411 af
= (flags
& PEER_EP_F_IPV6
) ? AF_INET6
: AF_INET
;
412 D_PEER(net
, peer
, "set local endpoint address to %s",
413 inet_ntop(af
, data
->local_addr
, addrstr
, sizeof(addrstr
)));
414 peer
->state
.has_local_ep_addr
= true;
415 memcpy(&peer
->state
.local_ep_addr
, data
->local_addr
, sizeof(data
->local_addr
));
419 network_pex_recv_peers(struct network
*net
, struct network_peer
*peer
,
420 const struct pex_peer_endpoint
*data
, size_t len
)
422 struct network_peer
*local
= &net
->net_config
.local_host
->peer
;
423 struct network_peer
*cur
;
425 for (; len
>= sizeof(*data
); len
-= sizeof(*data
), data
++) {
426 union network_endpoint
*ep
;
431 cur
= pex_msg_peer(net
, data
->peer_id
);
435 if (cur
== peer
|| cur
== local
)
438 D_PEER(net
, peer
, "received peer address for %s",
439 network_peer_name(cur
));
440 flags
= ntohs(data
->flags
);
441 ep
= &cur
->state
.next_endpoint
;
442 ep
->sa
.sa_family
= (flags
& PEER_EP_F_IPV6
) ? AF_INET6
: AF_INET
;
443 addr
= network_endpoint_addr(ep
, &len
);
444 memcpy(addr
, data
->addr
, len
);
445 ep
->in
.sin_port
= data
->port
;
450 network_pex_recv_query(struct network
*net
, struct network_peer
*peer
,
451 const uint8_t *data
, size_t len
)
453 struct network_peer
*cur
;
456 pex_msg_init(net
, PEX_MSG_NOTIFY_PEERS
);
457 for (; len
>= 8; data
+= 8, len
-= 8) {
458 cur
= pex_msg_peer(net
, data
);
459 if (!cur
|| !cur
->state
.connected
)
462 if (!pex_msg_add_peer_endpoint(net
, cur
, peer
))
469 D_PEER(net
, peer
, "send query response with %d hosts", resp
);
470 pex_msg_send(net
, peer
);
474 network_pex_recv_ping(struct network
*net
, struct network_peer
*peer
)
476 time_t now
= time(NULL
);
478 if (peer
->state
.last_request
== now
)
481 peer
->state
.last_request
= now
;
482 pex_msg_init(net
, PEX_MSG_PONG
);
483 pex_msg_send(net
, peer
);
487 network_pex_recv_update_request(struct network
*net
, struct network_peer
*peer
,
488 const uint8_t *data
, size_t len
,
489 struct sockaddr_in6
*addr
)
491 struct pex_update_request
*req
= (struct pex_update_request
*)data
;
492 struct pex_msg_update_send_ctx ctx
= {};
493 uint64_t req_version
= be64_to_cpu(req
->cur_version
);
497 if (len
< sizeof(struct pex_update_request
))
500 if (net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
504 query_count
= &peer
->state
.num_net_queries
;
506 query_count
= &net
->num_net_queries
;
508 if (++*query_count
> 10)
511 D("receive update request, local version=%"PRIu64
", remote version=%"PRIu64
, net
->net_data_version
, req_version
);
513 if (req_version
>= net
->net_data_version
) {
514 struct pex_update_response_no_data
*res
;
516 pex_msg_init_ext(net
, PEX_MSG_UPDATE_RESPONSE_NO_DATA
, !!addr
);
517 res
= pex_msg_append(sizeof(*res
));
518 res
->req_id
= req
->req_id
;
519 res
->cur_version
= cpu_to_be64(net
->net_data_version
);
520 pex_msg_send_ext(net
, peer
, addr
);
523 if (req_version
> net
->net_data_version
)
524 network_pex_send_update_request(net
, peer
, addr
);
526 if (!peer
|| !net
->net_data_len
)
529 if (req_version
>= net
->net_data_version
)
532 pex_msg_update_response_init(&ctx
, net
->config
.pubkey
, net
->config
.auth_key
,
533 peer
->key
, !!addr
, (void *)data
,
534 net
->net_data
, net
->net_data_len
);
536 pex_msg_send_ext(net
, peer
, addr
);
537 done
= !pex_msg_update_response_continue(&ctx
);
542 network_pex_recv_update_response(struct network
*net
, const uint8_t *data
, size_t len
,
543 struct sockaddr_in6
*addr
, enum pex_opcode op
)
545 struct network_peer
*peer
;
547 int net_data_len
= 0;
548 uint64_t version
= 0;
549 bool no_prev_data
= !net
->net_data_len
;
551 if (net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
554 net_data
= pex_msg_update_response_recv(data
, len
, op
, &net_data_len
, &version
);
558 if (version
<= net
->net_data_version
) {
563 D_NET(net
, "received updated network data, len=%d", net_data_len
);
566 net
->net_data
= net_data
;
567 net
->net_data_len
= net_data_len
;
568 net
->net_data_version
= version
;
569 if (network_save_dynamic(net
) < 0)
572 uloop_timeout_set(&net
->reload_timer
, no_prev_data
? 1 : UNETD_DATA_UPDATE_DELAY
);
573 vlist_for_each_element(&net
->peers
, peer
, node
) {
574 if (!peer
->state
.connected
)
576 network_pex_send_update_request(net
, peer
, NULL
);
581 network_pex_recv(struct network
*net
, struct network_peer
*peer
, struct pex_hdr
*hdr
)
583 const void *data
= hdr
+ 1;
585 if (hdr
->version
!= 0)
588 D_PEER(net
, peer
, "PEX rx op=%d", hdr
->opcode
);
589 switch (hdr
->opcode
) {
591 network_pex_recv_hello(net
, peer
, data
, hdr
->len
);
593 case PEX_MSG_NOTIFY_PEERS
:
594 network_pex_recv_peers(net
, peer
, data
, hdr
->len
);
597 network_pex_recv_query(net
, peer
, data
, hdr
->len
);
600 network_pex_recv_ping(net
, peer
);
604 case PEX_MSG_UPDATE_REQUEST
:
605 network_pex_recv_update_request(net
, peer
, data
, hdr
->len
,
608 case PEX_MSG_UPDATE_RESPONSE
:
609 case PEX_MSG_UPDATE_RESPONSE_DATA
:
610 case PEX_MSG_UPDATE_RESPONSE_NO_DATA
:
611 network_pex_recv_update_response(net
, data
, hdr
->len
,
614 case PEX_MSG_ENDPOINT_NOTIFY
:
620 network_pex_fd_cb(struct uloop_fd
*fd
, unsigned int events
)
622 struct network
*net
= container_of(fd
, struct network
, pex
.fd
);
623 struct network_peer
*local
= &net
->net_config
.local_host
->peer
;
624 struct network_peer
*peer
;
625 struct sockaddr_in6 sin6
;
626 static char buf
[PEX_BUF_SIZE
];
627 struct pex_hdr
*hdr
= (struct pex_hdr
*)buf
;
631 socklen_t slen
= sizeof(sin6
);
633 len
= recvfrom(fd
->fd
, buf
, sizeof(buf
), 0, (struct sockaddr
*)&sin6
, &slen
);
641 D_NET(net
, "recvfrom failed: %s", strerror(errno
));
642 network_pex_close(net
);
649 if (len
< sizeof(*hdr
))
652 hdr
->len
= ntohs(hdr
->len
);
653 if (len
- sizeof(hdr
) < hdr
->len
)
656 peer
= pex_msg_peer(net
, hdr
->id
);
660 if (memcmp(&sin6
.sin6_addr
, &peer
->local_addr
.in6
, sizeof(sin6
.sin6_addr
)) != 0)
666 network_pex_recv(net
, peer
, hdr
);
671 network_pex_create_host(struct network
*net
, union network_endpoint
*ep
)
673 struct network_pex
*pex
= &net
->pex
;
674 struct network_pex_host
*host
;
676 host
= calloc(1, sizeof(*host
));
677 memcpy(&host
->endpoint
, ep
, sizeof(host
->endpoint
));
678 list_add_tail(&host
->list
, &pex
->hosts
);
679 network_pex_host_request_update(net
, host
);
683 network_pex_open_auth_connect(struct network
*net
)
685 struct network_pex
*pex
= &net
->pex
;
686 struct network_peer
*peer
;
687 struct blob_attr
*cur
;
690 if (net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
693 uloop_timeout_set(&pex
->request_update_timer
, 5000);
695 vlist_for_each_element(&net
->peers
, peer
, node
) {
696 union network_endpoint ep
= {};
701 if (network_get_endpoint(&ep
, peer
->endpoint
,
702 UNETD_GLOBAL_PEX_PORT
, 0) < 0)
705 ep
.in
.sin_port
= htons(UNETD_GLOBAL_PEX_PORT
);
706 network_pex_create_host(net
, &ep
);
709 if (!net
->config
.auth_connect
)
712 blobmsg_for_each_attr(cur
, net
->config
.auth_connect
, rem
) {
713 union network_endpoint ep
= {};
715 if (network_get_endpoint(&ep
, blobmsg_get_string(cur
),
716 UNETD_GLOBAL_PEX_PORT
, 0) < 0)
719 network_pex_create_host(net
, &ep
);
724 int network_pex_open(struct network
*net
)
726 struct network_host
*local_host
= net
->net_config
.local_host
;
727 struct network_peer
*local
;
728 struct network_pex
*pex
= &net
->pex
;
729 struct sockaddr_in6 sin6
= {};
733 network_pex_open_auth_connect(net
);
735 if (!local_host
|| !local_host
->peer
.pex_port
)
738 local
= &local_host
->peer
;
739 fd
= socket(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
743 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
744 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
746 sin6
.sin6_family
= AF_INET6
;
747 memcpy(&sin6
.sin6_addr
, &local
->local_addr
.in6
,
748 sizeof(local
->local_addr
.in6
));
749 sin6
.sin6_port
= htons(local_host
->peer
.pex_port
);
751 if (bind(fd
, (struct sockaddr
*)&sin6
, sizeof(sin6
)) < 0) {
756 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
));
757 setsockopt(fd
, SOL_SOCKET
, SO_REUSEPORT
, &yes
, sizeof(yes
));
759 setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
760 network_name(net
), strlen(network_name(net
)));
764 pex
->fd
.cb
= network_pex_fd_cb
;
765 uloop_fd_add(&pex
->fd
, ULOOP_READ
);
774 void network_pex_close(struct network
*net
)
776 struct network_pex
*pex
= &net
->pex
;
777 struct network_pex_host
*host
, *tmp
;
779 uloop_timeout_cancel(&pex
->request_update_timer
);
780 list_for_each_entry_safe(host
, tmp
, &pex
->hosts
, list
) {
781 list_del(&host
->list
);
788 uloop_fd_delete(&pex
->fd
);
790 network_pex_init(net
);
793 static struct network
*
794 global_pex_find_network(const uint8_t *id
)
798 avl_for_each_element(&networks
, net
, node
) {
799 if (!memcmp(id
, net
->config
.auth_key
, PEX_ID_LEN
))
807 global_pex_recv(struct pex_hdr
*hdr
, struct sockaddr_in6
*addr
)
809 struct pex_ext_hdr
*ehdr
= (void *)(hdr
+ 1);
810 struct network_peer
*peer
;
812 void *data
= (void *)(ehdr
+ 1);
813 char buf
[INET6_ADDRSTRLEN
];
816 if (hdr
->version
!= 0)
819 net
= global_pex_find_network(ehdr
->auth_id
);
820 if (!net
|| net
->config
.type
!= NETWORK_TYPE_DYNAMIC
)
823 *(uint64_t *)hdr
->id
^= pex_network_hash(net
->config
.auth_key
, ehdr
->nonce
);
825 D("PEX global rx op=%d", hdr
->opcode
);
826 switch (hdr
->opcode
) {
828 case PEX_MSG_NOTIFY_PEERS
:
833 case PEX_MSG_UPDATE_REQUEST
:
834 peer
= pex_msg_peer(net
, hdr
->id
);
835 network_pex_recv_update_request(net
, peer
, data
, hdr
->len
,
838 case PEX_MSG_UPDATE_RESPONSE
:
839 case PEX_MSG_UPDATE_RESPONSE_DATA
:
840 case PEX_MSG_UPDATE_RESPONSE_NO_DATA
:
841 network_pex_recv_update_response(net
, data
, hdr
->len
, addr
, hdr
->opcode
);
843 case PEX_MSG_ENDPOINT_NOTIFY
:
844 peer
= pex_msg_peer(net
, hdr
->id
);
848 if (IN6_IS_ADDR_V4MAPPED(&addr
->sin6_addr
)) {
849 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
850 struct in_addr in
= *(struct in_addr
*)&addr
->sin6_addr
.s6_addr
[12];
851 int port
= addr
->sin6_port
;
853 memset(addr
, 0, sizeof(*addr
));
854 sin
->sin_port
= port
;
855 sin
->sin_family
= AF_INET
;
859 D_PEER(net
, peer
, "receive endpoint notification from %s",
860 inet_ntop(addr
->sin6_family
, network_endpoint_addr((void *)addr
, &addr_len
),
863 memcpy(&peer
->state
.next_endpoint
, addr
, sizeof(*addr
));
868 int global_pex_open(void)
870 struct sockaddr_in6 sin6
= {};
872 sin6
.sin6_family
= AF_INET6
;
873 sin6
.sin6_port
= htons(global_pex_port
);
875 return pex_open(&sin6
, sizeof(sin6
), global_pex_recv
, true);