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