From: Felix Fietkau Date: Thu, 15 Sep 2022 19:47:20 +0000 (+0200) Subject: pex: add utility function to get the sockets based on type / address family X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=dda15ea8b3b2121eec66f142f600d86366e3c117;p=project%2Funetd.git pex: add utility function to get the sockets based on type / address family Signed-off-by: Felix Fietkau --- diff --git a/pex-msg.c b/pex-msg.c index f4da871..1f00681 100644 --- a/pex-msg.c +++ b/pex-msg.c @@ -26,6 +26,16 @@ static pex_recv_cb_t pex_recv_cb; static pex_recv_control_cb_t pex_control_cb; static int pex_unix_tx_fd = -1; +int pex_socket(void) +{ + return pex_fd.fd; +} + +int pex_raw_socket(int family) +{ + return family == AF_INET ? pex_raw_v4_fd : pex_raw_v6_fd; +} + static const void * get_mapped_sockaddr(const void *addr) { @@ -290,7 +300,7 @@ int __pex_msg_send(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen) if (fd < 0) { hdr->len -= sizeof(struct pex_ext_hdr); if (ip_hdrlen) - fd = sa->sa_family == AF_INET6 ? pex_raw_v6_fd : pex_raw_v4_fd; + fd = pex_raw_socket(sa->sa_family); else { fd = pex_fd.fd; sa = addr = get_mapped_sockaddr(addr); diff --git a/pex-msg.h b/pex-msg.h index c777a6a..b365aeb 100644 --- a/pex-msg.h +++ b/pex-msg.h @@ -103,6 +103,8 @@ int pex_open(void *addr, size_t addr_len, pex_recv_cb_t cb, bool server); int pex_unix_open(const char *path, pex_recv_control_cb_t cb); void pex_close(void); +int pex_socket(void); +int pex_raw_socket(int family); uint64_t pex_network_hash(const uint8_t *auth_key, uint64_t req_id); struct pex_hdr *__pex_msg_init(const uint8_t *pubkey, uint8_t opcode); struct pex_hdr *__pex_msg_init_ext(const uint8_t *pubkey, const uint8_t *auth_key,