]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
e8daaf3e788b7fa64b82d43517c5ba5005133541
2 * "$Id: http-addr.c 11642 2014-02-27 15:57:59Z msweet $"
4 * HTTP address routines for CUPS.
6 * Copyright 2007-2013 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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "cups-private.h"
25 #endif /* HAVE_RESOLV_H */
27 # include <CoreFoundation/CoreFoundation.h>
28 # include <SystemConfiguration/SystemConfiguration.h>
29 #endif /* __APPLE__ */
33 * 'httpAddrAny()' - Check for the "any" address.
35 * @since CUPS 1.2/OS X 10.5@
38 int /* O - 1 if "any", 0 otherwise */
39 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
45 if (addr
->addr
.sa_family
== AF_INET6
&&
46 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
50 if (addr
->addr
.sa_family
== AF_INET
&&
51 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
59 * 'httpAddrEqual()' - Compare two addresses.
61 * @since CUPS 1.2/OS X 10.5@
64 int /* O - 1 if equal, 0 if not */
65 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
66 const http_addr_t
*addr2
) /* I - Second address */
74 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
78 if (addr1
->addr
.sa_family
== AF_LOCAL
)
79 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
83 if (addr1
->addr
.sa_family
== AF_INET6
)
84 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
87 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
92 * 'httpAddrLength()' - Return the length of the address in bytes.
94 * @since CUPS 1.2/OS X 10.5@
97 int /* O - Length in bytes */
98 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
104 if (addr
->addr
.sa_family
== AF_INET6
)
105 return (sizeof(addr
->ipv6
));
107 #endif /* AF_INET6 */
109 if (addr
->addr
.sa_family
== AF_LOCAL
)
110 return (offsetof(struct sockaddr_un
, sun_path
) +
111 strlen(addr
->un
.sun_path
) + 1);
113 #endif /* AF_LOCAL */
114 if (addr
->addr
.sa_family
== AF_INET
)
115 return (sizeof(addr
->ipv4
));
123 * 'httpAddrListen()' - Create a listening socket bound to the specified
126 * @since CUPS 1.7/OS X 10.9@
129 int /* O - Socket or -1 on error */
130 httpAddrListen(http_addr_t
*addr
, /* I - Address to bind to */
131 int port
) /* I - Port number to bind to */
133 int fd
= -1, /* Socket */
134 val
; /* Socket value */
138 * Range check input...
141 if (!addr
|| port
<= 0)
144 if ((fd
= socket(addr
->addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
146 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
151 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, CUPS_SOCAST
&val
, sizeof(val
));
154 if (addr
->addr
.sa_family
== AF_INET6
)
155 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, CUPS_SOCAST
&val
, sizeof(val
));
156 #endif /* IPV6_V6ONLY */
158 _httpAddrSetPort(addr
, port
);
160 if (bind(fd
, (struct sockaddr
*)addr
, httpAddrLength(addr
)))
162 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
171 _cupsSetHTTPError(HTTP_STATUS_ERROR
);
180 * Disable SIGPIPE for this socket.
184 setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, CUPS_SOCAST
&val
, sizeof(val
));
185 #endif /* SO_NOSIGPIPE */
192 * 'httpAddrLocalhost()' - Check for the local loopback address.
194 * @since CUPS 1.2/OS X 10.5@
197 int /* O - 1 if local host, 0 otherwise */
199 const http_addr_t
*addr
) /* I - Address to check */
205 if (addr
->addr
.sa_family
== AF_INET6
&&
206 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
208 #endif /* AF_INET6 */
211 if (addr
->addr
.sa_family
== AF_LOCAL
)
213 #endif /* AF_LOCAL */
215 if (addr
->addr
.sa_family
== AF_INET
&&
216 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
224 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
226 * @since CUPS 1.2/OS X 10.5@
229 char * /* O - Host name */
231 const http_addr_t
*addr
, /* I - Address to lookup */
232 char *name
, /* I - Host name buffer */
233 int namelen
) /* I - Size of name buffer */
235 _cups_globals_t
*cg
= _cupsGlobals();
239 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr
, name
,
243 * Range check input...
246 if (!addr
|| !name
|| namelen
<= 2)
248 if (name
&& namelen
>= 1)
255 if (addr
->addr
.sa_family
== AF_LOCAL
)
257 strlcpy(name
, addr
->un
.sun_path
, namelen
);
260 #endif /* AF_LOCAL */
263 * Optimize lookups for localhost/loopback addresses...
266 if (httpAddrLocalhost(addr
))
268 strlcpy(name
, "localhost", namelen
);
274 * STR #2920: Initialize resolver after failure in cups-polld
276 * If the previous lookup failed, re-initialize the resolver to prevent
277 * temporary network errors from persisting. This *should* be handled by
278 * the resolver libraries, but apparently the glibc folks do not agree.
280 * We set a flag at the end of this function if we encounter an error that
281 * requires reinitialization of the resolver functions. We then call
282 * res_init() if the flag is set on the next call here or in httpAddrLookup().
285 if (cg
->need_res_init
)
289 cg
->need_res_init
= 0;
291 #endif /* HAVE_RES_INIT */
293 #ifdef HAVE_GETNAMEINFO
296 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
298 * FWIW, I think this is really a bug in the implementation of
299 * getnameinfo(), but falling back on httpAddrString() is easy to
303 int error
= getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
308 if (error
== EAI_FAIL
)
309 cg
->need_res_init
= 1;
311 return (httpAddrString(addr
, name
, namelen
));
316 struct hostent
*host
; /* Host from name service */
320 if (addr
->addr
.sa_family
== AF_INET6
)
321 host
= gethostbyaddr((char *)&(addr
->ipv6
.sin6_addr
),
322 sizeof(struct in_addr
), AF_INET6
);
324 # endif /* AF_INET6 */
325 host
= gethostbyaddr((char *)&(addr
->ipv4
.sin_addr
),
326 sizeof(struct in_addr
), AF_INET
);
331 * No hostname, so return the raw address...
334 if (h_errno
== NO_RECOVERY
)
335 cg
->need_res_init
= 1;
337 return (httpAddrString(addr
, name
, namelen
));
340 strlcpy(name
, host
->h_name
, namelen
);
342 #endif /* HAVE_GETNAMEINFO */
344 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
351 * 'httpAddrPort()' - Get the port number associated with an address.
353 * @since CUPS 1.7/OS X 10.9@
356 int /* O - Port number */
357 httpAddrPort(http_addr_t
*addr
) /* I - Address */
362 else if (addr
->addr
.sa_family
== AF_INET6
)
363 return (ntohs(addr
->ipv6
.sin6_port
));
364 #endif /* AF_INET6 */
365 else if (addr
->addr
.sa_family
== AF_INET
)
366 return (ntohs(addr
->ipv4
.sin_port
));
371 /* For OS X 10.8 and earlier */
372 int _httpAddrPort(http_addr_t
*addr
) { return (httpAddrPort(addr
)); }
376 * '_httpAddrSetPort()' - Set the port number associated with an address.
380 _httpAddrSetPort(http_addr_t
*addr
, /* I - Address */
381 int port
) /* I - Port */
383 if (!addr
|| port
<= 0)
387 if (addr
->addr
.sa_family
== AF_INET6
)
388 addr
->ipv6
.sin6_port
= htons(port
);
390 #endif /* AF_INET6 */
391 if (addr
->addr
.sa_family
== AF_INET
)
392 addr
->ipv4
.sin_port
= htons(port
);
397 * 'httpAddrString()' - Convert an address to a numeric string.
399 * @since CUPS 1.2/OS X 10.5@
402 char * /* O - Numeric address string */
403 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
404 char *s
, /* I - String buffer */
405 int slen
) /* I - Length of string */
407 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr
, s
, slen
));
410 * Range check input...
413 if (!addr
|| !s
|| slen
<= 2)
422 if (addr
->addr
.sa_family
== AF_LOCAL
)
424 if (addr
->un
.sun_path
[0] == '/')
425 strlcpy(s
, addr
->un
.sun_path
, slen
);
427 strlcpy(s
, "localhost", slen
);
430 #endif /* AF_LOCAL */
431 if (addr
->addr
.sa_family
== AF_INET
)
433 unsigned temp
; /* Temporary address */
436 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
438 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
439 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
442 else if (addr
->addr
.sa_family
== AF_INET6
)
444 char *sptr
, /* Pointer into string */
445 temps
[64]; /* Temporary string for address */
447 # ifdef HAVE_GETNAMEINFO
448 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), temps
, sizeof(temps
),
449 NULL
, 0, NI_NUMERICHOST
))
452 * If we get an error back, then the address type is not supported
453 * and we should zero out the buffer...
460 else if ((sptr
= strchr(temps
, '%')) != NULL
)
463 * Convert "%zone" to "+zone" to match URI form...
470 int i
; /* Looping var */
471 unsigned temp
; /* Current value */
472 const char *prefix
; /* Prefix for address */
476 for (sptr
= temps
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
478 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
480 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
481 (temp
>> 16) & 0xffff);
483 sptr
+= strlen(sptr
);
487 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
489 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
, temp
);
490 sptr
+= strlen(sptr
);
496 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
501 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s:", prefix
);
503 sptr
+= strlen(sptr
);
507 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
509 if ((temp
& 0xffff0000) ||
510 (i
> 0 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1]))
512 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
513 (temp
>> 16) & 0xffff);
514 sptr
+= strlen(sptr
);
517 snprintf(sptr
, sizeof(temps
) - (sptr
- temps
), "%s%x", prefix
,
519 sptr
+= strlen(sptr
);
528 strlcpy(temps
, "::", sizeof(temps
));
536 strlcpy(sptr
, "::", sizeof(temps
) - (sptr
- temps
));
539 # endif /* HAVE_GETNAMEINFO */
542 * Add "[v1." and "]" around IPv6 address to convert to URI form.
545 snprintf(s
, slen
, "[v1.%s]", temps
);
547 #endif /* AF_INET6 */
549 strlcpy(s
, "UNKNOWN", slen
);
551 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
558 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
559 * address records for the specified name.
564 struct hostent
* /* O - Host entry */
565 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
567 const char *nameptr
; /* Pointer into name */
568 unsigned ip
[4]; /* IP address components */
569 _cups_globals_t
*cg
= _cupsGlobals();
570 /* Pointer to library globals */
573 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
576 * Avoid lookup delays and configuration problems when connecting
577 * to the localhost address...
580 if (!strcmp(name
, "localhost"))
584 * This function is needed because some operating systems have a
585 * buggy implementation of gethostbyname() that does not support
586 * IP addresses. If the first character of the name string is a
587 * number, then sscanf() is used to extract the IP components.
588 * We then pack the components into an IPv4 address manually,
589 * since the inet_aton() function is deprecated. We use the
590 * htonl() macro to get the right byte order for the address.
592 * We also support domain sockets when supported by the underlying
600 * A domain socket address, so make an AF_LOCAL entry and return it...
603 cg
->hostent
.h_name
= (char *)name
;
604 cg
->hostent
.h_aliases
= NULL
;
605 cg
->hostent
.h_addrtype
= AF_LOCAL
;
606 cg
->hostent
.h_length
= strlen(name
) + 1;
607 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
608 cg
->ip_ptrs
[0] = (char *)name
;
609 cg
->ip_ptrs
[1] = NULL
;
611 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
613 return (&cg
->hostent
);
615 #endif /* AF_LOCAL */
617 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
622 * We have an IPv4 address; break it up and provide the host entry
626 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
627 return (NULL
); /* Must have 4 numbers */
629 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
630 return (NULL
); /* Invalid byte ranges! */
632 cg
->ip_addr
= htonl((((((((unsigned)ip
[0] << 8) | (unsigned)ip
[1]) << 8) |
633 (unsigned)ip
[2]) << 8) |
637 * Fill in the host entry and return it...
640 cg
->hostent
.h_name
= (char *)name
;
641 cg
->hostent
.h_aliases
= NULL
;
642 cg
->hostent
.h_addrtype
= AF_INET
;
643 cg
->hostent
.h_length
= 4;
644 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
645 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
646 cg
->ip_ptrs
[1] = NULL
;
648 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
650 return (&cg
->hostent
);
655 * Use the gethostbyname() function to get the IPv4 address for
659 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
661 return (gethostbyname(name
));
667 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
669 * When "http" points to a connected socket, return the hostname or
670 * address that was used in the call to httpConnect() or httpConnectEncrypt().
671 * Otherwise, return the FQDN for the local system using both gethostname()
672 * and gethostbyname() to get the local hostname with domain.
674 * @since CUPS 1.2/OS X 10.5@
677 const char * /* O - FQDN for connection or system */
678 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
679 char *s
, /* I - String buffer for name */
680 int slen
) /* I - Size of buffer */
687 if (http
->hostname
[0] == '/')
688 strlcpy(s
, "localhost", slen
);
690 strlcpy(s
, http
->hostname
, slen
);
695 * Get the hostname...
698 if (gethostname(s
, slen
) < 0)
699 strlcpy(s
, "localhost", slen
);
703 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
705 * The hostname is not a FQDN, so use the local hostname from the
706 * SystemConfiguration framework...
709 SCDynamicStoreRef sc
= SCDynamicStoreCreate(kCFAllocatorDefault
,
710 CFSTR("libcups"), NULL
, NULL
);
711 /* System configuration data */
712 CFStringRef local
= sc
? SCDynamicStoreCopyLocalHostName(sc
) : NULL
;
713 /* Local host name */
714 char localStr
[1024]; /* Local host name C string */
716 if (local
&& CFStringGetCString(local
, localStr
, sizeof(localStr
),
717 kCFStringEncodingUTF8
))
720 * Append ".local." to the hostname we get...
723 snprintf(s
, slen
, "%s.local.", localStr
);
733 * The hostname is not a FQDN, so look it up...
736 struct hostent
*host
; /* Host entry to get FQDN */
738 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
741 * Use the resolved hostname...
744 strlcpy(s
, host
->h_name
, slen
);
746 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
751 * Return the hostname with as much domain info as we have...
759 * End of "$Id: http-addr.c 11642 2014-02-27 15:57:59Z msweet $".