Merge changes from r6781 to r6792 (CUPS 1.3.0)
[thirdparty/cups.git] / cups / http-addr.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: http-addr.c 6649 2007-07-11 21:46:42Z 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 {
ef416fc2 200 if (getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen,
b423cd4c 201 NULL, 0, 0))
ef416fc2 202 {
203 /*
204 * If we get an error back, then the address type is not supported
205 * and we should zero out the buffer...
206 */
207
208 name[0] = '\0';
209
210 return (NULL);
211 }
212 }
213#else
214 {
215 struct hostent *host; /* Host from name service */
216
217
218# ifdef AF_INET6
219 if (addr->addr.sa_family == AF_INET6)
220 host = gethostbyaddr(ADDR_CAST &(addr->ipv6.sin6_addr),
221 sizeof(struct in_addr), AF_INET6);
222 else
223# endif /* AF_INET6 */
224 host = gethostbyaddr(ADDR_CAST &(addr->ipv4.sin_addr),
225 sizeof(struct in_addr), AF_INET);
226
227 if (host == NULL)
228 {
229 /*
230 * No hostname, so return the raw address...
231 */
232
233 httpAddrString(addr, name, namelen);
234 return (NULL);
235 }
236
237 strlcpy(name, host->h_name, namelen);
238 }
239#endif /* HAVE_GETNAMEINFO */
240
241 return (name);
242}
243
244
245/*
ecdc0628 246 * 'httpAddrString()' - Convert an address to a numeric string.
ef416fc2 247 *
248 * @since CUPS 1.2@
249 */
250
ecdc0628 251char * /* O - Numeric address string */
252httpAddrString(const http_addr_t *addr, /* I - Address to convert */
253 char *s, /* I - String buffer */
254 int slen) /* I - Length of string */
ef416fc2 255{
256 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
257 addr, s, slen));
258
259 /*
260 * Range check input...
261 */
262
263 if (!addr || !s || slen <= 2)
264 {
265 if (s && slen >= 1)
266 *s = '\0';
267
268 return (NULL);
269 }
270
271#ifdef AF_LOCAL
272 if (addr->addr.sa_family == AF_LOCAL)
273 strlcpy(s, addr->un.sun_path, slen);
274 else
275#endif /* AF_LOCAL */
b423cd4c 276 if (addr->addr.sa_family == AF_INET)
ef416fc2 277 {
b423cd4c 278 unsigned temp; /* Temporary address */
ef416fc2 279
280
b423cd4c 281 temp = ntohl(addr->ipv4.sin_addr.s_addr);
282
283 snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255,
284 (temp >> 16) & 255, (temp >> 8) & 255, temp & 255);
285 }
286#ifdef AF_INET6
287 else if (addr->addr.sa_family == AF_INET6)
288 {
289# ifdef HAVE_GETNAMEINFO
ef416fc2 290 if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen,
b423cd4c 291 NULL, 0, NI_NUMERICHOST))
ef416fc2 292 {
293 /*
294 * If we get an error back, then the address type is not supported
295 * and we should zero out the buffer...
296 */
297
298 s[0] = '\0';
299
300 return (NULL);
301 }
b423cd4c 302# else
303 char *sptr; /* Pointer into string */
304 int i; /* Looping var */
305 unsigned temp; /* Current value */
306 const char *prefix; /* Prefix for address */
307
308
309 prefix = "";
310 for (sptr = s, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
ef416fc2 311 {
b423cd4c 312 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
ef416fc2 313
b423cd4c 314 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
315 prefix = ":";
316 slen -= strlen(sptr);
317 sptr += strlen(sptr);
ef416fc2 318
b423cd4c 319 temp &= 0xffff;
ef416fc2 320
b423cd4c 321 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
322 {
323 snprintf(sptr, slen, "%s%x", prefix, temp);
ef416fc2 324 slen -= strlen(sptr);
325 sptr += strlen(sptr);
ef416fc2 326 }
b423cd4c 327 }
328
329 if (i < 4)
330 {
331 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
332 i ++;
ef416fc2 333
334 if (i < 4)
335 {
b423cd4c 336 snprintf(sptr, slen, "%s:", prefix);
337 prefix = ":";
338 slen -= strlen(sptr);
339 sptr += strlen(sptr);
ef416fc2 340
b423cd4c 341 for (; i < 4; i ++)
ef416fc2 342 {
b423cd4c 343 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
ef416fc2 344
b423cd4c 345 if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1])
ef416fc2 346 {
b423cd4c 347 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
ef416fc2 348 slen -= strlen(sptr);
349 sptr += strlen(sptr);
b423cd4c 350 }
ef416fc2 351
b423cd4c 352 snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff);
353 slen -= strlen(sptr);
354 sptr += strlen(sptr);
ef416fc2 355 }
356 }
b423cd4c 357 else if (sptr == s)
358 {
359 /*
360 * Empty address...
361 */
ef416fc2 362
b423cd4c 363 strlcpy(s, "::", slen);
364 sptr = s + 2;
365 slen -= 2;
366 }
367 else
368 {
369 /*
370 * Empty at end...
371 */
ef416fc2 372
b423cd4c 373 strlcpy(sptr, "::", slen);
374 sptr += 2;
375 slen -= 2;
376 }
ef416fc2 377 }
b423cd4c 378# endif /* HAVE_GETNAMEINFO */
ef416fc2 379 }
b423cd4c 380#endif /* AF_INET6 */
381 else
382 strlcpy(s, "UNKNOWN", slen);
ef416fc2 383
384 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s));
385
386 return (s);
387}
388
389
390/*
391 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
392 * address records for the specified name.
393 *
394 * @deprecated@
395 */
396
397struct hostent * /* O - Host entry */
398httpGetHostByName(const char *name) /* I - Hostname or IP address */
399{
400 const char *nameptr; /* Pointer into name */
401 unsigned ip[4]; /* IP address components */
402 _cups_globals_t *cg = _cupsGlobals();
403 /* Pointer to library globals */
404
405
406 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name));
407
408 /*
409 * Avoid lookup delays and configuration problems when connecting
410 * to the localhost address...
411 */
412
413 if (!strcmp(name, "localhost"))
414 name = "127.0.0.1";
415
416 /*
417 * This function is needed because some operating systems have a
418 * buggy implementation of gethostbyname() that does not support
419 * IP addresses. If the first character of the name string is a
420 * number, then sscanf() is used to extract the IP components.
421 * We then pack the components into an IPv4 address manually,
422 * since the inet_aton() function is deprecated. We use the
423 * htonl() macro to get the right byte order for the address.
424 *
425 * We also support domain sockets when supported by the underlying
426 * OS...
427 */
428
429#ifdef AF_LOCAL
430 if (name[0] == '/')
431 {
432 /*
433 * A domain socket address, so make an AF_LOCAL entry and return it...
434 */
435
436 cg->hostent.h_name = (char *)name;
437 cg->hostent.h_aliases = NULL;
438 cg->hostent.h_addrtype = AF_LOCAL;
439 cg->hostent.h_length = strlen(name) + 1;
440 cg->hostent.h_addr_list = cg->ip_ptrs;
441 cg->ip_ptrs[0] = (char *)name;
442 cg->ip_ptrs[1] = NULL;
443
444 DEBUG_puts("httpGetHostByName: returning domain socket address...");
445
446 return (&cg->hostent);
447 }
448#endif /* AF_LOCAL */
449
450 for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++);
451
452 if (!*nameptr)
453 {
454 /*
455 * We have an IPv4 address; break it up and provide the host entry
456 * to the caller.
457 */
458
459 if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4)
460 return (NULL); /* Must have 4 numbers */
461
462 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
463 return (NULL); /* Invalid byte ranges! */
464
465 cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) |
466 ip[3]));
467
468 /*
469 * Fill in the host entry and return it...
470 */
471
472 cg->hostent.h_name = (char *)name;
473 cg->hostent.h_aliases = NULL;
474 cg->hostent.h_addrtype = AF_INET;
475 cg->hostent.h_length = 4;
476 cg->hostent.h_addr_list = cg->ip_ptrs;
477 cg->ip_ptrs[0] = (char *)&(cg->ip_addr);
478 cg->ip_ptrs[1] = NULL;
479
480 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
481
482 return (&cg->hostent);
483 }
484 else
485 {
486 /*
487 * Use the gethostbyname() function to get the IPv4 address for
488 * the name...
489 */
490
491 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
492
493 return (gethostbyname(name));
494 }
495}
496
497
498/*
757d2cad 499 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
ef416fc2 500 *
757d2cad 501 * When "http" points to a connected socket, return the hostname or
502 * address that was used in the call to httpConnect() or httpConnectEncrypt().
503 * Otherwise, return the FQDN for the local system using both gethostname()
504 * and gethostbyname() to get the local hostname with domain.
ef416fc2 505 *
506 * @since CUPS 1.2@
507 */
508
757d2cad 509const char * /* O - FQDN for connection or system */
510httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
511 char *s, /* I - String buffer for name */
512 int slen) /* I - Size of buffer */
ef416fc2 513{
514 struct hostent *host; /* Host entry to get FQDN */
515
516
89d46774 517 if (!s || slen <= 1)
518 return (NULL);
519
757d2cad 520 if (http)
480ef0fe 521 {
522 if (http->hostname[0] == '/')
523 strlcpy(s, "localhost", slen);
524 else
525 strlcpy(s, http->hostname, slen);
526 }
757d2cad 527 else
ef416fc2 528 {
529 /*
757d2cad 530 * Get the hostname...
ef416fc2 531 */
532
89d46774 533 if (gethostname(s, slen) < 0)
534 strlcpy(s, "localhost", slen);
757d2cad 535
536 if (!strchr(s, '.'))
537 {
538 /*
539 * The hostname is not a FQDN, so look it up...
540 */
541
89d46774 542 if ((host = gethostbyname(s)) != NULL && host->h_name)
757d2cad 543 strlcpy(s, host->h_name, slen);
544 }
ef416fc2 545 }
546
547 /*
548 * Return the hostname with as much domain info as we have...
549 */
550
551 return (s);
552}
553
554
555/*
bc44d920 556 * End of "$Id: http-addr.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 557 */