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