]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * HTTP address routines for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
24 #endif /* HAVE_RESOLV_H */
26 # include <CoreFoundation/CoreFoundation.h>
27 # include <SystemConfiguration/SystemConfiguration.h>
28 #endif /* __APPLE__ */
32 * 'httpAddrAny()' - Check for the "any" address.
34 * @since CUPS 1.2/macOS 10.5@
37 int /* O - 1 if "any", 0 otherwise */
38 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
44 if (addr
->addr
.sa_family
== AF_INET6
&&
45 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
49 if (addr
->addr
.sa_family
== AF_INET
&&
50 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
58 * 'httpAddrClose()' - Close a socket created by @link httpAddrConnect@ or
59 * @link httpAddrListen@.
61 * Pass @code NULL@ for sockets created with @link httpAddrConnect@ and the
62 * listen address for sockets created with @link httpAddrListen@. This will
63 * ensure that domain sockets are removed when closed.
65 * @since CUPS 2.0/OS 10.10@
68 int /* O - 0 on success, -1 on failure */
69 httpAddrClose(http_addr_t
*addr
, /* I - Listen address or @code NULL@ */
70 int fd
) /* I - Socket file descriptor */
80 if (addr
&& addr
->addr
.sa_family
== AF_LOCAL
)
81 return (unlink(addr
->un
.sun_path
));
89 * 'httpAddrEqual()' - Compare two addresses.
91 * @since CUPS 1.2/macOS 10.5@
94 int /* O - 1 if equal, 0 if not */
95 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
96 const http_addr_t
*addr2
) /* I - Second address */
101 if (!addr1
|| !addr2
)
104 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
108 if (addr1
->addr
.sa_family
== AF_LOCAL
)
109 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
110 #endif /* AF_LOCAL */
113 if (addr1
->addr
.sa_family
== AF_INET6
)
114 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
115 #endif /* AF_INET6 */
117 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
122 * 'httpAddrLength()' - Return the length of the address in bytes.
124 * @since CUPS 1.2/macOS 10.5@
127 int /* O - Length in bytes */
128 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
134 if (addr
->addr
.sa_family
== AF_INET6
)
135 return (sizeof(addr
->ipv6
));
137 #endif /* AF_INET6 */
139 if (addr
->addr
.sa_family
== AF_LOCAL
)
140 return ((int)(offsetof(struct sockaddr_un
, sun_path
) + strlen(addr
->un
.sun_path
) + 1));
142 #endif /* AF_LOCAL */
143 if (addr
->addr
.sa_family
== AF_INET
)
144 return (sizeof(addr
->ipv4
));
152 * 'httpAddrListen()' - Create a listening socket bound to the specified
155 * @since CUPS 1.7/macOS 10.9@
158 int /* O - Socket or -1 on error */
159 httpAddrListen(http_addr_t
*addr
, /* I - Address to bind to */
160 int port
) /* I - Port number to bind to */
162 int fd
= -1, /* Socket */
163 val
, /* Socket value */
164 status
; /* Bind status */
168 * Range check input...
171 if (!addr
|| port
< 0)
175 * Create the socket and set options...
178 if ((fd
= socket(addr
->addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
180 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
185 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, CUPS_SOCAST
&val
, sizeof(val
));
188 if (addr
->addr
.sa_family
== AF_INET6
)
189 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, CUPS_SOCAST
&val
, sizeof(val
));
190 #endif /* IPV6_V6ONLY */
197 if (addr
->addr
.sa_family
== AF_LOCAL
)
199 mode_t mask
; /* Umask setting */
202 * Remove any existing domain socket file...
205 unlink(addr
->un
.sun_path
);
208 * Save the current umask and set it to 0 so that all users can access
209 * the domain socket...
215 * Bind the domain socket...
218 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
221 * Restore the umask and fix permissions...
225 chmod(addr
->un
.sun_path
, 0140777);
228 #endif /* AF_LOCAL */
230 _httpAddrSetPort(addr
, port
);
232 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
237 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
250 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
262 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
267 * Disable SIGPIPE for this socket.
271 setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
272 #endif /* SO_NOSIGPIPE */
279 * 'httpAddrLocalhost()' - Check for the local loopback address.
281 * @since CUPS 1.2/macOS 10.5@
284 int /* O - 1 if local host, 0 otherwise */
286 const http_addr_t
*addr
) /* I - Address to check */
292 if (addr
->addr
.sa_family
== AF_INET6
&&
293 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
295 #endif /* AF_INET6 */
298 if (addr
->addr
.sa_family
== AF_LOCAL
)
300 #endif /* AF_LOCAL */
302 if (addr
->addr
.sa_family
== AF_INET
&&
303 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
311 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
313 * @since CUPS 1.2/macOS 10.5@
316 char * /* O - Host name */
318 const http_addr_t
*addr
, /* I - Address to lookup */
319 char *name
, /* I - Host name buffer */
320 int namelen
) /* I - Size of name buffer */
322 _cups_globals_t
*cg
= _cupsGlobals();
326 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", (void *)addr
, (void *)name
, namelen
));
329 * Range check input...
332 if (!addr
|| !name
|| namelen
<= 2)
334 if (name
&& namelen
>= 1)
341 if (addr
->addr
.sa_family
== AF_LOCAL
)
343 strlcpy(name
, addr
->un
.sun_path
, (size_t)namelen
);
346 #endif /* AF_LOCAL */
349 * Optimize lookups for localhost/loopback addresses...
352 if (httpAddrLocalhost(addr
))
354 strlcpy(name
, "localhost", (size_t)namelen
);
360 * STR #2920: Initialize resolver after failure in cups-polld
362 * If the previous lookup failed, re-initialize the resolver to prevent
363 * temporary network errors from persisting. This *should* be handled by
364 * the resolver libraries, but apparently the glibc folks do not agree.
366 * We set a flag at the end of this function if we encounter an error that
367 * requires reinitialization of the resolver functions. We then call
368 * res_init() if the flag is set on the next call here or in httpAddrLookup().
371 if (cg
->need_res_init
)
375 cg
->need_res_init
= 0;
377 #endif /* HAVE_RES_INIT */
379 #ifdef HAVE_GETNAMEINFO
382 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
384 * FWIW, I think this is really a bug in the implementation of
385 * getnameinfo(), but falling back on httpAddrString() is easy to
389 int error
= getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), name
, (socklen_t
)namelen
, NULL
, 0, 0);
393 if (error
== EAI_FAIL
)
394 cg
->need_res_init
= 1;
396 return (httpAddrString(addr
, name
, namelen
));
401 struct hostent
*host
; /* Host from name service */
405 if (addr
->addr
.sa_family
== AF_INET6
)
406 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
407 sizeof(struct in_addr
), AF_INET6
);
409 # endif /* AF_INET6 */
410 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
411 sizeof(struct in_addr
), AF_INET
);
416 * No hostname, so return the raw address...
419 if (h_errno
== NO_RECOVERY
)
420 cg
->need_res_init
= 1;
422 return (httpAddrString(addr
, name
, namelen
));
425 strlcpy(name
, host
->h_name
, (size_t)namelen
);
427 #endif /* HAVE_GETNAMEINFO */
429 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
436 * 'httpAddrFamily()' - Get the address family of an address.
439 int /* O - Address family */
440 httpAddrFamily(http_addr_t
*addr
) /* I - Address */
443 return (addr
->addr
.sa_family
);
450 * 'httpAddrPort()' - Get the port number associated with an address.
452 * @since CUPS 1.7/macOS 10.9@
455 int /* O - Port number */
456 httpAddrPort(http_addr_t
*addr
) /* I - Address */
461 else if (addr
->addr
.sa_family
== AF_INET6
)
462 return (ntohs(addr
->ipv6
.sin6_port
));
463 #endif /* AF_INET6 */
464 else if (addr
->addr
.sa_family
== AF_INET
)
465 return (ntohs(addr
->ipv4
.sin_port
));
472 * '_httpAddrSetPort()' - Set the port number associated with an address.
476 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
477 int port
) /* I - Port */
479 if (!addr
|| port
<= 0)
483 if (addr
->addr
.sa_family
== AF_INET6
)
484 addr
->ipv6
.sin6_port
= htons(port
);
486 #endif /* AF_INET6 */
487 if (addr
->addr
.sa_family
== AF_INET
)
488 addr
->ipv4
.sin_port
= htons(port
);
493 * 'httpAddrString()' - Convert an address to a numeric string.
495 * @since CUPS 1.2/macOS 10.5@
498 char * /* O - Numeric address string */
499 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
500 char *s
, /* I - String buffer */
501 int slen
) /* I - Length of string */
503 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", (void *)addr
, (void *)s
, slen
));
506 * Range check input...
509 if (!addr
|| !s
|| slen
<= 2)
518 if (addr
->addr
.sa_family
== AF_LOCAL
)
520 if (addr
->un
.sun_path
[0] == '/')
521 strlcpy(s
, addr
->un
.sun_path
, (size_t)slen
);
523 strlcpy(s
, "localhost", (size_t)slen
);
526 #endif /* AF_LOCAL */
527 if (addr
->addr
.sa_family
== AF_INET
)
529 unsigned temp
; /* Temporary address */
531 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
533 snprintf(s
, (size_t)slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
534 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
537 else if (addr
->addr
.sa_family
== AF_INET6
)
539 char *sptr
, /* Pointer into string */
540 temps
[64]; /* Temporary string for address */
542 # ifdef HAVE_GETNAMEINFO
543 if (getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), temps
, sizeof(temps
), NULL
, 0, NI_NUMERICHOST
))
546 * If we get an error back, then the address type is not supported
547 * and we should zero out the buffer...
554 else if ((sptr
= strchr(temps
, '%')) != NULL
)
557 * Convert "%zone" to "+zone" to match URI form...
564 int i
; /* Looping var */
565 unsigned temp
; /* Current value */
566 const char *prefix
; /* Prefix for address */
570 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
572 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
574 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
576 sptr
+= strlen(sptr
);
580 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
582 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
);
583 sptr
+= strlen(sptr
);
589 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
594 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s:", prefix
);
596 sptr
+= strlen(sptr
);
600 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
602 if ((temp
& 0xffff0000) ||
603 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
605 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
606 sptr
+= strlen(sptr
);
609 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
& 0xffff);
610 sptr
+= strlen(sptr
);
619 strlcpy(temps
, "::", sizeof(temps
));
627 strlcpy(sptr
, "::", sizeof(temps
) - (size_t)(sptr
- temps
));
630 # endif /* HAVE_GETNAMEINFO */
633 * Add "[v1." and "]" around IPv6 address to convert to URI form.
636 snprintf(s
, (size_t)slen
, "[v1.%s]", temps
);
638 #endif /* AF_INET6 */
640 strlcpy(s
, "UNKNOWN", (size_t)slen
);
642 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
649 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
651 * Returns @code NULL@ if the socket is currently unconnected.
653 * @since CUPS 2.0/OS 10.10@
656 http_addr_t
* /* O - Connected address or @code NULL@ */
657 httpGetAddress(http_t
*http
) /* I - HTTP connection */
660 return (http
->hostaddr
);
667 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
668 * address records for the specified name.
673 struct hostent
* /* O - Host entry */
674 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
676 const char *nameptr
; /* Pointer into name */
677 unsigned ip
[4]; /* IP address components */
678 _cups_globals_t
*cg
= _cupsGlobals();
679 /* Pointer to library globals */
682 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
685 * Avoid lookup delays and configuration problems when connecting
686 * to the localhost address...
689 if (!strcmp(name
, "localhost"))
693 * This function is needed because some operating systems have a
694 * buggy implementation of gethostbyname() that does not support
695 * IP addresses. If the first character of the name string is a
696 * number, then sscanf() is used to extract the IP components.
697 * We then pack the components into an IPv4 address manually,
698 * since the inet_aton() function is deprecated. We use the
699 * htonl() macro to get the right byte order for the address.
701 * We also support domain sockets when supported by the underlying
709 * A domain socket address, so make an AF_LOCAL entry and return it...
712 cg
->hostent
.h_name
= (char *)name
;
713 cg
->hostent
.h_aliases
= NULL
;
714 cg
->hostent
.h_addrtype
= AF_LOCAL
;
715 cg
->hostent
.h_length
= (int)strlen(name
) + 1;
716 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
717 cg
->ip_ptrs
[0] = (char *)name
;
718 cg
->ip_ptrs
[1] = NULL
;
720 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
722 return (&cg
->hostent
);
724 #endif /* AF_LOCAL */
726 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
731 * We have an IPv4 address; break it up and provide the host entry
735 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
736 return (NULL
); /* Must have 4 numbers */
738 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
739 return (NULL
); /* Invalid byte ranges! */
741 cg
->ip_addr
= htonl((((((((unsigned)ip
[0] << 8) | (unsigned)ip
[1]) << 8) |
742 (unsigned)ip
[2]) << 8) |
746 * Fill in the host entry and return it...
749 cg
->hostent
.h_name
= (char *)name
;
750 cg
->hostent
.h_aliases
= NULL
;
751 cg
->hostent
.h_addrtype
= AF_INET
;
752 cg
->hostent
.h_length
= 4;
753 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
754 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
755 cg
->ip_ptrs
[1] = NULL
;
757 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
759 return (&cg
->hostent
);
764 * Use the gethostbyname() function to get the IPv4 address for
768 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
770 return (gethostbyname(name
));
776 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
778 * When "http" points to a connected socket, return the hostname or
779 * address that was used in the call to httpConnect() or httpConnectEncrypt(),
780 * or the address of the client for the connection from httpAcceptConnection().
781 * Otherwise, return the FQDN for the local system using both gethostname()
782 * and gethostbyname() to get the local hostname with domain.
784 * @since CUPS 1.2/macOS 10.5@
787 const char * /* O - FQDN for connection or system */
788 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
789 char *s
, /* I - String buffer for name */
790 int slen
) /* I - Size of buffer */
796 if (http
->hostname
[0] == '/')
797 return ("localhost");
799 return (http
->hostname
);
801 else if (http
->hostname
[0] == '/')
802 strlcpy(s
, "localhost", (size_t)slen
);
804 strlcpy(s
, http
->hostname
, (size_t)slen
);
809 * Get the hostname...
815 if (gethostname(s
, (size_t)slen
) < 0)
816 strlcpy(s
, "localhost", (size_t)slen
);
820 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
822 * The hostname is not a FQDN, so use the local hostname from the
823 * SystemConfiguration framework...
826 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
827 CFSTR("libcups"), NULL
, NULL
);
828 /* System configuration data */
829 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
830 /* Local host name */
831 char localStr
[1024]; /* Local host name C string */
833 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
834 kCFStringEncodingUTF8
))
837 * Append ".local." to the hostname we get...
840 snprintf(s
, (size_t)slen
, "%s.local.", localStr
);
850 * The hostname is not a FQDN, so look it up...
853 struct hostent
*host
; /* Host entry to get FQDN */
855 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
858 * Use the resolved hostname...
861 strlcpy(s
, host
->h_name
, (size_t)slen
);
863 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
867 * Make sure .local hostnames end with a period...
870 if (strlen(s
) > 6 && !strcmp(s
+ strlen(s
) - 6, ".local"))
871 strlcat(s
, ".", (size_t)slen
);
875 * Return the hostname with as much domain info as we have...
883 * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
886 * @since CUPS 2.0/OS 10.10@
889 const char * /* O - Resolved hostname or @code NULL@ */
890 httpResolveHostname(http_t
*http
, /* I - HTTP connection */
891 char *buffer
, /* I - Hostname buffer */
892 size_t bufsize
) /* I - Size of buffer */
897 if (isdigit(http
->hostname
[0] & 255) || http
->hostname
[0] == '[')
899 char temp
[1024]; /* Temporary string */
901 if (httpAddrLookup(http
->hostaddr
, temp
, sizeof(temp
)))
902 strlcpy(http
->hostname
, temp
, sizeof(http
->hostname
));
909 if (http
->hostname
[0] == '/')
910 strlcpy(buffer
, "localhost", bufsize
);
912 strlcpy(buffer
, http
->hostname
, bufsize
);
916 else if (http
->hostname
[0] == '/')
917 return ("localhost");
919 return (http
->hostname
);