]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Cleanup socket.c
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 5 Jun 2017 20:26:06 +0000 (16:26 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 5 Jun 2017 20:26:06 +0000 (16:26 -0400)
Split the socket opening functions into udp and tcp, with the common logic in local functions

14 files changed:
src/include/inet.h
src/include/libradius.h
src/lib/util/inet.c
src/lib/util/socket.c
src/lib/util/udp.c
src/main/client.c
src/main/listen.c
src/main/realms.c
src/modules/proto_bfd/proto_bfd.c
src/modules/proto_dhcp/proto_dhcp.c
src/modules/rlm_realm/rlm_realm.c
src/protocols/radius/list.c
src/tests/util/radius1_test.c
src/tests/util/radius_schedule_test.c

index b02129c32dc820031c35bf78e35b1ef6606cafd4..f54e7752efca2400ac672e45d47695b0dd03e365 100644 (file)
@@ -86,6 +86,11 @@ extern struct in6_addr fr_inet_link_local6;
  */
 #define FR_IPADDR_PREFIX_STRLEN (FR_IPADDR_STRLEN + 1 + 3)
 
+/*
+ *     Utility functions
+ */
+int    fr_ipaddr_is_inaddr_any(fr_ipaddr_t *ipaddr);
+
 /*
  *     IP address masking
  */
index ec97fd32427894e9a1d5607d99666b3bb7921dc4..c383e70ec0f1442c9b9c75fcfac74fc96389a430 100644 (file)
@@ -247,17 +247,21 @@ unsigned int      fr_fifo_num_elements(fr_fifo_t *fi);
 /*
  *     socket.c
  */
-int            fr_socket(fr_ipaddr_t const *ipaddr, uint16_t port);
+int            fr_socket_ipaddr_is_inaddr_any(fr_ipaddr_t *ipaddr);
+
+bool           fr_socket_is_valid_proto(int proto);
 int            fr_socket_client_unix(char const *path, bool async);
 int            fr_socket_client_udp(fr_ipaddr_t const *src_ipaddr, fr_ipaddr_t const *dst_ipaddr,
                                     uint16_t dst_port, bool async);
 int            fr_socket_client_tcp(fr_ipaddr_t const *src_ipaddr, fr_ipaddr_t const *dst_ipaddr,
                                     uint16_t dst_port, bool async);
 int            fr_socket_wait_for_connect(int sockfd, struct timeval const *timeout);
-int            fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async);
+
+int            fr_socket_server_udp(fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async);
+int            fr_socket_server_tcp(fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async);
 int            fr_socket_server_bind(int sockfd, fr_ipaddr_t *ipaddr, int *port, char const *interface);
-int            fr_is_inaddr_any(fr_ipaddr_t *ipaddr);
 
+int            fr_socket(fr_ipaddr_t const *ipaddr, uint16_t port);
 #ifdef __cplusplus
 }
 #endif
index e9dc9556266316ada6ebe53f7b96a286460a42c9..4651451da08d4c254df78837cba5384047fe9f9a 100644 (file)
 bool           fr_dns_lookups = false;     //!< IP -> hostname lookups?
 bool           fr_hostname_lookups = true; //!< hostname -> IP lookups?
 
+/** Determine if an address is the INADDR_ANY address for its address family
+ *
+ * @param ipaddr to check.
+ * @return
+ *     - 0 if it's not.
+ *     - 1 if it is.
+ *     - -1 on error.
+ */
+int fr_ipaddr_is_inaddr_any(fr_ipaddr_t *ipaddr)
+{
+
+       if (ipaddr->af == AF_INET) {
+               if (ipaddr->addr.v4.s_addr == INADDR_ANY) {
+                       return 1;
+               }
+
+#ifdef HAVE_STRUCT_SOCKADDR_IN6
+       } else if (ipaddr->af == AF_INET6) {
+               if (IN6_IS_ADDR_UNSPECIFIED(&(ipaddr->addr.v6))) {
+                       return 1;
+               }
+#endif
+
+       } else {
+               fr_strerror_printf("Unknown address family");
+               return -1;
+       }
+
+       return 0;
+}
+
 /** Mask off a portion of an IPv4 address
  *
  * @param ipaddr to mask.
index 83a9e378205618c2aa87da9d6daf9c52c4e97255..770ed01632d23e21e58cab1202c5d2a8f3efac2e 100644 (file)
 
 #include <fcntl.h>
 
+/** Get the correct SOCK_* value from an IPPROTO_*
+ *
+ */
+static int socket_type_from_proto(int proto)
+{
+       switch (proto) {
+       case IPPROTO_TCP:
+#ifdef IPPROTO_SCTP
+       case IPPROTO_SCTP:      /* SCTP uses SOCK_STREAM too */
+#endif
+               return SOCK_STREAM;
+
+       case IPPROTO_UDP:
+               return SOCK_DGRAM;
+
+       default:
+               fr_strerror_printf("Unrecognised protocol %i", proto);
+               return -1;
+       }
+}
+
+/** Resolve a named service to a port
+ *
+ * @param[in] proto    The protocol. Either IPPROTO_TCP or IPPROTO_UDP.
+ * @param[in] port_name        The service name, i.e. "radius".
+ * @return
+ *     - > 0 the port port_name resolves to.
+ *     - < 0 on error.
+ */
+static int socket_port_from_service(int proto, char const *port_name)
+{
+       struct servent  *service;
+       char const      *proto_name;
+
+       if (!port_name) {
+               fr_strerror_printf("No port specified");
+               return -1;
+       }
+
+       switch (proto) {
+       case IPPROTO_UDP:
+               proto_name = "udp";
+               break;
+
+       case IPPROTO_TCP:
+               proto_name = "tcp";
+               break;
+
+#ifdef IPPROTO_SCTP
+       case IPPROTO_SCTP:
+               proto_name = "sctp";
+               break;
+#endif
+
+       default:
+               fr_strerror_printf("Unrecognised proto %i", proto);
+               return -1;
+       }
+
+       service = getservbyname(port_name, proto_name);
+       if (!service) {
+               fr_strerror_printf("Unknown service %s", port_name);
+               return -1;
+       }
+
+       return ntohs(service->s_port);
+}
+
+#ifdef FD_CLOEXEC
+static int socket_dont_inherit(int sockfd)
+{
+       int rcode;
+
+       /*
+        *      We don't want child processes inheriting these
+        *      file descriptors.
+        */
+       rcode = fcntl(sockfd, F_GETFD);
+       if (rcode >= 0) {
+               if (fcntl(sockfd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
+                       fr_strerror_printf("Failed setting close on exec: %s", fr_syserror(errno));
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+#else
+static socket_dont_inherit(UNUSED int sockfd)
+{
+       return 0;
+}
+#endif
+
+#ifdef HAVE_STRUCT_SOCKADDR_IN6
+/** Restrict wildcard sockets to v6 only
+ *
+ * If we don't do this we get v4 and v6 packets coming in on the same
+ * socket, which is weird.
+ *
+ * @param[in] sockfd to modify.
+ * @param[in] ipaddr we will be binding to.
+ * @return
+ *     - 0 on success.
+ *     - -1 on failure.
+ */
+static int socket_inaddr_any_v6only(int sockfd, fr_ipaddr_t const *ipaddr)
+{
+       /*
+        *      Listening on '::' does NOT get you IPv4 to
+        *      IPv6 mapping.  You've got to listen on an IPv4
+        *      address, too.  This makes the rest of the server
+        *      design a little simpler.
+        */
+       if (ipaddr->af == AF_INET6) {
+#  ifdef IPV6_V6ONLY
+               if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->addr.v6)) {
+                       int on = 1;
+
+                       if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
+                                      (char *)&on, sizeof(on)) < 0) {
+                               fr_strerror_printf("Failed setting socket to IPv6 only: %s", fr_syserror(errno));
+                               close(sockfd);
+                               return -1;
+                       }
+               }
+#  endif /* IPV6_V6ONLY */
+       }
+       return 0;
+}
+#else
+static int socket_inaddr_any_v6only(UNUSED int sockfd, UNUSED fr_ipaddr_t const *ipaddr)
+{
+       return 0;
+}
+#endif
+
+/** Check the proto value is sane/supported
+ *
+ * @param[in] proto to check
+ * @return
+ *     - true if it is.
+ *     - false if it's not.
+ */
+bool fr_socket_is_valid_proto(int proto)
+{
+       /*
+        *      Check the protocol is sane
+        */
+       switch (proto) {
+       case IPPROTO_UDP:
+       case IPPROTO_TCP:
+#ifdef IPPROTO_SCTP
+       case IPPROTO_SCTP:
+#endif
+               return true;
+
+       default:
+               fr_strerror_printf("Unknown IP protocol %d", proto);
+               return false;
+       }
+}
+
 #ifdef HAVE_SYS_UN_H
 #  include <sys/un.h>
 #  ifndef SUN_LEN
@@ -408,127 +571,83 @@ int fr_socket_wait_for_connect(int sockfd, struct timeval const *timeout)
        }
 }
 
-/** Open an IPv4 / IPv6, and UDP / TCP socket, server side.
+/** Open an IPv4/IPv6 UDP socket
  *
- * @param[in] proto IPPROTO_UDP or IPPROTO_TCP
- * @param[in] ipaddr The IP address to listen on
- * @param[in,out] port the port to listen on
- * @param[in] port_name if port==0, the name of the port
- * @param[in] async whether we block or not on reads and writes
+ * @param[in] ipaddr           The IP address to listen on
+ * @param[in,out] port         the port to listen on.  If *port == 0, the resolved
+ *                             service port will be written here.
+ * @param[in] port_name                if *port == 0, the name of the port
+ * @param[in] async            whether we block or not on reads and writes
  * @return
  *     - Socket FD on success.
  *     - -1 on failure.
  */
-int fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async)
+int fr_socket_server_udp(fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async)
 {
-#ifdef FD_CLOEXEC
-       int rcode;
-#endif
        int sockfd;
-       int sock_type;
-
-       if (!proto) proto = IPPROTO_UDP;
-
-       if ((proto != IPPROTO_UDP) && (proto != IPPROTO_TCP)) {
-               fr_strerror_printf("Unknown IP protocol %d", proto);
-               return -1;
-       }
 
+       /*
+        *      Check IP looks OK
+        */
        if (!ipaddr || ((ipaddr->af != AF_INET) && (ipaddr->af != AF_INET6))) {
                fr_strerror_printf("No address specified");
                return -1;
        }
 
+       /*
+        *      Check we have a port value or stuff we can resolve to a port
+        */
        if (!*port) {
-               struct servent  *svp;
-               char const *proto_name;
+               int ret;
 
                if (!port_name) {
-                       fr_strerror_printf("No port specified");
+                       fr_strerror_printf("No port or port_name specified");
                        return -1;
                }
 
-               if (proto == IPPROTO_UDP) {
-                       proto_name = "udp";
-               } else {
-                       proto_name = "tcp";
-               }
-
-               svp = getservbyname(port_name, proto_name);
-               if (!svp) {
-                       fr_strerror_printf("Unknown port %s", port_name);
-                       return -1;
-               }
+               ret = socket_port_from_service(IPPROTO_UDP, port_name);
+               if (ret < 0) return -1;
 
-
-               *port = ntohs(svp->s_port);
-       }
-
-       if (proto == IPPROTO_UDP) {
-               sock_type = SOCK_DGRAM;
-       } else {
-               sock_type = SOCK_STREAM;
+               *port = ret;
        }
 
-       sockfd = socket(ipaddr->af, sock_type, proto);
+       /*
+        *      Open the socket
+        */
+       sockfd = socket(ipaddr->af, socket_type_from_proto(IPPROTO_UDP), IPPROTO_UDP);
        if (sockfd < 0) {
                fr_strerror_printf("Failed creating UNIX socket: %s", fr_syserror(errno));
                return -1;
        }
 
-#ifdef FD_CLOEXEC
        /*
-        *      We don't want child processes inheriting these
-        *      file descriptors.
+        *      Make it non-blocking if asked
         */
-       rcode = fcntl(sockfd, F_GETFD);
-       if (rcode >= 0) {
-               if (fcntl(sockfd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
-                       close(sockfd);
-                       fr_strerror_printf("Failed setting close on exec: %s", fr_syserror(errno));
-                       return -1;
-               }
-       }
-#endif
-
        if (async && (fr_nonblock(sockfd) < 0)) {
+       error:
                close(sockfd);
                return -1;
        }
 
+       /*
+        *      Don't allow child processes to inherit the socket
+        */
+       if (socket_dont_inherit(sockfd) < 0) goto error;
+
 #ifdef WITH_UDPFROMTO
        /*
         *      Initialize udpfromto for UDP sockets.
         */
-       if ((proto == IPPROTO_UDP) && (udpfromto_init(sockfd) != 0)) {
+       if (udpfromto_init(sockfd) != 0) {
                fr_strerror_printf("Failed initializing udpfromto: %s", fr_syserror(errno));
-               close(sockfd);
-               return -1;
+               goto error;
        }
 #endif
 
-#ifdef HAVE_STRUCT_SOCKADDR_IN6
        /*
-        *      Listening on '::' does NOT get you IPv4 to
-        *      IPv6 mapping.  You've got to listen on an IPv4
-        *      address, too.  This makes the rest of the server
-        *      design a little simpler.
+        *      Make sure we don't get v4 and v6 packets on inaddr_any sockets.
         */
-       if (ipaddr->af == AF_INET6) {
-#  ifdef IPV6_V6ONLY
-               if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->addr.v6)) {
-                       int on = 1;
-
-                       if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
-                                      (char *)&on, sizeof(on)) < 0) {
-                               fr_strerror_printf("Failed setting socket to IPv6 only: %s", fr_syserror(errno));
-                               close(sockfd);
-                               return -1;
-                       }
-               }
-#  endif /* IPV6_V6ONLY */
-       }
-#endif /* HAVE_STRUCT_SOCKADDR_IN6 */
+       if (socket_inaddr_any_v6only(sockfd, ipaddr)) goto error;
 
 #if (defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)) || defined(IP_DONTFRAG)
        /*
@@ -539,7 +658,7 @@ int fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const
        if ((proto == IPPROTO_UDP) && (ipaddr->af == AF_INET)) {
                int flag;
 
-#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
+#  if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
 
                /*
                 *      Disable PMTU discovery.  On Linux, this
@@ -553,9 +672,9 @@ int fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const
                        close(sockfd);
                        return -1;
                }
-#endif
+#  endif
 
-#if defined(IP_DONTFRAG)
+#  if defined(IP_DONTFRAG)
                /*
                 *      Ensure that the "don't fragment" flag is zero.
                 */
@@ -566,24 +685,12 @@ int fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const
                        close(sockfd);
                        return -1;
                }
-#endif
+#  endif
        }
 #endif /* lots of things */
 
-#if defined(WITH_TCP)
-       if (proto == IPPROTO_TCP) {
-               int on = 1;
-
-               if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
-                       close(sockfd);
-                       fr_strerror_printf("Failed to reuse address: %s", fr_syserror(errno));
-                       return -1;
-               }
-       }
-#endif
-
 #ifdef SO_TIMESTAMP
-       if (proto == IPPROTO_UDP) {
+       {
                int on = 1;
 
                /*
@@ -602,12 +709,99 @@ int fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const
        return sockfd;
 }
 
-/** Bind to an IPv4 / IPv6, and UDP / TCP socket, server side.
+/** Open an IPv4/IPv6 TCP socket
  *
- * @param[in] sockfd the socket which was opened via fr_socket_server_base()
- * @param[in,out] ipaddr The IP address to bind to
- * @param[in] port the port to bind to
- * @param[in] interface the interface name to bind to
+ * @param[in] ipaddr           The IP address to listen on
+ * @param[in,out] port         the port to listen on.  If *port == 0, the resolved
+ *                             service port will be written here.
+ * @param[in] port_name                if *port == 0, the name of the port
+ * @param[in] async            whether we block or not on reads and writes
+ * @return
+ *     - Socket FD on success.
+ *     - -1 on failure.
+ */
+int fr_socket_server_tcp(fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async)
+{
+       int sockfd;
+
+       /*
+        *      Check IP looks OK
+        */
+       if (!ipaddr || ((ipaddr->af != AF_INET) && (ipaddr->af != AF_INET6))) {
+               fr_strerror_printf("No address specified");
+               return -1;
+       }
+
+       /*
+        *      Check we have a port value or stuff we can resolve to a port
+        */
+       if (!*port) {
+               int ret;
+
+               if (!port_name) {
+                       fr_strerror_printf("No port or port_name specified");
+                       return -1;
+               }
+
+               ret = socket_port_from_service(IPPROTO_TCP, port_name);
+               if (ret < 0) return -1;
+
+               *port = ret;
+       }
+
+       /*
+        *      Open the socket
+        */
+       sockfd = socket(ipaddr->af, socket_type_from_proto(IPPROTO_TCP), IPPROTO_TCP);
+       if (sockfd < 0) {
+               fr_strerror_printf("Failed creating UNIX socket: %s", fr_syserror(errno));
+               return -1;
+       }
+
+       /*
+        *      Make it non-blocking if asked
+        */
+       if (async && (fr_nonblock(sockfd) < 0)) {
+       error:
+               close(sockfd);
+               return -1;
+       }
+
+       /*
+        *      Don't allow child processes to inherit the socket
+        */
+       if (socket_dont_inherit(sockfd) < 0) goto error;
+
+       /*
+        *      Make sure we don't get v4 and v6 packets on inaddr_any sockets.
+        */
+       if (socket_inaddr_any_v6only(sockfd, ipaddr)) goto error;
+
+       {
+               int on = 1;
+
+               if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
+                       close(sockfd);
+                       fr_strerror_printf("Failed to reuse address: %s", fr_syserror(errno));
+                       return -1;
+               }
+       }
+
+       return sockfd;
+}
+
+/** Bind to an IPv4/IPv6, UDP/TCP socket
+ *
+ * Use one of
+ * - fr_socket_server_udp
+ * - fr_socket_server_tcp
+ *
+ * To open a file descriptor, then call this function to bind the socket to an IP address.
+ *
+ * @param[in] sockfd           the socket which opened by fr_socket_server_*.
+ * @param[in,out] ipaddr       The IP address to bind to
+ * @param[in] port             the port to bind to
+ * @param[in] interface                to bind to.
  * @return
  *     - 0 on success
  *     - -1 on failure.
@@ -652,7 +846,8 @@ int fr_socket_server_bind(int sockfd, fr_ipaddr_t *ipaddr, int *port, char const
                        if (ipaddr->scope_id == 0) {
                                ipaddr->scope_id = if_nametoindex(interface);
                                if (ipaddr->scope_id == 0) {
-                                       fr_strerror_printf("Failed finding interface %s: %s", interface, fr_syserror(errno));
+                                       fr_strerror_printf("Failed finding interface %s: %s",
+                                                          interface, fr_syserror(errno));
                                        return -1;
                                }
                        } /* else scope was defined: we're OK. */
@@ -664,7 +859,8 @@ int fr_socket_server_bind(int sockfd, fr_ipaddr_t *ipaddr, int *port, char const
                         *      IPv4: no link local addresses,
                         *      and no bind to device.
                         */
-                       fr_strerror_printf("Failed binding to interface %s: \"bind to device\" is unsupported", interface);
+                       fr_strerror_printf("Failed binding to interface %s: \"bind to device\" is unsupported",
+                                          interface);
                        return -1;
                }
 #endif
@@ -695,9 +891,7 @@ int fr_socket_server_bind(int sockfd, fr_ipaddr_t *ipaddr, int *port, char const
                return -1;
        }
 
-       if (!fr_ipaddr_from_sockaddr(&salocal, salen, ipaddr, &my_port)) {
-               return -1;
-       }
+       if (!fr_ipaddr_from_sockaddr(&salocal, salen, ipaddr, &my_port)) return -1;
 
        *port = my_port;
 
@@ -772,8 +966,7 @@ int fr_socket(fr_ipaddr_t const *ipaddr, uint16_t port)
                 *      flag is zero.
                 */
                flag = IP_PMTUDISC_DONT;
-               if (setsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER,
-                              &flag, sizeof(flag)) < 0) {
+               if (setsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER, &flag, sizeof(flag)) < 0) {
                        close(sockfd);
                        fr_strerror_printf("Failed setting sockopt "
                                           "IPPROTO_IP - IP_MTU_DISCOVER: %s",
@@ -787,8 +980,7 @@ int fr_socket(fr_ipaddr_t const *ipaddr, uint16_t port)
                 *      Ensure that the "don't fragment" flag is zero.
                 */
                flag = 0;
-               if (setsockopt(sockfd, IPPROTO_IP, IP_DONTFRAG,
-                          &flag, sizeof(flag)) < 0) {
+               if (setsockopt(sockfd, IPPROTO_IP, IP_DONTFRAG, &flag, sizeof(flag)) < 0) {
                        close(sockfd);
                        fr_strerror_printf("Failed setting sockopt "
                                           "IPPROTO_IP - IP_DONTFRAG: %s",
@@ -807,35 +999,3 @@ int fr_socket(fr_ipaddr_t const *ipaddr, uint16_t port)
 
        return sockfd;
 }
-
-
-/** Determine if an address is the INADDR_ANY address for its address family
- *
- * @param ipaddr to check.
- * @return
- *     - 0 if it's not.
- *     - 1 if it is.
- *     - -1 on error.
- */
-int fr_is_inaddr_any(fr_ipaddr_t *ipaddr)
-{
-
-       if (ipaddr->af == AF_INET) {
-               if (ipaddr->addr.v4.s_addr == INADDR_ANY) {
-                       return 1;
-               }
-
-#ifdef HAVE_STRUCT_SOCKADDR_IN6
-       } else if (ipaddr->af == AF_INET6) {
-               if (IN6_IS_ADDR_UNSPECIFIED(&(ipaddr->addr.v6))) {
-                       return 1;
-               }
-#endif
-
-       } else {
-               fr_strerror_printf("Unknown address family");
-               return -1;
-       }
-
-       return 0;
-}
index cb04660b279ffe8bbe148d70bada1f5794d70009..461b67b49c787735f8c0cedc2b99035fd07ddbfe 100644 (file)
@@ -77,7 +77,7 @@ ssize_t udp_send(int sockfd, void *data, size_t data_len, int flags,
                 *      use udpfromto.
                 */
                if ((src_ipaddr->af != AF_UNSPEC) && (dst_ipaddr->af != AF_UNSPEC) &&
-                   !fr_is_inaddr_any(src_ipaddr)) {
+                   !fr_ipaddr_is_inaddr_any(src_ipaddr)) {
                        struct sockaddr_storage src;
                        socklen_t               sizeof_src;
 
index d2bf2676e0687d54304023524169110db9c41c2b..c2b408b39bf4457f2e00942a9801d5d19cb466e2 100644 (file)
@@ -189,7 +189,7 @@ bool client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
         *      of 0.0.0.0/32 - which would require the src IP of
         *      the client to be all zeros.
         */
-       if (fr_is_inaddr_any(&client->ipaddr) == 1) switch (client->ipaddr.af) {
+       if (fr_ipaddr_is_inaddr_any(&client->ipaddr) == 1) switch (client->ipaddr.af) {
        case AF_INET:
                if (client->ipaddr.prefix == 32) client->ipaddr.prefix = 0;
                break;
index a8a837be34f5b42ff5ef0519eecd346433f097aa..40de6491e1bd8d5662260b482975ac264066902c 100644 (file)
@@ -2780,7 +2780,20 @@ static int listen_bind(rad_listen_t *this)
         *      Open the socket and set a whack of flags.
         */
        port = sock->my_port;
-       this->fd = fr_socket_server_base(sock->proto, &sock->my_ipaddr, &port, port_name, true);
+       switch (sock->proto) {
+       case IPPROTO_UDP:
+               this->fd = fr_socket_server_udp(&sock->my_ipaddr, &port, port_name, true);
+               break;
+
+       case IPPROTO_TCP:
+               this->fd = fr_socket_server_tcp(&sock->my_ipaddr, &port, port_name, true);
+               break;
+
+       default:
+               rad_assert(0);
+               return -1;
+       }
+
        if (this->fd < 0) {
                char buffer[256];
 
@@ -3307,7 +3320,7 @@ rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, i
 
                if (sock->my_port != port) continue;
                if (sock->proto != proto) continue;
-               if (!fr_is_inaddr_any(&sock->my_ipaddr)) continue;
+               if (!fr_ipaddr_is_inaddr_any(&sock->my_ipaddr)) continue;
 
                return this;
        }
index 613b37a4a7f43a22670f0d100681996497d1da19..d23832f5c2d2f8bdabcb96e7d1d6ef064cb22142 100644 (file)
@@ -629,7 +629,7 @@ home_server_t *home_server_afrom_cs(TALLOC_CTX *ctx, realm_config_t *rc, CONF_SE
         *      It has an IP address, it must be a remote server.
         */
        if (cf_pair_find(cs, "ipaddr") || cf_pair_find(cs, "ipv4addr") || cf_pair_find(cs, "ipv6addr")) {
-               if (fr_is_inaddr_any(&home->ipaddr) == 1) {
+               if (fr_ipaddr_is_inaddr_any(&home->ipaddr) == 1) {
                        cf_log_err_cs(cs, "Wildcard '*' addresses are not permitted for home servers");
                        goto error;
                }
index 5c787411ece32426de189387c32615a7285afaf6..e7c8ce1596fe2a7c6b3ef80e2b8bd118de531396 100644 (file)
@@ -1747,7 +1747,7 @@ static int bfd_socket_open(CONF_SECTION *cs, rad_listen_t *this)
 
        port = sock->my_port;
 
-       this->fd = fr_socket_server_base(IPPROTO_UDP, &sock->my_ipaddr, &port, "bfd-control", true);
+       this->fd = fr_socket_server_udp(&sock->my_ipaddr, &port, "bfd-control", true);
        if (this->fd < 0) {
                char buffer[256];
 
index 89387cd4ff09f1a9ae237f938bcbdb07b5e66531..6eb5dc3c85556d452b11a60e2c598bac3626b75b 100644 (file)
@@ -691,7 +691,7 @@ static const char *dhcp_pcap_filter_build(rad_listen_t *this)
                filter = talloc_strdup_append_buffer(filter, "bootps)");
        }
 
-       if (!fr_is_inaddr_any(&sock->lsock.my_ipaddr)) {
+       if (!fr_ipaddr_is_inaddr_any(&sock->lsock.my_ipaddr)) {
                char buffer[INET_ADDRSTRLEN];
                fr_inet_ntoh(&sock->lsock.my_ipaddr, buffer, sizeof(buffer));
 
@@ -774,7 +774,7 @@ static int dhcp_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
        } else {
                char buffer[INET_ADDRSTRLEN];
 
-               if (fr_is_inaddr_any(&sock->lsock.my_ipaddr) && sock->src_interface) {
+               if (fr_ipaddr_is_inaddr_any(&sock->lsock.my_ipaddr) && sock->src_interface) {
                        if (fr_ipaddr_from_ifname(&sock->src_ipaddr, AF_INET, sock->src_interface) < 0) {
                                WARN("Failed resolving interface %s to IP address: %s", sock->src_interface,
                                     fr_strerror());
@@ -791,7 +791,7 @@ static int dhcp_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
                /*
                 *      If src is not INADDR_ANY add a configuration item
                 */
-               if (!fr_is_inaddr_any(&sock->src_ipaddr)) {
+               if (!fr_ipaddr_is_inaddr_any(&sock->src_ipaddr)) {
                        /*
                         *      Magic defaults FTW.
                         *
index 38cc1128d5d53499bb28046a94131651cb332031..88012df732cde32bf0558c5206602c57e8fc97c1 100644 (file)
@@ -322,7 +322,7 @@ static int check_for_realm(void const *instance, REQUEST *request, REALM **retur
                 */
 #ifdef WITH_DETAIL
        } else if (request->listener && (request->listener->type == RAD_LISTEN_DETAIL) &&
-                  !fr_is_inaddr_any(&request->packet->src_ipaddr)) {
+                  !fr_ipaddr_is_inaddr_any(&request->packet->src_ipaddr)) {
                int i;
 
                /*
index cd14eec225f56f63c588916e9461c0c9325ddfa9..e673fcb57e9ebab771ce80a3eb68fcf3a0b04982 100644 (file)
@@ -295,10 +295,10 @@ bool fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd, int proto,
        ps->dst_ipaddr = *dst_ipaddr;
        ps->dst_port = dst_port;
 
-       ps->src_any = fr_is_inaddr_any(&ps->src_ipaddr);
+       ps->src_any = fr_ipaddr_is_inaddr_any(&ps->src_ipaddr);
        if (ps->src_any < 0) return false;
 
-       ps->dst_any = fr_is_inaddr_any(&ps->dst_ipaddr);
+       ps->dst_any = fr_ipaddr_is_inaddr_any(&ps->dst_ipaddr);
        if (ps->dst_any < 0) return false;
 
        /*
@@ -504,7 +504,7 @@ bool fr_packet_list_id_alloc(fr_packet_list_t *pl, int proto,
                request->src_ipaddr.af = request->dst_ipaddr.af;
        }
 
-       src_any = fr_is_inaddr_any(&request->src_ipaddr);
+       src_any = fr_ipaddr_is_inaddr_any(&request->src_ipaddr);
        if (src_any < 0) {
                fr_strerror_printf("Can't check src_ipaddr");
                return false;
@@ -513,7 +513,7 @@ bool fr_packet_list_id_alloc(fr_packet_list_t *pl, int proto,
        /*
         *      MUST specify a destination address.
         */
-       if (fr_is_inaddr_any(&request->dst_ipaddr) != 0) {
+       if (fr_ipaddr_is_inaddr_any(&request->dst_ipaddr) != 0) {
                fr_strerror_printf("Must specify a dst_ipaddr");
                return false;
        }
index cf30c20b6f693fe9d39a6726558eceb7178a69d1..af135c02afa6112c3a71885a903823aff52ce5f4 100644 (file)
@@ -52,7 +52,7 @@ RCSID("$Id$")
  *
  *     Declare these here until we move all of the new field to the REQUEST.
  */
-extern int             fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async);
+extern int             fr_socket_server_udp(fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async);
 extern int             fr_socket_server_bind(int sockfd, fr_ipaddr_t *ipaddr, int *port, char const *interface);
 
 
@@ -236,7 +236,7 @@ static void master_process(TALLOC_CTX *ctx)
        control_master = fr_control_create(ctx, kq_master, aq_master);
        rad_assert(control_master != NULL);
 
-       sockfd = fr_socket_server_base(IPPROTO_UDP, &my_ipaddr, &my_port, NULL, true);
+       sockfd = fr_socket_server_udp(&my_ipaddr, &my_port, NULL, true);
        if (sockfd < 0) {
                fprintf(stderr, "radius_test: Failed creating socket: %s\n", fr_strerror());
                exit(1);
index c12f2e50928ecd8c440a6142f389fba0c145ed1d..291998ed972eac4925dffe57525bf6ce172323c7 100644 (file)
@@ -63,7 +63,7 @@ static fr_packet_ctx_t  packet_ctx = { 0 };
  *
  *     Declare these here until we move all of the new field to the REQUEST.
  */
-extern int             fr_socket_server_base(int proto, fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async);
+extern int             fr_socket_server_udp(fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async);
 extern int             fr_socket_server_bind(int sockfd, fr_ipaddr_t *ipaddr, int *port, char const *interface);
 extern int             fr_fault_setup(char const *cmd, char const *program);
 
@@ -239,7 +239,7 @@ int main(int argc, char *argv[])
                exit(1);
        }
 
-       sockfd = fr_socket_server_base(IPPROTO_UDP, &my_ipaddr, &my_port, NULL, true);
+       sockfd = fr_socket_server_udp(&my_ipaddr, &my_port, NULL, true);
        if (sockfd < 0) {
                fprintf(stderr, "radius_test: Failed creating socket: %s\n", fr_strerror());
                exit(1);