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