]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $"
4 * HTTP address routines for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
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 * _httpAddrPort() - Get the port number associated with an address.
22 * httpAddrString() - Convert an IP address to a dotted string.
23 * httpGetHostByName() - Lookup a hostname or IP address, and return
24 * address records for the specified name.
25 * httpGetHostname() - Get the FQDN for the local system.
29 * Include necessary headers...
32 #include "cups-private.h"
35 #endif /* HAVE_RESOLV_H */
39 * 'httpAddrAny()' - Check for the "any" address.
41 * @since CUPS 1.2/Mac OS X 10.5@
44 int /* O - 1 if "any", 0 otherwise */
45 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
51 if (addr
->addr
.sa_family
== AF_INET6
&&
52 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
56 if (addr
->addr
.sa_family
== AF_INET
&&
57 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
65 * 'httpAddrEqual()' - Compare two addresses.
67 * @since CUPS 1.2/Mac OS X 10.5@
70 int /* O - 1 if equal, 0 if not */
71 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
72 const http_addr_t
*addr2
) /* I - Second address */
80 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
84 if (addr1
->addr
.sa_family
== AF_LOCAL
)
85 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
89 if (addr1
->addr
.sa_family
== AF_INET6
)
90 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
93 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
98 * 'httpAddrLength()' - Return the length of the address in bytes.
100 * @since CUPS 1.2/Mac OS X 10.5@
103 int /* O - Length in bytes */
104 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
110 if (addr
->addr
.sa_family
== AF_INET6
)
111 return (sizeof(addr
->ipv6
));
113 #endif /* AF_INET6 */
115 if (addr
->addr
.sa_family
== AF_LOCAL
)
116 return (offsetof(struct sockaddr_un
, sun_path
) +
117 strlen(addr
->un
.sun_path
) + 1);
119 #endif /* AF_LOCAL */
120 if (addr
->addr
.sa_family
== AF_INET
)
121 return (sizeof(addr
->ipv4
));
129 * 'httpAddrLocalhost()' - Check for the local loopback address.
131 * @since CUPS 1.2/Mac OS X 10.5@
134 int /* O - 1 if local host, 0 otherwise */
136 const http_addr_t
*addr
) /* I - Address to check */
142 if (addr
->addr
.sa_family
== AF_INET6
&&
143 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
145 #endif /* AF_INET6 */
148 if (addr
->addr
.sa_family
== AF_LOCAL
)
150 #endif /* AF_LOCAL */
152 if (addr
->addr
.sa_family
== AF_INET
&&
153 (ntohl(addr
->ipv4
.sin_addr
.s_addr
) & 0xff000000) == 0x7f000000)
161 # define ADDR_CAST (struct sockaddr *)
163 # define ADDR_CAST (char *)
168 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
170 * @since CUPS 1.2/Mac OS X 10.5@
173 char * /* O - Host name */
175 const http_addr_t
*addr
, /* I - Address to lookup */
176 char *name
, /* I - Host name buffer */
177 int namelen
) /* I - Size of name buffer */
179 _cups_globals_t
*cg
= _cupsGlobals();
183 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr
, name
,
187 * Range check input...
190 if (!addr
|| !name
|| namelen
<= 2)
192 if (name
&& namelen
>= 1)
199 if (addr
->addr
.sa_family
== AF_LOCAL
)
201 strlcpy(name
, addr
->un
.sun_path
, namelen
);
204 #endif /* AF_LOCAL */
207 * Optimize lookups for localhost/loopback addresses...
210 if (httpAddrLocalhost(addr
))
212 strlcpy(name
, "localhost", namelen
);
218 * STR #2920: Initialize resolver after failure in cups-polld
220 * If the previous lookup failed, re-initialize the resolver to prevent
221 * temporary network errors from persisting. This *should* be handled by
222 * the resolver libraries, but apparently the glibc folks do not agree.
224 * We set a flag at the end of this function if we encounter an error that
225 * requires reinitialization of the resolver functions. We then call
226 * res_init() if the flag is set on the next call here or in httpAddrLookup().
229 if (cg
->need_res_init
)
233 cg
->need_res_init
= 0;
235 #endif /* HAVE_RES_INIT */
237 #ifdef HAVE_GETNAMEINFO
240 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
242 * FWIW, I think this is really a bug in the implementation of
243 * getnameinfo(), but falling back on httpAddrString() is easy to
247 int error
= getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
252 if (error
== EAI_FAIL
)
253 cg
->need_res_init
= 1;
255 return (httpAddrString(addr
, name
, namelen
));
260 struct hostent
*host
; /* Host from name service */
264 if (addr
->addr
.sa_family
== AF_INET6
)
265 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
266 sizeof(struct in_addr
), AF_INET6
);
268 # endif /* AF_INET6 */
269 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
270 sizeof(struct in_addr
), AF_INET
);
275 * No hostname, so return the raw address...
278 if (h_errno
== NO_RECOVERY
)
279 cg
->need_res_init
= 1;
281 return (httpAddrString(addr
, name
, namelen
));
284 strlcpy(name
, host
->h_name
, namelen
);
286 #endif /* HAVE_GETNAMEINFO */
288 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name
));
295 * '_httpAddrPort()' - Get the port number associated with an address.
298 int /* O - Port number */
299 _httpAddrPort(http_addr_t
*addr
) /* I - Address */
304 else if (addr
->addr
.sa_family
== AF_INET6
)
305 return (ntohs(addr
->ipv6
.sin6_port
));
306 #endif /* AF_INET6 */
307 else if (addr
->addr
.sa_family
== AF_INET
)
308 return (ntohs(addr
->ipv4
.sin_port
));
315 * 'httpAddrString()' - Convert an address to a numeric string.
317 * @since CUPS 1.2/Mac OS X 10.5@
320 char * /* O - Numeric address string */
321 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
322 char *s
, /* I - String buffer */
323 int slen
) /* I - Length of string */
325 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr
, s
, slen
));
328 * Range check input...
331 if (!addr
|| !s
|| slen
<= 2)
340 if (addr
->addr
.sa_family
== AF_LOCAL
)
341 strlcpy(s
, addr
->un
.sun_path
, slen
);
343 #endif /* AF_LOCAL */
344 if (addr
->addr
.sa_family
== AF_INET
)
346 unsigned temp
; /* Temporary address */
349 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
351 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
352 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
355 else if (addr
->addr
.sa_family
== AF_INET6
)
357 # ifdef HAVE_GETNAMEINFO
358 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
359 NULL
, 0, NI_NUMERICHOST
))
362 * If we get an error back, then the address type is not supported
363 * and we should zero out the buffer...
371 char *sptr
; /* Pointer into string */
372 int i
; /* Looping var */
373 unsigned temp
; /* Current value */
374 const char *prefix
; /* Prefix for address */
378 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
380 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
382 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
384 slen
-= strlen(sptr
);
385 sptr
+= strlen(sptr
);
389 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
391 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
392 slen
-= strlen(sptr
);
393 sptr
+= strlen(sptr
);
399 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
404 snprintf(sptr
, slen
, "%s:", prefix
);
406 slen
-= strlen(sptr
);
407 sptr
+= strlen(sptr
);
411 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
413 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
415 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
416 slen
-= strlen(sptr
);
417 sptr
+= strlen(sptr
);
420 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
421 slen
-= strlen(sptr
);
422 sptr
+= strlen(sptr
);
431 strlcpy(s
, "::", slen
);
441 strlcpy(sptr
, "::", slen
);
446 # endif /* HAVE_GETNAMEINFO */
448 #endif /* AF_INET6 */
450 strlcpy(s
, "UNKNOWN", slen
);
452 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s
));
459 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
460 * address records for the specified name.
465 struct hostent
* /* O - Host entry */
466 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
468 const char *nameptr
; /* Pointer into name */
469 unsigned ip
[4]; /* IP address components */
470 _cups_globals_t
*cg
= _cupsGlobals();
471 /* Pointer to library globals */
474 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name
));
477 * Avoid lookup delays and configuration problems when connecting
478 * to the localhost address...
481 if (!strcmp(name
, "localhost"))
485 * This function is needed because some operating systems have a
486 * buggy implementation of gethostbyname() that does not support
487 * IP addresses. If the first character of the name string is a
488 * number, then sscanf() is used to extract the IP components.
489 * We then pack the components into an IPv4 address manually,
490 * since the inet_aton() function is deprecated. We use the
491 * htonl() macro to get the right byte order for the address.
493 * We also support domain sockets when supported by the underlying
501 * A domain socket address, so make an AF_LOCAL entry and return it...
504 cg
->hostent
.h_name
= (char *)name
;
505 cg
->hostent
.h_aliases
= NULL
;
506 cg
->hostent
.h_addrtype
= AF_LOCAL
;
507 cg
->hostent
.h_length
= strlen(name
) + 1;
508 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
509 cg
->ip_ptrs
[0] = (char *)name
;
510 cg
->ip_ptrs
[1] = NULL
;
512 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
514 return (&cg
->hostent
);
516 #endif /* AF_LOCAL */
518 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
523 * We have an IPv4 address; break it up and provide the host entry
527 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
528 return (NULL
); /* Must have 4 numbers */
530 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
531 return (NULL
); /* Invalid byte ranges! */
533 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
537 * Fill in the host entry and return it...
540 cg
->hostent
.h_name
= (char *)name
;
541 cg
->hostent
.h_aliases
= NULL
;
542 cg
->hostent
.h_addrtype
= AF_INET
;
543 cg
->hostent
.h_length
= 4;
544 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
545 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
546 cg
->ip_ptrs
[1] = NULL
;
548 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
550 return (&cg
->hostent
);
555 * Use the gethostbyname() function to get the IPv4 address for
559 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
561 return (gethostbyname(name
));
567 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
569 * When "http" points to a connected socket, return the hostname or
570 * address that was used in the call to httpConnect() or httpConnectEncrypt().
571 * Otherwise, return the FQDN for the local system using both gethostname()
572 * and gethostbyname() to get the local hostname with domain.
574 * @since CUPS 1.2/Mac OS X 10.5@
577 const char * /* O - FQDN for connection or system */
578 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
579 char *s
, /* I - String buffer for name */
580 int slen
) /* I - Size of buffer */
582 struct hostent
*host
; /* Host entry to get FQDN */
590 if (http
->hostname
[0] == '/')
591 strlcpy(s
, "localhost", slen
);
593 strlcpy(s
, http
->hostname
, slen
);
598 * Get the hostname...
601 if (gethostname(s
, slen
) < 0)
602 strlcpy(s
, "localhost", slen
);
607 * The hostname is not a FQDN, so look it up...
610 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
611 strlcpy(s
, host
->h_name
, slen
);
616 * Return the hostname with as much domain info as we have...
624 * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".