]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
d9ab970991e0dd23f61b8daf8e5c7a181d16f8e9
[thirdparty/cups.git] / cups / http-addr.c
1 /*
2 * "$Id$"
3 *
4 * HTTP address routines for CUPS.
5 *
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 *
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/".
14 */
15
16 /*
17 * Include necessary headers...
18 */
19
20 #include "cups-private.h"
21 #ifdef HAVE_RESOLV_H
22 # include <resolv.h>
23 #endif /* HAVE_RESOLV_H */
24 #ifdef __APPLE__
25 # include <CoreFoundation/CoreFoundation.h>
26 # include <SystemConfiguration/SystemConfiguration.h>
27 #endif /* __APPLE__ */
28
29
30 /*
31 * 'httpAddrAny()' - Check for the "any" address.
32 *
33 * @since CUPS 1.2/OS X 10.5@
34 */
35
36 int /* O - 1 if "any", 0 otherwise */
37 httpAddrAny(const http_addr_t *addr) /* I - Address to check */
38 {
39 if (!addr)
40 return (0);
41
42 #ifdef AF_INET6
43 if (addr->addr.sa_family == AF_INET6 &&
44 IN6_IS_ADDR_UNSPECIFIED(&(addr->ipv6.sin6_addr)))
45 return (1);
46 #endif /* AF_INET6 */
47
48 if (addr->addr.sa_family == AF_INET &&
49 ntohl(addr->ipv4.sin_addr.s_addr) == 0x00000000)
50 return (1);
51
52 return (0);
53 }
54
55
56 /*
57 * 'httpAddrEqual()' - Compare two addresses.
58 *
59 * @since CUPS 1.2/OS X 10.5@
60 */
61
62 int /* O - 1 if equal, 0 if not */
63 httpAddrEqual(const http_addr_t *addr1, /* I - First address */
64 const http_addr_t *addr2) /* I - Second address */
65 {
66 if (!addr1 && !addr2)
67 return (1);
68
69 if (!addr1 || !addr2)
70 return (0);
71
72 if (addr1->addr.sa_family != addr2->addr.sa_family)
73 return (0);
74
75 #ifdef AF_LOCAL
76 if (addr1->addr.sa_family == AF_LOCAL)
77 return (!strcmp(addr1->un.sun_path, addr2->un.sun_path));
78 #endif /* AF_LOCAL */
79
80 #ifdef AF_INET6
81 if (addr1->addr.sa_family == AF_INET6)
82 return (!memcmp(&(addr1->ipv6.sin6_addr), &(addr2->ipv6.sin6_addr), 16));
83 #endif /* AF_INET6 */
84
85 return (addr1->ipv4.sin_addr.s_addr == addr2->ipv4.sin_addr.s_addr);
86 }
87
88
89 /*
90 * 'httpAddrLength()' - Return the length of the address in bytes.
91 *
92 * @since CUPS 1.2/OS X 10.5@
93 */
94
95 int /* O - Length in bytes */
96 httpAddrLength(const http_addr_t *addr) /* I - Address */
97 {
98 if (!addr)
99 return (0);
100
101 #ifdef AF_INET6
102 if (addr->addr.sa_family == AF_INET6)
103 return (sizeof(addr->ipv6));
104 else
105 #endif /* AF_INET6 */
106 #ifdef AF_LOCAL
107 if (addr->addr.sa_family == AF_LOCAL)
108 return (offsetof(struct sockaddr_un, sun_path) +
109 strlen(addr->un.sun_path) + 1);
110 else
111 #endif /* AF_LOCAL */
112 if (addr->addr.sa_family == AF_INET)
113 return (sizeof(addr->ipv4));
114 else
115 return (0);
116
117 }
118
119
120 /*
121 * 'httpAddrListen()' - Create a listening socket bound to the specified
122 * address and port.
123 *
124 * @since CUPS 1.7/OS X 10.9@
125 */
126
127 int /* O - Socket or -1 on error */
128 httpAddrListen(http_addr_t *addr, /* I - Address to bind to */
129 int port) /* I - Port number to bind to */
130 {
131 int fd = -1, /* Socket */
132 val; /* Socket value */
133
134
135 /*
136 * Range check input...
137 */
138
139 if (!addr || port <= 0)
140 return (-1);
141
142 if ((fd = socket(addr->addr.sa_family, SOCK_STREAM, 0)) < 0)
143 {
144 _cupsSetHTTPError(HTTP_STATUS_ERROR);
145 return (-1);
146 }
147
148 val = 1;
149 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, CUPS_SOCAST &val, sizeof(val));
150
151 #ifdef IPV6_V6ONLY
152 if (addr->addr.sa_family == AF_INET6)
153 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, CUPS_SOCAST &val, sizeof(val));
154 #endif /* IPV6_V6ONLY */
155
156 _httpAddrSetPort(addr, port);
157
158 if (bind(fd, (struct sockaddr *)addr, httpAddrLength(addr)))
159 {
160 _cupsSetHTTPError(HTTP_STATUS_ERROR);
161
162 close(fd);
163
164 return (-1);
165 }
166
167 if (listen(fd, 5))
168 {
169 _cupsSetHTTPError(HTTP_STATUS_ERROR);
170
171 close(fd);
172
173 return (-1);
174 }
175
176 #ifdef SO_NOSIGPIPE
177 /*
178 * Disable SIGPIPE for this socket.
179 */
180
181 val = 1;
182 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val));
183 #endif /* SO_NOSIGPIPE */
184
185 return (fd);
186 }
187
188
189 /*
190 * 'httpAddrLocalhost()' - Check for the local loopback address.
191 *
192 * @since CUPS 1.2/OS X 10.5@
193 */
194
195 int /* O - 1 if local host, 0 otherwise */
196 httpAddrLocalhost(
197 const http_addr_t *addr) /* I - Address to check */
198 {
199 if (!addr)
200 return (1);
201
202 #ifdef AF_INET6
203 if (addr->addr.sa_family == AF_INET6 &&
204 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
205 return (1);
206 #endif /* AF_INET6 */
207
208 #ifdef AF_LOCAL
209 if (addr->addr.sa_family == AF_LOCAL)
210 return (1);
211 #endif /* AF_LOCAL */
212
213 if (addr->addr.sa_family == AF_INET &&
214 (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000)
215 return (1);
216
217 return (0);
218 }
219
220
221 /*
222 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
223 *
224 * @since CUPS 1.2/OS X 10.5@
225 */
226
227 char * /* O - Host name */
228 httpAddrLookup(
229 const http_addr_t *addr, /* I - Address to lookup */
230 char *name, /* I - Host name buffer */
231 int namelen) /* I - Size of name buffer */
232 {
233 _cups_globals_t *cg = _cupsGlobals();
234 /* Global data */
235
236
237 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name,
238 namelen));
239
240 /*
241 * Range check input...
242 */
243
244 if (!addr || !name || namelen <= 2)
245 {
246 if (name && namelen >= 1)
247 *name = '\0';
248
249 return (NULL);
250 }
251
252 #ifdef AF_LOCAL
253 if (addr->addr.sa_family == AF_LOCAL)
254 {
255 strlcpy(name, addr->un.sun_path, namelen);
256 return (name);
257 }
258 #endif /* AF_LOCAL */
259
260 /*
261 * Optimize lookups for localhost/loopback addresses...
262 */
263
264 if (httpAddrLocalhost(addr))
265 {
266 strlcpy(name, "localhost", namelen);
267 return (name);
268 }
269
270 #ifdef HAVE_RES_INIT
271 /*
272 * STR #2920: Initialize resolver after failure in cups-polld
273 *
274 * If the previous lookup failed, re-initialize the resolver to prevent
275 * temporary network errors from persisting. This *should* be handled by
276 * the resolver libraries, but apparently the glibc folks do not agree.
277 *
278 * We set a flag at the end of this function if we encounter an error that
279 * requires reinitialization of the resolver functions. We then call
280 * res_init() if the flag is set on the next call here or in httpAddrLookup().
281 */
282
283 if (cg->need_res_init)
284 {
285 res_init();
286
287 cg->need_res_init = 0;
288 }
289 #endif /* HAVE_RES_INIT */
290
291 #ifdef HAVE_GETNAMEINFO
292 {
293 /*
294 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
295 *
296 * FWIW, I think this is really a bug in the implementation of
297 * getnameinfo(), but falling back on httpAddrString() is easy to
298 * do...
299 */
300
301 int error = getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen,
302 NULL, 0, 0);
303
304 if (error)
305 {
306 if (error == EAI_FAIL)
307 cg->need_res_init = 1;
308
309 return (httpAddrString(addr, name, namelen));
310 }
311 }
312 #else
313 {
314 struct hostent *host; /* Host from name service */
315
316
317 # ifdef AF_INET6
318 if (addr->addr.sa_family == AF_INET6)
319 host = gethostbyaddr((char *)&(addr->ipv6.sin6_addr),
320 sizeof(struct in_addr), AF_INET6);
321 else
322 # endif /* AF_INET6 */
323 host = gethostbyaddr((char *)&(addr->ipv4.sin_addr),
324 sizeof(struct in_addr), AF_INET);
325
326 if (host == NULL)
327 {
328 /*
329 * No hostname, so return the raw address...
330 */
331
332 if (h_errno == NO_RECOVERY)
333 cg->need_res_init = 1;
334
335 return (httpAddrString(addr, name, namelen));
336 }
337
338 strlcpy(name, host->h_name, namelen);
339 }
340 #endif /* HAVE_GETNAMEINFO */
341
342 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name));
343
344 return (name);
345 }
346
347
348 /*
349 * 'httpAddrFamily()' - Get the address family of an address.
350 */
351
352 int /* O - Address family */
353 httpAddrFamily(http_addr_t *addr) /* I - Address */
354 {
355 if (addr)
356 return (addr->addr.sa_family);
357 else
358 return (0);
359 }
360
361
362 /*
363 * 'httpAddrPort()' - Get the port number associated with an address.
364 *
365 * @since CUPS 1.7/OS X 10.9@
366 */
367
368 int /* O - Port number */
369 httpAddrPort(http_addr_t *addr) /* I - Address */
370 {
371 if (!addr)
372 return (-1);
373 #ifdef AF_INET6
374 else if (addr->addr.sa_family == AF_INET6)
375 return (ntohs(addr->ipv6.sin6_port));
376 #endif /* AF_INET6 */
377 else if (addr->addr.sa_family == AF_INET)
378 return (ntohs(addr->ipv4.sin_port));
379 else
380 return (0);
381 }
382
383 /* For OS X 10.8 and earlier */
384 int _httpAddrPort(http_addr_t *addr) { return (httpAddrPort(addr)); }
385
386
387 /*
388 * '_httpAddrSetPort()' - Set the port number associated with an address.
389 */
390
391 void
392 _httpAddrSetPort(http_addr_t *addr, /* I - Address */
393 int port) /* I - Port */
394 {
395 if (!addr || port <= 0)
396 return;
397
398 #ifdef AF_INET6
399 if (addr->addr.sa_family == AF_INET6)
400 addr->ipv6.sin6_port = htons(port);
401 else
402 #endif /* AF_INET6 */
403 if (addr->addr.sa_family == AF_INET)
404 addr->ipv4.sin_port = htons(port);
405 }
406
407
408 /*
409 * 'httpAddrString()' - Convert an address to a numeric string.
410 *
411 * @since CUPS 1.2/OS X 10.5@
412 */
413
414 char * /* O - Numeric address string */
415 httpAddrString(const http_addr_t *addr, /* I - Address to convert */
416 char *s, /* I - String buffer */
417 int slen) /* I - Length of string */
418 {
419 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen));
420
421 /*
422 * Range check input...
423 */
424
425 if (!addr || !s || slen <= 2)
426 {
427 if (s && slen >= 1)
428 *s = '\0';
429
430 return (NULL);
431 }
432
433 #ifdef AF_LOCAL
434 if (addr->addr.sa_family == AF_LOCAL)
435 {
436 if (addr->un.sun_path[0] == '/')
437 strlcpy(s, addr->un.sun_path, slen);
438 else
439 strlcpy(s, "localhost", slen);
440 }
441 else
442 #endif /* AF_LOCAL */
443 if (addr->addr.sa_family == AF_INET)
444 {
445 unsigned temp; /* Temporary address */
446
447
448 temp = ntohl(addr->ipv4.sin_addr.s_addr);
449
450 snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255,
451 (temp >> 16) & 255, (temp >> 8) & 255, temp & 255);
452 }
453 #ifdef AF_INET6
454 else if (addr->addr.sa_family == AF_INET6)
455 {
456 char *sptr, /* Pointer into string */
457 temps[64]; /* Temporary string for address */
458
459 # ifdef HAVE_GETNAMEINFO
460 if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps),
461 NULL, 0, NI_NUMERICHOST))
462 {
463 /*
464 * If we get an error back, then the address type is not supported
465 * and we should zero out the buffer...
466 */
467
468 s[0] = '\0';
469
470 return (NULL);
471 }
472 else if ((sptr = strchr(temps, '%')) != NULL)
473 {
474 /*
475 * Convert "%zone" to "+zone" to match URI form...
476 */
477
478 *sptr = '+';
479 }
480
481 # else
482 int i; /* Looping var */
483 unsigned temp; /* Current value */
484 const char *prefix; /* Prefix for address */
485
486
487 prefix = "";
488 for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
489 {
490 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
491
492 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
493 (temp >> 16) & 0xffff);
494 prefix = ":";
495 sptr += strlen(sptr);
496
497 temp &= 0xffff;
498
499 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
500 {
501 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp);
502 sptr += strlen(sptr);
503 }
504 }
505
506 if (i < 4)
507 {
508 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
509 i ++;
510
511 if (i < 4)
512 {
513 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix);
514 prefix = ":";
515 sptr += strlen(sptr);
516
517 for (; i < 4; i ++)
518 {
519 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
520
521 if ((temp & 0xffff0000) ||
522 (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1]))
523 {
524 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
525 (temp >> 16) & 0xffff);
526 sptr += strlen(sptr);
527 }
528
529 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
530 temp & 0xffff);
531 sptr += strlen(sptr);
532 }
533 }
534 else if (sptr == s)
535 {
536 /*
537 * Empty address...
538 */
539
540 strlcpy(temps, "::", sizeof(temps));
541 }
542 else
543 {
544 /*
545 * Empty at end...
546 */
547
548 strlcpy(sptr, "::", sizeof(temps) - (sptr - temps));
549 }
550 }
551 # endif /* HAVE_GETNAMEINFO */
552
553 /*
554 * Add "[v1." and "]" around IPv6 address to convert to URI form.
555 */
556
557 snprintf(s, slen, "[v1.%s]", temps);
558 }
559 #endif /* AF_INET6 */
560 else
561 strlcpy(s, "UNKNOWN", slen);
562
563 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s));
564
565 return (s);
566 }
567
568
569 /*
570 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
571 *
572 * Returns @code NULL@ if the socket is currently unconnected.
573 *
574 * @since CUPS 2.0@
575 */
576
577 http_addr_t * /* O - Connected address or @code NULL@ */
578 httpGetAddress(http_t *http) /* I - HTTP connection */
579 {
580 if (http)
581 return (http->hostaddr);
582 else
583 return (NULL);
584 }
585
586
587 /*
588 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
589 * address records for the specified name.
590 *
591 * @deprecated@
592 */
593
594 struct hostent * /* O - Host entry */
595 httpGetHostByName(const char *name) /* I - Hostname or IP address */
596 {
597 const char *nameptr; /* Pointer into name */
598 unsigned ip[4]; /* IP address components */
599 _cups_globals_t *cg = _cupsGlobals();
600 /* Pointer to library globals */
601
602
603 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name));
604
605 /*
606 * Avoid lookup delays and configuration problems when connecting
607 * to the localhost address...
608 */
609
610 if (!strcmp(name, "localhost"))
611 name = "127.0.0.1";
612
613 /*
614 * This function is needed because some operating systems have a
615 * buggy implementation of gethostbyname() that does not support
616 * IP addresses. If the first character of the name string is a
617 * number, then sscanf() is used to extract the IP components.
618 * We then pack the components into an IPv4 address manually,
619 * since the inet_aton() function is deprecated. We use the
620 * htonl() macro to get the right byte order for the address.
621 *
622 * We also support domain sockets when supported by the underlying
623 * OS...
624 */
625
626 #ifdef AF_LOCAL
627 if (name[0] == '/')
628 {
629 /*
630 * A domain socket address, so make an AF_LOCAL entry and return it...
631 */
632
633 cg->hostent.h_name = (char *)name;
634 cg->hostent.h_aliases = NULL;
635 cg->hostent.h_addrtype = AF_LOCAL;
636 cg->hostent.h_length = strlen(name) + 1;
637 cg->hostent.h_addr_list = cg->ip_ptrs;
638 cg->ip_ptrs[0] = (char *)name;
639 cg->ip_ptrs[1] = NULL;
640
641 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
642
643 return (&cg->hostent);
644 }
645 #endif /* AF_LOCAL */
646
647 for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++);
648
649 if (!*nameptr)
650 {
651 /*
652 * We have an IPv4 address; break it up and provide the host entry
653 * to the caller.
654 */
655
656 if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4)
657 return (NULL); /* Must have 4 numbers */
658
659 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
660 return (NULL); /* Invalid byte ranges! */
661
662 cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) |
663 ip[3]));
664
665 /*
666 * Fill in the host entry and return it...
667 */
668
669 cg->hostent.h_name = (char *)name;
670 cg->hostent.h_aliases = NULL;
671 cg->hostent.h_addrtype = AF_INET;
672 cg->hostent.h_length = 4;
673 cg->hostent.h_addr_list = cg->ip_ptrs;
674 cg->ip_ptrs[0] = (char *)&(cg->ip_addr);
675 cg->ip_ptrs[1] = NULL;
676
677 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
678
679 return (&cg->hostent);
680 }
681 else
682 {
683 /*
684 * Use the gethostbyname() function to get the IPv4 address for
685 * the name...
686 */
687
688 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
689
690 return (gethostbyname(name));
691 }
692 }
693
694
695 /*
696 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
697 *
698 * When "http" points to a connected socket, return the hostname or
699 * address that was used in the call to httpConnect() or httpConnectEncrypt().
700 * Otherwise, return the FQDN for the local system using both gethostname()
701 * and gethostbyname() to get the local hostname with domain.
702 *
703 * @since CUPS 1.2/OS X 10.5@
704 */
705
706 const char * /* O - FQDN for connection or system */
707 httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
708 char *s, /* I - String buffer for name */
709 int slen) /* I - Size of buffer */
710 {
711 if (http)
712 {
713 if (!s || slen <= 1)
714 {
715 if (http->hostname[0] == '/')
716 return ("localhost");
717 else
718 return (http->hostname);
719 }
720 else if (http->hostname[0] == '/')
721 strlcpy(s, "localhost", slen);
722 else
723 strlcpy(s, http->hostname, slen);
724 }
725 else
726 {
727 /*
728 * Get the hostname...
729 */
730
731 if (!s || slen <= 1)
732 return (NULL);
733
734 if (gethostname(s, slen) < 0)
735 strlcpy(s, "localhost", slen);
736
737 if (!strchr(s, '.'))
738 {
739 #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
740 /*
741 * The hostname is not a FQDN, so use the local hostname from the
742 * SystemConfiguration framework...
743 */
744
745 SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault,
746 CFSTR("libcups"), NULL, NULL);
747 /* System configuration data */
748 CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL;
749 /* Local host name */
750 char localStr[1024]; /* Local host name C string */
751
752 if (local && CFStringGetCString(local, localStr, sizeof(localStr),
753 kCFStringEncodingUTF8))
754 {
755 /*
756 * Append ".local." to the hostname we get...
757 */
758
759 snprintf(s, slen, "%s.local.", localStr);
760 }
761
762 if (local)
763 CFRelease(local);
764 if (sc)
765 CFRelease(sc);
766
767 #else
768 /*
769 * The hostname is not a FQDN, so look it up...
770 */
771
772 struct hostent *host; /* Host entry to get FQDN */
773
774 if ((host = gethostbyname(s)) != NULL && host->h_name)
775 {
776 /*
777 * Use the resolved hostname...
778 */
779
780 strlcpy(s, host->h_name, slen);
781 }
782 #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
783 }
784 }
785
786 /*
787 * Return the hostname with as much domain info as we have...
788 */
789
790 return (s);
791 }
792
793
794 /*
795 * End of "$Id$".
796 */