]>
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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
15 #include "debug-internal.h"
19 #endif /* HAVE_RESOLV_H */
21 # include <CoreFoundation/CoreFoundation.h>
22 # include <SystemConfiguration/SystemConfiguration.h>
23 #endif /* __APPLE__ */
27 * 'httpAddrAny()' - Check for the "any" address.
29 * @since CUPS 1.2/macOS 10.5@
32 int /* O - 1 if "any", 0 otherwise */
33 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
39 if (addr
->addr
.sa_family
== AF_INET6
&&
40 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
44 if (addr
->addr
.sa_family
== AF_INET
&&
45 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
53 * 'httpAddrClose()' - Close a socket created by @link httpAddrConnect@ or
54 * @link httpAddrListen@.
56 * Pass @code NULL@ for sockets created with @link httpAddrConnect2@ and the
57 * listen address for sockets created with @link httpAddrListen@. This function
58 * ensures that domain sockets are removed when closed.
60 * @since CUPS 2.0/OS 10.10@
63 int /* O - 0 on success, -1 on failure */
64 httpAddrClose(http_addr_t
*addr
, /* I - Listen address or @code NULL@ */
65 int fd
) /* I - Socket file descriptor */
75 if (addr
&& addr
->addr
.sa_family
== AF_LOCAL
)
76 return (unlink(addr
->un
.sun_path
));
84 * 'httpAddrEqual()' - Compare two addresses.
86 * @since CUPS 1.2/macOS 10.5@
89 int /* O - 1 if equal, 0 if not */
90 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
91 const http_addr_t
*addr2
) /* I - Second address */
99 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
103 if (addr1
->addr
.sa_family
== AF_LOCAL
)
104 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
105 #endif /* AF_LOCAL */
108 if (addr1
->addr
.sa_family
== AF_INET6
)
109 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
110 #endif /* AF_INET6 */
112 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
117 * 'httpAddrLength()' - Return the length of the address in bytes.
119 * @since CUPS 1.2/macOS 10.5@
122 int /* O - Length in bytes */
123 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
129 if (addr
->addr
.sa_family
== AF_INET6
)
130 return (sizeof(addr
->ipv6
));
132 #endif /* AF_INET6 */
134 if (addr
->addr
.sa_family
== AF_LOCAL
)
135 return ((int)(offsetof(struct sockaddr_un
, sun_path
) + strlen(addr
->un
.sun_path
) + 1));
137 #endif /* AF_LOCAL */
138 if (addr
->addr
.sa_family
== AF_INET
)
139 return (sizeof(addr
->ipv4
));
147 * 'httpAddrListen()' - Create a listening socket bound to the specified
150 * @since CUPS 1.7/macOS 10.9@
153 int /* O - Socket or -1 on error */
154 httpAddrListen(http_addr_t
*addr
, /* I - Address to bind to */
155 int port
) /* I - Port number to bind to */
157 int fd
= -1, /* Socket */
158 val
, /* Socket value */
159 status
; /* Bind status */
163 * Range check input...
166 if (!addr
|| port
< 0)
170 * Create the socket and set options...
173 if ((fd
= socket(addr
->addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
175 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
180 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, CUPS_SOCAST
&val
, sizeof(val
));
183 if (addr
->addr
.sa_family
== AF_INET6
)
184 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, CUPS_SOCAST
&val
, sizeof(val
));
185 #endif /* IPV6_V6ONLY */
192 if (addr
->addr
.sa_family
== AF_LOCAL
)
194 mode_t mask
; /* Umask setting */
197 * Remove any existing domain socket file...
200 unlink(addr
->un
.sun_path
);
203 * Save the current umask and set it to 0 so that all users can access
204 * the domain socket...
210 * Bind the domain socket...
213 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
216 * Restore the umask and fix permissions...
220 chmod(addr
->un
.sun_path
, 0140777);
223 #endif /* AF_LOCAL */
225 _httpAddrSetPort(addr
, port
);
227 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
232 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
245 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
257 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
262 * Disable SIGPIPE for this socket.
266 setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
267 #endif /* SO_NOSIGPIPE */
274 * 'httpAddrLocalhost()' - Check for the local loopback address.
276 * @since CUPS 1.2/macOS 10.5@
279 int /* O - 1 if local host, 0 otherwise */
281 const http_addr_t
*addr
) /* I - Address to check */
287 if (addr
->addr
.sa_family
== AF_INET6
&&
288 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
290 #endif /* AF_INET6 */
293 if (addr
->addr
.sa_family
== AF_LOCAL
)
295 #endif /* AF_LOCAL */
297 if (addr
->addr
.sa_family
== AF_INET
&&
298 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
306 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
308 * @since CUPS 1.2/macOS 10.5@
311 char * /* O - Host name */
313 const http_addr_t
*addr
, /* I - Address to lookup */
314 char *name
, /* I - Host name buffer */
315 int namelen
) /* I - Size of name buffer */
317 _cups_globals_t
*cg
= _cupsGlobals();
321 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", (void *)addr
, (void *)name
, namelen
));
324 * Range check input...
327 if (!addr
|| !name
|| namelen
<= 2)
329 if (name
&& namelen
>= 1)
336 if (addr
->addr
.sa_family
== AF_LOCAL
)
338 strlcpy(name
, addr
->un
.sun_path
, (size_t)namelen
);
341 #endif /* AF_LOCAL */
344 * Optimize lookups for localhost/loopback addresses...
347 if (httpAddrLocalhost(addr
))
349 strlcpy(name
, "localhost", (size_t)namelen
);
355 * STR #2920: Initialize resolver after failure in cups-polld
357 * If the previous lookup failed, re-initialize the resolver to prevent
358 * temporary network errors from persisting. This *should* be handled by
359 * the resolver libraries, but apparently the glibc folks do not agree.
361 * We set a flag at the end of this function if we encounter an error that
362 * requires reinitialization of the resolver functions. We then call
363 * res_init() if the flag is set on the next call here or in httpAddrLookup().
366 if (cg
->need_res_init
)
370 cg
->need_res_init
= 0;
372 #endif /* HAVE_RES_INIT */
374 #ifdef HAVE_GETNAMEINFO
377 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
379 * FWIW, I think this is really a bug in the implementation of
380 * getnameinfo(), but falling back on httpAddrString() is easy to
384 int error
= getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), name
, (socklen_t
)namelen
, NULL
, 0, 0);
388 if (error
== EAI_FAIL
)
389 cg
->need_res_init
= 1;
391 return (httpAddrString(addr
, name
, namelen
));
396 struct hostent
*host
; /* Host from name service */
400 if (addr
->addr
.sa_family
== AF_INET6
)
401 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
402 sizeof(struct in_addr
), AF_INET6
);
404 # endif /* AF_INET6 */
405 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
406 sizeof(struct in_addr
), AF_INET
);
411 * No hostname, so return the raw address...
414 if (h_errno
== NO_RECOVERY
)
415 cg
->need_res_init
= 1;
417 return (httpAddrString(addr
, name
, namelen
));
420 strlcpy(name
, host
->h_name
, (size_t)namelen
);
422 #endif /* HAVE_GETNAMEINFO */
424 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
431 * 'httpAddrFamily()' - Get the address family of an address.
434 int /* O - Address family */
435 httpAddrFamily(http_addr_t
*addr
) /* I - Address */
438 return (addr
->addr
.sa_family
);
445 * 'httpAddrPort()' - Get the port number associated with an address.
447 * @since CUPS 1.7/macOS 10.9@
450 int /* O - Port number */
451 httpAddrPort(http_addr_t
*addr
) /* I - Address */
456 else if (addr
->addr
.sa_family
== AF_INET6
)
457 return (ntohs(addr
->ipv6
.sin6_port
));
458 #endif /* AF_INET6 */
459 else if (addr
->addr
.sa_family
== AF_INET
)
460 return (ntohs(addr
->ipv4
.sin_port
));
467 * '_httpAddrSetPort()' - Set the port number associated with an address.
471 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
472 int port
) /* I - Port */
474 if (!addr
|| port
<= 0)
478 if (addr
->addr
.sa_family
== AF_INET6
)
479 addr
->ipv6
.sin6_port
= htons(port
);
481 #endif /* AF_INET6 */
482 if (addr
->addr
.sa_family
== AF_INET
)
483 addr
->ipv4
.sin_port
= htons(port
);
488 * 'httpAddrString()' - Convert an address to a numeric string.
490 * @since CUPS 1.2/macOS 10.5@
493 char * /* O - Numeric address string */
494 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
495 char *s
, /* I - String buffer */
496 int slen
) /* I - Length of string */
498 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", (void *)addr
, (void *)s
, slen
));
501 * Range check input...
504 if (!addr
|| !s
|| slen
<= 2)
513 if (addr
->addr
.sa_family
== AF_LOCAL
)
515 if (addr
->un
.sun_path
[0] == '/')
516 strlcpy(s
, addr
->un
.sun_path
, (size_t)slen
);
518 strlcpy(s
, "localhost", (size_t)slen
);
521 #endif /* AF_LOCAL */
522 if (addr
->addr
.sa_family
== AF_INET
)
524 unsigned temp
; /* Temporary address */
526 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
528 snprintf(s
, (size_t)slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
529 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
532 else if (addr
->addr
.sa_family
== AF_INET6
)
534 char *sptr
, /* Pointer into string */
535 temps
[64]; /* Temporary string for address */
537 # ifdef HAVE_GETNAMEINFO
538 if (getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), temps
, sizeof(temps
), NULL
, 0, NI_NUMERICHOST
))
541 * If we get an error back, then the address type is not supported
542 * and we should zero out the buffer...
549 else if ((sptr
= strchr(temps
, '%')) != NULL
)
552 * Convert "%zone" to "+zone" to match URI form...
559 int i
; /* Looping var */
560 unsigned temp
; /* Current value */
561 const char *prefix
; /* Prefix for address */
565 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
567 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
569 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
571 sptr
+= strlen(sptr
);
575 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
577 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
);
578 sptr
+= strlen(sptr
);
584 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
589 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s:", prefix
);
591 sptr
+= strlen(sptr
);
595 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
597 if ((temp
& 0xffff0000) ||
598 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
600 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
601 sptr
+= strlen(sptr
);
604 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
& 0xffff);
605 sptr
+= strlen(sptr
);
614 strlcpy(temps
, "::", sizeof(temps
));
622 strlcpy(sptr
, "::", sizeof(temps
) - (size_t)(sptr
- temps
));
625 # endif /* HAVE_GETNAMEINFO */
628 * Add "[v1." and "]" around IPv6 address to convert to URI form.
631 snprintf(s
, (size_t)slen
, "[v1.%s]", temps
);
633 #endif /* AF_INET6 */
635 strlcpy(s
, "UNKNOWN", (size_t)slen
);
637 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
644 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
646 * For connections created with @link httpConnect2@, the address is for the
647 * server. For connections created with @link httpAccept@, the address is for
650 * Returns @code NULL@ if the socket is currently unconnected.
652 * @since CUPS 2.0/OS 10.10@
655 http_addr_t
* /* O - Connected address or @code NULL@ */
656 httpGetAddress(http_t
*http
) /* I - HTTP connection */
659 return (http
->hostaddr
);
666 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
667 * address records for the specified name.
669 * @deprecated@ @exclude all@
672 struct hostent
* /* O - Host entry */
673 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
675 const char *nameptr
; /* Pointer into name */
676 unsigned ip
[4]; /* IP address components */
677 _cups_globals_t
*cg
= _cupsGlobals();
678 /* Pointer to library globals */
681 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
684 * Avoid lookup delays and configuration problems when connecting
685 * to the localhost address...
688 if (!strcmp(name
, "localhost"))
692 * This function is needed because some operating systems have a
693 * buggy implementation of gethostbyname() that does not support
694 * IP addresses. If the first character of the name string is a
695 * number, then sscanf() is used to extract the IP components.
696 * We then pack the components into an IPv4 address manually,
697 * since the inet_aton() function is deprecated. We use the
698 * htonl() macro to get the right byte order for the address.
700 * We also support domain sockets when supported by the underlying
708 * A domain socket address, so make an AF_LOCAL entry and return it...
711 cg
->hostent
.h_name
= (char *)name
;
712 cg
->hostent
.h_aliases
= NULL
;
713 cg
->hostent
.h_addrtype
= AF_LOCAL
;
714 cg
->hostent
.h_length
= (int)strlen(name
) + 1;
715 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
716 cg
->ip_ptrs
[0] = (char *)name
;
717 cg
->ip_ptrs
[1] = NULL
;
719 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
721 return (&cg
->hostent
);
723 #endif /* AF_LOCAL */
725 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
730 * We have an IPv4 address; break it up and provide the host entry
734 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
735 return (NULL
); /* Must have 4 numbers */
737 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
738 return (NULL
); /* Invalid byte ranges! */
740 cg
->ip_addr
= htonl((((((((unsigned)ip
[0] << 8) | (unsigned)ip
[1]) << 8) |
741 (unsigned)ip
[2]) << 8) |
745 * Fill in the host entry and return it...
748 cg
->hostent
.h_name
= (char *)name
;
749 cg
->hostent
.h_aliases
= NULL
;
750 cg
->hostent
.h_addrtype
= AF_INET
;
751 cg
->hostent
.h_length
= 4;
752 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
753 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
754 cg
->ip_ptrs
[1] = NULL
;
756 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
758 return (&cg
->hostent
);
763 * Use the gethostbyname() function to get the IPv4 address for
767 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
769 return (gethostbyname(name
));
775 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
777 * When "http" points to a connected socket, return the hostname or
778 * address that was used in the call to httpConnect() or httpConnectEncrypt(),
779 * or the address of the client for the connection from httpAcceptConnection().
780 * Otherwise, return the FQDN for the local system using both gethostname()
781 * and gethostbyname() to get the local hostname with domain.
783 * @since CUPS 1.2/macOS 10.5@
786 const char * /* O - FQDN for connection or system */
787 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
788 char *s
, /* I - String buffer for name */
789 int slen
) /* I - Size of buffer */
795 if (http
->hostname
[0] == '/')
796 return ("localhost");
798 return (http
->hostname
);
800 else if (http
->hostname
[0] == '/')
801 strlcpy(s
, "localhost", (size_t)slen
);
803 strlcpy(s
, http
->hostname
, (size_t)slen
);
808 * Get the hostname...
814 if (gethostname(s
, (size_t)slen
) < 0)
815 strlcpy(s
, "localhost", (size_t)slen
);
819 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
821 * The hostname is not a FQDN, so use the local hostname from the
822 * SystemConfiguration framework...
825 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
826 CFSTR("libcups"), NULL
, NULL
);
827 /* System configuration data */
828 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
829 /* Local host name */
830 char localStr
[1024]; /* Local host name C string */
832 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
833 kCFStringEncodingUTF8
))
836 * Append ".local." to the hostname we get...
839 snprintf(s
, (size_t)slen
, "%s.local.", localStr
);
849 * The hostname is not a FQDN, so look it up...
852 struct hostent
*host
; /* Host entry to get FQDN */
854 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
857 * Use the resolved hostname...
860 strlcpy(s
, host
->h_name
, (size_t)slen
);
862 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
866 * Make sure .local hostnames end with a period...
869 if (strlen(s
) > 6 && !strcmp(s
+ strlen(s
) - 6, ".local"))
870 strlcat(s
, ".", (size_t)slen
);
874 * Convert the hostname to lowercase as needed...
879 char *ptr
; /* Pointer into string */
881 for (ptr
= s
; *ptr
; ptr
++)
882 *ptr
= (char)_cups_tolower((int)*ptr
);
886 * Return the hostname with as much domain info as we have...
894 * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
897 * @since CUPS 2.0/OS 10.10@
900 const char * /* O - Resolved hostname or @code NULL@ */
901 httpResolveHostname(http_t
*http
, /* I - HTTP connection */
902 char *buffer
, /* I - Hostname buffer */
903 size_t bufsize
) /* I - Size of buffer */
908 if (isdigit(http
->hostname
[0] & 255) || http
->hostname
[0] == '[')
910 char temp
[1024]; /* Temporary string */
912 if (httpAddrLookup(http
->hostaddr
, temp
, sizeof(temp
)))
913 strlcpy(http
->hostname
, temp
, sizeof(http
->hostname
));
920 if (http
->hostname
[0] == '/')
921 strlcpy(buffer
, "localhost", bufsize
);
923 strlcpy(buffer
, http
->hostname
, bufsize
);
927 else if (http
->hostname
[0] == '/')
928 return ("localhost");
930 return (http
->hostname
);