]>
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-2010 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 # ifdef HAVE_GETNAMEINFO
364 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
365 NULL
, 0, NI_NUMERICHOST
))
368 * If we get an error back, then the address type is not supported
369 * and we should zero out the buffer...
377 char *sptr
; /* Pointer into string */
378 int i
; /* Looping var */
379 unsigned temp
; /* Current value */
380 const char *prefix
; /* Prefix for address */
384 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
386 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
388 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
390 slen
-= strlen(sptr
);
391 sptr
+= strlen(sptr
);
395 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
397 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
398 slen
-= strlen(sptr
);
399 sptr
+= strlen(sptr
);
405 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
410 snprintf(sptr
, slen
, "%s:", prefix
);
412 slen
-= strlen(sptr
);
413 sptr
+= strlen(sptr
);
417 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
419 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
421 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
422 slen
-= strlen(sptr
);
423 sptr
+= strlen(sptr
);
426 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
427 slen
-= strlen(sptr
);
428 sptr
+= strlen(sptr
);
437 strlcpy(s
, "::", slen
);
447 strlcpy(sptr
, "::", slen
);
452 # endif /* HAVE_GETNAMEINFO */
454 #endif /* AF_INET6 */
456 strlcpy(s
, "UNKNOWN", slen
);
458 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
465 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
466 * address records for the specified name.
471 struct hostent
* /* O - Host entry */
472 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
474 const char *nameptr
; /* Pointer into name */
475 unsigned ip
[4]; /* IP address components */
476 _cups_globals_t
*cg
= _cupsGlobals();
477 /* Pointer to library globals */
480 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
483 * Avoid lookup delays and configuration problems when connecting
484 * to the localhost address...
487 if (!strcmp(name
, "localhost"))
491 * This function is needed because some operating systems have a
492 * buggy implementation of gethostbyname() that does not support
493 * IP addresses. If the first character of the name string is a
494 * number, then sscanf() is used to extract the IP components.
495 * We then pack the components into an IPv4 address manually,
496 * since the inet_aton() function is deprecated. We use the
497 * htonl() macro to get the right byte order for the address.
499 * We also support domain sockets when supported by the underlying
507 * A domain socket address, so make an AF_LOCAL entry and return it...
510 cg
->hostent
.h_name
= (char *)name
;
511 cg
->hostent
.h_aliases
= NULL
;
512 cg
->hostent
.h_addrtype
= AF_LOCAL
;
513 cg
->hostent
.h_length
= strlen(name
) + 1;
514 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
515 cg
->ip_ptrs
[0] = (char *)name
;
516 cg
->ip_ptrs
[1] = NULL
;
518 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
520 return (&cg
->hostent
);
522 #endif /* AF_LOCAL */
524 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
529 * We have an IPv4 address; break it up and provide the host entry
533 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
534 return (NULL
); /* Must have 4 numbers */
536 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
537 return (NULL
); /* Invalid byte ranges! */
539 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
543 * Fill in the host entry and return it...
546 cg
->hostent
.h_name
= (char *)name
;
547 cg
->hostent
.h_aliases
= NULL
;
548 cg
->hostent
.h_addrtype
= AF_INET
;
549 cg
->hostent
.h_length
= 4;
550 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
551 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
552 cg
->ip_ptrs
[1] = NULL
;
554 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
556 return (&cg
->hostent
);
561 * Use the gethostbyname() function to get the IPv4 address for
565 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
567 return (gethostbyname(name
));
573 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
575 * When "http" points to a connected socket, return the hostname or
576 * address that was used in the call to httpConnect() or httpConnectEncrypt().
577 * Otherwise, return the FQDN for the local system using both gethostname()
578 * and gethostbyname() to get the local hostname with domain.
580 * @since CUPS 1.2/Mac OS X 10.5@
583 const char * /* O - FQDN for connection or system */
584 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
585 char *s
, /* I - String buffer for name */
586 int slen
) /* I - Size of buffer */
593 if (http
->hostname
[0] == '/')
594 strlcpy(s
, "localhost", slen
);
596 strlcpy(s
, http
->hostname
, slen
);
601 * Get the hostname...
604 if (gethostname(s
, slen
) < 0)
605 strlcpy(s
, "localhost", slen
);
609 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
611 * The hostname is not a FQDN, so use the local hostname from the
612 * SystemConfiguration framework...
615 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
616 CFSTR("libcups"), NULL
, NULL
);
617 /* System configuration data */
618 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
619 /* Local host name */
620 char localStr
[1024]; /* Local host name C string */
622 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
623 kCFStringEncodingUTF8
))
626 * Append ".local." to the hostname we get...
629 snprintf(s
, slen
, "%s.local.", localStr
);
639 * The hostname is not a FQDN, so look it up...
642 struct hostent
*host
; /* Host entry to get FQDN */
644 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
647 * Use the resolved hostname...
650 strlcpy(s
, host
->h_name
, slen
);
652 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
657 * Return the hostname with as much domain info as we have...
665 * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".