]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/http-addrlist.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / cups / http-addrlist.c
index e62a556f490e51363f2f3613cb2a3bff8cc5489c..bfa519a22ec42e0312dfff075a38074a2b309bb8 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $"
+ * "$Id$"
  *
- *   HTTP address list routines for CUPS.
+ * HTTP address list routines for CUPS.
  *
- *   Copyright 2007-2012 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- *   httpAddrConnect() - Connect to any of the addresses in the list.
- *   httpAddrConnect2() - Connect to any of the addresses in the list with a
- *                       timeout and optional cancel.
- *   httpAddrCopyList() - Copy an address list.
- *   httpAddrFreeList() - Free an address list.
- *   httpAddrGetList() - Get a list of addresses for a hostname.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  */
 
 /*
@@ -34,7 +25,7 @@
 #  include <poll.h>
 #endif /* HAVE_POLL */
 #ifndef WIN32
-#  include <sys/fcntl.h>
+#  include <fcntl.h>
 #endif /* WIN32 */
 
 
@@ -59,7 +50,7 @@ httpAddrConnect(
  * 'httpAddrConnect2()' - Connect to any of the addresses in the list with a
  *                        timeout and optional cancel.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 http_addrlist_t *                      /* O - Connected address or NULL on failure */
@@ -95,7 +86,7 @@ httpAddrConnect2(
   if (!sock)
   {
     errno = EINVAL;
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
     return (NULL);
   }
 
@@ -122,7 +113,7 @@ httpAddrConnect2(
                  httpAddrString(&(addrlist->addr), temp, sizeof(temp)),
                  httpAddrPort(&(addrlist->addr))));
 
-    if ((*sock = (int)socket(_httpAddrFamily(&(addrlist->addr)), SOCK_STREAM,
+    if ((*sock = (int)socket(httpAddrFamily(&(addrlist->addr)), SOCK_STREAM,
                              0)) < 0)
     {
      /*
@@ -139,21 +130,16 @@ httpAddrConnect2(
     */
 
     val = 1;
-#ifdef WIN32
-    setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&val,
-               sizeof(val));
-#else
-    setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
-#endif /* WIN32 */
+    setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, CUPS_SOCAST &val, sizeof(val));
 
 #ifdef SO_REUSEPORT
     val = 1;
-    setsockopt(*sock, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
+    setsockopt(*sock, SOL_SOCKET, SO_REUSEPORT, CUPS_SOCAST &val, sizeof(val));
 #endif /* SO_REUSEPORT */
 
 #ifdef SO_NOSIGPIPE
     val = 1;
-    setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val));
+    setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val));
 #endif /* SO_NOSIGPIPE */
 
    /*
@@ -162,12 +148,7 @@ httpAddrConnect2(
     */
 
     val = 1;
-#ifdef WIN32
-    setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&val,
-               sizeof(val));
-#else
-    setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
-#endif /* WIN32 */
+    setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, CUPS_SOCAST &val, sizeof(val));
 
 #ifdef FD_CLOEXEC
    /*
@@ -197,8 +178,7 @@ httpAddrConnect2(
     * Then connect...
     */
 
-    if (!connect(*sock, &(addrlist->addr.addr),
-                 httpAddrLength(&(addrlist->addr))))
+    if (!connect(*sock, &(addrlist->addr.addr), (socklen_t)httpAddrLength(&(addrlist->addr))))
     {
       DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...",
                    httpAddrString(&(addrlist->addr), temp, sizeof(temp)),
@@ -235,11 +215,7 @@ httpAddrConnect2(
 
             DEBUG_puts("1httpAddrConnect2: Canceled connect()");
 
-#    ifdef WIN32
-           closesocket(*sock);
-#    else
-           close(*sock);
-#    endif /* WIN32 */
+            httpAddrClose(NULL, *sock);
 
            *sock = -1;
 
@@ -307,11 +283,7 @@ httpAddrConnect2(
     * Close this socket and move to the next address...
     */
 
-#ifdef WIN32
-    closesocket(*sock);
-#else
-    close(*sock);
-#endif /* WIN32 */
+    httpAddrClose(NULL, *sock);
 
     *sock    = -1;
     addrlist = addrlist->next;
@@ -319,9 +291,9 @@ httpAddrConnect2(
 
   if (!addrlist)
 #ifdef WIN32
-    _cupsSetError(IPP_SERVICE_UNAVAILABLE, "Connection failed", 0);
+    _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, "Connection failed", 0);
 #else
-    _cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno), 0);
+    _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, strerror(errno), 0);
 #endif /* WIN32 */
 
   return (addrlist);
@@ -332,7 +304,7 @@ httpAddrConnect2(
 /*
  * 'httpAddrCopyList()' - Copy an address list.
  *
- * @since CUPS 1.7@
+ * @since CUPS 1.7/OS X 10.9@
  */
 
 http_addrlist_t        *                       /* O - New address list or @code NULL@ on error */
@@ -370,7 +342,8 @@ httpAddrCopyList(
     else
       dst = current;
 
-    src = src->next;
+    prev = current;
+    src  = src->next;
   }
 
   return (dst);
@@ -473,6 +446,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
 
     if ((first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t))) != NULL)
     {
+      addr = first;
       first->addr.un.sun_family = AF_LOCAL;
       strlcpy(first->addr.un.sun_path, hostname, sizeof(first->addr.un.sun_path));
     }
@@ -558,7 +532,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
          if (!temp)
          {
            httpAddrFreeList(first);
-           _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+           _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
            return (NULL);
          }
 
@@ -593,7 +567,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
       if (error == EAI_FAIL)
         cg->need_res_init = 1;
 
-      _cupsSetError(IPP_INTERNAL_ERROR, gai_strerror(error), 0);
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, gai_strerror(error), 0);
     }
 
 #else
@@ -656,9 +630,10 @@ httpAddrGetList(const char *hostname,      /* I - Hostname, IP address, or NULL for p
            return (NULL);
 
           first->addr.ipv4.sin_family = AF_INET;
-          first->addr.ipv4.sin_addr.s_addr = htonl(((((((ip[0] << 8) |
-                                                      ip[1]) << 8) |
-                                                    ip[2]) << 8) | ip[3]));
+          first->addr.ipv4.sin_addr.s_addr = htonl((((((((unsigned)ip[0] << 8) |
+                                                      (unsigned)ip[1]) << 8) |
+                                                    (unsigned)ip[2]) << 8) |
+                                                  (unsigned)ip[3]));
           first->addr.ipv4.sin_port = htons(portnum);
        }
       }
@@ -717,7 +692,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
         if (h_errno == NO_RECOVERY)
           cg->need_res_init = 1;
 
-       _cupsSetError(IPP_INTERNAL_ERROR, hstrerror(h_errno), 0);
+       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, hstrerror(h_errno), 0);
       }
     }
 #endif /* HAVE_GETADDRINFO */
@@ -757,7 +732,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
     {
       httpAddrFreeList(first);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, _("Unknown service name."), 1);
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown service name."), 1);
       return (NULL);
     }
 
@@ -780,7 +755,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
        temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
        if (!temp)
        {
-         _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+         _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
          httpAddrFreeList(first);
          return (NULL);
        }
@@ -809,7 +784,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
        temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
        if (!temp)
        {
-         _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+         _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
          httpAddrFreeList(first);
          return (NULL);
        }
@@ -841,7 +816,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
        temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
        if (!temp)
        {
-         _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+         _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
          httpAddrFreeList(first);
          return (NULL);
        }
@@ -865,7 +840,7 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
        temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
        if (!temp)
        {
-         _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+         _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
          httpAddrFreeList(first);
          return (NULL);
        }
@@ -891,5 +866,5 @@ httpAddrGetList(const char *hostname,       /* I - Hostname, IP address, or NULL for p
 
 
 /*
- * End of "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $".
+ * End of "$Id$".
  */