]>
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...
39 * 'httpAddrAny()' - Check for the "any" address.
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.
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.
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.
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
) == 0x7f000001)
161 # define ADDR_CAST (struct sockaddr *)
163 # define ADDR_CAST (char *)
168 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
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 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)\n",
180 addr
, name
, namelen
));
183 * Range check input...
186 if (!addr
|| !name
|| namelen
<= 2)
188 if (name
&& namelen
>= 1)
195 if (addr
->addr
.sa_family
== AF_LOCAL
)
196 strlcpy(name
, addr
->un
.sun_path
, namelen
);
198 #endif /* AF_LOCAL */
199 #ifdef HAVE_GETNAMEINFO
202 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
204 * FWIW, I think this is really a bug in the implementation of
205 * getnameinfo(), but falling back on httpAddrString() is easy to
209 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
211 return (httpAddrString(addr
, name
, namelen
));
215 struct hostent
*host
; /* Host from name service */
219 if (addr
->addr
.sa_family
== AF_INET6
)
220 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
221 sizeof(struct in_addr
), AF_INET6
);
223 # endif /* AF_INET6 */
224 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
225 sizeof(struct in_addr
), AF_INET
);
230 * No hostname, so return the raw address...
233 httpAddrString(addr
, name
, namelen
);
237 strlcpy(name
, host
->h_name
, namelen
);
239 #endif /* HAVE_GETNAMEINFO */
246 * '_httpAddrPort()' - Get the port number associated with an address.
249 int /* O - Port number */
250 _httpAddrPort(http_addr_t
*addr
) /* I - Address */
255 else if (addr
->addr
.sa_family
== AF_INET6
)
256 return (ntohs(addr
->ipv6
.sin6_port
));
257 #endif /* AF_INET6 */
258 else if (addr
->addr
.sa_family
== AF_INET
)
259 return (ntohs(addr
->ipv4
.sin_port
));
266 * 'httpAddrString()' - Convert an address to a numeric string.
271 char * /* O - Numeric address string */
272 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
273 char *s
, /* I - String buffer */
274 int slen
) /* I - Length of string */
276 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
280 * Range check input...
283 if (!addr
|| !s
|| slen
<= 2)
292 if (addr
->addr
.sa_family
== AF_LOCAL
)
293 strlcpy(s
, addr
->un
.sun_path
, slen
);
295 #endif /* AF_LOCAL */
296 if (addr
->addr
.sa_family
== AF_INET
)
298 unsigned temp
; /* Temporary address */
301 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
303 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
304 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
307 else if (addr
->addr
.sa_family
== AF_INET6
)
309 # ifdef HAVE_GETNAMEINFO
310 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
311 NULL
, 0, NI_NUMERICHOST
))
314 * If we get an error back, then the address type is not supported
315 * and we should zero out the buffer...
323 char *sptr
; /* Pointer into string */
324 int i
; /* Looping var */
325 unsigned temp
; /* Current value */
326 const char *prefix
; /* Prefix for address */
330 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
332 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
334 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
336 slen
-= strlen(sptr
);
337 sptr
+= strlen(sptr
);
341 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
343 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
344 slen
-= strlen(sptr
);
345 sptr
+= strlen(sptr
);
351 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
356 snprintf(sptr
, slen
, "%s:", prefix
);
358 slen
-= strlen(sptr
);
359 sptr
+= strlen(sptr
);
363 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
365 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
367 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
368 slen
-= strlen(sptr
);
369 sptr
+= strlen(sptr
);
372 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
373 slen
-= strlen(sptr
);
374 sptr
+= strlen(sptr
);
383 strlcpy(s
, "::", slen
);
393 strlcpy(sptr
, "::", slen
);
398 # endif /* HAVE_GETNAMEINFO */
400 #endif /* AF_INET6 */
402 strlcpy(s
, "UNKNOWN", slen
);
404 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
411 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
412 * address records for the specified name.
417 struct hostent
* /* O - Host entry */
418 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
420 const char *nameptr
; /* Pointer into name */
421 unsigned ip
[4]; /* IP address components */
422 _cups_globals_t
*cg
= _cupsGlobals();
423 /* Pointer to library globals */
426 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
429 * Avoid lookup delays and configuration problems when connecting
430 * to the localhost address...
433 if (!strcmp(name
, "localhost"))
437 * This function is needed because some operating systems have a
438 * buggy implementation of gethostbyname() that does not support
439 * IP addresses. If the first character of the name string is a
440 * number, then sscanf() is used to extract the IP components.
441 * We then pack the components into an IPv4 address manually,
442 * since the inet_aton() function is deprecated. We use the
443 * htonl() macro to get the right byte order for the address.
445 * We also support domain sockets when supported by the underlying
453 * A domain socket address, so make an AF_LOCAL entry and return it...
456 cg
->hostent
.h_name
= (char *)name
;
457 cg
->hostent
.h_aliases
= NULL
;
458 cg
->hostent
.h_addrtype
= AF_LOCAL
;
459 cg
->hostent
.h_length
= strlen(name
) + 1;
460 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
461 cg
->ip_ptrs
[0] = (char *)name
;
462 cg
->ip_ptrs
[1] = NULL
;
464 DEBUG_puts("httpGetHostByName: returning domain socket address...");
466 return (&cg
->hostent
);
468 #endif /* AF_LOCAL */
470 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
475 * We have an IPv4 address; break it up and provide the host entry
479 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
480 return (NULL
); /* Must have 4 numbers */
482 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
483 return (NULL
); /* Invalid byte ranges! */
485 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
489 * Fill in the host entry and return it...
492 cg
->hostent
.h_name
= (char *)name
;
493 cg
->hostent
.h_aliases
= NULL
;
494 cg
->hostent
.h_addrtype
= AF_INET
;
495 cg
->hostent
.h_length
= 4;
496 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
497 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
498 cg
->ip_ptrs
[1] = NULL
;
500 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
502 return (&cg
->hostent
);
507 * Use the gethostbyname() function to get the IPv4 address for
511 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
513 return (gethostbyname(name
));
519 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
521 * When "http" points to a connected socket, return the hostname or
522 * address that was used in the call to httpConnect() or httpConnectEncrypt().
523 * Otherwise, return the FQDN for the local system using both gethostname()
524 * and gethostbyname() to get the local hostname with domain.
529 const char * /* O - FQDN for connection or system */
530 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
531 char *s
, /* I - String buffer for name */
532 int slen
) /* I - Size of buffer */
534 struct hostent
*host
; /* Host entry to get FQDN */
542 if (http
->hostname
[0] == '/')
543 strlcpy(s
, "localhost", slen
);
545 strlcpy(s
, http
->hostname
, slen
);
550 * Get the hostname...
553 if (gethostname(s
, slen
) < 0)
554 strlcpy(s
, "localhost", slen
);
559 * The hostname is not a FQDN, so look it up...
562 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
563 strlcpy(s
, host
->h_name
, slen
);
568 * Return the hostname with as much domain info as we have...
576 * End of "$Id: http-addr.c 6814 2007-08-20 20:09:25Z mike $".