]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * HTTP address routines for CUPS.
4 * Copyright 2007-2019 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
12 * Include necessary headers...
15 #include "cups-private.h"
16 #include "debug-internal.h"
20 #endif /* HAVE_RESOLV_H */
22 # include <CoreFoundation/CoreFoundation.h>
23 # ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
24 # include <SystemConfiguration/SystemConfiguration.h>
25 # endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
26 #endif /* __APPLE__ */
30 * 'httpAddrAny()' - Check for the "any" address.
32 * @since CUPS 1.2/macOS 10.5@
35 int /* O - 1 if "any", 0 otherwise */
36 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
42 if (addr
->addr
.sa_family
== AF_INET6
&&
43 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
47 if (addr
->addr
.sa_family
== AF_INET
&&
48 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
56 * 'httpAddrClose()' - Close a socket created by @link httpAddrConnect@ or
57 * @link httpAddrListen@.
59 * Pass @code NULL@ for sockets created with @link httpAddrConnect2@ and the
60 * listen address for sockets created with @link httpAddrListen@. This function
61 * ensures that domain sockets are removed when closed.
63 * @since CUPS 2.0/OS 10.10@
66 int /* O - 0 on success, -1 on failure */
67 httpAddrClose(http_addr_t
*addr
, /* I - Listen address or @code NULL@ */
68 int fd
) /* I - Socket file descriptor */
78 if (addr
&& addr
->addr
.sa_family
== AF_LOCAL
)
79 return (unlink(addr
->un
.sun_path
));
87 * 'httpAddrEqual()' - Compare two addresses.
89 * @since CUPS 1.2/macOS 10.5@
92 int /* O - 1 if equal, 0 if not */
93 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
94 const http_addr_t
*addr2
) /* I - Second address */
102 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
106 if (addr1
->addr
.sa_family
== AF_LOCAL
)
107 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
108 #endif /* AF_LOCAL */
111 if (addr1
->addr
.sa_family
== AF_INET6
)
112 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
113 #endif /* AF_INET6 */
115 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
120 * 'httpAddrLength()' - Return the length of the address in bytes.
122 * @since CUPS 1.2/macOS 10.5@
125 int /* O - Length in bytes */
126 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
132 if (addr
->addr
.sa_family
== AF_INET6
)
133 return (sizeof(addr
->ipv6
));
135 #endif /* AF_INET6 */
137 if (addr
->addr
.sa_family
== AF_LOCAL
)
138 return ((int)(offsetof(struct sockaddr_un
, sun_path
) + strlen(addr
->un
.sun_path
) + 1));
140 #endif /* AF_LOCAL */
141 if (addr
->addr
.sa_family
== AF_INET
)
142 return (sizeof(addr
->ipv4
));
150 * 'httpAddrListen()' - Create a listening socket bound to the specified
153 * @since CUPS 1.7/macOS 10.9@
156 int /* O - Socket or -1 on error */
157 httpAddrListen(http_addr_t
*addr
, /* I - Address to bind to */
158 int port
) /* I - Port number to bind to */
160 int fd
= -1, /* Socket */
161 val
, /* Socket value */
162 status
; /* Bind status */
166 * Range check input...
169 if (!addr
|| port
< 0)
173 * Create the socket and set options...
176 if ((fd
= socket(addr
->addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
178 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
183 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, CUPS_SOCAST
&val
, sizeof(val
));
186 if (addr
->addr
.sa_family
== AF_INET6
)
187 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, CUPS_SOCAST
&val
, sizeof(val
));
188 #endif /* IPV6_V6ONLY */
195 if (addr
->addr
.sa_family
== AF_LOCAL
)
197 mode_t mask
; /* Umask setting */
200 * Remove any existing domain socket file...
203 unlink(addr
->un
.sun_path
);
206 * Save the current umask and set it to 0 so that all users can access
207 * the domain socket...
213 * Bind the domain socket...
216 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
219 * Restore the umask and fix permissions...
223 chmod(addr
->un
.sun_path
, 0140777);
226 #endif /* AF_LOCAL */
228 _httpAddrSetPort(addr
, port
);
230 status
= bind(fd
, (struct sockaddr
*)addr
, (socklen_t
)httpAddrLength(addr
));
235 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
248 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
260 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
265 * Disable SIGPIPE for this socket.
269 setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
270 #endif /* SO_NOSIGPIPE */
277 * 'httpAddrLocalhost()' - Check for the local loopback address.
279 * @since CUPS 1.2/macOS 10.5@
282 int /* O - 1 if local host, 0 otherwise */
284 const http_addr_t
*addr
) /* I - Address to check */
290 if (addr
->addr
.sa_family
== AF_INET6
&&
291 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
293 #endif /* AF_INET6 */
296 if (addr
->addr
.sa_family
== AF_LOCAL
)
298 #endif /* AF_LOCAL */
300 if (addr
->addr
.sa_family
== AF_INET
&&
301 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
309 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
311 * @since CUPS 1.2/macOS 10.5@
314 char * /* O - Host name */
316 const http_addr_t
*addr
, /* I - Address to lookup */
317 char *name
, /* I - Host name buffer */
318 int namelen
) /* I - Size of name buffer */
320 _cups_globals_t
*cg
= _cupsGlobals();
324 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", (void *)addr
, (void *)name
, namelen
));
327 * Range check input...
330 if (!addr
|| !name
|| namelen
<= 2)
332 if (name
&& namelen
>= 1)
339 if (addr
->addr
.sa_family
== AF_LOCAL
)
341 strlcpy(name
, addr
->un
.sun_path
, (size_t)namelen
);
344 #endif /* AF_LOCAL */
347 * Optimize lookups for localhost/loopback addresses...
350 if (httpAddrLocalhost(addr
))
352 strlcpy(name
, "localhost", (size_t)namelen
);
358 * STR #2920: Initialize resolver after failure in cups-polld
360 * If the previous lookup failed, re-initialize the resolver to prevent
361 * temporary network errors from persisting. This *should* be handled by
362 * the resolver libraries, but apparently the glibc folks do not agree.
364 * We set a flag at the end of this function if we encounter an error that
365 * requires reinitialization of the resolver functions. We then call
366 * res_init() if the flag is set on the next call here or in httpAddrLookup().
369 if (cg
->need_res_init
)
373 cg
->need_res_init
= 0;
375 #endif /* HAVE_RES_INIT */
377 #ifdef HAVE_GETNAMEINFO
380 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
382 * FWIW, I think this is really a bug in the implementation of
383 * getnameinfo(), but falling back on httpAddrString() is easy to
387 int error
= getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), name
, (socklen_t
)namelen
, NULL
, 0, 0);
391 if (error
== EAI_FAIL
)
392 cg
->need_res_init
= 1;
394 return (httpAddrString(addr
, name
, namelen
));
399 struct hostent
*host
; /* Host from name service */
403 if (addr
->addr
.sa_family
== AF_INET6
)
404 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
405 sizeof(struct in_addr
), AF_INET6
);
407 # endif /* AF_INET6 */
408 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
409 sizeof(struct in_addr
), AF_INET
);
414 * No hostname, so return the raw address...
417 if (h_errno
== NO_RECOVERY
)
418 cg
->need_res_init
= 1;
420 return (httpAddrString(addr
, name
, namelen
));
423 strlcpy(name
, host
->h_name
, (size_t)namelen
);
425 #endif /* HAVE_GETNAMEINFO */
427 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
434 * 'httpAddrFamily()' - Get the address family of an address.
437 int /* O - Address family */
438 httpAddrFamily(http_addr_t
*addr
) /* I - Address */
441 return (addr
->addr
.sa_family
);
448 * 'httpAddrPort()' - Get the port number associated with an address.
450 * @since CUPS 1.7/macOS 10.9@
453 int /* O - Port number */
454 httpAddrPort(http_addr_t
*addr
) /* I - Address */
459 else if (addr
->addr
.sa_family
== AF_INET6
)
460 return (ntohs(addr
->ipv6
.sin6_port
));
461 #endif /* AF_INET6 */
462 else if (addr
->addr
.sa_family
== AF_INET
)
463 return (ntohs(addr
->ipv4
.sin_port
));
470 * '_httpAddrSetPort()' - Set the port number associated with an address.
474 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
475 int port
) /* I - Port */
477 if (!addr
|| port
<= 0)
481 if (addr
->addr
.sa_family
== AF_INET6
)
482 addr
->ipv6
.sin6_port
= htons(port
);
484 #endif /* AF_INET6 */
485 if (addr
->addr
.sa_family
== AF_INET
)
486 addr
->ipv4
.sin_port
= htons(port
);
491 * 'httpAddrString()' - Convert an address to a numeric string.
493 * @since CUPS 1.2/macOS 10.5@
496 char * /* O - Numeric address string */
497 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
498 char *s
, /* I - String buffer */
499 int slen
) /* I - Length of string */
501 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", (void *)addr
, (void *)s
, slen
));
504 * Range check input...
507 if (!addr
|| !s
|| slen
<= 2)
516 if (addr
->addr
.sa_family
== AF_LOCAL
)
518 if (addr
->un
.sun_path
[0] == '/')
519 strlcpy(s
, addr
->un
.sun_path
, (size_t)slen
);
521 strlcpy(s
, "localhost", (size_t)slen
);
524 #endif /* AF_LOCAL */
525 if (addr
->addr
.sa_family
== AF_INET
)
527 unsigned temp
; /* Temporary address */
529 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
531 snprintf(s
, (size_t)slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
532 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
535 else if (addr
->addr
.sa_family
== AF_INET6
)
537 char *sptr
, /* Pointer into string */
538 temps
[64]; /* Temporary string for address */
540 # ifdef HAVE_GETNAMEINFO
541 if (getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), temps
, sizeof(temps
), NULL
, 0, NI_NUMERICHOST
))
544 * If we get an error back, then the address type is not supported
545 * and we should zero out the buffer...
552 else if ((sptr
= strchr(temps
, '%')) != NULL
)
555 * Convert "%zone" to "+zone" to match URI form...
562 int i
; /* Looping var */
563 unsigned temp
; /* Current value */
564 const char *prefix
; /* Prefix for address */
568 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
570 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
572 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
574 sptr
+= strlen(sptr
);
578 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
580 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
);
581 sptr
+= strlen(sptr
);
587 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
592 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s:", prefix
);
594 sptr
+= strlen(sptr
);
598 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
600 if ((temp
& 0xffff0000) ||
601 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
603 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, (temp
>> 16) & 0xffff);
604 sptr
+= strlen(sptr
);
607 snprintf(sptr
, sizeof(temps
) - (size_t)(sptr
- temps
), "%s%x", prefix
, temp
& 0xffff);
608 sptr
+= strlen(sptr
);
617 strlcpy(temps
, "::", sizeof(temps
));
625 strlcpy(sptr
, "::", sizeof(temps
) - (size_t)(sptr
- temps
));
628 # endif /* HAVE_GETNAMEINFO */
631 * Add "[v1." and "]" around IPv6 address to convert to URI form.
634 snprintf(s
, (size_t)slen
, "[v1.%s]", temps
);
636 #endif /* AF_INET6 */
638 strlcpy(s
, "UNKNOWN", (size_t)slen
);
640 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
647 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
649 * For connections created with @link httpConnect2@, the address is for the
650 * server. For connections created with @link httpAccept@, the address is for
653 * Returns @code NULL@ if the socket is currently unconnected.
655 * @since CUPS 2.0/OS 10.10@
658 http_addr_t
* /* O - Connected address or @code NULL@ */
659 httpGetAddress(http_t
*http
) /* I - HTTP connection */
662 return (http
->hostaddr
);
669 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
670 * address records for the specified name.
672 * @deprecated@ @exclude all@
675 struct hostent
* /* O - Host entry */
676 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
678 const char *nameptr
; /* Pointer into name */
679 unsigned ip
[4]; /* IP address components */
680 _cups_globals_t
*cg
= _cupsGlobals();
681 /* Pointer to library globals */
684 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
687 * Avoid lookup delays and configuration problems when connecting
688 * to the localhost address...
691 if (!strcmp(name
, "localhost"))
695 * This function is needed because some operating systems have a
696 * buggy implementation of gethostbyname() that does not support
697 * IP addresses. If the first character of the name string is a
698 * number, then sscanf() is used to extract the IP components.
699 * We then pack the components into an IPv4 address manually,
700 * since the inet_aton() function is deprecated. We use the
701 * htonl() macro to get the right byte order for the address.
703 * We also support domain sockets when supported by the underlying
711 * A domain socket address, so make an AF_LOCAL entry and return it...
714 cg
->hostent
.h_name
= (char *)name
;
715 cg
->hostent
.h_aliases
= NULL
;
716 cg
->hostent
.h_addrtype
= AF_LOCAL
;
717 cg
->hostent
.h_length
= (int)strlen(name
) + 1;
718 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
719 cg
->ip_ptrs
[0] = (char *)name
;
720 cg
->ip_ptrs
[1] = NULL
;
722 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
724 return (&cg
->hostent
);
726 #endif /* AF_LOCAL */
728 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
733 * We have an IPv4 address; break it up and provide the host entry
737 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
738 return (NULL
); /* Must have 4 numbers */
740 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
741 return (NULL
); /* Invalid byte ranges! */
743 cg
->ip_addr
= htonl((((((((unsigned)ip
[0] << 8) | (unsigned)ip
[1]) << 8) |
744 (unsigned)ip
[2]) << 8) |
748 * Fill in the host entry and return it...
751 cg
->hostent
.h_name
= (char *)name
;
752 cg
->hostent
.h_aliases
= NULL
;
753 cg
->hostent
.h_addrtype
= AF_INET
;
754 cg
->hostent
.h_length
= 4;
755 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
756 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
757 cg
->ip_ptrs
[1] = NULL
;
759 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
761 return (&cg
->hostent
);
766 * Use the gethostbyname() function to get the IPv4 address for
770 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
772 return (gethostbyname(name
));
778 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
780 * When "http" points to a connected socket, return the hostname or
781 * address that was used in the call to httpConnect() or httpConnectEncrypt(),
782 * or the address of the client for the connection from httpAcceptConnection().
783 * Otherwise, return the FQDN for the local system using both gethostname()
784 * and gethostbyname() to get the local hostname with domain.
786 * @since CUPS 1.2/macOS 10.5@
789 const char * /* O - FQDN for connection or system */
790 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
791 char *s
, /* I - String buffer for name */
792 int slen
) /* I - Size of buffer */
798 if (http
->hostname
[0] == '/')
799 return ("localhost");
801 return (http
->hostname
);
803 else if (http
->hostname
[0] == '/')
804 strlcpy(s
, "localhost", (size_t)slen
);
806 strlcpy(s
, http
->hostname
, (size_t)slen
);
811 * Get the hostname...
817 if (gethostname(s
, (size_t)slen
) < 0)
818 strlcpy(s
, "localhost", (size_t)slen
);
822 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
824 * The hostname is not a FQDN, so use the local hostname from the
825 * SystemConfiguration framework...
828 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
829 CFSTR("libcups"), NULL
, NULL
);
830 /* System configuration data */
831 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
832 /* Local host name */
833 char localStr
[1024]; /* Local host name C string */
835 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
836 kCFStringEncodingUTF8
))
839 * Append ".local." to the hostname we get...
842 snprintf(s
, (size_t)slen
, "%s.local.", localStr
);
852 * The hostname is not a FQDN, so look it up...
855 struct hostent
*host
; /* Host entry to get FQDN */
857 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
860 * Use the resolved hostname...
863 strlcpy(s
, host
->h_name
, (size_t)slen
);
865 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
869 * Make sure .local hostnames end with a period...
872 if (strlen(s
) > 6 && !strcmp(s
+ strlen(s
) - 6, ".local"))
873 strlcat(s
, ".", (size_t)slen
);
877 * Convert the hostname to lowercase as needed...
882 char *ptr
; /* Pointer into string */
884 for (ptr
= s
; *ptr
; ptr
++)
885 *ptr
= (char)_cups_tolower((int)*ptr
);
889 * Return the hostname with as much domain info as we have...
897 * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
900 * @since CUPS 2.0/OS 10.10@
903 const char * /* O - Resolved hostname or @code NULL@ */
904 httpResolveHostname(http_t
*http
, /* I - HTTP connection */
905 char *buffer
, /* I - Hostname buffer */
906 size_t bufsize
) /* I - Size of buffer */
911 if (isdigit(http
->hostname
[0] & 255) || http
->hostname
[0] == '[')
913 char temp
[1024]; /* Temporary string */
915 if (httpAddrLookup(http
->hostaddr
, temp
, sizeof(temp
)))
916 strlcpy(http
->hostname
, temp
, sizeof(http
->hostname
));
923 if (http
->hostname
[0] == '/')
924 strlcpy(buffer
, "localhost", bufsize
);
926 strlcpy(buffer
, http
->hostname
, bufsize
);
930 else if (http
->hostname
[0] == '/')
931 return ("localhost");
933 return (http
->hostname
);