OSSP CVS Repository

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

Check-in Number: 1035
Date: 2001-Sep-26 10:55:19 (local)
2001-Sep-26 08:55:19 (UTC)
User:ms
Branch:
Comment: Cleanup before conversion to use sa_family socket calls.
Tickets:
Inspections:
Files:
ossp-pkg/l2/l2_ch_socket.c      1.28 -> 1.29     18 inserted, 18 deleted
ossp-pkg/l2/l2_sockmon.c      1.8 -> 1.9     93 inserted, 93 deleted

ossp-pkg/l2/l2_ch_socket.c 1.28 -> 1.29

--- l2_ch_socket.c       2001/09/13 19:18:34     1.28
+++ l2_ch_socket.c       2001/09/26 08:55:19     1.29
@@ -45,31 +45,31 @@
 /******************************************************************
 IPv4 source used from Unix Network Programming by Richard STEVENS
  ******************************************************************/
-static int l2_inet_pton(int iFamily, const char *pszAddress, void *pvMemref)
+static int l2_inet_pton(int iFamily, const char *szAddress, void *pvMemref)
 {
-    int             i             = 0;
+    int             i            = 0;
     struct in_addr  IP4Addr;
     struct in6_addr IP6Addr;
-    const char      *pszIndex     = NULL;    /* To index IPv6 validity */
-    char            *pszNextfield = "";      /* For IPv6 address trans */
+    const char      *szIndex     = NULL;    /* To index IPv6 validity */
+    char            *szNextfield = "";      /* For IPv6 address trans */
 
     assert(pvMemref != NULL);
     if (iFamily == AF_INET) {
-        if (inet_aton(pszAddress, &IP4Addr)) {
+        if (inet_aton(szAddress, &IP4Addr)) {
             memcpy(pvMemref, &IP4Addr, sizeof(struct in_addr));
             return 1; /* Success */
         }
         return 0;
     }
     else if (iFamily == AF_INET6) { /* Translate IPv6 addresses               */
-        pszIndex = pszAddress;      /* Initialize, verify in loop             */
-        for (i = 0; (i < 8) && pszNextfield; i++) { /* Iterate through fields */
+        szIndex = szAddress;        /* Initialize, verify in loop             */
+        for (i = 0; (i < 8) && szNextfield; i++) { /* Iterate through fields  */
             IP6Addr.__u6_addr.__u6_addr16[i] =\
-                ntohs((u_int16_t)strtol(pszIndex, &pszNextfield, 16));
-            pszIndex = pszNextfield + 1;
+                ntohs((u_int16_t)strtol(szIndex, &szNextfield, 16));
+            szIndex = szNextfield + 1;
         }
 
-        if ((i != 8) || (*pszNextfield))
+        if ((i != 8) || (*szNextfield))
             return 0;
         else { /* Success */
             memcpy(pvMemref, &IP6Addr, sizeof(struct in6_addr));
@@ -87,7 +87,7 @@
 
 /* declare private channel configuration */
 typedef struct {
-    char *pszHost;           /* IP Address or name of host to connect to    */
+    char *szHost;            /* IP Address or name of host to connect to    */
     int  iFamily;            /* IP version to use, AF_INET4 or AF_INET6     */
     int  iProto;             /* Protocol to use, IPPROTO_TCP or IPPROTO_UDP */
     int  iPort;              /* TCP Port to connect to                      */
@@ -104,7 +104,7 @@
         return L2_ERR_MEM;
 
     /* initialize configuration with reasonable defaults */
-    cfg->pszHost   = NULL;
+    cfg->szHost   = NULL;
     cfg->iFamily   = -1;
     cfg->iProto    = -1;
     cfg->iPort     = 0;
@@ -126,7 +126,7 @@
     /* feed and call generic parameter parsing engine */
     L2_PARAM_SET(pa[0], ipversion, INT,     &cfg->iFamily);
     L2_PARAM_SET(pa[1], protocol,  INT,     &cfg->iProto );
-    L2_PARAM_SET(pa[2], host,      CHARPTR, &cfg->pszHost);
+    L2_PARAM_SET(pa[2], host,      CHARPTR, &cfg->szHost);
     L2_PARAM_SET(pa[3], port,      INT,     &cfg->iPort  );
     L2_PARAM_END(pa[4]);
     rv = l2_util_setparams(pa, fmt, ap);
@@ -143,7 +143,7 @@
     struct sockaddr_in6 IP6Localsock, IP6Sockaddr;
 
     /* make sure a target is configured */
-    if (cfg->pszHost == NULL)
+    if (cfg->szHost == NULL)
         return L2_ERR_USE;
 
     /* open channel socket */
@@ -153,12 +153,12 @@
         memset(&IP4Sockaddr,  0, sizeof(IP4Sockaddr));
 
         /* resolve host numerically */
-        if (l2_inet_pton(AF_INET, cfg->pszHost, &IP4Sockaddr.sin_addr.s_addr) == 1) {
+        if (l2_inet_pton(AF_INET, cfg->szHost, &IP4Sockaddr.sin_addr.s_addr) == 1) {
             IP4Sockaddr.sin_family = AF_INET;
             IP4Sockaddr.sin_port   = htons(cfg->iPort);
         }
         /* resolve host nominally */
-        else if ((pHostentry = gethostbyname(cfg->pszHost)) != NULL) {
+        else if ((pHostentry = gethostbyname(cfg->szHost)) != NULL) {
             if (pHostentry->h_addrtype == AF_INET) {
                 IP4Sockaddr.sin_family = AF_INET;
                 IP4Sockaddr.sin_port   = htons(cfg->iPort);
@@ -214,12 +214,12 @@
         memset(&IP6Sockaddr,  0, sizeof(IP6Sockaddr));
 
         /* resolve host numerically */
-        if (l2_inet_pton(AF_INET6, cfg->pszHost, &IP6Sockaddr.sin6_addr.s6_addr) == 1) {
+        if (l2_inet_pton(AF_INET6, cfg->szHost, &IP6Sockaddr.sin6_addr.s6_addr) == 1) {
             IP6Sockaddr.sin6_family = AF_INET6;
             IP6Sockaddr.sin6_port   = htons(cfg->iPort);
         }
         /* resolve host nominally */
-        else if ((pHostentry = gethostbyname(cfg->pszHost)) != NULL) {
+        else if ((pHostentry = gethostbyname(cfg->szHost)) != NULL) {
             if (pHostentry->h_addrtype == AF_INET6) {
                 IP6Sockaddr.sin6_family = AF_INET6;
                 IP6Sockaddr.sin6_port   = htons(cfg->iPort);


ossp-pkg/l2/l2_sockmon.c 1.8 -> 1.9

--- l2_sockmon.c 2001/09/13 19:18:34     1.8
+++ l2_sockmon.c 2001/09/26 08:55:19     1.9
@@ -27,10 +27,13 @@
 **  l2_sockmon.c: Socket monitor for use with l2_test.c
 */
 
+#include <ctype.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <errno.h>
 #include <assert.h>
 
@@ -39,56 +42,73 @@
 #define BACKLOG 1024    /* max # pending connections */
 #define BUFFSIZ 256     /* max string size           */
 
-char *strlowerdup(const char *pszIn)
+char *strlowerdup(const char *);       /* prototypes */
+void dowork(int);
+int  myserver(int, int, int);
+void die(const char *);
+void closedie(int, const char *);
+
+void die(const char *szMsg)
+{
+    perror(szMsg);
+    exit(1);
+}
+
+void closedie(int iSock, const char *szMsg)
+{
+    perror(szMsg);
+    close(iSock);
+    exit(1);
+}
+
+char *strlowerdup(const char *szIn)
 {
-    int  i        = 0;
-    char *pszTemp = NULL;
+    int  i       = 0;
+    char *szTemp = NULL;
 
-    assert(pszIn != NULL);
+    assert(szIn != NULL);
 
-    if ((pszTemp = (char *)strdup(pszIn)) == NULL)
+    if ((szTemp = (char *)strdup(szIn)) == NULL)
         return NULL;
     else
     {
-        for (i = 0; *(pszTemp + i); i++)
-            if (isupper(*(pszTemp + i)))
-                *(pszTemp + i) = (char)tolower(*(pszTemp + i));
-        return pszTemp;
+        for (i = 0; *(szTemp + i); i++)
+            if (isupper(*(szTemp + i)))
+                *(szTemp + i) = (char)tolower(*(szTemp + i));
+        return szTemp;
     }
 }
 
 void dowork(int iSock)
 {
-    char pszBuf[BUFFSIZ];
+    char szBuf[BUFFSIZ];
     int  cc;
 
     while (1) { /* Exits when read finishes */
-        cc = read(iSock, pszBuf, sizeof (pszBuf));
-        if (cc == -1) {
-            perror("read");
-            exit(1);
-        }
+        cc = read(iSock, szBuf, sizeof (szBuf));
+        if (cc == -1)
+            die("read()");
 
         if (cc == 0) {
             /* EOF */
-            (void) close(iSock);
-            (void) printf("Connection closed\n");
+            close(iSock);
+            fprintf(stderr, "Connection closed\n");
             return;
         }
 
-        pszBuf[cc + 1] = '\0';
-        (void) printf("Read: %s", pszBuf);
+        szBuf[cc + 1] = '\0';
+        fprintf(stderr, "Read: %s", szBuf);
 
-        if (write(iSock, pszBuf, cc) == -1) {
-            perror("write");
-            exit(1);
-        }
+        if (write(iSock, szBuf, cc) == -1)
+            die("write()");
     }
 }
 
 int myserver(int iFamily, int iProtocol, int iPort)
 {
-    char pszBuf[BUFFSIZ];
+    char                szBuf[BUFFSIZ];
+    char                szAddrin4[INET_ADDRSTRLEN];
+    char                szAddrin6[INET6_ADDRSTRLEN];
     struct sockaddr_in  laddr4,   faddr4,    caddr4;
     struct sockaddr_in6 laddr6,   faddr6,    caddr6;
     int                 iSock,    iNewsock;
@@ -128,28 +148,19 @@
         return -1;
     }
 
-    /* Tell the system to allow local addresses to be reused. */
+    /* Allow local addresses to be reused. */
     iSockopt = 1;
-    if (setsockopt(iSock, SOL_SOCKET, SO_REUSEADDR, (void *)&iSockopt,\
-        sizeof (iSockopt)) == -1) {
-        perror("setsockopt(SO_REUSEADDR)");
-        (void) close(iSock);
-        return -1;
-    }
+    if (setsockopt(iSock, SOL_SOCKET, SO_REUSEADDR, (void *)&iSockopt, sizeof (iSockopt)) == -1)
+        closedie(iSock, "setsockopt(SO_REUSEADDR)");
 
     if (iFamily == AF_INET6) {
-        if (bind(iSock, (struct sockaddr *)&laddr6, sizeof (laddr6)) == -1) {
-            perror("bind"); /* Bad bind */
-            (void) close(iSock);
-            return -1;
-            }
+        if (bind(iSock, (struct sockaddr *)&laddr6, sizeof (laddr6)) == -1)
+            closedie(iSock, "bind()");
     }
+
     else if (iFamily == AF_INET) {
-        if (bind(iSock, (struct sockaddr *)&laddr4, sizeof (laddr4)) == -1) {
-            perror("bind"); /* Bad bind */
-            (void) close(iSock);
-            return -1;
-            }
+        if (bind(iSock, (struct sockaddr *)&laddr4, sizeof (laddr4)) == -1)
+            closedie(iSock, "bind()");
     }
 
     if (iProtocol == IPPROTO_UDP) {
@@ -159,30 +170,25 @@
 
             for (;;) { /* Receive messages */
                 faddrlen = sizeof(caddr6);
-                memset(pszBuf, 0x0, BUFFSIZ); /* Init the buffer */
-                cc = recvfrom(iSock, pszBuf, BUFFSIZ, 0, (struct sockaddr *)\
+                memset(szBuf, 0x0, BUFFSIZ); /* Init the buffer */
+                cc = recvfrom(iSock, szBuf, BUFFSIZ, 0, (struct sockaddr *)\
                     &caddr6, &faddrlen);
 
-                if (cc == -1) {
-                    perror("recvfrom");
-                    exit(1);
-                }
+                if (cc == -1)
+                    die("recvfrom()");
 
-                if (cc == 0) {
-                    /* EOF */
-                    (void) close(iSock);
-                    (void) printf("Connection closed\n");
-                    return;
+                if (cc == 0) { /* EOF */
+                    close(iSock);
+                    fprintf(stderr, "Connection closed\n");
+                    return 0;
                 }
 
                 if (cc != BUFFSIZ) {
-                    pszBuf[cc + 1] = '\0';
-                    (void) printf("Read: %s", pszBuf);
+                    szBuf[cc + 1] = '\0';
+                    fprintf(stderr, "Read: %s", szBuf);
 
-                    if (write(iSock, pszBuf, cc) == -1) {
-                        perror("write");
-                        exit(1);
-                    }
+                    if (write(iSock, szBuf, cc) == -1)
+                        die("write()");
                 }
                 else
                     return -1;
@@ -190,34 +196,29 @@
         }
         else if (iFamily == AF_INET) {
             /* initialize address structure */
-            memset(&caddr4,  0, sizeof(caddr4));
+            memset(&caddr4, 0, sizeof(caddr4));
 
             for (;;) { /* Receive messages */
                 faddrlen = sizeof(caddr4);
-                memset(pszBuf, 0x0, BUFFSIZ); /* Init the buffer */
-                cc = recvfrom(iSock, pszBuf, BUFFSIZ, 0, (struct sockaddr *)\
+                memset(szBuf, 0x0, BUFFSIZ); /* Init the buffer */
+                cc = recvfrom(iSock, szBuf, BUFFSIZ, 0, (struct sockaddr *)\
                     &caddr4, &faddrlen);
 
-                if (cc == -1) {
-                    perror("recvfrom");
-                    exit(1);
-                }
+                if (cc == -1)
+                    die("recvfrom()");
 
-                if (cc == 0) {
-                    /* EOF */
-                    (void) close(iSock);
-                    (void) printf("Connection closed\n");
-                    return;
+                if (cc == 0) { /* EOF */
+                    close(iSock);
+                    fprintf(stderr, "Connection closed\n");
+                    return 0;
                 }
 
                 if (cc != BUFFSIZ) {
-                    pszBuf[cc + 1] = '\0';
-                    (void) printf("Read: %s", pszBuf);
+                    szBuf[cc + 1] = '\0';
+                    fprintf(stderr, "Read: %s", szBuf);
 
-                    if (write(iSock, pszBuf, cc) == -1) {
-                        perror("write");
-                        exit(1);
-                    }
+                    if (write(iSock, szBuf, cc) == -1)
+                        die("write()");
                 }
                 else
                     return -1;
@@ -225,11 +226,8 @@
         }
     }
     else if (iProtocol == IPPROTO_TCP) { /* Only try to listen if we have TCP */
-        if (listen(iSock, BACKLOG) == -1) {
-            perror("listen"); /* Listen just blew up */
-            (void) close(iSock);
-            return -1;
-        }
+        if (listen(iSock, BACKLOG) == -1)
+            closedie(iSock, "listen()");
 
         /* Wait for a connection request */
         if (iFamily == AF_INET6) {
@@ -241,13 +239,17 @@
                 iNewsock = accept(iSock, (struct sockaddr *)&faddr6, &faddrlen);
                 if (iNewsock == -1) {
                     if (errno != EINTR && errno != ECONNABORTED) {
-                        perror("accept");
+                        perror("accept()");
                     }
                     continue;
                 }
-                (void) printf("Connection from %s/%d\n",\
-                    inet_ntop(faddr6.sin6_addr), ntohs(faddr6.sin6_port));
-                dowork(iNewsock); /* do some sockwork */
+
+                if (inet_ntop(AF_INET6, &faddr6.sin6_addr, szAddrin6, sizeof(szAddrin6)) != NULL) {
+                    fprintf(stderr, "Connection from %s/%d\n", szAddrin6, ntohs(faddr6.sin6_port));
+                    dowork(iNewsock); /* do some sockwork */
+                }
+                else
+                    die("inet_ntop()");
             }
         }
         else if (iFamily == AF_INET) {
@@ -259,19 +261,17 @@
                 iNewsock = accept(iSock, (struct sockaddr *)&faddr4, &faddrlen);
                 if (iNewsock == -1) {
                     if (errno != EINTR && errno != ECONNABORTED) {
-                        perror("accept");
+                        perror("accept()");
                     }
                     continue;
                 }
-                (void) printf("Connection from %s/%d\n",\
-                    inet_ntop(faddr4.sin_addr), ntohs(faddr4.sin_port));
+                inet_ntop(AF_INET6, &faddr4.sin_addr, szAddrin4, sizeof(szAddrin4));
+                fprintf(stderr, "Connection from %s/%d\n", szAddrin4, ntohs(faddr6.sin6_port));
                 dowork(iNewsock); /* do some sockwork */
             }
         }
-        /* NOTREACHED */
-    }
-    else
-        return -1; /* We are being asked to serve other than TCP or UDP */
+    } /* NOTREACHED */
+    return -1; /* Failure due to non UDP/TCP request, or execution flow */
 }
 
 int main(int argc, char *argv[])
@@ -281,7 +281,7 @@
     int iPort  = 0;
 
     if (argc != 4) {
-        (void) fprintf(stderr, "Usage: %s <IPv4-or-IPv6> <TCP-or-UDP> <Port>\n", argv[0]);
+        fprintf(stderr, "Usage: %s <IPv4-or-IPv6> <TCP-or-UDP> <Port>\n", argv[0]);
         exit(1); /* Dink donk if the user doesn't know how to use this */
     }
 

CVSTrac 2.0.1