]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
a3a628c54a012db52042d1024ca391e0ea8719c7
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/".
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/OS X 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.
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/OS X 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/OS X 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/OS X 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/OS X 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/OS X 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)", addr
, name
,
330 * Range check input...
333 if (!addr
|| !name
|| namelen
<= 2)
335 if (name
&& namelen
>= 1)
342 if (addr
->addr
.sa_family
== AF_LOCAL
)
344 strlcpy(name
, addr
->un
.sun_path
, namelen
);
347 #endif /* AF_LOCAL */
350 * Optimize lookups for localhost/loopback addresses...
353 if (httpAddrLocalhost(addr
))
355 strlcpy(name
, "localhost", namelen
);
361 * STR #2920: Initialize resolver after failure in cups-polld
363 * If the previous lookup failed, re-initialize the resolver to prevent
364 * temporary network errors from persisting. This *should* be handled by
365 * the resolver libraries, but apparently the glibc folks do not agree.
367 * We set a flag at the end of this function if we encounter an error that
368 * requires reinitialization of the resolver functions. We then call
369 * res_init() if the flag is set on the next call here or in httpAddrLookup().
372 if (cg
->need_res_init
)
376 cg
->need_res_init
= 0;
378 #endif /* HAVE_RES_INIT */
380 #ifdef HAVE_GETNAMEINFO
383 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
385 * FWIW, I think this is really a bug in the implementation of
386 * getnameinfo(), but falling back on httpAddrString() is easy to
390 int error
= getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), name
, (socklen_t
)namelen
, NULL
, 0, 0);
394 if (error
== EAI_FAIL
)
395 cg
->need_res_init
= 1;
397 return (httpAddrString(addr
, name
, namelen
));
402 struct hostent
*host
; /* Host from name service */
406 if (addr
->addr
.sa_family
== AF_INET6
)
407 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
408 sizeof(struct in_addr
), AF_INET6
);
410 # endif /* AF_INET6 */
411 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
412 sizeof(struct in_addr
), AF_INET
);
417 * No hostname, so return the raw address...
420 if (h_errno
== NO_RECOVERY
)
421 cg
->need_res_init
= 1;
423 return (httpAddrString(addr
, name
, namelen
));
426 strlcpy(name
, host
->h_name
, namelen
);
428 #endif /* HAVE_GETNAMEINFO */
430 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
437 * 'httpAddrFamily()' - Get the address family of an address.
440 int /* O - Address family */
441 httpAddrFamily(http_addr_t
*addr
) /* I - Address */
444 return (addr
->addr
.sa_family
);
451 * 'httpAddrPort()' - Get the port number associated with an address.
453 * @since CUPS 1.7/OS X 10.9@
456 int /* O - Port number */
457 httpAddrPort(http_addr_t
*addr
) /* I - Address */
462 else if (addr
->addr
.sa_family
== AF_INET6
)
463 return (ntohs(addr
->ipv6
.sin6_port
));
464 #endif /* AF_INET6 */
465 else if (addr
->addr
.sa_family
== AF_INET
)
466 return (ntohs(addr
->ipv4
.sin_port
));
473 * '_httpAddrSetPort()' - Set the port number associated with an address.
477 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
478 int port
) /* I - Port */
480 if (!addr
|| port
<= 0)
484 if (addr
->addr
.sa_family
== AF_INET6
)
485 addr
->ipv6
.sin6_port
= htons(port
);
487 #endif /* AF_INET6 */
488 if (addr
->addr
.sa_family
== AF_INET
)
489 addr
->ipv4
.sin_port
= htons(port
);
494 * 'httpAddrString()' - Convert an address to a numeric string.
496 * @since CUPS 1.2/OS X 10.5@
499 char * /* O - Numeric address string */
500 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
501 char *s
, /* I - String buffer */
502 int slen
) /* I - Length of string */
504 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr
, s
, slen
));
507 * Range check input...
510 if (!addr
|| !s
|| slen
<= 2)
519 if (addr
->addr
.sa_family
== AF_LOCAL
)
521 if (addr
->un
.sun_path
[0] == '/')
522 strlcpy(s
, addr
->un
.sun_path
, slen
);
524 strlcpy(s
, "localhost", slen
);
527 #endif /* AF_LOCAL */
528 if (addr
->addr
.sa_family
== AF_INET
)
530 unsigned temp
; /* Temporary address */
533 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
535 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
536 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
539 else if (addr
->addr
.sa_family
== AF_INET6
)
541 char *sptr
, /* Pointer into string */
542 temps
[64]; /* Temporary string for address */
544 # ifdef HAVE_GETNAMEINFO
545 if (getnameinfo(&addr
->addr
, (socklen_t
)httpAddrLength(addr
), temps
, sizeof(temps
), NULL
, 0, NI_NUMERICHOST
))
548 * If we get an error back, then the address type is not supported
549 * and we should zero out the buffer...
556 else if ((sptr
= strchr(temps
, '%')) != NULL
)
559 * Convert "%zone" to "+zone" to match URI form...
566 int i
; /* Looping var */
567 unsigned temp
; /* Current value */
568 const char *prefix
; /* Prefix for address */
572 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
574 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
576 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
577 (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
) - (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
) - (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
) - (sptr
- temps
), "%s%x", prefix
,
609 (temp
>> 16) & 0xffff);
610 sptr
+= strlen(sptr
);
613 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
615 sptr
+= strlen(sptr
);
624 strlcpy(temps
, "::", sizeof(temps
));
632 strlcpy(sptr
, "::", sizeof(temps
) - (sptr
- temps
));
635 # endif /* HAVE_GETNAMEINFO */
638 * Add "[v1." and "]" around IPv6 address to convert to URI form.
641 snprintf(s
, slen
, "[v1.%s]", temps
);
643 #endif /* AF_INET6 */
645 strlcpy(s
, "UNKNOWN", slen
);
647 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
654 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
656 * Returns @code NULL@ if the socket is currently unconnected.
661 http_addr_t
* /* O - Connected address or @code NULL@ */
662 httpGetAddress(http_t
*http
) /* I - HTTP connection */
665 return (http
->hostaddr
);
672 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
673 * address records for the specified name.
678 struct hostent
* /* O - Host entry */
679 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
681 const char *nameptr
; /* Pointer into name */
682 unsigned ip
[4]; /* IP address components */
683 _cups_globals_t
*cg
= _cupsGlobals();
684 /* Pointer to library globals */
687 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
690 * Avoid lookup delays and configuration problems when connecting
691 * to the localhost address...
694 if (!strcmp(name
, "localhost"))
698 * This function is needed because some operating systems have a
699 * buggy implementation of gethostbyname() that does not support
700 * IP addresses. If the first character of the name string is a
701 * number, then sscanf() is used to extract the IP components.
702 * We then pack the components into an IPv4 address manually,
703 * since the inet_aton() function is deprecated. We use the
704 * htonl() macro to get the right byte order for the address.
706 * We also support domain sockets when supported by the underlying
714 * A domain socket address, so make an AF_LOCAL entry and return it...
717 cg
->hostent
.h_name
= (char *)name
;
718 cg
->hostent
.h_aliases
= NULL
;
719 cg
->hostent
.h_addrtype
= AF_LOCAL
;
720 cg
->hostent
.h_length
= (int)strlen(name
) + 1;
721 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
722 cg
->ip_ptrs
[0] = (char *)name
;
723 cg
->ip_ptrs
[1] = NULL
;
725 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
727 return (&cg
->hostent
);
729 #endif /* AF_LOCAL */
731 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
736 * We have an IPv4 address; break it up and provide the host entry
740 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
741 return (NULL
); /* Must have 4 numbers */
743 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
744 return (NULL
); /* Invalid byte ranges! */
746 cg
->ip_addr
= htonl((((((((unsigned)ip
[0] << 8) | (unsigned)ip
[1]) << 8) |
747 (unsigned)ip
[2]) << 8) |
751 * Fill in the host entry and return it...
754 cg
->hostent
.h_name
= (char *)name
;
755 cg
->hostent
.h_aliases
= NULL
;
756 cg
->hostent
.h_addrtype
= AF_INET
;
757 cg
->hostent
.h_length
= 4;
758 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
759 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
760 cg
->ip_ptrs
[1] = NULL
;
762 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
764 return (&cg
->hostent
);
769 * Use the gethostbyname() function to get the IPv4 address for
773 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
775 return (gethostbyname(name
));
781 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
783 * When "http" points to a connected socket, return the hostname or
784 * address that was used in the call to httpConnect() or httpConnectEncrypt(),
785 * or the address of the client for the connection from httpAcceptConnection().
786 * Otherwise, return the FQDN for the local system using both gethostname()
787 * and gethostbyname() to get the local hostname with domain.
789 * @since CUPS 1.2/OS X 10.5@
792 const char * /* O - FQDN for connection or system */
793 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
794 char *s
, /* I - String buffer for name */
795 int slen
) /* I - Size of buffer */
801 if (http
->hostname
[0] == '/')
802 return ("localhost");
804 return (http
->hostname
);
806 else if (http
->hostname
[0] == '/')
807 strlcpy(s
, "localhost", slen
);
809 strlcpy(s
, http
->hostname
, slen
);
814 * Get the hostname...
820 if (gethostname(s
, (size_t)slen
) < 0)
821 strlcpy(s
, "localhost", slen
);
825 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
827 * The hostname is not a FQDN, so use the local hostname from the
828 * SystemConfiguration framework...
831 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
832 CFSTR("libcups"), NULL
, NULL
);
833 /* System configuration data */
834 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
835 /* Local host name */
836 char localStr
[1024]; /* Local host name C string */
838 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
839 kCFStringEncodingUTF8
))
842 * Append ".local." to the hostname we get...
845 snprintf(s
, slen
, "%s.local.", localStr
);
855 * The hostname is not a FQDN, so look it up...
858 struct hostent
*host
; /* Host entry to get FQDN */
860 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
863 * Use the resolved hostname...
866 strlcpy(s
, host
->h_name
, slen
);
868 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
873 * Return the hostname with as much domain info as we have...
881 * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
887 const char * /* O - Resolved hostname or @code NULL@ */
888 httpResolveHostname(http_t
*http
, /* I - HTTP connection */
889 char *buffer
, /* I - Hostname buffer */
890 size_t bufsize
) /* I - Size of buffer */
895 if (isdigit(http
->hostname
[0] & 255) || http
->hostname
[0] == '[')
897 char temp
[1024]; /* Temporary string */
899 if (httpAddrLookup(http
->hostaddr
, temp
, sizeof(temp
)))
900 strlcpy(http
->hostname
, temp
, sizeof(http
->hostname
));
907 if (http
->hostname
[0] == '/')
908 strlcpy(buffer
, "localhost", bufsize
);
910 strlcpy(buffer
, http
->hostname
, bufsize
);
914 else if (http
->hostname
[0] == '/')
915 return ("localhost");
917 return (http
->hostname
);