2 * "$Id: http-addr.c 11173 2013-07-23 12:31:34Z msweet $"
4 * HTTP address routines for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * httpAddrAny() - Check for the "any" address.
18 * httpAddrEqual() - Compare two addresses.
19 * httpAddrLocalhost() - Check for the local loopback address.
20 * httpAddrLookup() - Lookup the hostname associated with the address.
21 * _httpAddrPort() - Get the port number associated with an address.
22 * _httpAddrSetPort() - Set the port number associated with an address.
23 * httpAddrString() - Convert an IP address to a dotted string.
24 * httpGetHostByName() - Lookup a hostname or IP address, and return
25 * address records for the specified name.
26 * httpGetHostname() - Get the FQDN for the local system.
30 * Include necessary headers...
33 #include "cups-private.h"
36 #endif /* HAVE_RESOLV_H */
38 # include <CoreFoundation/CoreFoundation.h>
39 # include <SystemConfiguration/SystemConfiguration.h>
40 #endif /* __APPLE__ */
44 * 'httpAddrAny()' - Check for the "any" address.
46 * @since CUPS 1.2/OS X 10.5@
49 int /* O - 1 if "any", 0 otherwise */
50 httpAddrAny(const http_addr_t *addr) /* I - Address to check */
56 if (addr->addr.sa_family == AF_INET6 &&
57 IN6_IS_ADDR_UNSPECIFIED(&(addr->ipv6.sin6_addr)))
61 if (addr->addr.sa_family == AF_INET &&
62 ntohl(addr->ipv4.sin_addr.s_addr) == 0x00000000)
70 * 'httpAddrEqual()' - Compare two addresses.
72 * @since CUPS 1.2/OS X 10.5@
75 int /* O - 1 if equal, 0 if not */
76 httpAddrEqual(const http_addr_t *addr1, /* I - First address */
77 const http_addr_t *addr2) /* I - Second address */
85 if (addr1->addr.sa_family != addr2->addr.sa_family)
89 if (addr1->addr.sa_family == AF_LOCAL)
90 return (!strcmp(addr1->un.sun_path, addr2->un.sun_path));
94 if (addr1->addr.sa_family == AF_INET6)
95 return (!memcmp(&(addr1->ipv6.sin6_addr), &(addr2->ipv6.sin6_addr), 16));
98 return (addr1->ipv4.sin_addr.s_addr == addr2->ipv4.sin_addr.s_addr);
103 * 'httpAddrLength()' - Return the length of the address in bytes.
105 * @since CUPS 1.2/OS X 10.5@
108 int /* O - Length in bytes */
109 httpAddrLength(const http_addr_t *addr) /* I - Address */
115 if (addr->addr.sa_family == AF_INET6)
116 return (sizeof(addr->ipv6));
118 #endif /* AF_INET6 */
120 if (addr->addr.sa_family == AF_LOCAL)
121 return (offsetof(struct sockaddr_un, sun_path) +
122 strlen(addr->un.sun_path) + 1);
124 #endif /* AF_LOCAL */
125 if (addr->addr.sa_family == AF_INET)
126 return (sizeof(addr->ipv4));
134 * 'httpAddrLocalhost()' - Check for the local loopback address.
136 * @since CUPS 1.2/OS X 10.5@
139 int /* O - 1 if local host, 0 otherwise */
141 const http_addr_t *addr) /* I - Address to check */
147 if (addr->addr.sa_family == AF_INET6 &&
148 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
150 #endif /* AF_INET6 */
153 if (addr->addr.sa_family == AF_LOCAL)
155 #endif /* AF_LOCAL */
157 if (addr->addr.sa_family == AF_INET &&
158 (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000)
166 # define ADDR_CAST (struct sockaddr *)
168 # define ADDR_CAST (char *)
173 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
175 * @since CUPS 1.2/OS X 10.5@
178 char * /* O - Host name */
180 const http_addr_t *addr, /* I - Address to lookup */
181 char *name, /* I - Host name buffer */
182 int namelen) /* I - Size of name buffer */
184 _cups_globals_t *cg = _cupsGlobals();
188 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name,
192 * Range check input...
195 if (!addr || !name || namelen <= 2)
197 if (name && namelen >= 1)
204 if (addr->addr.sa_family == AF_LOCAL)
206 strlcpy(name, addr->un.sun_path, namelen);
209 #endif /* AF_LOCAL */
212 * Optimize lookups for localhost/loopback addresses...
215 if (httpAddrLocalhost(addr))
217 strlcpy(name, "localhost", namelen);
223 * STR #2920: Initialize resolver after failure in cups-polld
225 * If the previous lookup failed, re-initialize the resolver to prevent
226 * temporary network errors from persisting. This *should* be handled by
227 * the resolver libraries, but apparently the glibc folks do not agree.
229 * We set a flag at the end of this function if we encounter an error that
230 * requires reinitialization of the resolver functions. We then call
231 * res_init() if the flag is set on the next call here or in httpAddrLookup().
234 if (cg->need_res_init)
238 cg->need_res_init = 0;
240 #endif /* HAVE_RES_INIT */
242 #ifdef HAVE_GETNAMEINFO
245 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
247 * FWIW, I think this is really a bug in the implementation of
248 * getnameinfo(), but falling back on httpAddrString() is easy to
252 int error = getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen,
257 if (error == EAI_FAIL)
258 cg->need_res_init = 1;
260 return (httpAddrString(addr, name, namelen));
265 struct hostent *host; /* Host from name service */
269 if (addr->addr.sa_family == AF_INET6)
270 host = gethostbyaddr(ADDR_CAST &(addr->ipv6.sin6_addr),
271 sizeof(struct in_addr), AF_INET6);
273 # endif /* AF_INET6 */
274 host = gethostbyaddr(ADDR_CAST &(addr->ipv4.sin_addr),
275 sizeof(struct in_addr), AF_INET);
280 * No hostname, so return the raw address...
283 if (h_errno == NO_RECOVERY)
284 cg->need_res_init = 1;
286 return (httpAddrString(addr, name, namelen));
289 strlcpy(name, host->h_name, namelen);
291 #endif /* HAVE_GETNAMEINFO */
293 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name));
300 * '_httpAddrPort()' - Get the port number associated with an address.
303 int /* O - Port number */
304 _httpAddrPort(http_addr_t *addr) /* I - Address */
309 else if (addr->addr.sa_family == AF_INET6)
310 return (ntohs(addr->ipv6.sin6_port));
311 #endif /* AF_INET6 */
312 else if (addr->addr.sa_family == AF_INET)
313 return (ntohs(addr->ipv4.sin_port));
320 * '_httpAddrSetPort()' - Set the port number associated with an address.
324 _httpAddrSetPort(http_addr_t *addr, /* I - Address */
325 int port) /* I - Port */
327 if (!addr || port <= 0)
331 if (addr->addr.sa_family == AF_INET6)
332 addr->ipv6.sin6_port = htons(port);
334 #endif /* AF_INET6 */
335 if (addr->addr.sa_family == AF_INET)
336 addr->ipv4.sin_port = htons(port);
341 * 'httpAddrString()' - Convert an address to a numeric string.
343 * @since CUPS 1.2/OS X 10.5@
346 char * /* O - Numeric address string */
347 httpAddrString(const http_addr_t *addr, /* I - Address to convert */
348 char *s, /* I - String buffer */
349 int slen) /* I - Length of string */
351 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen));
354 * Range check input...
357 if (!addr || !s || slen <= 2)
366 if (addr->addr.sa_family == AF_LOCAL)
368 if (addr->un.sun_path[0] == '/')
369 strlcpy(s, addr->un.sun_path, slen);
371 strlcpy(s, "localhost", slen);
374 #endif /* AF_LOCAL */
375 if (addr->addr.sa_family == AF_INET)
377 unsigned temp; /* Temporary address */
380 temp = ntohl(addr->ipv4.sin_addr.s_addr);
382 snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255,
383 (temp >> 16) & 255, (temp >> 8) & 255, temp & 255);
386 else if (addr->addr.sa_family == AF_INET6)
388 char *sptr, /* Pointer into string */
389 temps[64]; /* Temporary string for address */
391 # ifdef HAVE_GETNAMEINFO
392 if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps),
393 NULL, 0, NI_NUMERICHOST))
396 * If we get an error back, then the address type is not supported
397 * and we should zero out the buffer...
404 else if ((sptr = strchr(temps, '%')) != NULL)
407 * Convert "%zone" to "+zone" to match URI form...
414 int i; /* Looping var */
415 unsigned temp; /* Current value */
416 const char *prefix; /* Prefix for address */
420 for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
422 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
424 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
425 (temp >> 16) & 0xffff);
427 sptr += strlen(sptr);
431 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
433 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp);
434 sptr += strlen(sptr);
440 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
445 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix);
447 sptr += strlen(sptr);
451 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
453 if ((temp & 0xffff0000) ||
454 (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1]))
456 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
457 (temp >> 16) & 0xffff);
458 sptr += strlen(sptr);
461 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
463 sptr += strlen(sptr);
472 strlcpy(temps, "::", sizeof(temps));
480 strlcpy(sptr, "::", sizeof(temps) - (sptr - temps));
483 # endif /* HAVE_GETNAMEINFO */
486 * Add "[v1." and "]" around IPv6 address to convert to URI form.
489 snprintf(s, slen, "[v1.%s]", temps);
491 #endif /* AF_INET6 */
493 strlcpy(s, "UNKNOWN", slen);
495 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s));
502 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
503 * address records for the specified name.
508 struct hostent * /* O - Host entry */
509 httpGetHostByName(const char *name) /* I - Hostname or IP address */
511 const char *nameptr; /* Pointer into name */
512 unsigned ip[4]; /* IP address components */
513 _cups_globals_t *cg = _cupsGlobals();
514 /* Pointer to library globals */
517 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name));
520 * Avoid lookup delays and configuration problems when connecting
521 * to the localhost address...
524 if (!strcmp(name, "localhost"))
528 * This function is needed because some operating systems have a
529 * buggy implementation of gethostbyname() that does not support
530 * IP addresses. If the first character of the name string is a
531 * number, then sscanf() is used to extract the IP components.
532 * We then pack the components into an IPv4 address manually,
533 * since the inet_aton() function is deprecated. We use the
534 * htonl() macro to get the right byte order for the address.
536 * We also support domain sockets when supported by the underlying
544 * A domain socket address, so make an AF_LOCAL entry and return it...
547 cg->hostent.h_name = (char *)name;
548 cg->hostent.h_aliases = NULL;
549 cg->hostent.h_addrtype = AF_LOCAL;
550 cg->hostent.h_length = strlen(name) + 1;
551 cg->hostent.h_addr_list = cg->ip_ptrs;
552 cg->ip_ptrs[0] = (char *)name;
553 cg->ip_ptrs[1] = NULL;
555 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
557 return (&cg->hostent);
559 #endif /* AF_LOCAL */
561 for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++);
566 * We have an IPv4 address; break it up and provide the host entry
570 if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4)
571 return (NULL); /* Must have 4 numbers */
573 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
574 return (NULL); /* Invalid byte ranges! */
576 cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) |
580 * Fill in the host entry and return it...
583 cg->hostent.h_name = (char *)name;
584 cg->hostent.h_aliases = NULL;
585 cg->hostent.h_addrtype = AF_INET;
586 cg->hostent.h_length = 4;
587 cg->hostent.h_addr_list = cg->ip_ptrs;
588 cg->ip_ptrs[0] = (char *)&(cg->ip_addr);
589 cg->ip_ptrs[1] = NULL;
591 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
593 return (&cg->hostent);
598 * Use the gethostbyname() function to get the IPv4 address for
602 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
604 return (gethostbyname(name));
610 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
612 * When "http" points to a connected socket, return the hostname or
613 * address that was used in the call to httpConnect() or httpConnectEncrypt().
614 * Otherwise, return the FQDN for the local system using both gethostname()
615 * and gethostbyname() to get the local hostname with domain.
617 * @since CUPS 1.2/OS X 10.5@
620 const char * /* O - FQDN for connection or system */
621 httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
622 char *s, /* I - String buffer for name */
623 int slen) /* I - Size of buffer */
630 if (http->hostname[0] == '/')
631 strlcpy(s, "localhost", slen);
633 strlcpy(s, http->hostname, slen);
638 * Get the hostname...
641 if (gethostname(s, slen) < 0)
642 strlcpy(s, "localhost", slen);
646 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
648 * The hostname is not a FQDN, so use the local hostname from the
649 * SystemConfiguration framework...
652 SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault,
653 CFSTR("libcups"), NULL, NULL);
654 /* System configuration data */
655 CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL;
656 /* Local host name */
657 char localStr[1024]; /* Local host name C string */
659 if (local && CFStringGetCString(local, localStr, sizeof(localStr),
660 kCFStringEncodingUTF8))
663 * Append ".local." to the hostname we get...
666 snprintf(s, slen, "%s.local.", localStr);
676 * The hostname is not a FQDN, so look it up...
679 struct hostent *host; /* Host entry to get FQDN */
681 if ((host = gethostbyname(s)) != NULL && host->h_name)
684 * Use the resolved hostname...
687 strlcpy(s, host->h_name, slen);
689 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
694 * Return the hostname with as much domain info as we have...
702 * End of "$Id: http-addr.c 11173 2013-07-23 12:31:34Z msweet $".