]> git.ipfire.org Git - thirdparty/cups.git/blame_incremental - cups/http-addr.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / http-addr.c
... / ...
CommitLineData
1/*
2 * "$Id: http-addr.c 5235 2006-03-06 13:02:23Z mike $"
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
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
75int /* O - 1 if equal, 0 if not */
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 &&
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
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 */
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 {
194 if (getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen,
195 NULL, 0, 0))
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/*
240 * 'httpAddrString()' - Convert an address to a numeric string.
241 *
242 * @since CUPS 1.2@
243 */
244
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 */
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 */
270 if (addr->addr.sa_family == AF_INET)
271 {
272 unsigned temp; /* Temporary address */
273
274
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
284 if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen,
285 NULL, 0, NI_NUMERICHOST))
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 }
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 ++)
305 {
306 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
307
308 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
309 prefix = ":";
310 slen -= strlen(sptr);
311 sptr += strlen(sptr);
312
313 temp &= 0xffff;
314
315 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
316 {
317 snprintf(sptr, slen, "%s%x", prefix, temp);
318 slen -= strlen(sptr);
319 sptr += strlen(sptr);
320 }
321 }
322
323 if (i < 4)
324 {
325 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
326 i ++;
327
328 if (i < 4)
329 {
330 snprintf(sptr, slen, "%s:", prefix);
331 prefix = ":";
332 slen -= strlen(sptr);
333 sptr += strlen(sptr);
334
335 for (; i < 4; i ++)
336 {
337 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
338
339 if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1])
340 {
341 snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff);
342 slen -= strlen(sptr);
343 sptr += strlen(sptr);
344 }
345
346 snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff);
347 slen -= strlen(sptr);
348 sptr += strlen(sptr);
349 }
350 }
351 else if (sptr == s)
352 {
353 /*
354 * Empty address...
355 */
356
357 strlcpy(s, "::", slen);
358 sptr = s + 2;
359 slen -= 2;
360 }
361 else
362 {
363 /*
364 * Empty at end...
365 */
366
367 strlcpy(sptr, "::", slen);
368 sptr += 2;
369 slen -= 2;
370 }
371 }
372# endif /* HAVE_GETNAMEINFO */
373 }
374#endif /* AF_INET6 */
375 else
376 strlcpy(s, "UNKNOWN", slen);
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 connection or local system.
494 *
495 * When "http" points to a connected socket, return the hostname or
496 * address that was used in the call to httpConnect() or httpConnectEncrypt().
497 * Otherwise, return the FQDN for the local system using both gethostname()
498 * and gethostbyname() to get the local hostname with domain.
499 *
500 * @since CUPS 1.2@
501 */
502
503const char * /* O - FQDN for connection or system */
504httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
505 char *s, /* I - String buffer for name */
506 int slen) /* I - Size of buffer */
507{
508 struct hostent *host; /* Host entry to get FQDN */
509
510
511 if (http)
512 strlcpy(s, http->hostname, slen);
513 else
514 {
515 /*
516 * Get the hostname...
517 */
518
519 gethostname(s, slen);
520
521 if (!strchr(s, '.'))
522 {
523 /*
524 * The hostname is not a FQDN, so look it up...
525 */
526
527 if ((host = gethostbyname(s)) != NULL)
528 strlcpy(s, host->h_name, slen);
529 }
530 }
531
532 /*
533 * Return the hostname with as much domain info as we have...
534 */
535
536 return (s);
537}
538
539
540/*
541 * End of "$Id: http-addr.c 5235 2006-03-06 13:02:23Z mike $".
542 */