]>
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 * httpAddrString() - Convert an IP address to a dotted string.
23 * httpGetHostByName() - Lookup a hostname or IP address, and return
24 * address records for the specified name.
25 * httpGetHostname() - Get the FQDN for the local system.
29 * Include necessary headers...
32 #include "cups-private.h"
35 #endif /* HAVE_RESOLV_H */
36 #ifdef HAVE_COREFOUNDATION
37 # include <CoreFoundation/CoreFoundation.h>
38 #endif /* HAVE_COREFOUNDATION */
39 #ifdef HAVE_SYSTEMCONFIGURATION
40 # include <SystemConfiguration/SystemConfiguration.h>
41 #endif /* HAVE_SYSTEMCONFIGURATION */
45 * 'httpAddrAny()' - Check for the "any" address.
47 * @since CUPS 1.2/Mac OS X 10.5@
50 int /* O - 1 if "any", 0 otherwise */
51 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
57 if (addr
->addr
.sa_family
== AF_INET6
&&
58 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
62 if (addr
->addr
.sa_family
== AF_INET
&&
63 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
71 * 'httpAddrEqual()' - Compare two addresses.
73 * @since CUPS 1.2/Mac OS X 10.5@
76 int /* O - 1 if equal, 0 if not */
77 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
78 const http_addr_t
*addr2
) /* I - Second address */
86 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
90 if (addr1
->addr
.sa_family
== AF_LOCAL
)
91 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
95 if (addr1
->addr
.sa_family
== AF_INET6
)
96 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
99 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
104 * 'httpAddrLength()' - Return the length of the address in bytes.
106 * @since CUPS 1.2/Mac OS X 10.5@
109 int /* O - Length in bytes */
110 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
116 if (addr
->addr
.sa_family
== AF_INET6
)
117 return (sizeof(addr
->ipv6
));
119 #endif /* AF_INET6 */
121 if (addr
->addr
.sa_family
== AF_LOCAL
)
122 return (offsetof(struct sockaddr_un
, sun_path
) +
123 strlen(addr
->un
.sun_path
) + 1);
125 #endif /* AF_LOCAL */
126 if (addr
->addr
.sa_family
== AF_INET
)
127 return (sizeof(addr
->ipv4
));
135 * 'httpAddrLocalhost()' - Check for the local loopback address.
137 * @since CUPS 1.2/Mac OS X 10.5@
140 int /* O - 1 if local host, 0 otherwise */
142 const http_addr_t
*addr
) /* I - Address to check */
148 if (addr
->addr
.sa_family
== AF_INET6
&&
149 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
151 #endif /* AF_INET6 */
154 if (addr
->addr
.sa_family
== AF_LOCAL
)
156 #endif /* AF_LOCAL */
158 if (addr
->addr
.sa_family
== AF_INET
&&
159 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
167 # define ADDR_CAST (struct sockaddr *)
169 # define ADDR_CAST (char *)
174 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
176 * @since CUPS 1.2/Mac OS X 10.5@
179 char * /* O - Host name */
181 const http_addr_t
*addr
, /* I - Address to lookup */
182 char *name
, /* I - Host name buffer */
183 int namelen
) /* I - Size of name buffer */
185 _cups_globals_t
*cg
= _cupsGlobals();
189 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr
, name
,
193 * Range check input...
196 if (!addr
|| !name
|| namelen
<= 2)
198 if (name
&& namelen
>= 1)
205 if (addr
->addr
.sa_family
== AF_LOCAL
)
207 strlcpy(name
, addr
->un
.sun_path
, namelen
);
210 #endif /* AF_LOCAL */
213 * Optimize lookups for localhost/loopback addresses...
216 if (httpAddrLocalhost(addr
))
218 strlcpy(name
, "localhost", namelen
);
224 * STR #2920: Initialize resolver after failure in cups-polld
226 * If the previous lookup failed, re-initialize the resolver to prevent
227 * temporary network errors from persisting. This *should* be handled by
228 * the resolver libraries, but apparently the glibc folks do not agree.
230 * We set a flag at the end of this function if we encounter an error that
231 * requires reinitialization of the resolver functions. We then call
232 * res_init() if the flag is set on the next call here or in httpAddrLookup().
235 if (cg
->need_res_init
)
239 cg
->need_res_init
= 0;
241 #endif /* HAVE_RES_INIT */
243 #ifdef HAVE_GETNAMEINFO
246 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
248 * FWIW, I think this is really a bug in the implementation of
249 * getnameinfo(), but falling back on httpAddrString() is easy to
253 int error
= getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
258 if (error
== EAI_FAIL
)
259 cg
->need_res_init
= 1;
261 return (httpAddrString(addr
, name
, namelen
));
266 struct hostent
*host
; /* Host from name service */
270 if (addr
->addr
.sa_family
== AF_INET6
)
271 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
272 sizeof(struct in_addr
), AF_INET6
);
274 # endif /* AF_INET6 */
275 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
276 sizeof(struct in_addr
), AF_INET
);
281 * No hostname, so return the raw address...
284 if (h_errno
== NO_RECOVERY
)
285 cg
->need_res_init
= 1;
287 return (httpAddrString(addr
, name
, namelen
));
290 strlcpy(name
, host
->h_name
, namelen
);
292 #endif /* HAVE_GETNAMEINFO */
294 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
301 * '_httpAddrPort()' - Get the port number associated with an address.
304 int /* O - Port number */
305 _httpAddrPort(http_addr_t
*addr
) /* I - Address */
310 else if (addr
->addr
.sa_family
== AF_INET6
)
311 return (ntohs(addr
->ipv6
.sin6_port
));
312 #endif /* AF_INET6 */
313 else if (addr
->addr
.sa_family
== AF_INET
)
314 return (ntohs(addr
->ipv4
.sin_port
));
321 * 'httpAddrString()' - Convert an address to a numeric string.
323 * @since CUPS 1.2/Mac OS X 10.5@
326 char * /* O - Numeric address string */
327 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
328 char *s
, /* I - String buffer */
329 int slen
) /* I - Length of string */
331 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr
, s
, slen
));
334 * Range check input...
337 if (!addr
|| !s
|| slen
<= 2)
346 if (addr
->addr
.sa_family
== AF_LOCAL
)
347 strlcpy(s
, addr
->un
.sun_path
, slen
);
349 #endif /* AF_LOCAL */
350 if (addr
->addr
.sa_family
== AF_INET
)
352 unsigned temp
; /* Temporary address */
355 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
357 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
358 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
361 else if (addr
->addr
.sa_family
== AF_INET6
)
363 char *sptr
, /* Pointer into string */
364 temps
[64]; /* Temporary string for address */
366 # ifdef HAVE_GETNAMEINFO
367 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), temps
, sizeof(temps
),
368 NULL
, 0, NI_NUMERICHOST
))
371 * If we get an error back, then the address type is not supported
372 * and we should zero out the buffer...
379 else if ((sptr
= strchr(temps
, '%')) != NULL
)
382 * Convert "%zone" to "+zone" to match URI form...
389 int i
; /* Looping var */
390 unsigned temp
; /* Current value */
391 const char *prefix
; /* Prefix for address */
395 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
397 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
399 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
400 (temp
>> 16) & 0xffff);
402 sptr
+= strlen(sptr
);
406 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
408 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
, temp
);
409 sptr
+= strlen(sptr
);
415 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
420 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s:", prefix
);
422 sptr
+= strlen(sptr
);
426 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
428 if ((temp
& 0xffff0000) ||
429 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
431 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
432 (temp
>> 16) & 0xffff);
433 sptr
+= strlen(sptr
);
436 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
438 sptr
+= strlen(sptr
);
447 strlcpy(temps
, "::", sizeof(temps
));
455 strlcpy(sptr
, "::", sizeof(temps
) - (sptr
- temps
));
458 # endif /* HAVE_GETNAMEINFO */
461 * Add "[v1." and "]" around IPv6 address to convert to URI form.
464 snprintf(s
, slen
, "[v1.%s]", temps
);
466 #endif /* AF_INET6 */
468 strlcpy(s
, "UNKNOWN", slen
);
470 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
477 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
478 * address records for the specified name.
483 struct hostent
* /* O - Host entry */
484 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
486 const char *nameptr
; /* Pointer into name */
487 unsigned ip
[4]; /* IP address components */
488 _cups_globals_t
*cg
= _cupsGlobals();
489 /* Pointer to library globals */
492 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
495 * Avoid lookup delays and configuration problems when connecting
496 * to the localhost address...
499 if (!strcmp(name
, "localhost"))
503 * This function is needed because some operating systems have a
504 * buggy implementation of gethostbyname() that does not support
505 * IP addresses. If the first character of the name string is a
506 * number, then sscanf() is used to extract the IP components.
507 * We then pack the components into an IPv4 address manually,
508 * since the inet_aton() function is deprecated. We use the
509 * htonl() macro to get the right byte order for the address.
511 * We also support domain sockets when supported by the underlying
519 * A domain socket address, so make an AF_LOCAL entry and return it...
522 cg
->hostent
.h_name
= (char *)name
;
523 cg
->hostent
.h_aliases
= NULL
;
524 cg
->hostent
.h_addrtype
= AF_LOCAL
;
525 cg
->hostent
.h_length
= strlen(name
) + 1;
526 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
527 cg
->ip_ptrs
[0] = (char *)name
;
528 cg
->ip_ptrs
[1] = NULL
;
530 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
532 return (&cg
->hostent
);
534 #endif /* AF_LOCAL */
536 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
541 * We have an IPv4 address; break it up and provide the host entry
545 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
546 return (NULL
); /* Must have 4 numbers */
548 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
549 return (NULL
); /* Invalid byte ranges! */
551 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
555 * Fill in the host entry and return it...
558 cg
->hostent
.h_name
= (char *)name
;
559 cg
->hostent
.h_aliases
= NULL
;
560 cg
->hostent
.h_addrtype
= AF_INET
;
561 cg
->hostent
.h_length
= 4;
562 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
563 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
564 cg
->ip_ptrs
[1] = NULL
;
566 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
568 return (&cg
->hostent
);
573 * Use the gethostbyname() function to get the IPv4 address for
577 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
579 return (gethostbyname(name
));
585 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
587 * When "http" points to a connected socket, return the hostname or
588 * address that was used in the call to httpConnect() or httpConnectEncrypt().
589 * Otherwise, return the FQDN for the local system using both gethostname()
590 * and gethostbyname() to get the local hostname with domain.
592 * @since CUPS 1.2/Mac OS X 10.5@
595 const char * /* O - FQDN for connection or system */
596 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
597 char *s
, /* I - String buffer for name */
598 int slen
) /* I - Size of buffer */
605 if (http
->hostname
[0] == '/')
606 strlcpy(s
, "localhost", slen
);
608 strlcpy(s
, http
->hostname
, slen
);
613 * Get the hostname...
616 if (gethostname(s
, slen
) < 0)
617 strlcpy(s
, "localhost", slen
);
621 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
623 * The hostname is not a FQDN, so use the local hostname from the
624 * SystemConfiguration framework...
627 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
628 CFSTR("libcups"), NULL
, NULL
);
629 /* System configuration data */
630 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
631 /* Local host name */
632 char localStr
[1024]; /* Local host name C string */
634 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
635 kCFStringEncodingUTF8
))
638 * Append ".local." to the hostname we get...
641 snprintf(s
, slen
, "%s.local.", localStr
);
651 * The hostname is not a FQDN, so look it up...
654 struct hostent
*host
; /* Host entry to get FQDN */
656 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
659 * Use the resolved hostname...
662 strlcpy(s
, host
->h_name
, slen
);
664 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
669 * Return the hostname with as much domain info as we have...
677 * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".