OSSP CVS Repository

ossp - Check-in [886]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 886
Date: 2001-Sep-07 15:03:07 (local)
2001-Sep-07 13:03:07 (UTC)
User:ms
Branch:
Comment: Bug fixes to both IPv4 and IPv6 socket binding.
Tickets:
Inspections:
Files:
ossp-pkg/l2/l2_ch_socket.c      1.19 -> 1.20     58 inserted, 8 deleted
ossp-pkg/l2/l2_sockmon.c      1.3 -> 1.4     26 inserted, 0 deleted

ossp-pkg/l2/l2_ch_socket.c 1.19 -> 1.20

--- l2_ch_socket.c       2001/09/07 10:23:31     1.19
+++ l2_ch_socket.c       2001/09/07 13:03:07     1.20
@@ -139,8 +139,8 @@
     int                 i;
     l2_ch_socket_t      *cfg = (l2_ch_socket_t *)ctx->vp;
     struct hostent      *pHostentry;
-    struct sockaddr_in  IP4Sockaddr;
-    struct sockaddr_in6 IP6Sockaddr;
+    struct sockaddr_in  IP4Localsock, IP4Sockaddr;
+    struct sockaddr_in6 IP6Localsock, IP6Sockaddr;
     struct in6_addr     *IP6Addr;
 
     /* make sure a target is configured */
@@ -149,6 +149,10 @@
 
     /* open channel socket */
     if (cfg->iFamily == AF_INET) { /* IPv4 */
+        /* initialize address structures */
+        memset(&IP4Localsock, 0, sizeof(IP4Localsock));
+        memset(&IP4Sockaddr,  0, sizeof(IP4Sockaddr));
+
         /* resolve host numerically */
         if (inet_pton(AF_INET, cfg->pszHost, &IP4Sockaddr.sin_addr.s_addr) == 1) {
             IP4Sockaddr.sin_family = AF_INET;
@@ -168,23 +172,48 @@
         else /* host string was not parsable for some reason */
             return L2_ERR_SYS;
 
-        if (cfg->iProto == IPPROTO_TCP) {        /* With TCP we must connect */
+        if (cfg->iProto == IPPROTO_TCP) {        /* with TCP we must connect */
             if ((cfg->iSocket = socket(PF_INET, SOCK_STREAM, cfg->iProto)) == -1)
                 return L2_ERR_SYS;
 
+            /* if we have a socket, then bind ourselves to any port number   */
+            IP4Localsock.sin_family      = AF_INET;
+            IP4Localsock.sin_addr.s_addr = htonl(INADDR_ANY);
+            IP4Localsock.sin_port        = htons(0);
+
+            if (bind(cfg->iSocket, (struct sockaddr *)&IP4Localsock,\
+                sizeof(IP4Localsock))) {
+                return L2_ERR_SYS;
+            }
+
             if (connect(cfg->iSocket, (struct sockaddr *)&IP4Sockaddr,
                 sizeof(IP4Sockaddr))) {
                 close(cfg->iSocket);
                 return L2_ERR_SYS;
             }
         }
-        else if (cfg->iProto == IPPROTO_UDP) /* With UDP we do not connect */
+        else if (cfg->iProto == IPPROTO_UDP) { /* with UDP we do not connect  */
             if ((cfg->iSocket = socket(PF_INET, SOCK_DGRAM, cfg->iProto)) == -1)
                 return L2_ERR_SYS;
 
+            /* if we have a socket, then bind ourselves to any port number   */
+            IP4Localsock.sin_family      = AF_INET;
+            IP4Localsock.sin_addr.s_addr = htonl(INADDR_ANY);
+            IP4Localsock.sin_port        = htons(0);
+
+            if (bind(cfg->iSocket, (struct sockaddr *)&IP4Localsock,\
+                sizeof(IP4Localsock))) {
+                return L2_ERR_SYS;
+            }
+        }
+
         return L2_OK;
     }
     else if (cfg->iFamily == AF_INET6) { /* IPv6 */
+        /* initialize address structures */
+        memset(&IP6Localsock, 0, sizeof(IP6Localsock));
+        memset(&IP6Sockaddr,  0, sizeof(IP6Sockaddr));
+
         /* resolve host numerically */
         if (inet_pton(AF_INET6, cfg->pszHost, &IP6Sockaddr.sin6_addr.s6_addr) == 1) {
             IP6Sockaddr.sin6_family = AF_INET6;
@@ -199,25 +228,46 @@
                     sizeof(IP6Sockaddr.sin6_addr.__u6_addr));
             }
             else
-                return L2_ERR_USE; /* what?? we specify IPv6 when it is not? */
+                return L2_ERR_USE; /* what?? we specify IPv6 when it is not?  */
         }
-        else /* host string was not parsable for some reason */
+        else                  /* host string was not parsable for some reason */
             return L2_ERR_SYS;
 
-        if (cfg->iProto == IPPROTO_TCP) { /* With TCP we must connect */
+        if (cfg->iProto == IPPROTO_TCP) {         /* with TCP we must connect */
             if ((cfg->iSocket = socket(PF_INET6, SOCK_STREAM, cfg->iProto)) == -1)
                 return L2_ERR_SYS;
 
+            /* if we have a socket, then bind ourselves to any port number    */
+            IP6Localsock.sin6_family = AF_INET6;
+            IP6Localsock.sin6_addr   = in6addr_any;
+            IP6Localsock.sin6_port   = htons(0);
+
+            if (bind(cfg->iSocket, (struct sockaddr *)&IP6Localsock,\
+                sizeof(IP6Localsock))) {
+                return L2_ERR_SYS;
+            }
+
             if (connect(cfg->iSocket, (struct sockaddr *)&IP6Sockaddr,\
                 sizeof(IP6Sockaddr))) {
                 close(cfg->iSocket);
                 return L2_ERR_SYS;
             }
         }
-        else if (cfg->iProto == IPPROTO_UDP) /* With UDP we do not connect */
+        else if (cfg->iProto == IPPROTO_UDP) {  /* with UDP we do not connect */
             if ((cfg->iSocket = socket(PF_INET, SOCK_DGRAM, cfg->iProto)) == -1)
                 return L2_ERR_SYS;
 
+            /* if we have a socket, then bind ourselves to any port number    */
+            IP6Localsock.sin6_family = AF_INET6;
+            IP6Localsock.sin6_addr   = in6addr_any;
+            IP6Localsock.sin6_port   = htons(0);
+
+            if (bind(cfg->iSocket, (struct sockaddr *)&IP6Localsock,\
+                sizeof(IP6Localsock))) {
+                return L2_ERR_SYS;
+            }
+        }
+
         return L2_OK;
     }
 


ossp-pkg/l2/l2_sockmon.c 1.3 -> 1.4

--- l2_sockmon.c 2001/09/06 19:29:14     1.3
+++ l2_sockmon.c 2001/09/07 13:03:07     1.4
@@ -27,6 +27,8 @@
 **  l2_sockmon.c: Socket monitor for use with l2_test.c
 */
 
+#include <l2.h>
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -100,6 +102,10 @@
     /* Set up an IPv4 TCP socket to listen on for connections. */
     if (iFamily == AF_INET6)
     {
+        /* initialize address structures */
+        memset(&laddr6,  0, sizeof(laddr6));
+        memset(&laddr6,  0, sizeof(laddr6));
+
         laddr6.sin6_family      = AF_INET6;
         laddr6.sin6_flowinfo    = 0;
         laddr6.sin6_port        = iPort;
@@ -107,6 +113,10 @@
     }
     else if (iFamily == AF_INET)
     {
+        /* initialize address structures */
+        memset(&laddr4,  0, sizeof(laddr4));
+        memset(&laddr4,  0, sizeof(laddr4));
+
         laddr4.sin_family       = AF_INET;
         laddr4.sin_port         = iPort;
         laddr4.sin_addr.s_addr  = INADDR_ANY;
@@ -149,6 +159,10 @@
 
     if (iProtocol == IPPROTO_UDP) {
         if (iFamily == AF_INET6) {
+            /* initialize address structures */
+            memset(&caddr6,  0, sizeof(caddr6));
+            memset(&caddr6,  0, sizeof(caddr6));
+
             for (;;) { /* Receive messages */
                 faddrlen = sizeof(caddr6);
                 memset(pszBuf, 0x0, BUFFSIZ); /* Init the buffer */
@@ -181,6 +195,10 @@
             }
         }
         else if (iFamily == AF_INET) {
+            /* initialize address structures */
+            memset(&caddr4,  0, sizeof(caddr4));
+            memset(&caddr4,  0, sizeof(caddr4));
+
             for (;;) { /* Receive messages */
                 faddrlen = sizeof(caddr4);
                 memset(pszBuf, 0x0, BUFFSIZ); /* Init the buffer */
@@ -222,6 +240,10 @@
 
         /* Wait for a connection request */
         if (iFamily == AF_INET6) {
+            /* initialize address structures */
+            memset(&faddr6,  0, sizeof(faddr6));
+            memset(&faddr6,  0, sizeof(faddr6));
+
             for (;;) {
                 faddrlen = sizeof (faddr6);
                 iNewsock = accept(iSock, (struct sockaddr *)&faddr6, &faddrlen);
@@ -237,6 +259,10 @@
             }
         }
         else if (iFamily == AF_INET) {
+            /* initialize address structures */
+            memset(&faddr4,  0, sizeof(faddr4));
+            memset(&faddr4,  0, sizeof(faddr4));
+
             for (;;) {
                 faddrlen = sizeof (faddr4);
                 iNewsock = accept(iSock, (struct sockaddr *)&faddr4, &faddrlen);

CVSTrac 2.0.1