]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
Merge changes from CUPS 1.5svn-r9049 (private header support)
[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-2010 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
37
38 /*
39 * 'httpAddrAny()' - Check for the "any" address.
40 *
41 * @since CUPS 1.2/Mac OS X 10.5@
42 */
43
44 int /* O - 1 if "any", 0 otherwise */
45 httpAddrAny(const http_addr_t *addr) /* I - Address to check */
46 {
47 if (!addr)
48 return (0);
49
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 *
67 * @since CUPS 1.2/Mac OS X 10.5@
68 */
69
70 int /* O - 1 if equal, 0 if not */
71 httpAddrEqual(const http_addr_t *addr1, /* I - First address */
72 const http_addr_t *addr2) /* I - Second address */
73 {
74 if (!addr1 && !addr2)
75 return (1);
76
77 if (!addr1 || !addr2)
78 return (0);
79
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 *
100 * @since CUPS 1.2/Mac OS X 10.5@
101 */
102
103 int /* O - Length in bytes */
104 httpAddrLength(const http_addr_t *addr) /* I - Address */
105 {
106 if (!addr)
107 return (0);
108
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 *
131 * @since CUPS 1.2/Mac OS X 10.5@
132 */
133
134 int /* O - 1 if local host, 0 otherwise */
135 httpAddrLocalhost(
136 const http_addr_t *addr) /* I - Address to check */
137 {
138 if (!addr)
139 return (1);
140
141 #ifdef AF_INET6
142 if (addr->addr.sa_family == AF_INET6 &&
143 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
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 &&
153 (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000)
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 *
170 * @since CUPS 1.2/Mac OS X 10.5@
171 */
172
173 char * /* O - Host name */
174 httpAddrLookup(
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 */
178 {
179 _cups_globals_t *cg = _cupsGlobals();
180 /* Global data */
181
182
183 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name,
184 namelen));
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)
200 {
201 strlcpy(name, addr->un.sun_path, namelen);
202 return (name);
203 }
204 #endif /* AF_LOCAL */
205
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
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
237 #ifdef HAVE_GETNAMEINFO
238 {
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 */
246
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
255 return (httpAddrString(addr, name, namelen));
256 }
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
278 if (h_errno == NO_RECOVERY)
279 cg->need_res_init = 1;
280
281 return (httpAddrString(addr, name, namelen));
282 }
283
284 strlcpy(name, host->h_name, namelen);
285 }
286 #endif /* HAVE_GETNAMEINFO */
287
288 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name));
289
290 return (name);
291 }
292
293
294 /*
295 * '_httpAddrPort()' - Get the port number associated with an address.
296 */
297
298 int /* O - Port number */
299 _httpAddrPort(http_addr_t *addr) /* I - Address */
300 {
301 if (!addr)
302 return (ippPort());
303 #ifdef AF_INET6
304 else if (addr->addr.sa_family == AF_INET6)
305 return (ntohs(addr->ipv6.sin6_port));
306 #endif /* AF_INET6 */
307 else if (addr->addr.sa_family == AF_INET)
308 return (ntohs(addr->ipv4.sin_port));
309 else
310 return (ippPort());
311 }
312
313
314 /*
315 * 'httpAddrString()' - Convert an address to a numeric string.
316 *
317 * @since CUPS 1.2/Mac OS X 10.5@
318 */
319
320 char * /* O - Numeric address string */
321 httpAddrString(const http_addr_t *addr, /* I - Address to convert */
322 char *s, /* I - String buffer */
323 int slen) /* I - Length of string */
324 {
325 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen));
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 */
344 if (addr->addr.sa_family == AF_INET)
345 {
346 unsigned temp; /* Temporary address */
347
348
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
358 if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen,
359 NULL, 0, NI_NUMERICHOST))
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 }
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 ++)
379 {
380 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
381
382 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
383 prefix = ":";
384 slen -= strlen(sptr);
385 sptr += strlen(sptr);
386
387 temp &= 0xffff;
388
389 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
390 {
391 snprintf(sptr, slen, "%s%x", prefix, temp);
392 slen -= strlen(sptr);
393 sptr += strlen(sptr);
394 }
395 }
396
397 if (i < 4)
398 {
399 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
400 i ++;
401
402 if (i < 4)
403 {
404 snprintf(sptr, slen, "%s:", prefix);
405 prefix = ":";
406 slen -= strlen(sptr);
407 sptr += strlen(sptr);
408
409 for (; i < 4; i ++)
410 {
411 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
412
413 if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1])
414 {
415 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
416 slen -= strlen(sptr);
417 sptr += strlen(sptr);
418 }
419
420 snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff);
421 slen -= strlen(sptr);
422 sptr += strlen(sptr);
423 }
424 }
425 else if (sptr == s)
426 {
427 /*
428 * Empty address...
429 */
430
431 strlcpy(s, "::", slen);
432 sptr = s + 2;
433 slen -= 2;
434 }
435 else
436 {
437 /*
438 * Empty at end...
439 */
440
441 strlcpy(sptr, "::", slen);
442 sptr += 2;
443 slen -= 2;
444 }
445 }
446 # endif /* HAVE_GETNAMEINFO */
447 }
448 #endif /* AF_INET6 */
449 else
450 strlcpy(s, "UNKNOWN", slen);
451
452 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s));
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
465 struct hostent * /* O - Host entry */
466 httpGetHostByName(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
474 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name));
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
512 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
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
548 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
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
559 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
560
561 return (gethostbyname(name));
562 }
563 }
564
565
566 /*
567 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
568 *
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.
573 *
574 * @since CUPS 1.2/Mac OS X 10.5@
575 */
576
577 const char * /* O - FQDN for connection or system */
578 httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
579 char *s, /* I - String buffer for name */
580 int slen) /* I - Size of buffer */
581 {
582 struct hostent *host; /* Host entry to get FQDN */
583
584
585 if (!s || slen <= 1)
586 return (NULL);
587
588 if (http)
589 {
590 if (http->hostname[0] == '/')
591 strlcpy(s, "localhost", slen);
592 else
593 strlcpy(s, http->hostname, slen);
594 }
595 else
596 {
597 /*
598 * Get the hostname...
599 */
600
601 if (gethostname(s, slen) < 0)
602 strlcpy(s, "localhost", slen);
603
604 if (!strchr(s, '.'))
605 {
606 /*
607 * The hostname is not a FQDN, so look it up...
608 */
609
610 if ((host = gethostbyname(s)) != NULL && host->h_name)
611 strlcpy(s, host->h_name, slen);
612 }
613 }
614
615 /*
616 * Return the hostname with as much domain info as we have...
617 */
618
619 return (s);
620 }
621
622
623 /*
624 * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".
625 */