*
* 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
* 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 */
/*
* '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 */
/*
* '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 */
/*
* '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 */
/*
* '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 */
}
+/*
+ * '_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 */
#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)
#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))
{
/*
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);
}
}
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);
}
}
* Empty address...
*/
- strlcpy(s, "::", slen);
- sptr = s + 2;
- slen -= 2;
+ strlcpy(temps, "::", sizeof(temps));
}
else
{
* 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
* 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 */