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