]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/http-addr.c
Merge changes from CUPS 1.5svn-r9374.
[thirdparty/cups.git] / cups / http-addr.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $"
ef416fc2 3 *
71e16022 4 * HTTP address routines for CUPS.
ef416fc2 5 *
71e16022 6 * Copyright 2007-2010 by Apple Inc.
ecdc0628 7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 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.
1ff0402e 21 * _httpAddrPort() - Get the port number associated with an address.
ef416fc2 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
71e16022 32#include "cups-private.h"
49d87452
MS
33#ifdef HAVE_RESOLV_H
34# include <resolv.h>
35#endif /* HAVE_RESOLV_H */
1106b00e
MS
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 */
ef416fc2 42
43
44/*
45 * 'httpAddrAny()' - Check for the "any" address.
46 *
426c6a59 47 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 48 */
49
50int /* O - 1 if "any", 0 otherwise */
51httpAddrAny(const http_addr_t *addr) /* I - Address to check */
52{
89d46774 53 if (!addr)
54 return (0);
55
ef416fc2 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 *
426c6a59 73 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 74 */
75
ecdc0628 76int /* O - 1 if equal, 0 if not */
ef416fc2 77httpAddrEqual(const http_addr_t *addr1, /* I - First address */
78 const http_addr_t *addr2) /* I - Second address */
79{
89d46774 80 if (!addr1 && !addr2)
81 return (1);
82
83 if (!addr1 || !addr2)
84 return (0);
85
ef416fc2 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 *
426c6a59 106 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 107 */
108
109int /* O - Length in bytes */
110httpAddrLength(const http_addr_t *addr) /* I - Address */
111{
89d46774 112 if (!addr)
113 return (0);
114
ef416fc2 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 *
426c6a59 137 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 138 */
139
140int /* O - 1 if local host, 0 otherwise */
141httpAddrLocalhost(
142 const http_addr_t *addr) /* I - Address to check */
143{
89d46774 144 if (!addr)
145 return (1);
146
ef416fc2 147#ifdef AF_INET6
148 if (addr->addr.sa_family == AF_INET6 &&
fa73b229 149 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
ef416fc2 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 &&
e07d4801 159 (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000)
ef416fc2 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 *
426c6a59 176 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 177 */
178
ecdc0628 179char * /* O - Host name */
180httpAddrLookup(
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 */
ef416fc2 184{
49d87452
MS
185 _cups_globals_t *cg = _cupsGlobals();
186 /* Global data */
187
188
e07d4801
MS
189 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name,
190 namelen));
ef416fc2 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)
49d87452 206 {
ef416fc2 207 strlcpy(name, addr->un.sun_path, namelen);
49d87452
MS
208 return (name);
209 }
ef416fc2 210#endif /* AF_LOCAL */
49d87452 211
d2354e63
MS
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
49d87452
MS
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
ef416fc2 243#ifdef HAVE_GETNAMEINFO
244 {
db1f069b
MS
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 */
ef416fc2 252
49d87452
MS
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
db1f069b 261 return (httpAddrString(addr, name, namelen));
49d87452 262 }
ef416fc2 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
49d87452
MS
284 if (h_errno == NO_RECOVERY)
285 cg->need_res_init = 1;
286
287 return (httpAddrString(addr, name, namelen));
ef416fc2 288 }
289
290 strlcpy(name, host->h_name, namelen);
291 }
292#endif /* HAVE_GETNAMEINFO */
293
e07d4801
MS
294 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name));
295
ef416fc2 296 return (name);
297}
298
299
1ff0402e
MS
300/*
301 * '_httpAddrPort()' - Get the port number associated with an address.
302 */
303
304int /* O - Port number */
305_httpAddrPort(http_addr_t *addr) /* I - Address */
306{
5f64df29
MS
307 if (!addr)
308 return (ippPort());
1ff0402e 309#ifdef AF_INET6
5f64df29 310 else if (addr->addr.sa_family == AF_INET6)
1ff0402e 311 return (ntohs(addr->ipv6.sin6_port));
1ff0402e 312#endif /* AF_INET6 */
5f64df29 313 else if (addr->addr.sa_family == AF_INET)
1ff0402e
MS
314 return (ntohs(addr->ipv4.sin_port));
315 else
316 return (ippPort());
317}
318
319
ef416fc2 320/*
ecdc0628 321 * 'httpAddrString()' - Convert an address to a numeric string.
ef416fc2 322 *
426c6a59 323 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 324 */
325
ecdc0628 326char * /* O - Numeric address string */
327httpAddrString(const http_addr_t *addr, /* I - Address to convert */
328 char *s, /* I - String buffer */
329 int slen) /* I - Length of string */
ef416fc2 330{
e07d4801 331 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen));
ef416fc2 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 */
b423cd4c 350 if (addr->addr.sa_family == AF_INET)
ef416fc2 351 {
b423cd4c 352 unsigned temp; /* Temporary address */
ef416fc2 353
354
b423cd4c 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# ifdef HAVE_GETNAMEINFO
ef416fc2 364 if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen,
b423cd4c 365 NULL, 0, NI_NUMERICHOST))
ef416fc2 366 {
367 /*
368 * If we get an error back, then the address type is not supported
369 * and we should zero out the buffer...
370 */
371
372 s[0] = '\0';
373
374 return (NULL);
375 }
b423cd4c 376# else
377 char *sptr; /* Pointer into string */
378 int i; /* Looping var */
379 unsigned temp; /* Current value */
380 const char *prefix; /* Prefix for address */
381
382
383 prefix = "";
384 for (sptr = s, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
ef416fc2 385 {
b423cd4c 386 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
ef416fc2 387
b423cd4c 388 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
389 prefix = ":";
390 slen -= strlen(sptr);
391 sptr += strlen(sptr);
ef416fc2 392
b423cd4c 393 temp &= 0xffff;
ef416fc2 394
b423cd4c 395 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
396 {
397 snprintf(sptr, slen, "%s%x", prefix, temp);
ef416fc2 398 slen -= strlen(sptr);
399 sptr += strlen(sptr);
ef416fc2 400 }
b423cd4c 401 }
402
403 if (i < 4)
404 {
405 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
406 i ++;
ef416fc2 407
408 if (i < 4)
409 {
b423cd4c 410 snprintf(sptr, slen, "%s:", prefix);
411 prefix = ":";
412 slen -= strlen(sptr);
413 sptr += strlen(sptr);
ef416fc2 414
b423cd4c 415 for (; i < 4; i ++)
ef416fc2 416 {
b423cd4c 417 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
ef416fc2 418
b423cd4c 419 if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1])
ef416fc2 420 {
b423cd4c 421 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
ef416fc2 422 slen -= strlen(sptr);
423 sptr += strlen(sptr);
b423cd4c 424 }
ef416fc2 425
b423cd4c 426 snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff);
427 slen -= strlen(sptr);
428 sptr += strlen(sptr);
ef416fc2 429 }
430 }
b423cd4c 431 else if (sptr == s)
432 {
433 /*
434 * Empty address...
435 */
ef416fc2 436
b423cd4c 437 strlcpy(s, "::", slen);
438 sptr = s + 2;
439 slen -= 2;
440 }
441 else
442 {
443 /*
444 * Empty at end...
445 */
ef416fc2 446
b423cd4c 447 strlcpy(sptr, "::", slen);
448 sptr += 2;
449 slen -= 2;
450 }
ef416fc2 451 }
b423cd4c 452# endif /* HAVE_GETNAMEINFO */
ef416fc2 453 }
b423cd4c 454#endif /* AF_INET6 */
455 else
456 strlcpy(s, "UNKNOWN", slen);
ef416fc2 457
e07d4801 458 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s));
ef416fc2 459
460 return (s);
461}
462
463
464/*
465 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
466 * address records for the specified name.
467 *
468 * @deprecated@
469 */
470
471struct hostent * /* O - Host entry */
472httpGetHostByName(const char *name) /* I - Hostname or IP address */
473{
474 const char *nameptr; /* Pointer into name */
475 unsigned ip[4]; /* IP address components */
476 _cups_globals_t *cg = _cupsGlobals();
477 /* Pointer to library globals */
478
479
e07d4801 480 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name));
ef416fc2 481
482 /*
483 * Avoid lookup delays and configuration problems when connecting
484 * to the localhost address...
485 */
486
487 if (!strcmp(name, "localhost"))
488 name = "127.0.0.1";
489
490 /*
491 * This function is needed because some operating systems have a
492 * buggy implementation of gethostbyname() that does not support
493 * IP addresses. If the first character of the name string is a
494 * number, then sscanf() is used to extract the IP components.
495 * We then pack the components into an IPv4 address manually,
496 * since the inet_aton() function is deprecated. We use the
497 * htonl() macro to get the right byte order for the address.
498 *
499 * We also support domain sockets when supported by the underlying
500 * OS...
501 */
502
503#ifdef AF_LOCAL
504 if (name[0] == '/')
505 {
506 /*
507 * A domain socket address, so make an AF_LOCAL entry and return it...
508 */
509
510 cg->hostent.h_name = (char *)name;
511 cg->hostent.h_aliases = NULL;
512 cg->hostent.h_addrtype = AF_LOCAL;
513 cg->hostent.h_length = strlen(name) + 1;
514 cg->hostent.h_addr_list = cg->ip_ptrs;
515 cg->ip_ptrs[0] = (char *)name;
516 cg->ip_ptrs[1] = NULL;
517
e07d4801 518 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
ef416fc2 519
520 return (&cg->hostent);
521 }
522#endif /* AF_LOCAL */
523
524 for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++);
525
526 if (!*nameptr)
527 {
528 /*
529 * We have an IPv4 address; break it up and provide the host entry
530 * to the caller.
531 */
532
533 if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4)
534 return (NULL); /* Must have 4 numbers */
535
536 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
537 return (NULL); /* Invalid byte ranges! */
538
539 cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) |
540 ip[3]));
541
542 /*
543 * Fill in the host entry and return it...
544 */
545
546 cg->hostent.h_name = (char *)name;
547 cg->hostent.h_aliases = NULL;
548 cg->hostent.h_addrtype = AF_INET;
549 cg->hostent.h_length = 4;
550 cg->hostent.h_addr_list = cg->ip_ptrs;
551 cg->ip_ptrs[0] = (char *)&(cg->ip_addr);
552 cg->ip_ptrs[1] = NULL;
553
e07d4801 554 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
ef416fc2 555
556 return (&cg->hostent);
557 }
558 else
559 {
560 /*
561 * Use the gethostbyname() function to get the IPv4 address for
562 * the name...
563 */
564
e07d4801 565 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
ef416fc2 566
567 return (gethostbyname(name));
568 }
569}
570
571
572/*
757d2cad 573 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
ef416fc2 574 *
757d2cad 575 * When "http" points to a connected socket, return the hostname or
576 * address that was used in the call to httpConnect() or httpConnectEncrypt().
577 * Otherwise, return the FQDN for the local system using both gethostname()
578 * and gethostbyname() to get the local hostname with domain.
ef416fc2 579 *
426c6a59 580 * @since CUPS 1.2/Mac OS X 10.5@
ef416fc2 581 */
582
757d2cad 583const char * /* O - FQDN for connection or system */
584httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
585 char *s, /* I - String buffer for name */
586 int slen) /* I - Size of buffer */
ef416fc2 587{
89d46774 588 if (!s || slen <= 1)
589 return (NULL);
590
757d2cad 591 if (http)
480ef0fe 592 {
593 if (http->hostname[0] == '/')
594 strlcpy(s, "localhost", slen);
595 else
596 strlcpy(s, http->hostname, slen);
597 }
757d2cad 598 else
ef416fc2 599 {
600 /*
757d2cad 601 * Get the hostname...
ef416fc2 602 */
603
89d46774 604 if (gethostname(s, slen) < 0)
605 strlcpy(s, "localhost", slen);
757d2cad 606
607 if (!strchr(s, '.'))
608 {
1106b00e
MS
609#ifdef HAVE_SYSTEMCONFIGURATION
610 /*
611 * The hostname is not a FQDN, so use the local hostname from the
612 * SystemConfiguration framework...
613 */
614
615 SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault,
616 CFSTR("libcups"), NULL, NULL);
617 /* System configuration data */
618 CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL;
619 /* Local host name */
620 char localStr[1024]; /* Local host name C string */
621
622 if (local && CFStringGetCString(local, localStr, sizeof(localStr),
623 kCFStringEncodingUTF8))
624 {
625 /*
626 * Append ".local." to the hostname we get...
627 */
628
629 snprintf(s, slen, "%s.local.", localStr);
630 }
631
632 if (local)
633 CFRelease(local);
634 if (sc)
635 CFRelease(sc);
636
637#else
757d2cad 638 /*
639 * The hostname is not a FQDN, so look it up...
640 */
641
1106b00e
MS
642 struct hostent *host; /* Host entry to get FQDN */
643
89d46774 644 if ((host = gethostbyname(s)) != NULL && host->h_name)
1106b00e
MS
645 {
646 /*
647 * Use the resolved hostname...
648 */
649
757d2cad 650 strlcpy(s, host->h_name, slen);
1106b00e
MS
651 }
652#endif /* HAVE_SYSTEMCONFIGURATION */
757d2cad 653 }
ef416fc2 654 }
655
656 /*
657 * Return the hostname with as much domain info as we have...
658 */
659
660 return (s);
661}
662
663
664/*
b19ccc9e 665 * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".
ef416fc2 666 */