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