]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
More svn:properties changes.
[thirdparty/cups.git] / cups / http-addr.c
1 /*
2 * "$Id$"
3 *
4 * HTTP address routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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
52 int /* O - 1 if "any", 0 otherwise */
53 httpAddrAny(const http_addr_t *addr) /* I - Address to check */
54 {
55 if (!addr)
56 return (0);
57
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
78 int /* O - 1 if equal, 0 if not */
79 httpAddrEqual(const http_addr_t *addr1, /* I - First address */
80 const http_addr_t *addr2) /* I - Second address */
81 {
82 if (!addr1 && !addr2)
83 return (1);
84
85 if (!addr1 || !addr2)
86 return (0);
87
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
111 int /* O - Length in bytes */
112 httpAddrLength(const http_addr_t *addr) /* I - Address */
113 {
114 if (!addr)
115 return (0);
116
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
142 int /* O - 1 if local host, 0 otherwise */
143 httpAddrLocalhost(
144 const http_addr_t *addr) /* I - Address to check */
145 {
146 if (!addr)
147 return (1);
148
149 #ifdef AF_INET6
150 if (addr->addr.sa_family == AF_INET6 &&
151 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
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
181 char * /* O - Host name */
182 httpAddrLookup(
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 */
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 {
209 if (getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen,
210 NULL, 0, 0))
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 /*
255 * 'httpAddrString()' - Convert an address to a numeric string.
256 *
257 * @since CUPS 1.2@
258 */
259
260 char * /* O - Numeric address string */
261 httpAddrString(const http_addr_t *addr, /* I - Address to convert */
262 char *s, /* I - String buffer */
263 int slen) /* I - Length of string */
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 */
285 if (addr->addr.sa_family == AF_INET)
286 {
287 unsigned temp; /* Temporary address */
288
289
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
299 if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen,
300 NULL, 0, NI_NUMERICHOST))
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 }
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 ++)
320 {
321 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
322
323 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
324 prefix = ":";
325 slen -= strlen(sptr);
326 sptr += strlen(sptr);
327
328 temp &= 0xffff;
329
330 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
331 {
332 snprintf(sptr, slen, "%s%x", prefix, temp);
333 slen -= strlen(sptr);
334 sptr += strlen(sptr);
335 }
336 }
337
338 if (i < 4)
339 {
340 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
341 i ++;
342
343 if (i < 4)
344 {
345 snprintf(sptr, slen, "%s:", prefix);
346 prefix = ":";
347 slen -= strlen(sptr);
348 sptr += strlen(sptr);
349
350 for (; i < 4; i ++)
351 {
352 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
353
354 if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1])
355 {
356 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
357 slen -= strlen(sptr);
358 sptr += strlen(sptr);
359 }
360
361 snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff);
362 slen -= strlen(sptr);
363 sptr += strlen(sptr);
364 }
365 }
366 else if (sptr == s)
367 {
368 /*
369 * Empty address...
370 */
371
372 strlcpy(s, "::", slen);
373 sptr = s + 2;
374 slen -= 2;
375 }
376 else
377 {
378 /*
379 * Empty at end...
380 */
381
382 strlcpy(sptr, "::", slen);
383 sptr += 2;
384 slen -= 2;
385 }
386 }
387 # endif /* HAVE_GETNAMEINFO */
388 }
389 #endif /* AF_INET6 */
390 else
391 strlcpy(s, "UNKNOWN", slen);
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
406 struct hostent * /* O - Host entry */
407 httpGetHostByName(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 /*
508 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
509 *
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.
514 *
515 * @since CUPS 1.2@
516 */
517
518 const char * /* O - FQDN for connection or system */
519 httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
520 char *s, /* I - String buffer for name */
521 int slen) /* I - Size of buffer */
522 {
523 struct hostent *host; /* Host entry to get FQDN */
524
525
526 if (!s || slen <= 1)
527 return (NULL);
528
529 if (http)
530 {
531 if (http->hostname[0] == '/')
532 strlcpy(s, "localhost", slen);
533 else
534 strlcpy(s, http->hostname, slen);
535 }
536 else
537 {
538 /*
539 * Get the hostname...
540 */
541
542 if (gethostname(s, slen) < 0)
543 strlcpy(s, "localhost", slen);
544
545 if (!strchr(s, '.'))
546 {
547 /*
548 * The hostname is not a FQDN, so look it up...
549 */
550
551 if ((host = gethostbyname(s)) != NULL && host->h_name)
552 strlcpy(s, host->h_name, slen);
553 }
554 }
555
556 /*
557 * Return the hostname with as much domain info as we have...
558 */
559
560 return (s);
561 }
562
563
564 /*
565 * End of "$Id$".
566 */