]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * "$Id: http-addr.c 6814 2007-08-20 20:09:25Z mike $"
4 * HTTP address routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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.
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.
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.
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.
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.
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 */
212 * STR #2920: Initialize resolver after failure in cups-polld
214 * If the previous lookup failed, re-initialize the resolver to prevent
215 * temporary network errors from persisting. This *should* be handled by
216 * the resolver libraries, but apparently the glibc folks do not agree.
218 * We set a flag at the end of this function if we encounter an error that
219 * requires reinitialization of the resolver functions. We then call
220 * res_init() if the flag is set on the next call here or in httpAddrLookup().
223 if (cg
->need_res_init
)
227 cg
->need_res_init
= 0;
229 #endif /* HAVE_RES_INIT */
231 #ifdef HAVE_GETNAMEINFO
234 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
236 * FWIW, I think this is really a bug in the implementation of
237 * getnameinfo(), but falling back on httpAddrString() is easy to
241 int error
= getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
246 if (error
== EAI_FAIL
)
247 cg
->need_res_init
= 1;
249 return (httpAddrString(addr
, name
, namelen
));
254 struct hostent
*host
; /* Host from name service */
258 if (addr
->addr
.sa_family
== AF_INET6
)
259 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
260 sizeof(struct in_addr
), AF_INET6
);
262 # endif /* AF_INET6 */
263 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
264 sizeof(struct in_addr
), AF_INET
);
269 * No hostname, so return the raw address...
272 if (h_errno
== NO_RECOVERY
)
273 cg
->need_res_init
= 1;
275 return (httpAddrString(addr
, name
, namelen
));
278 strlcpy(name
, host
->h_name
, namelen
);
280 #endif /* HAVE_GETNAMEINFO */
287 * '_httpAddrPort()' - Get the port number associated with an address.
290 int /* O - Port number */
291 _httpAddrPort(http_addr_t
*addr
) /* I - Address */
296 else if (addr
->addr
.sa_family
== AF_INET6
)
297 return (ntohs(addr
->ipv6
.sin6_port
));
298 #endif /* AF_INET6 */
299 else if (addr
->addr
.sa_family
== AF_INET
)
300 return (ntohs(addr
->ipv4
.sin_port
));
307 * 'httpAddrString()' - Convert an address to a numeric string.
312 char * /* O - Numeric address string */
313 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
314 char *s
, /* I - String buffer */
315 int slen
) /* I - Length of string */
317 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
321 * Range check input...
324 if (!addr
|| !s
|| slen
<= 2)
333 if (addr
->addr
.sa_family
== AF_LOCAL
)
334 strlcpy(s
, addr
->un
.sun_path
, slen
);
336 #endif /* AF_LOCAL */
337 if (addr
->addr
.sa_family
== AF_INET
)
339 unsigned temp
; /* Temporary address */
342 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
344 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
345 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
348 else if (addr
->addr
.sa_family
== AF_INET6
)
350 # ifdef HAVE_GETNAMEINFO
351 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
352 NULL
, 0, NI_NUMERICHOST
))
355 * If we get an error back, then the address type is not supported
356 * and we should zero out the buffer...
364 char *sptr
; /* Pointer into string */
365 int i
; /* Looping var */
366 unsigned temp
; /* Current value */
367 const char *prefix
; /* Prefix for address */
371 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
373 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
375 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
377 slen
-= strlen(sptr
);
378 sptr
+= strlen(sptr
);
382 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
384 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
385 slen
-= strlen(sptr
);
386 sptr
+= strlen(sptr
);
392 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
397 snprintf(sptr
, slen
, "%s:", prefix
);
399 slen
-= strlen(sptr
);
400 sptr
+= strlen(sptr
);
404 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
406 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
408 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
409 slen
-= strlen(sptr
);
410 sptr
+= strlen(sptr
);
413 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
414 slen
-= strlen(sptr
);
415 sptr
+= strlen(sptr
);
424 strlcpy(s
, "::", slen
);
434 strlcpy(sptr
, "::", slen
);
439 # endif /* HAVE_GETNAMEINFO */
441 #endif /* AF_INET6 */
443 strlcpy(s
, "UNKNOWN", slen
);
445 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
452 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
453 * address records for the specified name.
458 struct hostent
* /* O - Host entry */
459 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
461 const char *nameptr
; /* Pointer into name */
462 unsigned ip
[4]; /* IP address components */
463 _cups_globals_t
*cg
= _cupsGlobals();
464 /* Pointer to library globals */
467 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
470 * Avoid lookup delays and configuration problems when connecting
471 * to the localhost address...
474 if (!strcmp(name
, "localhost"))
478 * This function is needed because some operating systems have a
479 * buggy implementation of gethostbyname() that does not support
480 * IP addresses. If the first character of the name string is a
481 * number, then sscanf() is used to extract the IP components.
482 * We then pack the components into an IPv4 address manually,
483 * since the inet_aton() function is deprecated. We use the
484 * htonl() macro to get the right byte order for the address.
486 * We also support domain sockets when supported by the underlying
494 * A domain socket address, so make an AF_LOCAL entry and return it...
497 cg
->hostent
.h_name
= (char *)name
;
498 cg
->hostent
.h_aliases
= NULL
;
499 cg
->hostent
.h_addrtype
= AF_LOCAL
;
500 cg
->hostent
.h_length
= strlen(name
) + 1;
501 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
502 cg
->ip_ptrs
[0] = (char *)name
;
503 cg
->ip_ptrs
[1] = NULL
;
505 DEBUG_puts("httpGetHostByName: returning domain socket address...");
507 return (&cg
->hostent
);
509 #endif /* AF_LOCAL */
511 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
516 * We have an IPv4 address; break it up and provide the host entry
520 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
521 return (NULL
); /* Must have 4 numbers */
523 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
524 return (NULL
); /* Invalid byte ranges! */
526 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
530 * Fill in the host entry and return it...
533 cg
->hostent
.h_name
= (char *)name
;
534 cg
->hostent
.h_aliases
= NULL
;
535 cg
->hostent
.h_addrtype
= AF_INET
;
536 cg
->hostent
.h_length
= 4;
537 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
538 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
539 cg
->ip_ptrs
[1] = NULL
;
541 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
543 return (&cg
->hostent
);
548 * Use the gethostbyname() function to get the IPv4 address for
552 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
554 return (gethostbyname(name
));
560 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
562 * When "http" points to a connected socket, return the hostname or
563 * address that was used in the call to httpConnect() or httpConnectEncrypt().
564 * Otherwise, return the FQDN for the local system using both gethostname()
565 * and gethostbyname() to get the local hostname with domain.
570 const char * /* O - FQDN for connection or system */
571 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
572 char *s
, /* I - String buffer for name */
573 int slen
) /* I - Size of buffer */
575 struct hostent
*host
; /* Host entry to get FQDN */
583 if (http
->hostname
[0] == '/')
584 strlcpy(s
, "localhost", slen
);
586 strlcpy(s
, http
->hostname
, slen
);
591 * Get the hostname...
594 if (gethostname(s
, slen
) < 0)
595 strlcpy(s
, "localhost", slen
);
600 * The hostname is not a FQDN, so look it up...
603 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
604 strlcpy(s
, host
->h_name
, slen
);
609 * Return the hostname with as much domain info as we have...
617 * End of "$Id: http-addr.c 6814 2007-08-20 20:09:25Z mike $".