]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $"
4 * HTTP address routines for CUPS.
6 * Copyright 2007-2011 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 */
37 #ifdef HAVE_COREFOUNDATION
38 # include <CoreFoundation/CoreFoundation.h>
39 #endif /* HAVE_COREFOUNDATION */
40 #ifdef HAVE_SYSTEMCONFIGURATION
41 # include <SystemConfiguration/SystemConfiguration.h>
42 #endif /* HAVE_SYSTEMCONFIGURATION */
46 * 'httpAddrAny()' - Check for the "any" address.
48 * @since CUPS 1.2/Mac OS X 10.5@
51 int /* O - 1 if "any", 0 otherwise */
52 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
58 if (addr
->addr
.sa_family
== AF_INET6
&&
59 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
63 if (addr
->addr
.sa_family
== AF_INET
&&
64 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
72 * 'httpAddrEqual()' - Compare two addresses.
74 * @since CUPS 1.2/Mac OS X 10.5@
77 int /* O - 1 if equal, 0 if not */
78 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
79 const http_addr_t
*addr2
) /* I - Second address */
87 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
91 if (addr1
->addr
.sa_family
== AF_LOCAL
)
92 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
96 if (addr1
->addr
.sa_family
== AF_INET6
)
97 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
100 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
105 * 'httpAddrLength()' - Return the length of the address in bytes.
107 * @since CUPS 1.2/Mac OS X 10.5@
110 int /* O - Length in bytes */
111 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
117 if (addr
->addr
.sa_family
== AF_INET6
)
118 return (sizeof(addr
->ipv6
));
120 #endif /* AF_INET6 */
122 if (addr
->addr
.sa_family
== AF_LOCAL
)
123 return (offsetof(struct sockaddr_un
, sun_path
) +
124 strlen(addr
->un
.sun_path
) + 1);
126 #endif /* AF_LOCAL */
127 if (addr
->addr
.sa_family
== AF_INET
)
128 return (sizeof(addr
->ipv4
));
136 * 'httpAddrLocalhost()' - Check for the local loopback address.
138 * @since CUPS 1.2/Mac OS X 10.5@
141 int /* O - 1 if local host, 0 otherwise */
143 const http_addr_t
*addr
) /* I - Address to check */
149 if (addr
->addr
.sa_family
== AF_INET6
&&
150 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
152 #endif /* AF_INET6 */
155 if (addr
->addr
.sa_family
== AF_LOCAL
)
157 #endif /* AF_LOCAL */
159 if (addr
->addr
.sa_family
== AF_INET
&&
160 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
168 # define ADDR_CAST (struct sockaddr *)
170 # define ADDR_CAST (char *)
175 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
177 * @since CUPS 1.2/Mac OS X 10.5@
180 char * /* O - Host name */
182 const http_addr_t
*addr
, /* I - Address to lookup */
183 char *name
, /* I - Host name buffer */
184 int namelen
) /* I - Size of name buffer */
186 _cups_globals_t
*cg
= _cupsGlobals();
190 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr
, name
,
194 * Range check input...
197 if (!addr
|| !name
|| namelen
<= 2)
199 if (name
&& namelen
>= 1)
206 if (addr
->addr
.sa_family
== AF_LOCAL
)
208 strlcpy(name
, addr
->un
.sun_path
, namelen
);
211 #endif /* AF_LOCAL */
214 * Optimize lookups for localhost/loopback addresses...
217 if (httpAddrLocalhost(addr
))
219 strlcpy(name
, "localhost", namelen
);
225 * STR #2920: Initialize resolver after failure in cups-polld
227 * If the previous lookup failed, re-initialize the resolver to prevent
228 * temporary network errors from persisting. This *should* be handled by
229 * the resolver libraries, but apparently the glibc folks do not agree.
231 * We set a flag at the end of this function if we encounter an error that
232 * requires reinitialization of the resolver functions. We then call
233 * res_init() if the flag is set on the next call here or in httpAddrLookup().
236 if (cg
->need_res_init
)
240 cg
->need_res_init
= 0;
242 #endif /* HAVE_RES_INIT */
244 #ifdef HAVE_GETNAMEINFO
247 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
249 * FWIW, I think this is really a bug in the implementation of
250 * getnameinfo(), but falling back on httpAddrString() is easy to
254 int error
= getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
259 if (error
== EAI_FAIL
)
260 cg
->need_res_init
= 1;
262 return (httpAddrString(addr
, name
, namelen
));
267 struct hostent
*host
; /* Host from name service */
271 if (addr
->addr
.sa_family
== AF_INET6
)
272 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
273 sizeof(struct in_addr
), AF_INET6
);
275 # endif /* AF_INET6 */
276 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
277 sizeof(struct in_addr
), AF_INET
);
282 * No hostname, so return the raw address...
285 if (h_errno
== NO_RECOVERY
)
286 cg
->need_res_init
= 1;
288 return (httpAddrString(addr
, name
, namelen
));
291 strlcpy(name
, host
->h_name
, namelen
);
293 #endif /* HAVE_GETNAMEINFO */
295 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
302 * '_httpAddrPort()' - Get the port number associated with an address.
305 int /* O - Port number */
306 _httpAddrPort(http_addr_t
*addr
) /* I - Address */
311 else if (addr
->addr
.sa_family
== AF_INET6
)
312 return (ntohs(addr
->ipv6
.sin6_port
));
313 #endif /* AF_INET6 */
314 else if (addr
->addr
.sa_family
== AF_INET
)
315 return (ntohs(addr
->ipv4
.sin_port
));
322 * '_httpAddrSetPort()' - Set the port number associated with an address.
326 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
327 int port
) /* I - Port */
329 if (!addr
|| port
<= 0)
333 if (addr
->addr
.sa_family
== AF_INET6
)
334 addr
->ipv6
.sin6_port
= htons(port
);
336 #endif /* AF_INET6 */
337 if (addr
->addr
.sa_family
== AF_INET
)
338 addr
->ipv4
.sin_port
= htons(port
);
343 * 'httpAddrString()' - Convert an address to a numeric string.
345 * @since CUPS 1.2/Mac OS X 10.5@
348 char * /* O - Numeric address string */
349 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
350 char *s
, /* I - String buffer */
351 int slen
) /* I - Length of string */
353 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr
, s
, slen
));
356 * Range check input...
359 if (!addr
|| !s
|| slen
<= 2)
368 if (addr
->addr
.sa_family
== AF_LOCAL
)
369 strlcpy(s
, addr
->un
.sun_path
, slen
);
371 #endif /* AF_LOCAL */
372 if (addr
->addr
.sa_family
== AF_INET
)
374 unsigned temp
; /* Temporary address */
377 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
379 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
380 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
383 else if (addr
->addr
.sa_family
== AF_INET6
)
385 char *sptr
, /* Pointer into string */
386 temps
[64]; /* Temporary string for address */
388 # ifdef HAVE_GETNAMEINFO
389 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), temps
, sizeof(temps
),
390 NULL
, 0, NI_NUMERICHOST
))
393 * If we get an error back, then the address type is not supported
394 * and we should zero out the buffer...
401 else if ((sptr
= strchr(temps
, '%')) != NULL
)
404 * Convert "%zone" to "+zone" to match URI form...
411 int i
; /* Looping var */
412 unsigned temp
; /* Current value */
413 const char *prefix
; /* Prefix for address */
417 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
419 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
421 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
422 (temp
>> 16) & 0xffff);
424 sptr
+= strlen(sptr
);
428 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
430 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
, temp
);
431 sptr
+= strlen(sptr
);
437 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
442 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s:", prefix
);
444 sptr
+= strlen(sptr
);
448 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
450 if ((temp
& 0xffff0000) ||
451 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
453 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
454 (temp
>> 16) & 0xffff);
455 sptr
+= strlen(sptr
);
458 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
460 sptr
+= strlen(sptr
);
469 strlcpy(temps
, "::", sizeof(temps
));
477 strlcpy(sptr
, "::", sizeof(temps
) - (sptr
- temps
));
480 # endif /* HAVE_GETNAMEINFO */
483 * Add "[v1." and "]" around IPv6 address to convert to URI form.
486 snprintf(s
, slen
, "[v1.%s]", temps
);
488 #endif /* AF_INET6 */
490 strlcpy(s
, "UNKNOWN", slen
);
492 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
499 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
500 * address records for the specified name.
505 struct hostent
* /* O - Host entry */
506 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
508 const char *nameptr
; /* Pointer into name */
509 unsigned ip
[4]; /* IP address components */
510 _cups_globals_t
*cg
= _cupsGlobals();
511 /* Pointer to library globals */
514 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
517 * Avoid lookup delays and configuration problems when connecting
518 * to the localhost address...
521 if (!strcmp(name
, "localhost"))
525 * This function is needed because some operating systems have a
526 * buggy implementation of gethostbyname() that does not support
527 * IP addresses. If the first character of the name string is a
528 * number, then sscanf() is used to extract the IP components.
529 * We then pack the components into an IPv4 address manually,
530 * since the inet_aton() function is deprecated. We use the
531 * htonl() macro to get the right byte order for the address.
533 * We also support domain sockets when supported by the underlying
541 * A domain socket address, so make an AF_LOCAL entry and return it...
544 cg
->hostent
.h_name
= (char *)name
;
545 cg
->hostent
.h_aliases
= NULL
;
546 cg
->hostent
.h_addrtype
= AF_LOCAL
;
547 cg
->hostent
.h_length
= strlen(name
) + 1;
548 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
549 cg
->ip_ptrs
[0] = (char *)name
;
550 cg
->ip_ptrs
[1] = NULL
;
552 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
554 return (&cg
->hostent
);
556 #endif /* AF_LOCAL */
558 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
563 * We have an IPv4 address; break it up and provide the host entry
567 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
568 return (NULL
); /* Must have 4 numbers */
570 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
571 return (NULL
); /* Invalid byte ranges! */
573 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
577 * Fill in the host entry and return it...
580 cg
->hostent
.h_name
= (char *)name
;
581 cg
->hostent
.h_aliases
= NULL
;
582 cg
->hostent
.h_addrtype
= AF_INET
;
583 cg
->hostent
.h_length
= 4;
584 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
585 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
586 cg
->ip_ptrs
[1] = NULL
;
588 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
590 return (&cg
->hostent
);
595 * Use the gethostbyname() function to get the IPv4 address for
599 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
601 return (gethostbyname(name
));
607 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
609 * When "http" points to a connected socket, return the hostname or
610 * address that was used in the call to httpConnect() or httpConnectEncrypt().
611 * Otherwise, return the FQDN for the local system using both gethostname()
612 * and gethostbyname() to get the local hostname with domain.
614 * @since CUPS 1.2/Mac OS X 10.5@
617 const char * /* O - FQDN for connection or system */
618 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
619 char *s
, /* I - String buffer for name */
620 int slen
) /* I - Size of buffer */
627 if (http
->hostname
[0] == '/')
628 strlcpy(s
, "localhost", slen
);
630 strlcpy(s
, http
->hostname
, slen
);
635 * Get the hostname...
638 if (gethostname(s
, slen
) < 0)
639 strlcpy(s
, "localhost", slen
);
643 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
645 * The hostname is not a FQDN, so use the local hostname from the
646 * SystemConfiguration framework...
649 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
650 CFSTR("libcups"), NULL
, NULL
);
651 /* System configuration data */
652 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
653 /* Local host name */
654 char localStr
[1024]; /* Local host name C string */
656 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
657 kCFStringEncodingUTF8
))
660 * Append ".local." to the hostname we get...
663 snprintf(s
, slen
, "%s.local.", localStr
);
673 * The hostname is not a FQDN, so look it up...
676 struct hostent
*host
; /* Host entry to get FQDN */
678 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
681 * Use the resolved hostname...
684 strlcpy(s
, host
->h_name
, slen
);
686 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
691 * Return the hostname with as much domain info as we have...
699 * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".