]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
4 * HTTP address routines for CUPS.
6 * Copyright 2007-2013 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 * Include necessary headers...
20 #include "cups-private.h"
23 #endif /* HAVE_RESOLV_H */
25 # include <CoreFoundation/CoreFoundation.h>
26 # include <SystemConfiguration/SystemConfiguration.h>
27 #endif /* __APPLE__ */
31 * 'httpAddrAny()' - Check for the "any" address.
33 * @since CUPS 1.2/OS X 10.5@
36 int /* O - 1 if "any", 0 otherwise */
37 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
43 if (addr
->addr
.sa_family
== AF_INET6
&&
44 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
48 if (addr
->addr
.sa_family
== AF_INET
&&
49 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
57 * 'httpAddrEqual()' - Compare two addresses.
59 * @since CUPS 1.2/OS X 10.5@
62 int /* O - 1 if equal, 0 if not */
63 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
64 const http_addr_t
*addr2
) /* I - Second address */
72 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
76 if (addr1
->addr
.sa_family
== AF_LOCAL
)
77 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
81 if (addr1
->addr
.sa_family
== AF_INET6
)
82 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
85 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
90 * 'httpAddrLength()' - Return the length of the address in bytes.
92 * @since CUPS 1.2/OS X 10.5@
95 int /* O - Length in bytes */
96 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
102 if (addr
->addr
.sa_family
== AF_INET6
)
103 return (sizeof(addr
->ipv6
));
105 #endif /* AF_INET6 */
107 if (addr
->addr
.sa_family
== AF_LOCAL
)
108 return (offsetof(struct sockaddr_un
, sun_path
) +
109 strlen(addr
->un
.sun_path
) + 1);
111 #endif /* AF_LOCAL */
112 if (addr
->addr
.sa_family
== AF_INET
)
113 return (sizeof(addr
->ipv4
));
121 * 'httpAddrListen()' - Create a listening socket bound to the specified
124 * @since CUPS 1.7/OS X 10.9@
127 int /* O - Socket or -1 on error */
128 httpAddrListen(http_addr_t
*addr
, /* I - Address to bind to */
129 int port
) /* I - Port number to bind to */
131 int fd
= -1, /* Socket */
132 val
; /* Socket value */
136 * Range check input...
139 if (!addr
|| port
<= 0)
142 if ((fd
= socket(addr
->addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
144 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
149 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, CUPS_SOCAST
&val
, sizeof(val
));
152 if (addr
->addr
.sa_family
== AF_INET6
)
153 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, CUPS_SOCAST
&val
, sizeof(val
));
154 #endif /* IPV6_V6ONLY */
156 _httpAddrSetPort(addr
, port
);
158 if (bind(fd
, (struct sockaddr
*)addr
, httpAddrLength(addr
)))
160 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
169 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
178 * Disable SIGPIPE for this socket.
182 setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
183 #endif /* SO_NOSIGPIPE */
190 * 'httpAddrLocalhost()' - Check for the local loopback address.
192 * @since CUPS 1.2/OS X 10.5@
195 int /* O - 1 if local host, 0 otherwise */
197 const http_addr_t
*addr
) /* I - Address to check */
203 if (addr
->addr
.sa_family
== AF_INET6
&&
204 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
206 #endif /* AF_INET6 */
209 if (addr
->addr
.sa_family
== AF_LOCAL
)
211 #endif /* AF_LOCAL */
213 if (addr
->addr
.sa_family
== AF_INET
&&
214 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
222 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
224 * @since CUPS 1.2/OS X 10.5@
227 char * /* O - Host name */
229 const http_addr_t
*addr
, /* I - Address to lookup */
230 char *name
, /* I - Host name buffer */
231 int namelen
) /* I - Size of name buffer */
233 _cups_globals_t
*cg
= _cupsGlobals();
237 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr
, name
,
241 * Range check input...
244 if (!addr
|| !name
|| namelen
<= 2)
246 if (name
&& namelen
>= 1)
253 if (addr
->addr
.sa_family
== AF_LOCAL
)
255 strlcpy(name
, addr
->un
.sun_path
, namelen
);
258 #endif /* AF_LOCAL */
261 * Optimize lookups for localhost/loopback addresses...
264 if (httpAddrLocalhost(addr
))
266 strlcpy(name
, "localhost", namelen
);
272 * STR #2920: Initialize resolver after failure in cups-polld
274 * If the previous lookup failed, re-initialize the resolver to prevent
275 * temporary network errors from persisting. This *should* be handled by
276 * the resolver libraries, but apparently the glibc folks do not agree.
278 * We set a flag at the end of this function if we encounter an error that
279 * requires reinitialization of the resolver functions. We then call
280 * res_init() if the flag is set on the next call here or in httpAddrLookup().
283 if (cg
->need_res_init
)
287 cg
->need_res_init
= 0;
289 #endif /* HAVE_RES_INIT */
291 #ifdef HAVE_GETNAMEINFO
294 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
296 * FWIW, I think this is really a bug in the implementation of
297 * getnameinfo(), but falling back on httpAddrString() is easy to
301 int error
= getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
306 if (error
== EAI_FAIL
)
307 cg
->need_res_init
= 1;
309 return (httpAddrString(addr
, name
, namelen
));
314 struct hostent
*host
; /* Host from name service */
318 if (addr
->addr
.sa_family
== AF_INET6
)
319 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
320 sizeof(struct in_addr
), AF_INET6
);
322 # endif /* AF_INET6 */
323 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
324 sizeof(struct in_addr
), AF_INET
);
329 * No hostname, so return the raw address...
332 if (h_errno
== NO_RECOVERY
)
333 cg
->need_res_init
= 1;
335 return (httpAddrString(addr
, name
, namelen
));
338 strlcpy(name
, host
->h_name
, namelen
);
340 #endif /* HAVE_GETNAMEINFO */
342 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
349 * 'httpAddrFamily()' - Get the address family of an address.
352 int /* O - Address family */
353 httpAddrFamily(http_addr_t
*addr
) /* I - Address */
356 return (addr
->addr
.sa_family
);
363 * 'httpAddrPort()' - Get the port number associated with an address.
365 * @since CUPS 1.7/OS X 10.9@
368 int /* O - Port number */
369 httpAddrPort(http_addr_t
*addr
) /* I - Address */
374 else if (addr
->addr
.sa_family
== AF_INET6
)
375 return (ntohs(addr
->ipv6
.sin6_port
));
376 #endif /* AF_INET6 */
377 else if (addr
->addr
.sa_family
== AF_INET
)
378 return (ntohs(addr
->ipv4
.sin_port
));
383 /* For OS X 10.8 and earlier */
384 int _httpAddrPort(http_addr_t
*addr
) { return (httpAddrPort(addr
)); }
388 * '_httpAddrSetPort()' - Set the port number associated with an address.
392 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
393 int port
) /* I - Port */
395 if (!addr
|| port
<= 0)
399 if (addr
->addr
.sa_family
== AF_INET6
)
400 addr
->ipv6
.sin6_port
= htons(port
);
402 #endif /* AF_INET6 */
403 if (addr
->addr
.sa_family
== AF_INET
)
404 addr
->ipv4
.sin_port
= htons(port
);
409 * 'httpAddrString()' - Convert an address to a numeric string.
411 * @since CUPS 1.2/OS X 10.5@
414 char * /* O - Numeric address string */
415 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
416 char *s
, /* I - String buffer */
417 int slen
) /* I - Length of string */
419 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr
, s
, slen
));
422 * Range check input...
425 if (!addr
|| !s
|| slen
<= 2)
434 if (addr
->addr
.sa_family
== AF_LOCAL
)
436 if (addr
->un
.sun_path
[0] == '/')
437 strlcpy(s
, addr
->un
.sun_path
, slen
);
439 strlcpy(s
, "localhost", slen
);
442 #endif /* AF_LOCAL */
443 if (addr
->addr
.sa_family
== AF_INET
)
445 unsigned temp
; /* Temporary address */
448 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
450 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
451 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
454 else if (addr
->addr
.sa_family
== AF_INET6
)
456 char *sptr
, /* Pointer into string */
457 temps
[64]; /* Temporary string for address */
459 # ifdef HAVE_GETNAMEINFO
460 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), temps
, sizeof(temps
),
461 NULL
, 0, NI_NUMERICHOST
))
464 * If we get an error back, then the address type is not supported
465 * and we should zero out the buffer...
472 else if ((sptr
= strchr(temps
, '%')) != NULL
)
475 * Convert "%zone" to "+zone" to match URI form...
482 int i
; /* Looping var */
483 unsigned temp
; /* Current value */
484 const char *prefix
; /* Prefix for address */
488 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
490 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
492 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
493 (temp
>> 16) & 0xffff);
495 sptr
+= strlen(sptr
);
499 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
501 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
, temp
);
502 sptr
+= strlen(sptr
);
508 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
513 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s:", prefix
);
515 sptr
+= strlen(sptr
);
519 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
521 if ((temp
& 0xffff0000) ||
522 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
524 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
525 (temp
>> 16) & 0xffff);
526 sptr
+= strlen(sptr
);
529 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
531 sptr
+= strlen(sptr
);
540 strlcpy(temps
, "::", sizeof(temps
));
548 strlcpy(sptr
, "::", sizeof(temps
) - (sptr
- temps
));
551 # endif /* HAVE_GETNAMEINFO */
554 * Add "[v1." and "]" around IPv6 address to convert to URI form.
557 snprintf(s
, slen
, "[v1.%s]", temps
);
559 #endif /* AF_INET6 */
561 strlcpy(s
, "UNKNOWN", slen
);
563 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
570 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
572 * Returns @code NULL@ if the socket is currently unconnected.
577 http_addr_t
* /* O - Connected address or @code NULL@ */
578 httpGetAddress(http_t
*http
) /* I - HTTP connection */
581 return (http
->hostaddr
);
588 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
589 * address records for the specified name.
594 struct hostent
* /* O - Host entry */
595 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
597 const char *nameptr
; /* Pointer into name */
598 unsigned ip
[4]; /* IP address components */
599 _cups_globals_t
*cg
= _cupsGlobals();
600 /* Pointer to library globals */
603 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
606 * Avoid lookup delays and configuration problems when connecting
607 * to the localhost address...
610 if (!strcmp(name
, "localhost"))
614 * This function is needed because some operating systems have a
615 * buggy implementation of gethostbyname() that does not support
616 * IP addresses. If the first character of the name string is a
617 * number, then sscanf() is used to extract the IP components.
618 * We then pack the components into an IPv4 address manually,
619 * since the inet_aton() function is deprecated. We use the
620 * htonl() macro to get the right byte order for the address.
622 * We also support domain sockets when supported by the underlying
630 * A domain socket address, so make an AF_LOCAL entry and return it...
633 cg
->hostent
.h_name
= (char *)name
;
634 cg
->hostent
.h_aliases
= NULL
;
635 cg
->hostent
.h_addrtype
= AF_LOCAL
;
636 cg
->hostent
.h_length
= strlen(name
) + 1;
637 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
638 cg
->ip_ptrs
[0] = (char *)name
;
639 cg
->ip_ptrs
[1] = NULL
;
641 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
643 return (&cg
->hostent
);
645 #endif /* AF_LOCAL */
647 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
652 * We have an IPv4 address; break it up and provide the host entry
656 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
657 return (NULL
); /* Must have 4 numbers */
659 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
660 return (NULL
); /* Invalid byte ranges! */
662 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
666 * Fill in the host entry and return it...
669 cg
->hostent
.h_name
= (char *)name
;
670 cg
->hostent
.h_aliases
= NULL
;
671 cg
->hostent
.h_addrtype
= AF_INET
;
672 cg
->hostent
.h_length
= 4;
673 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
674 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
675 cg
->ip_ptrs
[1] = NULL
;
677 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
679 return (&cg
->hostent
);
684 * Use the gethostbyname() function to get the IPv4 address for
688 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
690 return (gethostbyname(name
));
696 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
698 * When "http" points to a connected socket, return the hostname or
699 * address that was used in the call to httpConnect() or httpConnectEncrypt(),
700 * or the address of the client for the connection from httpAcceptConnection().
701 * Otherwise, return the FQDN for the local system using both gethostname()
702 * and gethostbyname() to get the local hostname with domain.
704 * @since CUPS 1.2/OS X 10.5@
707 const char * /* O - FQDN for connection or system */
708 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
709 char *s
, /* I - String buffer for name */
710 int slen
) /* I - Size of buffer */
716 if (http
->hostname
[0] == '/')
717 return ("localhost");
719 return (http
->hostname
);
721 else if (http
->hostname
[0] == '/')
722 strlcpy(s
, "localhost", slen
);
724 strlcpy(s
, http
->hostname
, slen
);
729 * Get the hostname...
735 if (gethostname(s
, slen
) < 0)
736 strlcpy(s
, "localhost", slen
);
740 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
742 * The hostname is not a FQDN, so use the local hostname from the
743 * SystemConfiguration framework...
746 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
747 CFSTR("libcups"), NULL
, NULL
);
748 /* System configuration data */
749 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
750 /* Local host name */
751 char localStr
[1024]; /* Local host name C string */
753 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
754 kCFStringEncodingUTF8
))
757 * Append ".local." to the hostname we get...
760 snprintf(s
, slen
, "%s.local.", localStr
);
770 * The hostname is not a FQDN, so look it up...
773 struct hostent
*host
; /* Host entry to get FQDN */
775 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
778 * Use the resolved hostname...
781 strlcpy(s
, host
->h_name
, slen
);
783 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
788 * Return the hostname with as much domain info as we have...
796 * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
802 const char * /* O - Resolved hostname or @code NULL@ */
803 httpResolveHostname(http_t
*http
, /* I - HTTP connection */
804 char *buffer
, /* I - Hostname buffer */
805 size_t bufsize
) /* I - Size of buffer */
810 if (isdigit(http
->hostname
[0] & 255) || http
->hostname
[0] == '[')
812 char temp
[1024]; /* Temporary string */
814 if (httpAddrLookup(http
->hostaddr
, temp
, sizeof(temp
)))
815 strlcpy(http
->hostname
, temp
, sizeof(http
->hostname
));
822 if (http
->hostname
[0] == '/')
823 strlcpy(buffer
, "localhost", bufsize
);
825 strlcpy(buffer
, http
->hostname
, bufsize
);
829 else if (http
->hostname
[0] == '/')
830 return ("localhost");
832 return (http
->hostname
);