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