]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * "$Id: http-addr.c 6816 2007-08-20 20:16:00Z mike $"
4 * HTTP address routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 * httpAddrString() - Convert an IP address to a dotted string.
22 * httpGetHostByName() - Lookup a hostname or IP address, and return
23 * address records for the specified name.
24 * httpGetHostname() - Get the FQDN for the local system.
28 * Include necessary headers...
38 * 'httpAddrAny()' - Check for the "any" address.
43 int /* O - 1 if "any", 0 otherwise */
44 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
50 if (addr
->addr
.sa_family
== AF_INET6
&&
51 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
55 if (addr
->addr
.sa_family
== AF_INET
&&
56 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
64 * 'httpAddrEqual()' - Compare two addresses.
69 int /* O - 1 if equal, 0 if not */
70 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
71 const http_addr_t
*addr2
) /* I - Second address */
79 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
83 if (addr1
->addr
.sa_family
== AF_LOCAL
)
84 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
88 if (addr1
->addr
.sa_family
== AF_INET6
)
89 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
92 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
97 * 'httpAddrLength()' - Return the length of the address in bytes.
102 int /* O - Length in bytes */
103 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
109 if (addr
->addr
.sa_family
== AF_INET6
)
110 return (sizeof(addr
->ipv6
));
112 #endif /* AF_INET6 */
114 if (addr
->addr
.sa_family
== AF_LOCAL
)
115 return (offsetof(struct sockaddr_un
, sun_path
) +
116 strlen(addr
->un
.sun_path
) + 1);
118 #endif /* AF_LOCAL */
119 if (addr
->addr
.sa_family
== AF_INET
)
120 return (sizeof(addr
->ipv4
));
128 * 'httpAddrLocalhost()' - Check for the local loopback address.
133 int /* O - 1 if local host, 0 otherwise */
135 const http_addr_t
*addr
) /* I - Address to check */
141 if (addr
->addr
.sa_family
== AF_INET6
&&
142 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
144 #endif /* AF_INET6 */
147 if (addr
->addr
.sa_family
== AF_LOCAL
)
149 #endif /* AF_LOCAL */
151 if (addr
->addr
.sa_family
== AF_INET
&&
152 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x7f000001)
160 # define ADDR_CAST (struct sockaddr *)
162 # define ADDR_CAST (char *)
167 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
172 char * /* O - Host name */
174 const http_addr_t
*addr
, /* I - Address to lookup */
175 char *name
, /* I - Host name buffer */
176 int namelen
) /* I - Size of name buffer */
178 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)\n",
179 addr
, name
, namelen
));
182 * Range check input...
185 if (!addr
|| !name
|| namelen
<= 2)
187 if (name
&& namelen
>= 1)
194 if (addr
->addr
.sa_family
== AF_LOCAL
)
195 strlcpy(name
, addr
->un
.sun_path
, namelen
);
197 #endif /* AF_LOCAL */
198 #ifdef HAVE_GETNAMEINFO
201 * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN
203 * FWIW, I think this is really a bug in the implementation of
204 * getnameinfo(), but falling back on httpAddrString() is easy to
208 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
210 return (httpAddrString(addr
, name
, namelen
));
214 struct hostent
*host
; /* Host from name service */
218 if (addr
->addr
.sa_family
== AF_INET6
)
219 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
220 sizeof(struct in_addr
), AF_INET6
);
222 # endif /* AF_INET6 */
223 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
224 sizeof(struct in_addr
), AF_INET
);
229 * No hostname, so return the raw address...
232 httpAddrString(addr
, name
, namelen
);
236 strlcpy(name
, host
->h_name
, namelen
);
238 #endif /* HAVE_GETNAMEINFO */
245 * 'httpAddrString()' - Convert an address to a numeric string.
250 char * /* O - Numeric address string */
251 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
252 char *s
, /* I - String buffer */
253 int slen
) /* I - Length of string */
255 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
259 * Range check input...
262 if (!addr
|| !s
|| slen
<= 2)
271 if (addr
->addr
.sa_family
== AF_LOCAL
)
272 strlcpy(s
, addr
->un
.sun_path
, slen
);
274 #endif /* AF_LOCAL */
275 if (addr
->addr
.sa_family
== AF_INET
)
277 unsigned temp
; /* Temporary address */
280 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
282 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
283 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
286 else if (addr
->addr
.sa_family
== AF_INET6
)
288 # ifdef HAVE_GETNAMEINFO
289 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
290 NULL
, 0, NI_NUMERICHOST
))
293 * If we get an error back, then the address type is not supported
294 * and we should zero out the buffer...
302 char *sptr
; /* Pointer into string */
303 int i
; /* Looping var */
304 unsigned temp
; /* Current value */
305 const char *prefix
; /* Prefix for address */
309 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
311 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
313 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
315 slen
-= strlen(sptr
);
316 sptr
+= strlen(sptr
);
320 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
322 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
323 slen
-= strlen(sptr
);
324 sptr
+= strlen(sptr
);
330 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
335 snprintf(sptr
, slen
, "%s:", prefix
);
337 slen
-= strlen(sptr
);
338 sptr
+= strlen(sptr
);
342 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
344 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
346 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
347 slen
-= strlen(sptr
);
348 sptr
+= strlen(sptr
);
351 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
352 slen
-= strlen(sptr
);
353 sptr
+= strlen(sptr
);
362 strlcpy(s
, "::", slen
);
372 strlcpy(sptr
, "::", slen
);
377 # endif /* HAVE_GETNAMEINFO */
379 #endif /* AF_INET6 */
381 strlcpy(s
, "UNKNOWN", slen
);
383 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
390 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
391 * address records for the specified name.
396 struct hostent
* /* O - Host entry */
397 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
399 const char *nameptr
; /* Pointer into name */
400 unsigned ip
[4]; /* IP address components */
401 _cups_globals_t
*cg
= _cupsGlobals();
402 /* Pointer to library globals */
405 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
408 * Avoid lookup delays and configuration problems when connecting
409 * to the localhost address...
412 if (!strcmp(name
, "localhost"))
416 * This function is needed because some operating systems have a
417 * buggy implementation of gethostbyname() that does not support
418 * IP addresses. If the first character of the name string is a
419 * number, then sscanf() is used to extract the IP components.
420 * We then pack the components into an IPv4 address manually,
421 * since the inet_aton() function is deprecated. We use the
422 * htonl() macro to get the right byte order for the address.
424 * We also support domain sockets when supported by the underlying
432 * A domain socket address, so make an AF_LOCAL entry and return it...
435 cg
->hostent
.h_name
= (char *)name
;
436 cg
->hostent
.h_aliases
= NULL
;
437 cg
->hostent
.h_addrtype
= AF_LOCAL
;
438 cg
->hostent
.h_length
= strlen(name
) + 1;
439 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
440 cg
->ip_ptrs
[0] = (char *)name
;
441 cg
->ip_ptrs
[1] = NULL
;
443 DEBUG_puts("httpGetHostByName: returning domain socket address...");
445 return (&cg
->hostent
);
447 #endif /* AF_LOCAL */
449 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
454 * We have an IPv4 address; break it up and provide the host entry
458 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
459 return (NULL
); /* Must have 4 numbers */
461 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
462 return (NULL
); /* Invalid byte ranges! */
464 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
468 * Fill in the host entry and return it...
471 cg
->hostent
.h_name
= (char *)name
;
472 cg
->hostent
.h_aliases
= NULL
;
473 cg
->hostent
.h_addrtype
= AF_INET
;
474 cg
->hostent
.h_length
= 4;
475 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
476 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
477 cg
->ip_ptrs
[1] = NULL
;
479 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
481 return (&cg
->hostent
);
486 * Use the gethostbyname() function to get the IPv4 address for
490 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
492 return (gethostbyname(name
));
498 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
500 * When "http" points to a connected socket, return the hostname or
501 * address that was used in the call to httpConnect() or httpConnectEncrypt().
502 * Otherwise, return the FQDN for the local system using both gethostname()
503 * and gethostbyname() to get the local hostname with domain.
508 const char * /* O - FQDN for connection or system */
509 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
510 char *s
, /* I - String buffer for name */
511 int slen
) /* I - Size of buffer */
513 struct hostent
*host
; /* Host entry to get FQDN */
521 if (http
->hostname
[0] == '/')
522 strlcpy(s
, "localhost", slen
);
524 strlcpy(s
, http
->hostname
, slen
);
529 * Get the hostname...
532 if (gethostname(s
, slen
) < 0)
533 strlcpy(s
, "localhost", slen
);
538 * The hostname is not a FQDN, so look it up...
541 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
542 strlcpy(s
, host
->h_name
, slen
);
547 * Return the hostname with as much domain info as we have...
555 * End of "$Id: http-addr.c 6816 2007-08-20 20:16:00Z mike $".