apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Trawick <trawi...@bellsouth.net>
Subject [PATCH] small step toward's David Reid's IPv6 patch
Date Wed, 15 Nov 2000 19:35:56 GMT
I started with the part of David's patch which uses apr_sockaddr_t
instead of sockaddr_in inside apr_socket_t, got everything to compile
(and seem to run properly), and simplified some of the code which
allocates storage for apr_socket_t and fills out fields in
apr_sockaddr_t.

I didn't add all the code from the patch that supports IPv6
throughout.  I didn't add any of the API enhancements.

Is this bit easier to review perhaps?

Real deviations from David's (AFAIK):
. I got rid of the addr_len field in apr_socket_t.

Index: include/apr_network_io.h
===================================================================
RCS file: /home/cvspublic/apache-2.0/src/lib/apr/include/apr_network_io.h,v
retrieving revision 1.69
diff -u -r1.69 apr_network_io.h
--- include/apr_network_io.h	2000/11/15 18:11:34	1.69
+++ include/apr_network_io.h	2000/11/15 19:21:20
@@ -162,10 +162,11 @@
                                     * 16 for v4 or 46 for v6
                                     * used in inet_ntop...
                                     */
-     void *ipaddr_ptr;              /* This points to the IP address
+    void *ipaddr_ptr;              /* This points to the IP address
                                     * structure within the appropriate
                                     * sockaddr structure.
                                     */
+    int ipaddr_len;
 } apr_sockaddr_t;
 
 #if APR_HAS_SENDFILE
Index: include/arch/unix/networkio.h
===================================================================
RCS file: /home/cvspublic/apache-2.0/src/lib/apr/include/arch/unix/networkio.h,v
retrieving revision 1.33
diff -u -r1.33 networkio.h
--- include/arch/unix/networkio.h	2000/09/29 04:03:20	1.33
+++ include/arch/unix/networkio.h	2000/11/15 19:21:20
@@ -122,9 +122,8 @@
 struct apr_socket_t {
     apr_pool_t *cntxt;
     int socketdes;
-    struct sockaddr_in *local_addr;
-    struct sockaddr_in *remote_addr;
-    apr_socklen_t addr_len;
+    apr_sockaddr_t *local_addr;
+    apr_sockaddr_t *remote_addr;
     apr_interval_time_t timeout; 
 #ifndef HAVE_POLL
     int connected;
@@ -149,6 +148,9 @@
     apr_int16_t *revents;
 
 };
 
 #endif  /* ! NETWORK_IO_H */
 
Index: network_io/unix/sa_common.c
===================================================================
RCS file: /home/cvspublic/apache-2.0/src/lib/apr/network_io/unix/sa_common.c,v
retrieving revision 1.4
diff -u -r1.4 sa_common.c
--- network_io/unix/sa_common.c	2000/11/11 06:05:59	1.4
+++ network_io/unix/sa_common.c	2000/11/15 19:21:21
@@ -68,10 +68,11 @@
 apr_status_t apr_set_port(apr_socket_t *sock, apr_interface_e which, 
                          apr_port_t port)
 {
+        /* XXX IPv6 */
     if (which == APR_LOCAL)
-        sock->local_addr->sin_port = htons(port);
+        sock->local_addr->sa.sin.sin_port = htons(port);
     else if (which == APR_REMOTE)
-        sock->remote_addr->sin_port = htons(port);
+        sock->remote_addr->sa.sin.sin_port = htons(port);
     else
         return APR_EINVAL;
     return APR_SUCCESS;
@@ -89,9 +90,10 @@
             }
         }
 
-        *port = ntohs(sock->local_addr->sin_port);
+        /* XXX IPv6 */
+        *port = ntohs(sock->local_addr->sa.sin.sin_port);
     } else if (which == APR_REMOTE)
-        *port = ntohs(sock->remote_addr->sin_port);
+        *port = ntohs(sock->remote_addr->sa.sin.sin_port);
     else
         return APR_EINVAL;
     return APR_SUCCESS;
@@ -107,9 +109,11 @@
                 return rv;
             }
         }
-        *addr = apr_pstrdup(sock->cntxt, inet_ntoa(sock->local_addr->sin_addr));
+        /* XXX IPv6 */
+        *addr = apr_pstrdup(sock->cntxt, inet_ntoa(sock->local_addr->sa.sin.sin_addr));
     } else if (which == APR_REMOTE)
-        *addr = apr_pstrdup(sock->cntxt, inet_ntoa(sock->remote_addr->sin_addr));
+        /* XXX IPv6 */
+        *addr = apr_pstrdup(sock->cntxt, inet_ntoa(sock->remote_addr->sa.sin.sin_addr));
     else 
         return APR_EINVAL;
 
@@ -149,9 +153,11 @@
             }
         }
 
-        *addr = *(apr_in_addr_t*)&sock->local_addr->sin_addr;
+        /* XXX IPv6 */
+        *addr = *(apr_in_addr_t *)&sock->local_addr->sa.sin.sin_addr;
     } else if (which == APR_REMOTE) {
-        *addr = *(apr_in_addr_t*)&sock->remote_addr->sin_addr;
+        /* XXX IPv6 */
+        *addr = *(apr_in_addr_t *)&sock->remote_addr->sa.sin.sin_addr;
     } else {
         return APR_EINVAL;
     }
Index: network_io/unix/sockaddr.c
===================================================================
RCS file: /home/cvspublic/apache-2.0/src/lib/apr/network_io/unix/sockaddr.c,v
retrieving revision 1.19
diff -u -r1.19 sockaddr.c
--- network_io/unix/sockaddr.c	2000/11/09 15:01:26	1.19
+++ network_io/unix/sockaddr.c	2000/11/15 19:21:21
@@ -75,12 +75,13 @@
 apr_status_t apr_set_ipaddr(apr_socket_t *sock, apr_interface_e which, const char *addr)
 {
     u_long ipaddr;
-    struct sockaddr_in* sa_ptr;
-    
+    struct sockaddr_in *sa_ptr;
+
+    /* XXX IPv6 */
     if (which == APR_LOCAL)
-        sa_ptr = sock->local_addr;
+        sa_ptr = &sock->local_addr->sa.sin;
     else if (which == APR_REMOTE)
-        sa_ptr = sock->remote_addr;
+        sa_ptr = &sock->remote_addr->sa.sin;
     else
         return APR_EINVAL;
     
@@ -100,6 +101,7 @@
 }
 
 #if APR_HAVE_NETINET_IN_H
+/* XXX IPv6 */
 apr_status_t apr_get_local_name(struct sockaddr_in **name, apr_socket_t *sock)
 {
     if (sock->local_port_unknown || sock->local_interface_unknown) {
@@ -110,15 +112,15 @@
         }
     }
 
-    *name = sock->local_addr;
+    *name = &sock->local_addr->sa.sin;
     return APR_SUCCESS;
 }
 
-
 
+/* XXX IPv6 */
 apr_status_t apr_get_remote_name(struct sockaddr_in **name, apr_socket_t *sock)
 {
-    *name = sock->remote_addr;
+    *name = &sock->remote_addr->sa.sin;
     return APR_SUCCESS;
 }
 #endif
Index: network_io/unix/sockets.c
===================================================================
RCS file: /home/cvspublic/apache-2.0/src/lib/apr/network_io/unix/sockets.c,v
retrieving revision 1.54
diff -u -r1.54 sockets.c
--- network_io/unix/sockets.c	2000/11/02 03:02:49	1.54
+++ network_io/unix/sockets.c	2000/11/15 19:21:21
@@ -67,33 +67,65 @@
     }
 }
 
-apr_status_t apr_create_tcp_socket(apr_socket_t **new, apr_pool_t *cont)
+static void set_socket_vars(apr_socket_t *sock, int family)
 {
-    (*new) = (apr_socket_t *)apr_pcalloc(cont, sizeof(apr_socket_t));
+    sock->local_addr->sa.sin.sin_family = family;
+    sock->remote_addr->sa.sin.sin_family = family;
 
-    if ((*new) == NULL) {
-        return APR_ENOMEM;
+    if (family == AF_INET) {
+        sock->local_addr->sa_len = sizeof(struct sockaddr_in);
+        sock->local_addr->addr_str_len = 16;
+        sock->local_addr->ipaddr_ptr = &(sock->local_addr->sa.sin.sin_addr);
+        sock->local_addr->ipaddr_len = sizeof(struct in_addr);
+
+        sock->remote_addr->sa_len = sizeof(struct sockaddr_in);
+        sock->remote_addr->addr_str_len = 16;
+        sock->remote_addr->ipaddr_ptr = &(sock->remote_addr->sa.sin.sin_addr);
+        sock->remote_addr->ipaddr_len = sizeof(struct in_addr);
+    }
+#if APR_HAVE_IPV6
+    else if (family == AF_INET6) {
+        sock->local_addr->sa_len = sizeof(struct sockaddr_in6);
+        sock->local_addr->addr_str_len = 46;
+        sock->local_addr->ipaddr_ptr = &(sock->local_addr->sa.sin6.sin6_addr);
+        sock->local_addr->ipaddr_len = sizeof(struct in6_addr);
+
+        sock->remote_addr->sa_len = sizeof(struct sockaddr_in6);
+        sock->remote_addr->addr_str_len = 46;
+        sock->remote_addr->ipaddr_ptr = &(sock->remote_addr->sa.sin6.sin6_addr);
+        sock->remote_addr->ipaddr_len = sizeof(struct in6_addr);
     }
-    (*new)->cntxt = cont; 
-    (*new)->local_addr = (struct sockaddr_in *)apr_pcalloc((*new)->cntxt,
-                         sizeof(struct sockaddr_in));
-    (*new)->remote_addr = (struct sockaddr_in *)apr_pcalloc((*new)->cntxt,
-                          sizeof(struct sockaddr_in));
+#endif
+}                                                                                       
          
+static void alloc_socket(apr_socket_t **new, apr_pool_t *p)
+{
+    *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t));
+    (*new)->cntxt = p;
+    (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->cntxt,
+                                                       sizeof(apr_sockaddr_t));
+    (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->cntxt,
+                                                        sizeof(apr_sockaddr_t));
+}
+
+apr_status_t apr_create_tcp_socket(apr_socket_t **new, apr_pool_t *cont)
+{
+    int family = AF_INET;
+    int proto = IPPROTO_TCP;
+    int type = SOCK_STREAM;
 
+    alloc_socket(new, cont);
+
     if ((*new)->local_addr == NULL || (*new)->remote_addr == NULL) {
         return APR_ENOMEM;
     }
- 
-    (*new)->socketdes = socket(AF_INET ,SOCK_STREAM, IPPROTO_TCP);
 
-    (*new)->local_addr->sin_family = AF_INET;
-    (*new)->remote_addr->sin_family = AF_INET;
+    (*new)->socketdes = socket(family, type, proto);
 
-    (*new)->addr_len = sizeof(*(*new)->local_addr);
-    
     if ((*new)->socketdes < 0) {
         return errno;
     }
+    set_socket_vars(*new, family);
+
     (*new)->timeout = -1;
     apr_register_cleanup((*new)->cntxt, (void *)(*new), 
                         socket_cleanup, apr_null_cleanup);
@@ -113,10 +145,12 @@
 
 apr_status_t apr_bind(apr_socket_t *sock)
 {
-    if (bind(sock->socketdes, (struct sockaddr *)sock->local_addr, sock->addr_len)
== -1)
+    if (bind(sock->socketdes, 
+             (struct sockaddr *)&sock->local_addr->sa, sock->local_addr->sa_len)
== -1)
         return errno;
     else {
-        if (sock->local_addr->sin_port == 0) { /* no need for ntohs() when comparing
w/ 0 */
+        /* XXX fix me for IPv6 */
+        if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing
w/ 0 */
             sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
         }
         return APR_SUCCESS;
@@ -133,28 +167,22 @@
 
 apr_status_t apr_accept(apr_socket_t **new, apr_socket_t *sock, apr_pool_t *connection_context)
 {
-    (*new) = (apr_socket_t *)apr_pcalloc(connection_context, 
-                            sizeof(apr_socket_t));
+    alloc_socket(new, connection_context);
+    set_socket_vars(*new, sock->local_addr->sa.sin.sin_family);
 
-    (*new)->cntxt = connection_context;
-    (*new)->local_addr = (struct sockaddr_in *)apr_pcalloc((*new)->cntxt, 
-                 sizeof(struct sockaddr_in));
-
-    (*new)->remote_addr = (struct sockaddr_in *)apr_pcalloc((*new)->cntxt, 
-                 sizeof(struct sockaddr_in));
-    (*new)->addr_len = sizeof(struct sockaddr_in);
 #ifndef HAVE_POLL
     (*new)->connected = 1;
 #endif
     (*new)->timeout = -1;
     
-    (*new)->socketdes = accept(sock->socketdes, (struct sockaddr *)(*new)->remote_addr,
-                        &(*new)->addr_len);
+    (*new)->remote_addr->sa_len = sizeof((*new)->remote_addr->sa);
+    (*new)->socketdes = accept(sock->socketdes, 
+                               (struct sockaddr *)&(*new)->remote_addr->sa,
+                               &(*new)->remote_addr->sa_len);
 
     if ((*new)->socketdes < 0) {
         return errno;
     }
-
     *(*new)->local_addr = *sock->local_addr;
 
     if (sock->local_port_unknown) {
@@ -163,7 +191,8 @@
     }
 
     if (sock->local_interface_unknown ||
-        sock->local_addr->sin_addr.s_addr == 0) {
+        /* XXX IPv6 issue */
+        sock->local_addr->sa.sin.sin_addr.s_addr == 0) {
         /* If the interface address inside the listening socket's local_addr wasn't 
          * up-to-date, we don't know local interface of the connected socket either.
          *
@@ -199,26 +228,30 @@
         if (!hp)  {
             return (h_errno + APR_OS_START_SYSERR);
         }
-    
-        memcpy((char *)&sock->remote_addr->sin_addr, hp->h_addr_list[0], 
+
+        /* XXX IPv6: move name resolution out of this function */
+        memcpy((char *)&sock->remote_addr->sa.sin.sin_addr, hp->h_addr_list[0],

                hp->h_length);
 
-        sock->addr_len = sizeof(*sock->remote_addr);
 #ifndef GETHOSTBYNAME_HANDLES_NAS
         }
 #endif
     }
 
-    if ((connect(sock->socketdes, (const struct sockaddr *)sock->remote_addr,
-        sock->addr_len) < 0) && (errno != EINPROGRESS)) {
+    if ((connect(sock->socketdes, 
+                 (const struct sockaddr *)&sock->remote_addr->sa.sin,
+                 sock->remote_addr->sa_len) < 0) &&
+        (errno != EINPROGRESS)) {
         return errno;
     }
     else {
-        if (sock->local_addr->sin_port == 0) {
+        /* XXX IPv6 */
+        if (sock->local_addr->sa.sin.sin_port == 0) {
             /* connect() got us an ephemeral port */
             sock->local_port_unknown = 1;
         }
-        if (sock->local_addr->sin_addr.s_addr == 0) {
+        /* XXX IPv6 */
+        if (sock->local_addr->sa.sin.sin_addr.s_addr == 0) {
             /* not bound to specific local interface; connect() had to assign
              * one for the socket
              */
@@ -252,19 +285,9 @@
                            apr_pool_t *cont)
 {
     if ((*sock) == NULL) {
-        (*sock) = (apr_socket_t *)apr_pcalloc(cont, sizeof(apr_socket_t));
-        (*sock)->cntxt = cont;
-        (*sock)->local_addr = (struct sockaddr_in *)apr_pcalloc((*sock)->cntxt,
-                             sizeof(struct sockaddr_in));
-        (*sock)->remote_addr = (struct sockaddr_in *)apr_pcalloc((*sock)->cntxt,
-                              sizeof(struct sockaddr_in));
-
-        if ((*sock)->local_addr == NULL || (*sock)->remote_addr == NULL) {
-            return APR_ENOMEM;
-        }
-     
-        (*sock)->addr_len = sizeof(*(*sock)->local_addr);
+        alloc_socket(sock, cont);
         (*sock)->timeout = -1;
+        /* XXX IPv6 figure out the family here! */
     }
     (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
     (*sock)->socketdes = *thesock;
Index: network_io/unix/sockopt.c
===================================================================
RCS file: /home/cvspublic/apache-2.0/src/lib/apr/network_io/unix/sockopt.c,v
retrieving revision 1.36
diff -u -r1.36 sockopt.c
--- network_io/unix/sockopt.c	2000/11/10 16:11:13	1.36
+++ network_io/unix/sockopt.c	2000/11/15 19:21:21
@@ -208,9 +208,9 @@
     apr_in_addr_t sa_ptr;
 
     if (which == APR_LOCAL)
-        sa_ptr = sock->local_addr->sin_addr;
+        sa_ptr = sock->local_addr->sa.sin.sin_addr;
     else if (which == APR_REMOTE)
-        sa_ptr = sock->remote_addr->sin_addr;
+        sa_ptr = sock->remote_addr->sa.sin.sin_addr;
     else
         return APR_EINVAL;

-- 
Jeff Trawick | trawick@ibm.net | PGP public key at web site:
     http://www.geocities.com/SiliconValley/Park/9289/
          Born in Roswell... married an alien...

Mime
View raw message