]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / http-addr.c
1 /*
2 * "$Id: http-addr.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * HTTP address routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
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
43 int /* O - 1 if "any", 0 otherwise */
44 httpAddrAny(const http_addr_t *addr) /* I - Address to check */
45 {
46 if (!addr)
47 return (0);
48
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
69 int /* O - 1 if equal, 0 if not */
70 httpAddrEqual(const http_addr_t *addr1, /* I - First address */
71 const http_addr_t *addr2) /* I - Second address */
72 {
73 if (!addr1 && !addr2)
74 return (1);
75
76 if (!addr1 || !addr2)
77 return (0);
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
102 int /* O - Length in bytes */
103 httpAddrLength(const http_addr_t *addr) /* I - Address */
104 {
105 if (!addr)
106 return (0);
107
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
133 int /* O - 1 if local host, 0 otherwise */
134 httpAddrLocalhost(
135 const http_addr_t *addr) /* I - Address to check */
136 {
137 if (!addr)
138 return (1);
139
140 #ifdef AF_INET6
141 if (addr->addr.sa_family == AF_INET6 &&
142 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
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
172 char * /* O - Host name */
173 httpAddrLookup(
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 */
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 {
200 if (getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen,
201 NULL, 0, 0))
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 /*
246 * 'httpAddrString()' - Convert an address to a numeric string.
247 *
248 * @since CUPS 1.2@
249 */
250
251 char * /* O - Numeric address string */
252 httpAddrString(const http_addr_t *addr, /* I - Address to convert */
253 char *s, /* I - String buffer */
254 int slen) /* I - Length of string */
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 */
276 if (addr->addr.sa_family == AF_INET)
277 {
278 unsigned temp; /* Temporary address */
279
280
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
290 if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen,
291 NULL, 0, NI_NUMERICHOST))
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 }
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 ++)
311 {
312 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
313
314 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
315 prefix = ":";
316 slen -= strlen(sptr);
317 sptr += strlen(sptr);
318
319 temp &= 0xffff;
320
321 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
322 {
323 snprintf(sptr, slen, "%s%x", prefix, temp);
324 slen -= strlen(sptr);
325 sptr += strlen(sptr);
326 }
327 }
328
329 if (i < 4)
330 {
331 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
332 i ++;
333
334 if (i < 4)
335 {
336 snprintf(sptr, slen, "%s:", prefix);
337 prefix = ":";
338 slen -= strlen(sptr);
339 sptr += strlen(sptr);
340
341 for (; i < 4; i ++)
342 {
343 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
344
345 if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1])
346 {
347 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
348 slen -= strlen(sptr);
349 sptr += strlen(sptr);
350 }
351
352 snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff);
353 slen -= strlen(sptr);
354 sptr += strlen(sptr);
355 }
356 }
357 else if (sptr == s)
358 {
359 /*
360 * Empty address...
361 */
362
363 strlcpy(s, "::", slen);
364 sptr = s + 2;
365 slen -= 2;
366 }
367 else
368 {
369 /*
370 * Empty at end...
371 */
372
373 strlcpy(sptr, "::", slen);
374 sptr += 2;
375 slen -= 2;
376 }
377 }
378 # endif /* HAVE_GETNAMEINFO */
379 }
380 #endif /* AF_INET6 */
381 else
382 strlcpy(s, "UNKNOWN", slen);
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
397 struct hostent * /* O - Host entry */
398 httpGetHostByName(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 /*
499 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
500 *
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.
505 *
506 * @since CUPS 1.2@
507 */
508
509 const char * /* O - FQDN for connection or system */
510 httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
511 char *s, /* I - String buffer for name */
512 int slen) /* I - Size of buffer */
513 {
514 struct hostent *host; /* Host entry to get FQDN */
515
516
517 if (!s || slen <= 1)
518 return (NULL);
519
520 if (http)
521 {
522 if (http->hostname[0] == '/')
523 strlcpy(s, "localhost", slen);
524 else
525 strlcpy(s, http->hostname, slen);
526 }
527 else
528 {
529 /*
530 * Get the hostname...
531 */
532
533 if (gethostname(s, slen) < 0)
534 strlcpy(s, "localhost", slen);
535
536 if (!strchr(s, '.'))
537 {
538 /*
539 * The hostname is not a FQDN, so look it up...
540 */
541
542 if ((host = gethostbyname(s)) != NULL && host->h_name)
543 strlcpy(s, host->h_name, slen);
544 }
545 }
546
547 /*
548 * Return the hostname with as much domain info as we have...
549 */
550
551 return (s);
552 }
553
554
555 /*
556 * End of "$Id: http-addr.c 6649 2007-07-11 21:46:42Z mike $".
557 */