]>
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"
18 #endif /* HAVE_RESOLV_H */
20 # include <CoreFoundation/CoreFoundation.h>
21 # include <SystemConfiguration/SystemConfiguration.h>
22 #endif /* __APPLE__ */
26 * 'httpAddrAny()' - Check for the "any" address.
28 * @since CUPS 1.2/macOS 10.5@
31 int /* O - 1 if "any", 0 otherwise */
32 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
38 if (addr
->addr
.sa_family
== AF_INET6
&&
39 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
43 if (addr
->addr
.sa_family
== AF_INET
&&
44 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
52 * 'httpAddrClose()' - Close a socket created by @link httpAddrConnect@ or
53 * @link httpAddrListen@.
55 * Pass @code NULL@ for sockets created with @link httpAddrConnect2@ and the
56 * listen address for sockets created with @link httpAddrListen@. This function
57 * ensures that domain sockets are removed when closed.
59 * @since CUPS 2.0/OS 10.10@
62 int /* O - 0 on success, -1 on failure */
63 httpAddrClose(http_addr_t
*addr
, /* I - Listen address or @code NULL@ */
64 int fd
) /* I - Socket file descriptor */
74 if (addr
&& addr
->addr
.sa_family
== AF_LOCAL
)
75 return (unlink(addr
->un
.sun_path
));
83 * 'httpAddrEqual()' - Compare two addresses.
85 * @since CUPS 1.2/macOS 10.5@
88 int /* O - 1 if equal, 0 if not */
89 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
90 const http_addr_t
*addr2
) /* I - Second address */
98 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
102 if (addr1
->addr
.sa_family
== AF_LOCAL
)
103 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
104 #endif /* AF_LOCAL */
107 if (addr1
->addr
.sa_family
== AF_INET6
)
108 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
109 #endif /* AF_INET6 */
111 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
116 * 'httpAddrLength()' - Return the length of the address in bytes.
118 * @since CUPS 1.2/macOS 10.5@
121 int /* O - Length in bytes */
122 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
128 if (addr
->addr
.sa_family
== AF_INET6
)
129 return (sizeof(addr
->ipv6
));
131 #endif /* AF_INET6 */
133 if (addr
->addr
.sa_family
== AF_LOCAL
)
134 return ((int)(offsetof(struct sockaddr_un
, sun_path
) + strlen(addr
->un
.sun_path
) + 1));
136 #endif /* AF_LOCAL */
137 if (addr
->addr
.sa_family
== AF_INET
)
138 return (sizeof(addr
->ipv4
));
146 * 'httpAddrListen()' - Create a listening socket bound to the specified
149 * @since CUPS 1.7/macOS 10.9@
152 int /* O - Socket or -1 on error */
153 httpAddrListen(http_addr_t
*addr
, /* I - Address to bind to */
154 int port
) /* I - Port number to bind to */
156 int fd
= -1, /* Socket */
157 val
, /* Socket value */
158 status
; /* Bind status */
162 * Range check input...
165 if (!addr
|| port
< 0)
169 * Create the socket and set options...
172 if ((fd
= socket(addr
->addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
174 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
179 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, CUPS_SOCAST
&val
, sizeof(val
));
182 if (addr
->addr
.sa_family
== AF_INET6
)
183 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, CUPS_SOCAST
&val
, sizeof(val
));
184 #endif /* IPV6_V6ONLY */
191 if (addr
->addr
.sa_family
== AF_LOCAL
)
193 mode_t mask
; /* Umask setting */
196 * Remove any existing domain socket file...
199 unlink(addr
->un
.sun_path
);
202 * Save the current umask and set it to 0 so that all users can access
203 * the domain socket...
209 * Bind the domain socket...
212 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
215 * Restore the umask and fix permissions...
219 chmod(addr
->un
.sun_path
, 0140777);
222 #endif /* AF_LOCAL */
224 _httpAddrSetPort(addr
, port
);
226 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
231 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
244 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
256 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
261 * Disable SIGPIPE for this socket.
265 setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
266 #endif /* SO_NOSIGPIPE */
273 * 'httpAddrLocalhost()' - Check for the local loopback address.
275 * @since CUPS 1.2/macOS 10.5@
278 int /* O - 1 if local host, 0 otherwise */
280 const http_addr_t
*addr
) /* I - Address to check */
286 if (addr
->addr
.sa_family
== AF_INET6
&&
287 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
289 #endif /* AF_INET6 */
292 if (addr
->addr
.sa_family
== AF_LOCAL
)
294 #endif /* AF_LOCAL */
296 if (addr
->addr
.sa_family
== AF_INET
&&
297 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
305 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
307 * @since CUPS 1.2/macOS 10.5@
310 char * /* O - Host name */
312 const http_addr_t
*addr
, /* I - Address to lookup */
313 char *name
, /* I - Host name buffer */
314 int namelen
) /* I - Size of name buffer */
316 _cups_globals_t
*cg
= _cupsGlobals();
320 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", (void *)addr
, (void *)name
, namelen
));
323 * Range check input...
326 if (!addr
|| !name
|| namelen
<= 2)
328 if (name
&& namelen
>= 1)
335 if (addr
->addr
.sa_family
== AF_LOCAL
)
337 strlcpy(name
, addr
->un
.sun_path
, (size_t)namelen
);
340 #endif /* AF_LOCAL */
343 * Optimize lookups for localhost/loopback addresses...
346 if (httpAddrLocalhost(addr
))
348 strlcpy(name
, "localhost", (size_t)namelen
);
354 * STR #2920: Initialize resolver after failure in cups-polld
356 * If the previous lookup failed, re-initialize the resolver to prevent
357 * temporary network errors from persisting. This *should* be handled by
358 * the resolver libraries, but apparently the glibc folks do not agree.
360 * We set a flag at the end of this function if we encounter an error that
361 * requires reinitialization of the resolver functions. We then call
362 * res_init() if the flag is set on the next call here or in httpAddrLookup().
365 if (cg
->need_res_init
)
369 cg
->need_res_init
= 0;
371 #endif /* HAVE_RES_INIT */
373 #ifdef HAVE_GETNAMEINFO
376 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
378 * FWIW, I think this is really a bug in the implementation of
379 * getnameinfo(), but falling back on httpAddrString() is easy to
383 int error
= getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), name
, (socklen_t
)namelen
, NULL
, 0, 0);
387 if (error
== EAI_FAIL
)
388 cg
->need_res_init
= 1;
390 return (httpAddrString(addr
, name
, namelen
));
395 struct hostent
*host
; /* Host from name service */
399 if (addr
->addr
.sa_family
== AF_INET6
)
400 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
401 sizeof(struct in_addr
), AF_INET6
);
403 # endif /* AF_INET6 */
404 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
405 sizeof(struct in_addr
), AF_INET
);
410 * No hostname, so return the raw address...
413 if (h_errno
== NO_RECOVERY
)
414 cg
->need_res_init
= 1;
416 return (httpAddrString(addr
, name
, namelen
));
419 strlcpy(name
, host
->h_name
, (size_t)namelen
);
421 #endif /* HAVE_GETNAMEINFO */
423 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
430 * 'httpAddrFamily()' - Get the address family of an address.
433 int /* O - Address family */
434 httpAddrFamily(http_addr_t
*addr
) /* I - Address */
437 return (addr
->addr
.sa_family
);
444 * 'httpAddrPort()' - Get the port number associated with an address.
446 * @since CUPS 1.7/macOS 10.9@
449 int /* O - Port number */
450 httpAddrPort(http_addr_t
*addr
) /* I - Address */
455 else if (addr
->addr
.sa_family
== AF_INET6
)
456 return (ntohs(addr
->ipv6
.sin6_port
));
457 #endif /* AF_INET6 */
458 else if (addr
->addr
.sa_family
== AF_INET
)
459 return (ntohs(addr
->ipv4
.sin_port
));
466 * '_httpAddrSetPort()' - Set the port number associated with an address.
470 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
471 int port
) /* I - Port */
473 if (!addr
|| port
<= 0)
477 if (addr
->addr
.sa_family
== AF_INET6
)
478 addr
->ipv6
.sin6_port
= htons(port
);
480 #endif /* AF_INET6 */
481 if (addr
->addr
.sa_family
== AF_INET
)
482 addr
->ipv4
.sin_port
= htons(port
);
487 * 'httpAddrString()' - Convert an address to a numeric string.
489 * @since CUPS 1.2/macOS 10.5@
492 char * /* O - Numeric address string */
493 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
494 char *s
, /* I - String buffer */
495 int slen
) /* I - Length of string */
497 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", (void *)addr
, (void *)s
, slen
));
500 * Range check input...
503 if (!addr
|| !s
|| slen
<= 2)
512 if (addr
->addr
.sa_family
== AF_LOCAL
)
514 if (addr
->un
.sun_path
[0] == '/')
515 strlcpy(s
, addr
->un
.sun_path
, (size_t)slen
);
517 strlcpy(s
, "localhost", (size_t)slen
);
520 #endif /* AF_LOCAL */
521 if (addr
->addr
.sa_family
== AF_INET
)
523 unsigned temp
; /* Temporary address */
525 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
527 snprintf(s
, (size_t)slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
528 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
531 else if (addr
->addr
.sa_family
== AF_INET6
)
533 char *sptr
, /* Pointer into string */
534 temps
[64]; /* Temporary string for address */
536 # ifdef HAVE_GETNAMEINFO
537 if (getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), temps
, sizeof(temps
), NULL
, 0, NI_NUMERICHOST
))
540 * If we get an error back, then the address type is not supported
541 * and we should zero out the buffer...
548 else if ((sptr
= strchr(temps
, '%')) != NULL
)
551 * Convert "%zone" to "+zone" to match URI form...
558 int i
; /* Looping var */
559 unsigned temp
; /* Current value */
560 const char *prefix
; /* Prefix for address */
564 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
566 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
568 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
570 sptr
+= strlen(sptr
);
574 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
576 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
);
577 sptr
+= strlen(sptr
);
583 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
588 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s:", prefix
);
590 sptr
+= strlen(sptr
);
594 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
596 if ((temp
& 0xffff0000) ||
597 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
599 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
600 sptr
+= strlen(sptr
);
603 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
& 0xffff);
604 sptr
+= strlen(sptr
);
613 strlcpy(temps
, "::", sizeof(temps
));
621 strlcpy(sptr
, "::", sizeof(temps
) - (size_t)(sptr
- temps
));
624 # endif /* HAVE_GETNAMEINFO */
627 * Add "[v1." and "]" around IPv6 address to convert to URI form.
630 snprintf(s
, (size_t)slen
, "[v1.%s]", temps
);
632 #endif /* AF_INET6 */
634 strlcpy(s
, "UNKNOWN", (size_t)slen
);
636 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
643 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
645 * For connections created with @link httpConnect2@, the address is for the
646 * server. For connections created with @link httpAccept@, the address is for
649 * Returns @code NULL@ if the socket is currently unconnected.
651 * @since CUPS 2.0/OS 10.10@
654 http_addr_t
* /* O - Connected address or @code NULL@ */
655 httpGetAddress(http_t
*http
) /* I - HTTP connection */
658 return (http
->hostaddr
);
665 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
666 * address records for the specified name.
668 * @deprecated@ @exclude all@
671 struct hostent
* /* O - Host entry */
672 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
674 const char *nameptr
; /* Pointer into name */
675 unsigned ip
[4]; /* IP address components */
676 _cups_globals_t
*cg
= _cupsGlobals();
677 /* Pointer to library globals */
680 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
683 * Avoid lookup delays and configuration problems when connecting
684 * to the localhost address...
687 if (!strcmp(name
, "localhost"))
691 * This function is needed because some operating systems have a
692 * buggy implementation of gethostbyname() that does not support
693 * IP addresses. If the first character of the name string is a
694 * number, then sscanf() is used to extract the IP components.
695 * We then pack the components into an IPv4 address manually,
696 * since the inet_aton() function is deprecated. We use the
697 * htonl() macro to get the right byte order for the address.
699 * We also support domain sockets when supported by the underlying
707 * A domain socket address, so make an AF_LOCAL entry and return it...
710 cg
->hostent
.h_name
= (char *)name
;
711 cg
->hostent
.h_aliases
= NULL
;
712 cg
->hostent
.h_addrtype
= AF_LOCAL
;
713 cg
->hostent
.h_length
= (int)strlen(name
) + 1;
714 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
715 cg
->ip_ptrs
[0] = (char *)name
;
716 cg
->ip_ptrs
[1] = NULL
;
718 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
720 return (&cg
->hostent
);
722 #endif /* AF_LOCAL */
724 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
729 * We have an IPv4 address; break it up and provide the host entry
733 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
734 return (NULL
); /* Must have 4 numbers */
736 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
737 return (NULL
); /* Invalid byte ranges! */
739 cg
->ip_addr
= htonl((((((((unsigned)ip
[0] << 8) | (unsigned)ip
[1]) << 8) |
740 (unsigned)ip
[2]) << 8) |
744 * Fill in the host entry and return it...
747 cg
->hostent
.h_name
= (char *)name
;
748 cg
->hostent
.h_aliases
= NULL
;
749 cg
->hostent
.h_addrtype
= AF_INET
;
750 cg
->hostent
.h_length
= 4;
751 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
752 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
753 cg
->ip_ptrs
[1] = NULL
;
755 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
757 return (&cg
->hostent
);
762 * Use the gethostbyname() function to get the IPv4 address for
766 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
768 return (gethostbyname(name
));
774 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
776 * When "http" points to a connected socket, return the hostname or
777 * address that was used in the call to httpConnect() or httpConnectEncrypt(),
778 * or the address of the client for the connection from httpAcceptConnection().
779 * Otherwise, return the FQDN for the local system using both gethostname()
780 * and gethostbyname() to get the local hostname with domain.
782 * @since CUPS 1.2/macOS 10.5@
785 const char * /* O - FQDN for connection or system */
786 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
787 char *s
, /* I - String buffer for name */
788 int slen
) /* I - Size of buffer */
794 if (http
->hostname
[0] == '/')
795 return ("localhost");
797 return (http
->hostname
);
799 else if (http
->hostname
[0] == '/')
800 strlcpy(s
, "localhost", (size_t)slen
);
802 strlcpy(s
, http
->hostname
, (size_t)slen
);
807 * Get the hostname...
813 if (gethostname(s
, (size_t)slen
) < 0)
814 strlcpy(s
, "localhost", (size_t)slen
);
818 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
820 * The hostname is not a FQDN, so use the local hostname from the
821 * SystemConfiguration framework...
824 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
825 CFSTR("libcups"), NULL
, NULL
);
826 /* System configuration data */
827 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
828 /* Local host name */
829 char localStr
[1024]; /* Local host name C string */
831 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
832 kCFStringEncodingUTF8
))
835 * Append ".local." to the hostname we get...
838 snprintf(s
, (size_t)slen
, "%s.local.", localStr
);
848 * The hostname is not a FQDN, so look it up...
851 struct hostent
*host
; /* Host entry to get FQDN */
853 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
856 * Use the resolved hostname...
859 strlcpy(s
, host
->h_name
, (size_t)slen
);
861 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
865 * Make sure .local hostnames end with a period...
868 if (strlen(s
) > 6 && !strcmp(s
+ strlen(s
) - 6, ".local"))
869 strlcat(s
, ".", (size_t)slen
);
873 * Convert the hostname to lowercase as needed...
878 char *ptr
; /* Pointer into string */
880 for (ptr
= s
; *ptr
; ptr
++)
881 *ptr
= (char)_cups_tolower((int)*ptr
);
885 * Return the hostname with as much domain info as we have...
893 * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
896 * @since CUPS 2.0/OS 10.10@
899 const char * /* O - Resolved hostname or @code NULL@ */
900 httpResolveHostname(http_t
*http
, /* I - HTTP connection */
901 char *buffer
, /* I - Hostname buffer */
902 size_t bufsize
) /* I - Size of buffer */
907 if (isdigit(http
->hostname
[0] & 255) || http
->hostname
[0] == '[')
909 char temp
[1024]; /* Temporary string */
911 if (httpAddrLookup(http
->hostaddr
, temp
, sizeof(temp
)))
912 strlcpy(http
->hostname
, temp
, sizeof(http
->hostname
));
919 if (http
->hostname
[0] == '/')
920 strlcpy(buffer
, "localhost", bufsize
);
922 strlcpy(buffer
, http
->hostname
, bufsize
);
926 else if (http
->hostname
[0] == '/')
927 return ("localhost");
929 return (http
->hostname
);