From: Alan T. DeKok Date: Thu, 4 Jan 2024 14:10:00 +0000 (-0500) Subject: add address family to fr_socket_t X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab6462dcdfbfeabd4c0f4c9e44e5e54df283d96f;p=thirdparty%2Ffreeradius-server.git add address family to fr_socket_t AF_UNIX isn't an IPPROTO, so we can't use the proto field. and AF_UNIX uses SOCK_STREAM instead of IPPROTO_TCP, and SOCK_SEQPACKET instead of IPPROTO_UDP. Note that SOCK_DGRAM is an unconnected, one-way unix socket! --- diff --git a/src/lib/util/socket.h b/src/lib/util/socket.h index d05c9f19501..45b668ea369 100644 --- a/src/lib/util/socket.h +++ b/src/lib/util/socket.h @@ -75,6 +75,7 @@ typedef struct { char const *path; //!< Unix socket path. } unix; }; + int af; //!< AF_INET, AF_INET6, or AF_UNIX int proto; //!< Protocol. int fd; //!< File descriptor if this is a live socket. @@ -87,7 +88,7 @@ typedef struct { * - true if it is. * - false if it's not. */ -static inline bool fr_socket_is_inet(int proto) +static inline bool fr_socket_proto_is_known(int proto) { /* * Check the protocol is sane @@ -123,21 +124,10 @@ static inline void fr_socket_addr_swap(fr_socket_t *dst, fr_socket_t const *src) if (dst != src) *dst = tmp; /* copy non-address fields over */ - switch (src->proto) { - case IPPROTO_UDP: - case IPPROTO_TCP: -#ifdef IPPROTO_SCTP - case IPPROTO_SCTP: -#endif - dst->inet.dst_ipaddr = tmp.inet.src_ipaddr; - dst->inet.dst_port = tmp.inet.src_port; - dst->inet.src_ipaddr = tmp.inet.dst_ipaddr; - dst->inet.src_port = tmp.inet.dst_port; - break; - - default: - return; - } + dst->inet.dst_ipaddr = tmp.inet.src_ipaddr; + dst->inet.dst_port = tmp.inet.src_port; + dst->inet.src_ipaddr = tmp.inet.dst_ipaddr; + dst->inet.src_port = tmp.inet.dst_port; } /** Initialise a fr_socket_t for connecting to a remote host using a specific src interface, address and port @@ -163,9 +153,10 @@ static inline fr_socket_t *fr_socket_addr_init_inet(fr_socket_t *addr, int ifindex, fr_ipaddr_t const *src_ipaddr, int src_port, fr_ipaddr_t const *dst_ipaddr, int dst_port) { - if (!fr_socket_is_inet(proto)) return NULL; + if (!fr_socket_proto_is_known(proto)) return NULL; *addr = (fr_socket_t){ + .af = src_ipaddr->af, .proto = proto, .inet = { .ifindex = ifindex, @@ -222,9 +213,10 @@ static inline fr_socket_t *fr_socket_addr_alloc_inet(TALLOC_CTX *ctx, int proto, static inline fr_socket_t *fr_socket_addr_init_inet_src(fr_socket_t *addr, int proto, int ifindex, fr_ipaddr_t const *ipaddr, int port) { - if (!fr_socket_is_inet(proto)) return NULL; + if (!fr_socket_proto_is_known(proto)) return NULL; *addr = (fr_socket_t){ + .af = ipaddr->af, .proto = proto, .inet = { .ifindex = ifindex, @@ -267,9 +259,10 @@ static inline fr_socket_t *fr_socket_addr_alloc_inet_src(TALLOC_CTX *ctx, int pr */ static inline fr_socket_t *fr_socket_addr_init_inet_dst(fr_socket_t *addr, int proto, fr_ipaddr_t const *ipaddr, int port) { - if (!fr_socket_is_inet(proto)) return NULL; + if (!fr_socket_proto_is_known(proto)) return NULL; *addr = (fr_socket_t){ + .af = ipaddr->af, .proto = proto, .inet = { .dst_ipaddr = *ipaddr,