OSSP CVS Repository

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

Check-in Number: 4468
Date: 2004-Mar-26 18:14:48 (local)
2004-Mar-26 17:14:48 (UTC)
User:thl
Branch:
Comment: import most recent lib_sa
Tickets:
Inspections:
Files:
ossp-pkg/l2/l2_ut_sa.ac      1.7 -> 1.8     48 inserted, 4 deleted
ossp-pkg/l2/l2_ut_sa.c      1.22 -> 1.23     184 inserted, 123 deleted
ossp-pkg/l2/l2_ut_sa.h      1.19 -> 1.20     5 inserted, 3 deleted

ossp-pkg/l2/l2_ut_sa.ac 1.7 -> 1.8

--- l2_ut_sa.ac  2002/11/09 14:44:31     1.7
+++ l2_ut_sa.ac  2004/03/26 17:14:48     1.8
@@ -1,8 +1,8 @@
 dnl ##
 dnl ##  OSSP sa - Socket Abstraction
-dnl ##  Copyright (c) 2001-2002 Ralf S. Engelschall <rse@engelschall.com>
-dnl ##  Copyright (c) 2001-2002 The OSSP Project <http://www.ossp.org/>
-dnl ##  Copyright (c) 2001-2002 Cable & Wireless Deutschland <http://www.cw.com/de/>
+dnl ##  Copyright (c) 2001-2003 Ralf S. Engelschall <rse@engelschall.com>
+dnl ##  Copyright (c) 2001-2003 The OSSP Project <http://www.ossp.org/>
+dnl ##  Copyright (c) 2001-2003 Cable & Wireless Deutschland <http://www.cw.com/de/>
 dnl ##
 dnl ##  This file is part of OSSP sa, a socket abstraction library which
 dnl ##  can be found at http://www.ossp.org/pkg/lib/sa/.
@@ -53,6 +53,48 @@
     fi
 ])
 
+dnl #  Check whether to use SO_RCVTIMEO|SO_SNDTIMEO with setsockopt(2)
+dnl #   configure.ac:
+dnl #     SA_CHECK_SOCKOPT(SO_RCVTIMEO)
+dnl #     SA_CHECK_SOCKOPT(SO_SNDTIMEO)
+dnl #   config.h:
+dnl #     #undef USE_SO_RCVTIMEO or #define USE_SO_RCVTIMEO 1
+dnl #     #undef USE_SO_SNDTIMEO or #define USE_SO_SNDTIMEO 1
+
+AC_DEFUN(SA_CHECK_SOCKOPT, [
+AC_MSG_CHECKING(whether to use $1 with setsockopt(2))
+AC_TRY_RUN([
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+
+int main(void)
+{
+    int s;
+    struct timeval timeo;
+    timeo.tv_sec  = 3;
+    timeo.tv_usec = 3;
+
+#ifndef $1
+    exit(3);
+#else
+    if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1)
+        exit(2);
+
+    /* fails on Solaris 2.6,8,9,10 and Debian 2.2 because
+       SO_RCVTIMEO|SO_SNDTIMEO are defined but not implemented */
+    if (setsockopt(s, SOL_SOCKET, $1, (void *)&timeo, sizeof(timeo)) == -1)
+        exit(1);
+
+    exit(0);
+#endif
+}
+]
+, [ AC_MSG_RESULT([yes]) AC_DEFINE(USE_$1, 1, [Define to use $1 with setsockopt(2)]) ]
+, [ AC_MSG_RESULT([no]) ]
+)
+])dnl
+
 dnl #   Check for anything OSSP SA wants to know
 dnl #   configure.in:
 dnl #     SA_CHECK_ALL
@@ -72,10 +114,12 @@
     AC_CHECK_HEADERS(string.h sys/types.h sys/socket.h netdb.h netinet/in.h)
 
     #   check for system functions
-    AC_CHECK_FUNCS(inet_addr inet_aton inet_pton inet_ntoa inet_ntop snprintf)
+    AC_CHECK_FUNCS(inet_addr inet_aton inet_pton inet_ntoa inet_ntop snprintf getaddrinfo)
 
     dnl # check for network/socket size type
     SA_CHECK_TYPEDEF(socklen_t, sys/socket.h)
     SA_CHECK_TYPEDEF(ssize_t, sys/types.h)
+    SA_CHECK_SOCKOPT(SO_RCVTIMEO)
+    SA_CHECK_SOCKOPT(SO_SNDTIMEO)
 ])
 


ossp-pkg/l2/l2_ut_sa.c 1.22 -> 1.23

--- l2_ut_sa.c   2004/02/17 09:21:04     1.22
+++ l2_ut_sa.c   2004/03/26 17:14:48     1.23
@@ -29,7 +29,9 @@
 */
 
 /* include optional Autoconf header */
-#include "l2_config.h"
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 /* include system API headers */
 #include <stdio.h>       /* for "s[n]printf()" */
@@ -37,7 +39,7 @@
 #include <stdarg.h>      /* for "va_XXX()" and "va_list" */
 #include <string.h>      /* for "strXXX()" and "size_t" */
 #include <sys/types.h>   /* for general prerequisites */
-#include <ctype.h>       /* for isXXX() */
+#include <ctype.h>       /* for "isXXX()" */
 #include <errno.h>       /* for "EXXX" */
 #include <fcntl.h>       /* for "F_XXX" and "O_XXX" */
 #include <unistd.h>      /* for standard Unix stuff */
@@ -102,7 +104,7 @@
 #endif
 
 /* backward compatibility for ssize_t */
-#if !defined(HAVE_SSIZE_T)
+#if defined(HAVE_CONFIG_H) && !defined(HAVE_SSIZE_T)
 #define ssize_t long
 #endif
 
@@ -114,49 +116,49 @@
 /* system call structure declaration macros */
 #define SA_SC_DECLARE_0(rc_t, fn) \
     struct { \
-        union { void (*any)(); \
+        union { void (*any)(void); \
                 rc_t (*std)(void); \
                 rc_t (*ctx)(void *); } fptr; \
         void *fctx; \
     } sc_##fn;
 #define SA_SC_DECLARE_1(rc_t, fn, a1_t) \
     struct { \
-        union { void (*any)(); \
+        union { void (*any)(void); \
                 rc_t (*std)(a1_t); \
                 rc_t (*ctx)(void *, a1_t); } fptr; \
         void *fctx; \
     } sc_##fn;
 #define SA_SC_DECLARE_2(rc_t, fn, a1_t, a2_t) \
     struct { \
-        union { void (*any)(); \
+        union { void (*any)(void); \
                 rc_t (*std)(a1_t, a2_t); \
                 rc_t (*ctx)(void *, a1_t, a2_t); } fptr; \
         void *fctx; \
     } sc_##fn;
 #define SA_SC_DECLARE_3(rc_t, fn, a1_t, a2_t, a3_t) \
     struct { \
-        union { void (*any)(); \
+        union { void (*any)(void); \
                 rc_t (*std)(a1_t, a2_t, a3_t); \
                 rc_t (*ctx)(void *, a1_t, a2_t, a3_t); } fptr; \
         void *fctx; \
     } sc_##fn;
 #define SA_SC_DECLARE_4(rc_t, fn, a1_t, a2_t, a3_t, a4_t) \
     struct { \
-        union { void (*any)(); \
+        union { void (*any)(void); \
                 rc_t (*std)(a1_t, a2_t, a3_t, a4_t); \
                 rc_t (*ctx)(void *, a1_t, a2_t, a3_t, a4_t); } fptr; \
         void *fctx; \
     } sc_##fn;
 #define SA_SC_DECLARE_5(rc_t, fn, a1_t, a2_t, a3_t, a4_t, a5_t) \
     struct { \
-        union { void (*any)(); \
+        union { void (*any)(void); \
                 rc_t (*std)(a1_t, a2_t, a3_t, a4_t, a5_t); \
                 rc_t (*ctx)(void *, a1_t, a2_t, a3_t, a4_t, a5_t); } fptr; \
         void *fctx; \
     } sc_##fn;
 #define SA_SC_DECLARE_6(rc_t, fn, a1_t, a2_t, a3_t, a4_t, a5_t, a6_t) \
     struct { \
-        union { void (*any)(); \
+        union { void (*any)(void); \
                 rc_t (*std)(a1_t, a2_t, a3_t, a4_t, a5_t, a6_t); \
                 rc_t (*ctx)(void *, a1_t, a2_t, a3_t, a4_t, a5_t, a6_t); } fptr; \
         void *fctx; \
@@ -165,7 +167,7 @@
 /* system call structure assignment macro */
 #define SA_SC_ASSIGN(sa, fn, ptr, ctx) \
     do { \
-        (sa)->scSysCall.sc_##fn.fptr.any = (void (*)())(ptr); \
+        (sa)->scSysCall.sc_##fn.fptr.any = (void (*)(void))(ptr); \
         (sa)->scSysCall.sc_##fn.fctx = (ctx); \
     } while (0)
 
@@ -180,7 +182,7 @@
 #define SA_SC_CALL_0(sa, fn) \
     (   (sa)->scSysCall.sc_##fn.fctx != NULL \
      ? ((sa)->scSysCall.sc_##fn.fptr.ctx)((sa)->scSysCall.sc_##fn.fctx) \
-     : ((sa)->scSysCall.sc_##fn.fptr.std)() )
+     : ((sa)->scSysCall.sc_##fn.fptr.std)(void) )
 #define SA_SC_CALL_1(sa, fn, a1) \
     (   (sa)->scSysCall.sc_##fn.fctx != NULL \
      ? ((sa)->scSysCall.sc_##fn.fptr.ctx)((sa)->scSysCall.sc_##fn.fctx, a1) \
@@ -250,6 +252,7 @@
     ((tv).tv_sec == 0 && (tv).tv_usec == 0)
 
 /* convert Internet address from presentation to network format */
+#ifndef HAVE_GETADDRINFO
 static int sa_inet_pton(int family, const char *strptr, void *addrptr)
 {
 #ifdef HAVE_INET_PTON
@@ -276,6 +279,7 @@
     return 0;
 #endif
 }
+#endif /* !HAVE_GETADDRINFO */
 
 /* convert Internet address from network to presentation format */
 static const char *sa_inet_ntop(int family, const void *src, char *dst, size_t size)
@@ -283,8 +287,10 @@
 #ifdef HAVE_INET_NTOP
     return inet_ntop(family, src, dst, size);
 #else
+#ifdef HAVE_INET_NTOA
     char *cp;
     int n;
+#endif
 
     if (family == AF_INET) {
 #ifdef HAVE_INET_NTOA
@@ -325,19 +331,19 @@
             if (c == '%') {
                 /* expand "%%" */
                 cp = &c;
-                n = sizeof(char);
+                n = (int)sizeof(char);
             }
             else if (c == 'c') {
                 /* expand "%c" */
                 c = (char)va_arg(ap, int);
                 cp = &c;
-                n = sizeof(char);
+                n = (int)sizeof(char);
             }
             else if (c == 's') {
                 /* expand "%s" */
                 if ((cp = (char *)va_arg(ap, char *)) == NULL)
                     cp = "(null)";
-                n = strlen(cp);
+                n = (int)strlen(cp);
             }
             else if (c == 'd') {
                 /* expand "%d" */
@@ -348,7 +354,7 @@
                 sprintf(ibuf, "%d", d);                /* implicitly secure */
 #endif
                 cp = ibuf;
-                n = strlen(cp);
+                n = (int)strlen(cp);
             }
             else {
                 /* any other "%X" */
@@ -362,11 +368,11 @@
             cp = (char *)format;
             if ((format = strchr(cp, '%')) == NULL)
                 format = strchr(cp, '\0');
-            n = format - cp;
+            n = (int)(format - cp);
         }
         /* perform output operation */
         if (output != NULL)
-            if ((n = output(ctx, cp, n)) == -1)
+            if ((n = output(ctx, cp, (size_t)n)) == -1)
                 break;
         bytes += n;
     }
@@ -389,7 +395,7 @@
     memcpy(ctx->bufptr, buffer, bufsize);
     ctx->bufptr += bufsize;
     ctx->buflen -= bufsize;
-    return bufsize;
+    return (int)bufsize;
 }
 
 /* minimal vsnprintf(3) variant which supports %{c,s,d} only */
@@ -477,13 +483,19 @@
     socklen_t sl;
     struct sockaddr *sa;
     struct sockaddr_un un;
+#ifdef HAVE_GETADDRINFO
+    struct addrinfo ai_hints;
+    struct addrinfo *ai = NULL;
+    int err;
+#else
     struct sockaddr_in sa4;
 #ifdef AF_INET6
     struct sockaddr_in6 sa6;
 #endif
-    int bIPv6;
     struct hostent *he;
+#endif
     struct servent *se;
+    int bIPv6;
     int bNumeric;
     char *cpHost;
     char *cpPort;
@@ -493,7 +505,8 @@
     char uribuf[1024];
     char *cp;
     int i;
-    int n;
+    size_t n;
+    int k;
 
     /* argument sanity check(s) */
     if (saa == NULL || uri == NULL)
@@ -501,8 +514,10 @@
 
     /* on-the-fly create or just take over URI */
     va_start(ap, uri);
-    sa_mvsnprintf(uribuf, sizeof(uribuf), uri, ap);
+    k = sa_mvsnprintf(uribuf, sizeof(uribuf), uri, ap);
     va_end(ap);
+    if (k == -1)
+        return SA_RC(SA_ERR_MEM);
 
     /* initialize result variables */
     sa = NULL;
@@ -539,7 +554,7 @@
 
         /* provide results */
         sa = (struct sockaddr *)&un;
-        sl = sizeof(un);
+        sl = (socklen_t)sizeof(un);
         sf = AF_LOCAL;
     }
     else if (strncmp(uri, "inet://", 7) == 0) {
@@ -591,6 +606,28 @@
             nPort = ntohs(se->s_port);
         }
 
+#ifdef HAVE_GETADDRINFO
+        memset(&ai_hints, 0, sizeof(ai_hints));
+        ai_hints.ai_family = PF_UNSPEC;
+        if ((err = getaddrinfo(cpHost, NULL, &ai_hints, &ai)) != 0) {
+            if (err == EAI_MEMORY)
+                return SA_RC(SA_ERR_MEM);
+            else if (err == EAI_SYSTEM)
+                return SA_RC(SA_ERR_SYS);
+            else
+                return SA_RC(SA_ERR_ARG);
+        }
+        sa = ai->ai_addr;
+        sl = ai->ai_addrlen;
+        sf = ai->ai_family;
+        if (sf == AF_INET)
+            ((struct sockaddr_in *)sa)->sin_port = htons(nPort);
+        else if (sf == AF_INET6)
+            ((struct sockaddr_in6 *)sa)->sin6_port = htons(nPort);
+        else
+            return SA_RC(SA_ERR_ARG);
+#else /* !HAVE_GETADDRINFO */
+
         /* mandatory(!) socket address structure initialization */
         memset(&sa4, 0, sizeof(sa4));
 #ifdef AF_INET6
@@ -603,7 +640,7 @@
             sa4.sin_family = AF_INET;
             sa4.sin_port = htons(nPort);
             sa = (struct sockaddr *)&sa4;
-            sl = sizeof(sa4);
+            sl = (socklen_t)sizeof(sa4);
             sf = AF_INET;
         }
 #ifdef AF_INET6
@@ -611,7 +648,7 @@
             sa6.sin6_family = AF_INET6;
             sa6.sin6_port = htons(nPort);
             sa = (struct sockaddr *)&sa6;
-            sl = sizeof(sa6);
+            sl = (socklen_t)sizeof(sa6);
             sf = AF_INET6;
         }
 #endif
@@ -622,7 +659,7 @@
                 memcpy(&sa4.sin_addr.s_addr, he->h_addr_list[0],
                        sizeof(sa4.sin_addr.s_addr));
                 sa = (struct sockaddr *)&sa4;
-                sl = sizeof(sa4);
+                sl = (socklen_t)sizeof(sa4);
                 sf = AF_INET;
             }
 #ifdef AF_INET6
@@ -632,7 +669,7 @@
                 memcpy(&sa6.sin6_addr.s6_addr, he->h_addr_list[0],
                        sizeof(sa6.sin6_addr.s6_addr));
                 sa = (struct sockaddr *)&sa6;
-                sl = sizeof(sa6);
+                sl = (socklen_t)sizeof(sa6);
                 sf = AF_INET6;
             }
 #endif
@@ -641,6 +678,7 @@
         }
         else
             return SA_RC(SA_ERR_ARG);
+#endif /* !HAVE_GETADDRINFO */
     }
     else
         return SA_RC(SA_ERR_ARG);
@@ -648,11 +686,16 @@
     /* fill-in result address structure */
     if (saa->saBuf != NULL)
         free(saa->saBuf);
-    if ((saa->saBuf = (struct sockaddr *)malloc(sl)) == NULL)
+    if ((saa->saBuf = (struct sockaddr *)malloc((size_t)sl)) == NULL)
         return SA_RC(SA_ERR_MEM);
-    memcpy(saa->saBuf, sa, sl);
+    memcpy(saa->saBuf, sa, (size_t)sl);
     saa->slBuf = sl;
-    saa->nFamily = sf;
+    saa->nFamily = (int)sf;
+
+#ifdef HAVE_GETADDRINFO
+    if (ai != NULL)
+        freeaddrinfo(ai);
+#endif
 
     return SA_OK;
 }
@@ -676,13 +719,13 @@
     /* create result address structure */
     if (saa->saBuf != NULL)
         free(saa->saBuf);
-    if ((saa->saBuf = (struct sockaddr *)malloc(salen)) == NULL)
+    if ((saa->saBuf = (struct sockaddr *)malloc((size_t)salen)) == NULL)
         return SA_RC(SA_ERR_MEM);
-    memcpy(saa->saBuf, sabuf, salen);
+    memcpy(saa->saBuf, sabuf, (size_t)salen);
     saa->slBuf = salen;
 
     /* remember family */
-    saa->nFamily = sabuf->sa_family;
+    saa->nFamily = (int)(sabuf->sa_family);
 
     return SA_OK;
 }
@@ -708,27 +751,35 @@
         un = (struct sockaddr_un *)((void *)saa->saBuf);
         if (   (   saa->slBuf >= (socklen_t)(&(((struct sockaddr_un *)0)->sun_path[0]))
                 && un->sun_path[0] == '\0')
-            || saa->slBuf < sizeof(struct sockaddr_un))
+            || (size_t)(saa->slBuf) < sizeof(struct sockaddr_un)) {
             /* in case the remote side of a Unix Domain socket was not
                bound, a "struct sockaddr_un" can occur with a length less
                than the expected one. Then there is actually no path at all.
                This has been verified under FreeBSD, Linux and Solaris. */
-            sa_msnprintf(uribuf, sizeof(uribuf), "unix:/NOT-BOUND");
-        else
-            sa_msnprintf(uribuf, sizeof(uribuf), "unix:%s", un->sun_path);
+            if (sa_msnprintf(uribuf, sizeof(uribuf), "unix:/NOT-BOUND") == -1)
+                return SA_RC(SA_ERR_FMT);
+        }
+        else {
+            if (sa_msnprintf(uribuf, sizeof(uribuf), "unix:%s", un->sun_path) == -1)
+                return SA_RC(SA_ERR_FMT);
+        }
     }
     else if (saa->nFamily == AF_INET) {
         sa4 = (struct sockaddr_in *)((void *)saa->saBuf);
-        sa_inet_ntop(AF_INET, &sa4->sin_addr.s_addr, caHost, sizeof(caHost));
+        if (sa_inet_ntop(AF_INET, &sa4->sin_addr.s_addr, caHost, sizeof(caHost)) == NULL)
+            return SA_RC(SA_ERR_NET);
         nPort = ntohs(sa4->sin_port);
-        sa_msnprintf(uribuf, sizeof(uribuf), "inet://%s:%d", caHost, nPort);
+        if (sa_msnprintf(uribuf, sizeof(uribuf), "inet://%s:%d", caHost, nPort) == -1)
+            return SA_RC(SA_ERR_FMT);
     }
 #ifdef AF_INET6
     else if (saa->nFamily == AF_INET6) {
         sa6 = (struct sockaddr_in6 *)((void *)saa->saBuf);
-        sa_inet_ntop(AF_INET6, &sa6->sin6_addr.s6_addr, caHost, sizeof(caHost));
+        if (sa_inet_ntop(AF_INET6, &sa6->sin6_addr.s6_addr, caHost, sizeof(caHost)) == NULL)
+            return SA_RC(SA_ERR_NET);
         nPort = ntohs(sa6->sin6_port);
-        sa_msnprintf(uribuf, sizeof(uribuf), "inet://[%s]:%d", caHost, nPort);
+        if (sa_msnprintf(uribuf, sizeof(uribuf), "inet://[%s]:%d", caHost, nPort) == -1)
+            return SA_RC(SA_ERR_FMT);
     }
 #endif
     else
@@ -748,9 +799,9 @@
         return SA_RC(SA_ERR_ARG);
 
     /* export underlying address structure */
-    if ((*sabuf = (struct sockaddr *)malloc(saa->slBuf)) == NULL)
+    if ((*sabuf = (struct sockaddr *)malloc((size_t)saa->slBuf)) == NULL)
         return SA_RC(SA_ERR_MEM);
-    memmove(*sabuf, saa->saBuf, saa->slBuf);
+    memmove(*sabuf, saa->saBuf, (size_t)saa->slBuf);
     *salen = saa->slBuf;
 
     return SA_OK;
@@ -789,10 +840,10 @@
         if (prefixlen < 0) {
             if (l1 != l2)
                 return SA_RC(SA_ERR_MTC);
-            nBits = l1;
+            nBits = (int)l1;
         }
         else {
-            if (l1 < prefixlen || l2 < prefixlen)
+            if ((int)l1 < prefixlen || (int)l2 < prefixlen)
                 return SA_RC(SA_ERR_MTC);
             nBits = prefixlen;
         }
@@ -819,9 +870,9 @@
             ucp2 += 12;
         }
         for (i = 0; i < 10; i++)
-            if (ucp0[i] != 0x00)
+            if ((int)ucp0[i] != 0x00)
                 return SA_RC(SA_ERR_MTC);
-        if (!(ucp0[10] == 0xFF && ucp0[11] == 0xFF))
+        if (!((int)ucp0[10] == 0xFF && (int)ucp0[11] == 0xFF))
             return SA_RC(SA_ERR_MTC);
         nBits = 32;
     }
@@ -862,13 +913,13 @@
     nBytes = (prefixlen / 8);
     nBits  = (prefixlen % 8);
     if (nBytes > 0) {
-        if (memcmp(ucp1, ucp2, nBytes) != 0)
+        if (memcmp(ucp1, ucp2, (size_t)nBytes) != 0)
             return SA_RC(SA_ERR_MTC);
     }
     if (nBits > 0) {
-        uc1 = ucp1[nBytes];
-        uc2 = ucp2[nBytes];
-        mask = (0xFF << (8-nBits)) & 0xFF;
+        uc1 = (unsigned int)ucp1[nBytes];
+        uc2 = (unsigned int)ucp2[nBytes];
+        mask = ((unsigned int)0xFF << (8-nBits)) & (unsigned int)0xFF;
         if ((uc1 & mask) != (uc2 & mask))
             return SA_RC(SA_ERR_MTC);
     }
@@ -888,7 +939,7 @@
     if (sa->fdSocket == -1)
         return SA_OK;
 
-#if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
+#if defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO)
     if (SA_TVISZERO(sa->tvTimeout[SA_TIMEOUT_READ])) {
         if (setsockopt(sa->fdSocket, SOL_SOCKET, SO_RCVTIMEO,
                        (const void *)(&sa->tvTimeout[SA_TIMEOUT_READ]),
@@ -917,7 +968,7 @@
 
     /* check for pending options */
     rv = SA_OK;
-    for (i = 0; i < (sizeof(sa->optInfo)/sizeof(sa->optInfo[0])); i++) {
+    for (i = 0; i < (int)(sizeof(sa->optInfo)/sizeof(sa->optInfo[0])); i++) {
         if (sa->optInfo[i].todo) {
             switch (i) {
                 /* enable/disable Nagle's Algorithm (see RFC898) */
@@ -1081,7 +1132,7 @@
         return SA_RC(SA_ERR_USE);
 
     /* close socket */
-    close(sa->fdSocket);
+    (void)close(sa->fdSocket);
     sa->fdSocket = -1;
 
     return SA_OK;
@@ -1112,13 +1163,13 @@
     sa->cpWriteBuf     = NULL;
 
     /* init timeval object attributes */
-    for (i = 0; i < (sizeof(sa->tvTimeout)/sizeof(sa->tvTimeout[0])); i++) {
+    for (i = 0; i < (int)(sizeof(sa->tvTimeout)/sizeof(sa->tvTimeout[0])); i++) {
         sa->tvTimeout[i].tv_sec  = 0;
         sa->tvTimeout[i].tv_usec = 0;
     }
 
     /* init options object attributes */
-    for (i = 0; i < (sizeof(sa->optInfo)/sizeof(sa->optInfo[0])); i++) {
+    for (i = 0; i < (int)(sizeof(sa->optInfo)/sizeof(sa->optInfo[0])); i++) {
         sa->optInfo[i].todo  = FALSE;
         sa->optInfo[i].value = 0;
     }
@@ -1146,7 +1197,7 @@
         return SA_RC(SA_ERR_ARG);
 
     /* kill underlying socket */
-    sa_socket_kill(sa);
+    (void)sa_socket_kill(sa);
 
     /* free object and sub-objects */
     if (sa->cpReadBuf != NULL)
@@ -1188,7 +1239,7 @@
         return SA_RC(SA_ERR_ARG);
 
     if (id == SA_TIMEOUT_ALL) {
-        for (i = 0; i < (sizeof(sa->tvTimeout)/sizeof(sa->tvTimeout[0])); i++) {
+        for (i = 0; i < (int)(sizeof(sa->tvTimeout)/sizeof(sa->tvTimeout[0])); i++) {
             sa->tvTimeout[i].tv_sec  = sec;
             sa->tvTimeout[i].tv_usec = usec;
         }
@@ -1216,7 +1267,7 @@
 
     if (id == SA_BUFFER_READ) {
         /* configure read/incoming buffer */
-        if (sa->nReadLen > size)
+        if (sa->nReadLen > (int)size)
             return SA_RC(SA_ERR_USE);
         if (size > 0) {
             if (sa->cpReadBuf == NULL)
@@ -1226,7 +1277,7 @@
             if (cp == NULL)
                 return SA_RC(SA_ERR_MEM);
             sa->cpReadBuf = cp;
-            sa->nReadSize = size;
+            sa->nReadSize = (int)size;
         }
         else {
             if (sa->cpReadBuf != NULL)
@@ -1237,7 +1288,7 @@
     }
     else if (id == SA_BUFFER_WRITE) {
         /* configure write/outgoing buffer */
-        if (sa->nWriteLen > size)
+        if (sa->nWriteLen > (int)size)
             return SA_RC(SA_ERR_USE);
         if (size > 0) {
             if (sa->cpWriteBuf == NULL)
@@ -1247,7 +1298,7 @@
             if (cp == NULL)
                 return SA_RC(SA_ERR_MEM);
             sa->cpWriteBuf = cp;
-            sa->nWriteSize = size;
+            sa->nWriteSize = (int)size;
         }
         else {
             if (sa->cpWriteBuf != NULL)
@@ -1342,7 +1393,7 @@
 }
 
 /* override system call */
-sa_rc_t sa_syscall(sa_t *sa, sa_syscall_t id, void (*fptr)(), void *fctx)
+sa_rc_t sa_syscall(sa_t *sa, sa_syscall_t id, void (*fptr)(void), void *fctx)
 {
     sa_rc_t rv;
 
@@ -1384,7 +1435,7 @@
     /* remove a possibly existing old Unix Domain socket on filesystem */
     if (laddr->nFamily == AF_LOCAL) {
         un = (struct sockaddr_un *)((void *)laddr->saBuf);
-        unlink(un->sun_path);
+        (void)unlink(un->sun_path);
     }
 
     /* perform bind operation on underlying socket */
@@ -1427,7 +1478,7 @@
 
         /* temporarily switch underlying socket to non-blocking mode */
         flags = fcntl(sa->fdSocket, F_GETFL, 0);
-        fcntl(sa->fdSocket, F_SETFL, flags|O_NONBLOCK);
+        (void)fcntl(sa->fdSocket, F_SETFL, flags|O_NONBLOCK);
 
         /* perform the connect operation */
         if ((n = SA_SC_CALL_3(sa, connect, sa->fdSocket, raddr->saBuf, raddr->slBuf)) < 0) {
@@ -1457,25 +1508,25 @@
             goto done;
         }
         else if (n == 0) {
-            close(sa->fdSocket); /* stop TCP three-way handshake */
+            (void)close(sa->fdSocket); /* stop TCP three-way handshake */
             sa->fdSocket = -1;
             rv = SA_ERR_TMT;
             goto done;
         }
 
         /* fetch pending error */
-        len = sizeof(error);
+        len = (socklen_t)sizeof(error);
         if (getsockopt(sa->fdSocket, SOL_SOCKET, SO_ERROR, (void *)&error, &len) < 0)
             error = errno;
 
         done:
 
         /* reset socket flags */
-        fcntl(sa->fdSocket, F_SETFL, flags);
+        (void)fcntl(sa->fdSocket, F_SETFL, flags);
 
         /* optionally set errno */
         if (error != 0) {
-            close(sa->fdSocket); /* just in case */
+            (void)close(sa->fdSocket); /* just in case */
             sa->fdSocket = -1;
             errno = error;
             rv = SA_ERR_SYS;
@@ -1550,7 +1601,7 @@
     }
 
     /* perform accept operation on underlying socket */
-    sa_size = sizeof(sa_buf);
+    sa_size = (socklen_t)sizeof(sa_buf);
     if ((s = SA_SC_CALL_3(sa, accept, sa->fdSocket, (struct sockaddr *)&sa_buf, &sa_size)) == -1)
         return SA_RC(SA_ERR_SYS);
 
@@ -1558,13 +1609,13 @@
     if ((rv = sa_addr_create(caddr)) != SA_OK)
         return SA_RC(rv);
     if ((rv = sa_addr_s2a(*caddr, (struct sockaddr *)&sa_buf, sa_size)) != SA_OK) {
-        sa_addr_destroy(*caddr);
+        (void)sa_addr_destroy(*caddr);
         return SA_RC(rv);
     }
 
     /* create result socket object */
     if ((rv = sa_create(csa)) != SA_OK) {
-        sa_addr_destroy(*caddr);
+        (void)sa_addr_destroy(*caddr);
         return SA_RC(rv);
     }
 
@@ -1581,7 +1632,7 @@
     SA_SC_COPY((*csa), sa, sendto);
 
     /* copy-over original timeout values */
-    for (i = 0; i < (sizeof(sa->tvTimeout)/sizeof(sa->tvTimeout[0])); i++) {
+    for (i = 0; i < (int)(sizeof(sa->tvTimeout)/sizeof(sa->tvTimeout[0])); i++) {
         (*csa)->tvTimeout[i].tv_sec  = sa->tvTimeout[i].tv_sec;
         (*csa)->tvTimeout[i].tv_usec = sa->tvTimeout[i].tv_usec;
     }
@@ -1614,7 +1665,7 @@
         return SA_RC(SA_ERR_USE);
 
     /* determine remote address of underlying socket */
-    sa_size = sizeof(sa_buf);
+    sa_size = (socklen_t)sizeof(sa_buf);
     if (getpeername(sa->fdSocket, (struct sockaddr *)&sa_buf, &sa_size) < 0)
         return SA_RC(SA_ERR_SYS);
 
@@ -1622,7 +1673,7 @@
     if ((rv = sa_addr_create(raddr)) != SA_OK)
         return SA_RC(rv);
     if ((rv = sa_addr_s2a(*raddr, (struct sockaddr *)&sa_buf, sa_size)) != SA_OK) {
-        sa_addr_destroy(*raddr);
+        (void)sa_addr_destroy(*raddr);
         return SA_RC(rv);
     }
 
@@ -1650,7 +1701,7 @@
         return SA_RC(SA_ERR_USE);
 
     /* determine local address of underlying socket */
-    sa_size = sizeof(sa_buf);
+    sa_size = (socklen_t)sizeof(sa_buf);
     if (getsockname(sa->fdSocket, (struct sockaddr *)&sa_buf, &sa_size) < 0)
         return SA_RC(SA_ERR_SYS);
 
@@ -1658,7 +1709,7 @@
     if ((rv = sa_addr_create(laddr)) != SA_OK)
         return SA_RC(rv);
     if ((rv = sa_addr_s2a(*laddr, (struct sockaddr *)&sa_buf, sa_size)) != SA_OK) {
-        sa_addr_destroy(*laddr);
+        (void)sa_addr_destroy(*laddr);
         return SA_RC(rv);
     }
 
@@ -1686,13 +1737,13 @@
 static int sa_read_raw(sa_t *sa, char *cpBuf, int nBufLen)
 {
     int rv;
-#if !(defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO))
+#if !(defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO))
     fd_set fds;
 #endif
 
     /* if timeout is enabled, perform explicit/smart blocking instead
        of implicitly/hard blocking in the read(2) system call */
-#if !(defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO))
+#if !(defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO))
     if (!SA_TVISZERO(sa->tvTimeout[SA_TIMEOUT_READ])) {
         FD_ZERO(&fds);
         FD_SET(sa->fdSocket, &fds);
@@ -1709,10 +1760,10 @@
 
     /* perform read operation on underlying socket */
     do {
-        rv = SA_SC_CALL_3(sa, read, sa->fdSocket, cpBuf, nBufLen);
+        rv = (int)SA_SC_CALL_3(sa, read, sa->fdSocket, cpBuf, (size_t)nBufLen);
     } while (rv == -1 && errno == EINTR);
 
-#if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
+#if defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO)
     if (rv == -1 && errno == EWOULDBLOCK)
         errno = ETIMEDOUT;
 #endif
@@ -1744,8 +1795,8 @@
     if (sa->nReadSize == 0) {
         /* user-space unbuffered I/O */
         if (sa->nWriteLen > 0)
-            sa_flush(sa);
-        res = sa_read_raw(sa, cpBuf, nBufReq);
+            (void)sa_flush(sa);
+        res = sa_read_raw(sa, cpBuf, (int)nBufReq);
         if (res == 0)
             rv = SA_ERR_EOF;
         else if (res < 0 && errno == ETIMEDOUT)
@@ -1757,7 +1808,7 @@
         /* user-space buffered I/O */
         res = 0;
         for (;;) {
-            if (nBufReq <= sa->nReadLen) {
+            if ((int)nBufReq <= sa->nReadLen) {
                 /* buffer holds enough data, so just use this */
                 memmove(cpBuf, sa->cpReadBuf, nBufReq);
                 memmove(sa->cpReadBuf, sa->cpReadBuf+nBufReq, sa->nReadLen-nBufReq);
@@ -1767,17 +1818,17 @@
             else {
                 if (sa->nReadLen > 0) {
                     /* fetch already existing buffer contents as a start */
-                    memmove(cpBuf, sa->cpReadBuf, sa->nReadLen);
+                    memmove(cpBuf, sa->cpReadBuf, (size_t)sa->nReadLen);
                     nBufReq -= sa->nReadLen;
                     cpBuf   += sa->nReadLen;
                     res     += sa->nReadLen;
                     sa->nReadLen = 0;
                 }
                 if (sa->nWriteLen > 0)
-                    sa_flush(sa);
-                if (nBufReq >= sa->nReadSize) {
+                    (void)sa_flush(sa);
+                if ((int)nBufReq >= sa->nReadSize) {
                     /* buffer is too small at all, so read directly */
-                    n = sa_read_raw(sa, cpBuf, nBufReq);
+                    n = sa_read_raw(sa, cpBuf, (int)nBufReq);
                     if (n > 0)
                         res += n;
                     else if (n == 0)
@@ -1846,7 +1897,7 @@
         rv = sa_read(sa, &c, 1, &n);
         if (rv != SA_OK)
             break;
-        if (n <= 0)
+        if (n == 0)
             break;
         cpBuf[res++] = c;
         if (c == '\n')
@@ -1865,13 +1916,13 @@
 static int sa_write_raw(sa_t *sa, const char *cpBuf, int nBufLen)
 {
     int rv;
-#if !(defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO))
+#if !(defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO))
     fd_set fds;
 #endif
 
     /* if timeout is enabled, perform explicit/smart blocking instead
        of implicitly/hard blocking in the write(2) system call */
-#if !(defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO))
+#if !(defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO))
     if (!SA_TVISZERO(sa->tvTimeout[SA_TIMEOUT_WRITE])) {
         FD_ZERO(&fds);
         FD_SET(sa->fdSocket, &fds);
@@ -1888,10 +1939,10 @@
 
     /* perform write operation on underlying socket */
     do {
-        rv = SA_SC_CALL_3(sa, write, sa->fdSocket, cpBuf, nBufLen);
+        rv = (int)SA_SC_CALL_3(sa, write, sa->fdSocket, cpBuf, (size_t)nBufLen);
     } while (rv == -1 && errno == EINTR);
 
-#if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
+#if defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO)
     if (rv == -1 && errno == EWOULDBLOCK)
         errno = ETIMEDOUT;
 #endif
@@ -1921,7 +1972,7 @@
     rv = SA_OK;
     if (sa->nWriteSize == 0) {
         /* user-space unbuffered I/O */
-        res = sa_write_raw(sa, cpBuf, nBufReq);
+        res = sa_write_raw(sa, cpBuf, (int)nBufReq);
         if (res < 0 && errno == ETIMEDOUT)
             rv = SA_ERR_TMT;
         else if (res < 0)
@@ -1929,15 +1980,15 @@
     }
     else {
         /* user-space buffered I/O */
-        if (nBufReq > (sa->nWriteSize - sa->nWriteLen)) {
+        if ((int)nBufReq > (sa->nWriteSize - sa->nWriteLen)) {
             /* not enough space in buffer, so flush buffer first */
-            sa_flush(sa);
+            (void)sa_flush(sa);
         }
         res = 0;
-        if (nBufReq >= sa->nWriteSize) {
+        if ((int)nBufReq >= sa->nWriteSize) {
             /* buffer too small at all, so write immediately */
             while (nBufReq > 0) {
-                n = sa_write_raw(sa, cpBuf, nBufReq);
+                n = sa_write_raw(sa, cpBuf, (int)nBufReq);
                 if (n < 0 && errno == ETIMEDOUT)
                     rv = (res == 0 ? SA_ERR_TMT : SA_OK);
                 else if (n < 0)
@@ -1953,7 +2004,7 @@
             /* (again) enough sprace in buffer, so store data */
             memmove(sa->cpWriteBuf+sa->nWriteLen, cpBuf, nBufReq);
             sa->nWriteLen += nBufReq;
-            res = nBufReq;
+            res = (int)nBufReq;
         }
     }
 
@@ -1985,7 +2036,7 @@
 sa_rc_t sa_writef(sa_t *sa, const char *cpFmt, ...)
 {
     va_list ap;
-    size_t n;
+    int n;
     sa_writef_cb_t ctx;
 
     /* argument sanity check(s) */
@@ -2005,6 +2056,8 @@
     ctx.sa = sa;
     ctx.rv = SA_OK;
     n = sa_mvxprintf(sa_writef_cb, &ctx, cpFmt, ap);
+    if (n == -1 && ctx.rv == SA_OK)
+        ctx.rv = SA_ERR_FMT;
     va_end(ap);
 
     return ctx.rv;
@@ -2039,7 +2092,7 @@
                 rv = SA_ERR_SYS;
             if (n <= 0)
                 break;
-            memmove(sa->cpWriteBuf, sa->cpWriteBuf+n, sa->nWriteLen-n);
+            memmove(sa->cpWriteBuf, sa->cpWriteBuf+n, (size_t)(sa->nWriteLen-n));
             sa->nWriteLen -= n;
         }
         sa->nWriteLen = 0;
@@ -2077,7 +2130,7 @@
 
     /* flush write buffers */
     if ((how & SHUT_WR) || (how & SHUT_RDWR))
-        sa_flush(sa);
+        (void)sa_flush(sa);
 
     /* perform shutdown operation on underlying socket */
     if (shutdown(sa->fdSocket, how) == -1)
@@ -2097,7 +2150,8 @@
 #endif
     } sa_buf;
     socklen_t sa_size;
-    size_t n;
+    ssize_t n;
+    int k;
     fd_set fds;
 
     /* argument sanity check(s) */
@@ -2118,17 +2172,17 @@
         FD_ZERO(&fds);
         FD_SET(sa->fdSocket, &fds);
         do {
-            n = SA_SC_CALL_5(sa, select, sa->fdSocket+1, &fds, NULL, NULL,
+            k = SA_SC_CALL_5(sa, select, sa->fdSocket+1, &fds, NULL, NULL,
                              &sa->tvTimeout[SA_TIMEOUT_READ]);
-        } while (n == -1 && errno == EINTR);
-        if (n == 0)
+        } while (k == -1 && errno == EINTR);
+        if (k == 0)
             errno = ETIMEDOUT;
-        if (n <= 0)
+        if (k <= 0)
             return SA_RC(SA_ERR_SYS);
     }
 
     /* perform receive operation on underlying socket */
-    sa_size = sizeof(sa_buf);
+    sa_size = (socklen_t)sizeof(sa_buf);
     if ((n = SA_SC_CALL_6(sa, recvfrom, sa->fdSocket, buf, buflen, 0,
                           (struct sockaddr *)&sa_buf, &sa_size)) == -1)
         return SA_RC(SA_ERR_SYS);
@@ -2137,13 +2191,13 @@
     if ((rv = sa_addr_create(raddr)) != SA_OK)
         return rv;
     if ((rv = sa_addr_s2a(*raddr, (struct sockaddr *)&sa_buf, sa_size)) != SA_OK) {
-        sa_addr_destroy(*raddr);
+        (void)sa_addr_destroy(*raddr);
         return rv;
     }
 
     /* pass actual number of received bytes to caller */
     if (bufdone != NULL)
-        *bufdone = n;
+        *bufdone = (size_t)n;
 
     return SA_OK;
 }
@@ -2151,7 +2205,8 @@
 /* send data via socket */
 sa_rc_t sa_send(sa_t *sa, sa_addr_t *raddr, const char *buf, size_t buflen, size_t *bufdone)
 {
-    size_t n;
+    ssize_t n;
+    int k;
     fd_set fds;
     sa_rc_t rv;
 
@@ -2174,12 +2229,12 @@
         FD_ZERO(&fds);
         FD_SET(sa->fdSocket, &fds);
         do {
-            n = SA_SC_CALL_5(sa, select, sa->fdSocket+1, NULL, &fds, NULL,
+            k = SA_SC_CALL_5(sa, select, sa->fdSocket+1, NULL, &fds, NULL,
                              &sa->tvTimeout[SA_TIMEOUT_WRITE]);
-        } while (n == -1 && errno == EINTR);
-        if (n == 0)
+        } while (k == -1 && errno == EINTR);
+        if (k == 0)
             errno = ETIMEDOUT;
-        if (n <= 0)
+        if (k <= 0)
             return SA_RC(SA_ERR_SYS);
     }
 
@@ -2189,7 +2244,7 @@
 
     /* pass actual number of sent bytes to caller */
     if (bufdone != NULL)
-        *bufdone = n;
+        *bufdone = (size_t)n;
 
     return SA_OK;
 }
@@ -2198,7 +2253,7 @@
 sa_rc_t sa_sendf(sa_t *sa, sa_addr_t *raddr, const char *cpFmt, ...)
 {
     va_list ap;
-    size_t nBuf;
+    int nBuf;
     char *cpBuf;
     sa_rc_t rv;
     char caBuf[1024];
@@ -2209,11 +2264,12 @@
 
     /* format string into temporary buffer */
     va_start(ap, cpFmt);
-    nBuf = sa_mvsnprintf(NULL, 0, cpFmt, ap);
+    if ((nBuf = sa_mvsnprintf(NULL, 0, cpFmt, ap)) == -1)
+        return SA_RC(SA_ERR_FMT);
     va_end(ap);
-    if ((nBuf+1) > sizeof(caBuf)) {
+    if ((nBuf+1) > (int)sizeof(caBuf)) {
         /* requires a larger buffer, so allocate dynamically */
-        if ((cpBuf = (char *)malloc(nBuf+1)) == NULL)
+        if ((cpBuf = (char *)malloc((size_t)(nBuf+1))) == NULL)
             return SA_RC(SA_ERR_MEM);
     }
     else {
@@ -2221,14 +2277,17 @@
         cpBuf = caBuf;
     }
     va_start(ap, cpFmt);
-    sa_mvsnprintf(cpBuf, nBuf+1, cpFmt, ap);
+    rv = SA_OK;
+    if (sa_mvsnprintf(cpBuf, (size_t)(nBuf+1), cpFmt, ap) == -1)
+        rv = SA_ERR_FMT;
     va_end(ap);
 
     /* pass-through to sa_send() */
-    rv = sa_send(sa, raddr, cpBuf, nBuf, NULL);
+    if (rv == SA_OK)
+        rv = sa_send(sa, raddr, cpBuf, (size_t)nBuf, NULL);
 
     /* cleanup dynamically allocated buffer */
-    if ((nBuf+1) > sizeof(caBuf))
+    if ((nBuf+1) > (int)sizeof(caBuf))
         free(cpBuf);
 
     return rv;
@@ -2248,6 +2307,8 @@
     else if (rv == SA_ERR_EOF) sz = "End Of Communication";
     else if (rv == SA_ERR_TMT) sz = "Communication Timeout";
     else if (rv == SA_ERR_SYS) sz = "Operating System Error";
+    else if (rv == SA_ERR_NET) sz = "Networking Error";
+    else if (rv == SA_ERR_FMT) sz = "Formatting Error";
     else if (rv == SA_ERR_IMP) sz = "Implementation Not Available";
     else if (rv == SA_ERR_INT) sz = "Internal Error";
     else                       sz = "Invalid Result Code";


ossp-pkg/l2/l2_ut_sa.h 1.19 -> 1.20

--- l2_ut_sa.h   2003/11/10 15:14:11     1.19
+++ l2_ut_sa.h   2004/03/26 17:14:48     1.20
@@ -31,18 +31,18 @@
 #ifndef __SA_H__
 #define __SA_H__
 
-#define SA_PREFIX l2_util_
-
 /* system definitions of "size_t", "socklen_t", "struct sockaddr *" */
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 
 /* include optional Autoconf header */
+#define SA_PREFIX l2_util_
 #include "l2_config.h"
 
+
 /* fallback for POSIX socklen_t */
-#if !defined(HAVE_SOCKLEN_T)
+#if defined(HAVE_CONFIG_H) && !defined(HAVE_SOCKLEN_T)
 typedef int socklen_t;
 #endif
 
@@ -118,6 +118,8 @@
     SA_ERR_EOF, /* End Of Communication         */
     SA_ERR_TMT, /* Communication Timeout        */
     SA_ERR_SYS, /* Operating System Error       */
+    SA_ERR_NET, /* Networking Error             */
+    SA_ERR_FMT, /* Formatting Error             */
     SA_ERR_IMP, /* Implementation Not Available */
     SA_ERR_INT  /* Internal Error               */
 } sa_rc_t;

CVSTrac 2.0.1