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