]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
4 * HTTP address routines for CUPS.
6 * Copyright 2007-2014 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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "cups-private.h"
26 #endif /* HAVE_RESOLV_H */
28 # include <CoreFoundation/CoreFoundation.h>
29 # include <SystemConfiguration/SystemConfiguration.h>
30 #endif /* __APPLE__ */
34 * 'httpAddrAny()' - Check for the "any" address.
36 * @since CUPS 1.2/OS X 10.5@
39 int /* O - 1 if "any", 0 otherwise */
40 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
46 if (addr
->addr
.sa_family
== AF_INET6
&&
47 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
51 if (addr
->addr
.sa_family
== AF_INET
&&
52 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
60 * 'httpAddrClose()' - Close a socket created by @link httpAddrConnect@ or
61 * @link httpAddrListen@.
63 * Pass @code NULL@ for sockets created with @link httpAddrConnect@ and the
64 * listen address for sockets created with @link httpAddrListen@. This will
65 * ensure that domain sockets are removed when closed.
67 * @since CUPS 2.0/OS 10.10@
70 int /* O - 0 on success, -1 on failure */
71 httpAddrClose(http_addr_t
*addr
, /* I - Listen address or @code NULL@ */
72 int fd
) /* I - Socket file descriptor */
82 if (addr
&& addr
->addr
.sa_family
== AF_LOCAL
)
83 return (unlink(addr
->un
.sun_path
));
91 * 'httpAddrEqual()' - Compare two addresses.
93 * @since CUPS 1.2/OS X 10.5@
96 int /* O - 1 if equal, 0 if not */
97 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
98 const http_addr_t
*addr2
) /* I - Second address */
100 if (!addr1
&& !addr2
)
103 if (!addr1
|| !addr2
)
106 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
110 if (addr1
->addr
.sa_family
== AF_LOCAL
)
111 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
112 #endif /* AF_LOCAL */
115 if (addr1
->addr
.sa_family
== AF_INET6
)
116 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
117 #endif /* AF_INET6 */
119 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
124 * 'httpAddrLength()' - Return the length of the address in bytes.
126 * @since CUPS 1.2/OS X 10.5@
129 int /* O - Length in bytes */
130 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
136 if (addr
->addr
.sa_family
== AF_INET6
)
137 return (sizeof(addr
->ipv6
));
139 #endif /* AF_INET6 */
141 if (addr
->addr
.sa_family
== AF_LOCAL
)
142 return ((int)(offsetof(struct sockaddr_un
, sun_path
) + strlen(addr
->un
.sun_path
) + 1));
144 #endif /* AF_LOCAL */
145 if (addr
->addr
.sa_family
== AF_INET
)
146 return (sizeof(addr
->ipv4
));
154 * 'httpAddrListen()' - Create a listening socket bound to the specified
157 * @since CUPS 1.7/OS X 10.9@
160 int /* O - Socket or -1 on error */
161 httpAddrListen(http_addr_t
*addr
, /* I - Address to bind to */
162 int port
) /* I - Port number to bind to */
164 int fd
= -1, /* Socket */
165 val
, /* Socket value */
166 status
; /* Bind status */
170 * Range check input...
173 if (!addr
|| port
< 0)
177 * Create the socket and set options...
180 if ((fd
= socket(addr
->addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
182 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
187 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, CUPS_SOCAST
&val
, sizeof(val
));
190 if (addr
->addr
.sa_family
== AF_INET6
)
191 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, CUPS_SOCAST
&val
, sizeof(val
));
192 #endif /* IPV6_V6ONLY */
199 if (addr
->addr
.sa_family
== AF_LOCAL
)
201 mode_t mask
; /* Umask setting */
204 * Remove any existing domain socket file...
207 unlink(addr
->un
.sun_path
);
210 * Save the current umask and set it to 0 so that all users can access
211 * the domain socket...
217 * Bind the domain socket...
220 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
223 * Restore the umask and fix permissions...
227 chmod(addr
->un
.sun_path
, 0140777);
230 #endif /* AF_LOCAL */
232 _httpAddrSetPort(addr
, port
);
234 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
239 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
252 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
264 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
269 * Disable SIGPIPE for this socket.
273 setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
274 #endif /* SO_NOSIGPIPE */
281 * 'httpAddrLocalhost()' - Check for the local loopback address.
283 * @since CUPS 1.2/OS X 10.5@
286 int /* O - 1 if local host, 0 otherwise */
288 const http_addr_t
*addr
) /* I - Address to check */
294 if (addr
->addr
.sa_family
== AF_INET6
&&
295 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
297 #endif /* AF_INET6 */
300 if (addr
->addr
.sa_family
== AF_LOCAL
)
302 #endif /* AF_LOCAL */
304 if (addr
->addr
.sa_family
== AF_INET
&&
305 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
313 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
315 * @since CUPS 1.2/OS X 10.5@
318 char * /* O - Host name */
320 const http_addr_t
*addr
, /* I - Address to lookup */
321 char *name
, /* I - Host name buffer */
322 int namelen
) /* I - Size of name buffer */
324 _cups_globals_t
*cg
= _cupsGlobals();
328 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr
, name
,
332 * Range check input...
335 if (!addr
|| !name
|| namelen
<= 2)
337 if (name
&& namelen
>= 1)
344 if (addr
->addr
.sa_family
== AF_LOCAL
)
346 strlcpy(name
, addr
->un
.sun_path
, (size_t)namelen
);
349 #endif /* AF_LOCAL */
352 * Optimize lookups for localhost/loopback addresses...
355 if (httpAddrLocalhost(addr
))
357 strlcpy(name
, "localhost", (size_t)namelen
);
363 * STR #2920: Initialize resolver after failure in cups-polld
365 * If the previous lookup failed, re-initialize the resolver to prevent
366 * temporary network errors from persisting. This *should* be handled by
367 * the resolver libraries, but apparently the glibc folks do not agree.
369 * We set a flag at the end of this function if we encounter an error that
370 * requires reinitialization of the resolver functions. We then call
371 * res_init() if the flag is set on the next call here or in httpAddrLookup().
374 if (cg
->need_res_init
)
378 cg
->need_res_init
= 0;
380 #endif /* HAVE_RES_INIT */
382 #ifdef HAVE_GETNAMEINFO
385 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
387 * FWIW, I think this is really a bug in the implementation of
388 * getnameinfo(), but falling back on httpAddrString() is easy to
392 int error
= getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), name
, (socklen_t
)namelen
, NULL
, 0, 0);
396 if (error
== EAI_FAIL
)
397 cg
->need_res_init
= 1;
399 return (httpAddrString(addr
, name
, namelen
));
404 struct hostent
*host
; /* Host from name service */
408 if (addr
->addr
.sa_family
== AF_INET6
)
409 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
410 sizeof(struct in_addr
), AF_INET6
);
412 # endif /* AF_INET6 */
413 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
414 sizeof(struct in_addr
), AF_INET
);
419 * No hostname, so return the raw address...
422 if (h_errno
== NO_RECOVERY
)
423 cg
->need_res_init
= 1;
425 return (httpAddrString(addr
, name
, namelen
));
428 strlcpy(name
, host
->h_name
, (size_t)namelen
);
430 #endif /* HAVE_GETNAMEINFO */
432 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
439 * 'httpAddrFamily()' - Get the address family of an address.
442 int /* O - Address family */
443 httpAddrFamily(http_addr_t
*addr
) /* I - Address */
446 return (addr
->addr
.sa_family
);
453 * 'httpAddrPort()' - Get the port number associated with an address.
455 * @since CUPS 1.7/OS X 10.9@
458 int /* O - Port number */
459 httpAddrPort(http_addr_t
*addr
) /* I - Address */
464 else if (addr
->addr
.sa_family
== AF_INET6
)
465 return (ntohs(addr
->ipv6
.sin6_port
));
466 #endif /* AF_INET6 */
467 else if (addr
->addr
.sa_family
== AF_INET
)
468 return (ntohs(addr
->ipv4
.sin_port
));
475 * '_httpAddrSetPort()' - Set the port number associated with an address.
479 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
480 int port
) /* I - Port */
482 if (!addr
|| port
<= 0)
486 if (addr
->addr
.sa_family
== AF_INET6
)
487 addr
->ipv6
.sin6_port
= htons(port
);
489 #endif /* AF_INET6 */
490 if (addr
->addr
.sa_family
== AF_INET
)
491 addr
->ipv4
.sin_port
= htons(port
);
496 * 'httpAddrString()' - Convert an address to a numeric string.
498 * @since CUPS 1.2/OS X 10.5@
501 char * /* O - Numeric address string */
502 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
503 char *s
, /* I - String buffer */
504 int slen
) /* I - Length of string */
506 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr
, s
, slen
));
509 * Range check input...
512 if (!addr
|| !s
|| slen
<= 2)
521 if (addr
->addr
.sa_family
== AF_LOCAL
)
523 if (addr
->un
.sun_path
[0] == '/')
524 strlcpy(s
, addr
->un
.sun_path
, (size_t)slen
);
526 strlcpy(s
, "localhost", (size_t)slen
);
529 #endif /* AF_LOCAL */
530 if (addr
->addr
.sa_family
== AF_INET
)
532 unsigned temp
; /* Temporary address */
534 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
536 snprintf(s
, (size_t)slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
537 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
540 else if (addr
->addr
.sa_family
== AF_INET6
)
542 char *sptr
, /* Pointer into string */
543 temps
[64]; /* Temporary string for address */
545 # ifdef HAVE_GETNAMEINFO
546 if (getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), temps
, sizeof(temps
), NULL
, 0, NI_NUMERICHOST
))
549 * If we get an error back, then the address type is not supported
550 * and we should zero out the buffer...
557 else if ((sptr
= strchr(temps
, '%')) != NULL
)
560 * Convert "%zone" to "+zone" to match URI form...
567 int i
; /* Looping var */
568 unsigned temp
; /* Current value */
569 const char *prefix
; /* Prefix for address */
573 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
575 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
577 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
579 sptr
+= strlen(sptr
);
583 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
585 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
);
586 sptr
+= strlen(sptr
);
592 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
597 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s:", prefix
);
599 sptr
+= strlen(sptr
);
603 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
605 if ((temp
& 0xffff0000) ||
606 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
608 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
609 sptr
+= strlen(sptr
);
612 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
& 0xffff);
613 sptr
+= strlen(sptr
);
622 strlcpy(temps
, "::", sizeof(temps
));
630 strlcpy(sptr
, "::", sizeof(temps
) - (size_t)(sptr
- temps
));
633 # endif /* HAVE_GETNAMEINFO */
636 * Add "[v1." and "]" around IPv6 address to convert to URI form.
639 snprintf(s
, (size_t)slen
, "[v1.%s]", temps
);
641 #endif /* AF_INET6 */
643 strlcpy(s
, "UNKNOWN", (size_t)slen
);
645 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
652 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
654 * Returns @code NULL@ if the socket is currently unconnected.
656 * @since CUPS 2.0/OS 10.10@
659 http_addr_t
* /* O - Connected address or @code NULL@ */
660 httpGetAddress(http_t
*http
) /* I - HTTP connection */
663 return (http
->hostaddr
);
670 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
671 * address records for the specified name.
676 struct hostent
* /* O - Host entry */
677 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
679 const char *nameptr
; /* Pointer into name */
680 unsigned ip
[4]; /* IP address components */
681 _cups_globals_t
*cg
= _cupsGlobals();
682 /* Pointer to library globals */
685 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
688 * Avoid lookup delays and configuration problems when connecting
689 * to the localhost address...
692 if (!strcmp(name
, "localhost"))
696 * This function is needed because some operating systems have a
697 * buggy implementation of gethostbyname() that does not support
698 * IP addresses. If the first character of the name string is a
699 * number, then sscanf() is used to extract the IP components.
700 * We then pack the components into an IPv4 address manually,
701 * since the inet_aton() function is deprecated. We use the
702 * htonl() macro to get the right byte order for the address.
704 * We also support domain sockets when supported by the underlying
712 * A domain socket address, so make an AF_LOCAL entry and return it...
715 cg
->hostent
.h_name
= (char *)name
;
716 cg
->hostent
.h_aliases
= NULL
;
717 cg
->hostent
.h_addrtype
= AF_LOCAL
;
718 cg
->hostent
.h_length
= (int)strlen(name
) + 1;
719 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
720 cg
->ip_ptrs
[0] = (char *)name
;
721 cg
->ip_ptrs
[1] = NULL
;
723 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
725 return (&cg
->hostent
);
727 #endif /* AF_LOCAL */
729 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
734 * We have an IPv4 address; break it up and provide the host entry
738 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
739 return (NULL
); /* Must have 4 numbers */
741 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
742 return (NULL
); /* Invalid byte ranges! */
744 cg
->ip_addr
= htonl((((((((unsigned)ip
[0] << 8) | (unsigned)ip
[1]) << 8) |
745 (unsigned)ip
[2]) << 8) |
749 * Fill in the host entry and return it...
752 cg
->hostent
.h_name
= (char *)name
;
753 cg
->hostent
.h_aliases
= NULL
;
754 cg
->hostent
.h_addrtype
= AF_INET
;
755 cg
->hostent
.h_length
= 4;
756 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
757 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
758 cg
->ip_ptrs
[1] = NULL
;
760 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
762 return (&cg
->hostent
);
767 * Use the gethostbyname() function to get the IPv4 address for
771 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
773 return (gethostbyname(name
));
779 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
781 * When "http" points to a connected socket, return the hostname or
782 * address that was used in the call to httpConnect() or httpConnectEncrypt(),
783 * or the address of the client for the connection from httpAcceptConnection().
784 * Otherwise, return the FQDN for the local system using both gethostname()
785 * and gethostbyname() to get the local hostname with domain.
787 * @since CUPS 1.2/OS X 10.5@
790 const char * /* O - FQDN for connection or system */
791 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
792 char *s
, /* I - String buffer for name */
793 int slen
) /* I - Size of buffer */
799 if (http
->hostname
[0] == '/')
800 return ("localhost");
802 return (http
->hostname
);
804 else if (http
->hostname
[0] == '/')
805 strlcpy(s
, "localhost", (size_t)slen
);
807 strlcpy(s
, http
->hostname
, (size_t)slen
);
812 * Get the hostname...
818 if (gethostname(s
, (size_t)slen
) < 0)
819 strlcpy(s
, "localhost", (size_t)slen
);
823 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
825 * The hostname is not a FQDN, so use the local hostname from the
826 * SystemConfiguration framework...
829 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
830 CFSTR("libcups"), NULL
, NULL
);
831 /* System configuration data */
832 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
833 /* Local host name */
834 char localStr
[1024]; /* Local host name C string */
836 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
837 kCFStringEncodingUTF8
))
840 * Append ".local." to the hostname we get...
843 snprintf(s
, (size_t)slen
, "%s.local.", localStr
);
853 * The hostname is not a FQDN, so look it up...
856 struct hostent
*host
; /* Host entry to get FQDN */
858 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
861 * Use the resolved hostname...
864 strlcpy(s
, host
->h_name
, (size_t)slen
);
866 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
870 * Make sure .local hostnames end with a period...
873 if (strlen(s
) > 6 && !strcmp(s
+ strlen(s
) - 6, ".local"))
874 strlcat(s
, ".", (size_t)slen
);
878 * Return the hostname with as much domain info as we have...
886 * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
889 * @since CUPS 2.0/OS 10.10@
892 const char * /* O - Resolved hostname or @code NULL@ */
893 httpResolveHostname(http_t
*http
, /* I - HTTP connection */
894 char *buffer
, /* I - Hostname buffer */
895 size_t bufsize
) /* I - Size of buffer */
900 if (isdigit(http
->hostname
[0] & 255) || http
->hostname
[0] == '[')
902 char temp
[1024]; /* Temporary string */
904 if (httpAddrLookup(http
->hostaddr
, temp
, sizeof(temp
)))
905 strlcpy(http
->hostname
, temp
, sizeof(http
->hostname
));
912 if (http
->hostname
[0] == '/')
913 strlcpy(buffer
, "localhost", bufsize
);
915 strlcpy(buffer
, http
->hostname
, bufsize
);
919 else if (http
->hostname
[0] == '/')
920 return ("localhost");
922 return (http
->hostname
);