]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/http-addr.c
Merge changes from CUPS 1.6svn-r10510.
[thirdparty/cups.git] / cups / http-addr.c
index 93d280d36e72ab778a3b3fbb8d4adac3473503b5..4b09d8eaceb108962f94b40de2cbecdcbf784bb5 100644 (file)
@@ -3,7 +3,7 @@
  *
  *   HTTP address routines for CUPS.
  *
- *   Copyright 2007-2010 by Apple Inc.
+ *   Copyright 2007-2012 by Apple Inc.
  *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -19,6 +19,7 @@
  *   httpAddrLocalhost() - Check for the local loopback address.
  *   httpAddrLookup()    - Lookup the hostname associated with the address.
  *   _httpAddrPort()     - Get the port number associated with an address.
+ *   _httpAddrSetPort()  - Set the port number associated with an address.
  *   httpAddrString()    - Convert an IP address to a dotted string.
  *   httpGetHostByName() - Lookup a hostname or IP address, and return
  *                         address records for the specified name.
 #ifdef HAVE_RESOLV_H
 #  include <resolv.h>
 #endif /* HAVE_RESOLV_H */
-#ifdef HAVE_COREFOUNDATION
+#ifdef __APPLE__
 #  include <CoreFoundation/CoreFoundation.h>
-#endif /* HAVE_COREFOUNDATION */
-#ifdef HAVE_SYSTEMCONFIGURATION
 #  include <SystemConfiguration/SystemConfiguration.h>
-#endif /* HAVE_SYSTEMCONFIGURATION */
+#endif /* __APPLE__ */
 
 
 /*
  * 'httpAddrAny()' - Check for the "any" address.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - 1 if "any", 0 otherwise */
@@ -70,7 +69,7 @@ httpAddrAny(const http_addr_t *addr)  /* I - Address to check */
 /*
  * 'httpAddrEqual()' - Compare two addresses.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                            /* O - 1 if equal, 0 if not */
@@ -103,7 +102,7 @@ httpAddrEqual(const http_addr_t *addr1,             /* I - First address */
 /*
  * 'httpAddrLength()' - Return the length of the address in bytes.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - Length in bytes */
@@ -134,7 +133,7 @@ httpAddrLength(const http_addr_t *addr)     /* I - Address */
 /*
  * 'httpAddrLocalhost()' - Check for the local loopback address.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - 1 if local host, 0 otherwise */
@@ -173,7 +172,7 @@ httpAddrLocalhost(
 /*
  * 'httpAddrLookup()' - Lookup the hostname associated with the address.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 char *                                 /* O - Host name */
@@ -317,10 +316,31 @@ _httpAddrPort(http_addr_t *addr)  /* I - Address */
 }
 
 
+/*
+ * '_httpAddrSetPort()' - Set the port number associated with an address.
+ */
+
+void
+_httpAddrSetPort(http_addr_t *addr,    /* I - Address */
+                 int         port)     /* I - Port */
+{
+  if (!addr || port <= 0)
+    return;
+
+#ifdef AF_INET6
+  if (addr->addr.sa_family == AF_INET6)
+    addr->ipv6.sin6_port = htons(port);
+  else
+#endif /* AF_INET6 */
+  if (addr->addr.sa_family == AF_INET)
+    addr->ipv4.sin_port = htons(port);
+}
+
+
 /*
  * 'httpAddrString()' - Convert an address to a numeric string.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 char *                                 /* O - Numeric address string */
@@ -344,7 +364,12 @@ httpAddrString(const http_addr_t *addr,    /* I - Address to convert */
 
 #ifdef AF_LOCAL
   if (addr->addr.sa_family == AF_LOCAL)
-    strlcpy(s, addr->un.sun_path, slen);
+  {
+    if (addr->un.sun_path[0] == '/')
+      strlcpy(s, addr->un.sun_path, slen);
+    else
+      strlcpy(s, "localhost", slen);
+  }
   else
 #endif /* AF_LOCAL */
   if (addr->addr.sa_family == AF_INET)
@@ -360,8 +385,11 @@ httpAddrString(const http_addr_t *addr,    /* I - Address to convert */
 #ifdef AF_INET6
   else if (addr->addr.sa_family == AF_INET6)
   {
+    char       *sptr,                  /* Pointer into string */
+               temps[64];              /* Temporary string for address */
+
 #  ifdef HAVE_GETNAMEINFO
-    if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen,
+    if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps),
                     NULL, 0, NI_NUMERICHOST))
     {
      /*
@@ -373,29 +401,36 @@ httpAddrString(const http_addr_t *addr,   /* I - Address to convert */
 
       return (NULL);
     }
+    else if ((sptr = strchr(temps, '%')) != NULL)
+    {
+     /*
+      * Convert "%zone" to "+zone" to match URI form...
+      */
+
+      *sptr = '+';
+    }
+
 #  else
-    char       *sptr;                  /* Pointer into string */
     int                i;                      /* Looping var */
     unsigned   temp;                   /* Current value */
     const char *prefix;                /* Prefix for address */
 
 
     prefix = "";
-    for (sptr = s, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
+    for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
     {
       temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
 
-      snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
+      snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
+               (temp >> 16) & 0xffff);
       prefix = ":";
-      slen -= strlen(sptr);
       sptr += strlen(sptr);
 
       temp &= 0xffff;
 
       if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
       {
-        snprintf(sptr, slen, "%s%x", prefix, temp);
-       slen -= strlen(sptr);
+        snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp);
        sptr += strlen(sptr);
       }
     }
@@ -407,24 +442,24 @@ httpAddrString(const http_addr_t *addr,   /* I - Address to convert */
 
       if (i < 4)
       {
-        snprintf(sptr, slen, "%s:", prefix);
+        snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix);
        prefix = ":";
-       slen -= strlen(sptr);
        sptr += strlen(sptr);
 
        for (; i < 4; i ++)
        {
           temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
 
-          if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1])
+          if ((temp & 0xffff0000) ||
+             (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1]))
          {
-            snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
-           slen -= strlen(sptr);
+            snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
+                    (temp >> 16) & 0xffff);
            sptr += strlen(sptr);
           }
 
-          snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff);
-         slen -= strlen(sptr);
+          snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
+                  temp & 0xffff);
          sptr += strlen(sptr);
        }
       }
@@ -434,9 +469,7 @@ httpAddrString(const http_addr_t *addr,     /* I - Address to convert */
         * Empty address...
        */
 
-        strlcpy(s, "::", slen);
-       sptr = s + 2;
-       slen -= 2;
+        strlcpy(temps, "::", sizeof(temps));
       }
       else
       {
@@ -444,12 +477,16 @@ httpAddrString(const http_addr_t *addr,   /* I - Address to convert */
        * Empty at end...
        */
 
-        strlcpy(sptr, "::", slen);
-       sptr += 2;
-       slen -= 2;
+        strlcpy(sptr, "::", sizeof(temps) - (sptr - temps));
       }
     }
 #  endif /* HAVE_GETNAMEINFO */
+
+   /*
+    * Add "[v1." and "]" around IPv6 address to convert to URI form.
+    */
+
+    snprintf(s, slen, "[v1.%s]", temps);
   }
 #endif /* AF_INET6 */
   else
@@ -577,7 +614,7 @@ httpGetHostByName(const char *name) /* I - Hostname or IP address */
  * Otherwise, return the FQDN for the local system using both gethostname()
  * and gethostbyname() to get the local hostname with domain.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 const char *                           /* O - FQDN for connection or system */