]>
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 the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 "http-private.h"
39 #endif /* HAVE_RESOLV_H */
43 * 'httpAddrAny()' - Check for the "any" address.
45 * @since CUPS 1.2/Mac OS X 10.5@
48 int /* O - 1 if "any", 0 otherwise */
49 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
55 if (addr
->addr
.sa_family
== AF_INET6
&&
56 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
60 if (addr
->addr
.sa_family
== AF_INET
&&
61 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
69 * 'httpAddrEqual()' - Compare two addresses.
71 * @since CUPS 1.2/Mac OS X 10.5@
74 int /* O - 1 if equal, 0 if not */
75 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
76 const http_addr_t
*addr2
) /* I - Second address */
84 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
88 if (addr1
->addr
.sa_family
== AF_LOCAL
)
89 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
93 if (addr1
->addr
.sa_family
== AF_INET6
)
94 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
97 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
102 * 'httpAddrLength()' - Return the length of the address in bytes.
104 * @since CUPS 1.2/Mac OS X 10.5@
107 int /* O - Length in bytes */
108 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
114 if (addr
->addr
.sa_family
== AF_INET6
)
115 return (sizeof(addr
->ipv6
));
117 #endif /* AF_INET6 */
119 if (addr
->addr
.sa_family
== AF_LOCAL
)
120 return (offsetof(struct sockaddr_un
, sun_path
) +
121 strlen(addr
->un
.sun_path
) + 1);
123 #endif /* AF_LOCAL */
124 if (addr
->addr
.sa_family
== AF_INET
)
125 return (sizeof(addr
->ipv4
));
133 * 'httpAddrLocalhost()' - Check for the local loopback address.
135 * @since CUPS 1.2/Mac OS X 10.5@
138 int /* O - 1 if local host, 0 otherwise */
140 const http_addr_t
*addr
) /* I - Address to check */
146 if (addr
->addr
.sa_family
== AF_INET6
&&
147 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
149 #endif /* AF_INET6 */
152 if (addr
->addr
.sa_family
== AF_LOCAL
)
154 #endif /* AF_LOCAL */
156 if (addr
->addr
.sa_family
== AF_INET
&&
157 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x7f000001)
165 # define ADDR_CAST (struct sockaddr *)
167 # define ADDR_CAST (char *)
172 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
174 * @since CUPS 1.2/Mac OS X 10.5@
177 char * /* O - Host name */
179 const http_addr_t
*addr
, /* I - Address to lookup */
180 char *name
, /* I - Host name buffer */
181 int namelen
) /* I - Size of name buffer */
183 _cups_globals_t
*cg
= _cupsGlobals();
187 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)\n",
188 addr
, name
, namelen
));
191 * Range check input...
194 if (!addr
|| !name
|| namelen
<= 2)
196 if (name
&& namelen
>= 1)
203 if (addr
->addr
.sa_family
== AF_LOCAL
)
205 strlcpy(name
, addr
->un
.sun_path
, namelen
);
208 #endif /* AF_LOCAL */
211 * Optimize lookups for localhost/loopback addresses...
214 if (httpAddrLocalhost(addr
))
216 strlcpy(name
, "localhost", namelen
);
222 * STR #2920: Initialize resolver after failure in cups-polld
224 * If the previous lookup failed, re-initialize the resolver to prevent
225 * temporary network errors from persisting. This *should* be handled by
226 * the resolver libraries, but apparently the glibc folks do not agree.
228 * We set a flag at the end of this function if we encounter an error that
229 * requires reinitialization of the resolver functions. We then call
230 * res_init() if the flag is set on the next call here or in httpAddrLookup().
233 if (cg
->need_res_init
)
237 cg
->need_res_init
= 0;
239 #endif /* HAVE_RES_INIT */
241 #ifdef HAVE_GETNAMEINFO
244 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
246 * FWIW, I think this is really a bug in the implementation of
247 * getnameinfo(), but falling back on httpAddrString() is easy to
251 int error
= getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
256 if (error
== EAI_FAIL
)
257 cg
->need_res_init
= 1;
259 return (httpAddrString(addr
, name
, namelen
));
264 struct hostent
*host
; /* Host from name service */
268 if (addr
->addr
.sa_family
== AF_INET6
)
269 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
270 sizeof(struct in_addr
), AF_INET6
);
272 # endif /* AF_INET6 */
273 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
274 sizeof(struct in_addr
), AF_INET
);
279 * No hostname, so return the raw address...
282 if (h_errno
== NO_RECOVERY
)
283 cg
->need_res_init
= 1;
285 return (httpAddrString(addr
, name
, namelen
));
288 strlcpy(name
, host
->h_name
, namelen
);
290 #endif /* HAVE_GETNAMEINFO */
297 * '_httpAddrPort()' - Get the port number associated with an address.
300 int /* O - Port number */
301 _httpAddrPort(http_addr_t
*addr
) /* I - Address */
306 else if (addr
->addr
.sa_family
== AF_INET6
)
307 return (ntohs(addr
->ipv6
.sin6_port
));
308 #endif /* AF_INET6 */
309 else if (addr
->addr
.sa_family
== AF_INET
)
310 return (ntohs(addr
->ipv4
.sin_port
));
317 * 'httpAddrString()' - Convert an address to a numeric string.
319 * @since CUPS 1.2/Mac OS X 10.5@
322 char * /* O - Numeric address string */
323 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
324 char *s
, /* I - String buffer */
325 int slen
) /* I - Length of string */
327 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
331 * Range check input...
334 if (!addr
|| !s
|| slen
<= 2)
343 if (addr
->addr
.sa_family
== AF_LOCAL
)
344 strlcpy(s
, addr
->un
.sun_path
, slen
);
346 #endif /* AF_LOCAL */
347 if (addr
->addr
.sa_family
== AF_INET
)
349 unsigned temp
; /* Temporary address */
352 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
354 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
355 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
358 else if (addr
->addr
.sa_family
== AF_INET6
)
360 # ifdef HAVE_GETNAMEINFO
361 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
362 NULL
, 0, NI_NUMERICHOST
))
365 * If we get an error back, then the address type is not supported
366 * and we should zero out the buffer...
374 char *sptr
; /* Pointer into string */
375 int i
; /* Looping var */
376 unsigned temp
; /* Current value */
377 const char *prefix
; /* Prefix for address */
381 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
383 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
385 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
387 slen
-= strlen(sptr
);
388 sptr
+= strlen(sptr
);
392 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
394 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
395 slen
-= strlen(sptr
);
396 sptr
+= strlen(sptr
);
402 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
407 snprintf(sptr
, slen
, "%s:", prefix
);
409 slen
-= strlen(sptr
);
410 sptr
+= strlen(sptr
);
414 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
416 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
418 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
419 slen
-= strlen(sptr
);
420 sptr
+= strlen(sptr
);
423 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
424 slen
-= strlen(sptr
);
425 sptr
+= strlen(sptr
);
434 strlcpy(s
, "::", slen
);
444 strlcpy(sptr
, "::", slen
);
449 # endif /* HAVE_GETNAMEINFO */
451 #endif /* AF_INET6 */
453 strlcpy(s
, "UNKNOWN", slen
);
455 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
462 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
463 * address records for the specified name.
468 struct hostent
* /* O - Host entry */
469 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
471 const char *nameptr
; /* Pointer into name */
472 unsigned ip
[4]; /* IP address components */
473 _cups_globals_t
*cg
= _cupsGlobals();
474 /* Pointer to library globals */
477 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
480 * Avoid lookup delays and configuration problems when connecting
481 * to the localhost address...
484 if (!strcmp(name
, "localhost"))
488 * This function is needed because some operating systems have a
489 * buggy implementation of gethostbyname() that does not support
490 * IP addresses. If the first character of the name string is a
491 * number, then sscanf() is used to extract the IP components.
492 * We then pack the components into an IPv4 address manually,
493 * since the inet_aton() function is deprecated. We use the
494 * htonl() macro to get the right byte order for the address.
496 * We also support domain sockets when supported by the underlying
504 * A domain socket address, so make an AF_LOCAL entry and return it...
507 cg
->hostent
.h_name
= (char *)name
;
508 cg
->hostent
.h_aliases
= NULL
;
509 cg
->hostent
.h_addrtype
= AF_LOCAL
;
510 cg
->hostent
.h_length
= strlen(name
) + 1;
511 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
512 cg
->ip_ptrs
[0] = (char *)name
;
513 cg
->ip_ptrs
[1] = NULL
;
515 DEBUG_puts("httpGetHostByName: returning domain socket address...");
517 return (&cg
->hostent
);
519 #endif /* AF_LOCAL */
521 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
526 * We have an IPv4 address; break it up and provide the host entry
530 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
531 return (NULL
); /* Must have 4 numbers */
533 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
534 return (NULL
); /* Invalid byte ranges! */
536 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
540 * Fill in the host entry and return it...
543 cg
->hostent
.h_name
= (char *)name
;
544 cg
->hostent
.h_aliases
= NULL
;
545 cg
->hostent
.h_addrtype
= AF_INET
;
546 cg
->hostent
.h_length
= 4;
547 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
548 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
549 cg
->ip_ptrs
[1] = NULL
;
551 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
553 return (&cg
->hostent
);
558 * Use the gethostbyname() function to get the IPv4 address for
562 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
564 return (gethostbyname(name
));
570 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
572 * When "http" points to a connected socket, return the hostname or
573 * address that was used in the call to httpConnect() or httpConnectEncrypt().
574 * Otherwise, return the FQDN for the local system using both gethostname()
575 * and gethostbyname() to get the local hostname with domain.
577 * @since CUPS 1.2/Mac OS X 10.5@
580 const char * /* O - FQDN for connection or system */
581 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
582 char *s
, /* I - String buffer for name */
583 int slen
) /* I - Size of buffer */
585 struct hostent
*host
; /* Host entry to get FQDN */
593 if (http
->hostname
[0] == '/')
594 strlcpy(s
, "localhost", slen
);
596 strlcpy(s
, http
->hostname
, slen
);
601 * Get the hostname...
604 if (gethostname(s
, slen
) < 0)
605 strlcpy(s
, "localhost", slen
);
610 * The hostname is not a FQDN, so look it up...
613 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
614 strlcpy(s
, host
->h_name
, slen
);
619 * Return the hostname with as much domain info as we have...
627 * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $".