]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add address family to fr_socket_t
authorAlan T. DeKok <aland@freeradius.org>
Thu, 4 Jan 2024 14:10:00 +0000 (09:10 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 5 Jan 2024 00:21:27 +0000 (19:21 -0500)
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!

src/lib/util/socket.h

index d05c9f195018c67e2efe8742dfd75e11c34a6874..45b668ea369591f2106b7599507a5ebf5b3aa9f2 100644 (file)
@@ -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,