Use new functions to copy sockaddr to ipaddr
[freeradius.git] / src / lib / packet.c
index 29c3235..625a0f5 100644 (file)
@@ -175,7 +175,6 @@ void fr_request_from_reply(RADIUS_PACKET *request,
 int fr_socket(fr_ipaddr_t *ipaddr, int port)
 {
        int sockfd;
-       uint16_t sport;
        struct sockaddr_storage salocal;
        socklen_t       salen;
 
@@ -201,31 +200,13 @@ int fr_socket(fr_ipaddr_t *ipaddr, int port)
        }
 #endif
 
-       sport = port;
-       sport = htons(sport);
-       memset(&salocal, 0, sizeof(salocal));
-       if (ipaddr->af == AF_INET) {
-               struct sockaddr_in s4;
 
-               s4.sin_family = AF_INET;
-               s4.sin_addr = ipaddr->ipaddr.ip4addr;
-               s4.sin_port = sport;
-               salen = sizeof(s4);
-               memset(&salocal, 0, sizeof(salocal));
-               memcpy(&salocal, &s4, salen);
+       if (!fr_ipaddr2sockaddr(ipaddr, port, &salocal, &salen)) {
+               return sockfd;
+       }
 
 #ifdef HAVE_STRUCT_SOCKADDR_IN6
-       } else if (ipaddr->af == AF_INET6) {
-               struct sockaddr_in6 s6;
-
-               s6.sin6_family = AF_INET6;
-               s6.sin6_addr = ipaddr->ipaddr.ip6addr;
-               s6.sin6_port = sport;
-               salen = sizeof(s6);
-               memset(&salocal, 0, sizeof(salocal));
-               memcpy(&salocal, &s6, salen);
-
-#if 1
+       if (ipaddr->af == AF_INET6) {
                /*
                 *      Listening on '::' does NOT get you IPv4 to
                 *      IPv6 mapping.  You've got to listen on an IPv4
@@ -241,10 +222,7 @@ int fr_socket(fr_ipaddr_t *ipaddr, int port)
                                   (char *)&on, sizeof(on));
                }
 #endif /* IPV6_V6ONLY */
-#endif
 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
-       } else {
-               return sockfd;  /* don't bind it */
        }
 
        if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
@@ -380,29 +358,20 @@ int fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd)
                return 0;
        }
 
+       if (!fr_sockaddr2ipaddr(&src, sizeof_src, &ps->ipaddr, &ps->port)) {
+               return 0;
+       }
+
        /*
         *      Grab IP addresses & ports from the sockaddr.
         */
-       ps->ipaddr.af = src.ss_family;
        if (src.ss_family == AF_INET) {
-               struct sockaddr_in      s4;
-
-               memcpy(&s4, &src, sizeof(s4));
-               ps->ipaddr.ipaddr.ip4addr = s4.sin_addr;
-               ps->port = ntohs(s4.sin_port);
-
                if (ps->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY) {
                        ps->inaddr_any = 1;
                }
 
 #ifdef HAVE_STRUCT_SOCKADDR_IN6
        } else if (src.ss_family == AF_INET6) {
-               struct sockaddr_in6     s6;
-
-               memcpy(&s6, &src, sizeof(s6));
-               ps->ipaddr.ipaddr.ip6addr = s6.sin6_addr;
-               ps->port = ntohs(s6.sin6_port);
-
                if (IN6_IS_ADDR_UNSPECIFIED(&ps->ipaddr.ipaddr.ip6addr)) {
                        ps->inaddr_any = 1;
                }