]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * "$Id: http-addr.c 6649 2007-07-11 21:46:42Z 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
200 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
204 * If we get an error back, then the address type is not supported
205 * and we should zero out the buffer...
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 * 'httpAddrString()' - Convert an address to a numeric string.
251 char * /* O - Numeric address string */
252 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
253 char *s
, /* I - String buffer */
254 int slen
) /* I - Length of string */
256 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
260 * Range check input...
263 if (!addr
|| !s
|| slen
<= 2)
272 if (addr
->addr
.sa_family
== AF_LOCAL
)
273 strlcpy(s
, addr
->un
.sun_path
, slen
);
275 #endif /* AF_LOCAL */
276 if (addr
->addr
.sa_family
== AF_INET
)
278 unsigned temp
; /* Temporary address */
281 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
283 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
284 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
287 else if (addr
->addr
.sa_family
== AF_INET6
)
289 # ifdef HAVE_GETNAMEINFO
290 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
291 NULL
, 0, NI_NUMERICHOST
))
294 * If we get an error back, then the address type is not supported
295 * and we should zero out the buffer...
303 char *sptr
; /* Pointer into string */
304 int i
; /* Looping var */
305 unsigned temp
; /* Current value */
306 const char *prefix
; /* Prefix for address */
310 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
312 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
314 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
316 slen
-= strlen(sptr
);
317 sptr
+= strlen(sptr
);
321 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
323 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
324 slen
-= strlen(sptr
);
325 sptr
+= strlen(sptr
);
331 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
336 snprintf(sptr
, slen
, "%s:", prefix
);
338 slen
-= strlen(sptr
);
339 sptr
+= strlen(sptr
);
343 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
345 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
347 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
348 slen
-= strlen(sptr
);
349 sptr
+= strlen(sptr
);
352 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
353 slen
-= strlen(sptr
);
354 sptr
+= strlen(sptr
);
363 strlcpy(s
, "::", slen
);
373 strlcpy(sptr
, "::", slen
);
378 # endif /* HAVE_GETNAMEINFO */
380 #endif /* AF_INET6 */
382 strlcpy(s
, "UNKNOWN", slen
);
384 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
391 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
392 * address records for the specified name.
397 struct hostent
* /* O - Host entry */
398 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
400 const char *nameptr
; /* Pointer into name */
401 unsigned ip
[4]; /* IP address components */
402 _cups_globals_t
*cg
= _cupsGlobals();
403 /* Pointer to library globals */
406 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
409 * Avoid lookup delays and configuration problems when connecting
410 * to the localhost address...
413 if (!strcmp(name
, "localhost"))
417 * This function is needed because some operating systems have a
418 * buggy implementation of gethostbyname() that does not support
419 * IP addresses. If the first character of the name string is a
420 * number, then sscanf() is used to extract the IP components.
421 * We then pack the components into an IPv4 address manually,
422 * since the inet_aton() function is deprecated. We use the
423 * htonl() macro to get the right byte order for the address.
425 * We also support domain sockets when supported by the underlying
433 * A domain socket address, so make an AF_LOCAL entry and return it...
436 cg
->hostent
.h_name
= (char *)name
;
437 cg
->hostent
.h_aliases
= NULL
;
438 cg
->hostent
.h_addrtype
= AF_LOCAL
;
439 cg
->hostent
.h_length
= strlen(name
) + 1;
440 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
441 cg
->ip_ptrs
[0] = (char *)name
;
442 cg
->ip_ptrs
[1] = NULL
;
444 DEBUG_puts("httpGetHostByName: returning domain socket address...");
446 return (&cg
->hostent
);
448 #endif /* AF_LOCAL */
450 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
455 * We have an IPv4 address; break it up and provide the host entry
459 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
460 return (NULL
); /* Must have 4 numbers */
462 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
463 return (NULL
); /* Invalid byte ranges! */
465 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
469 * Fill in the host entry and return it...
472 cg
->hostent
.h_name
= (char *)name
;
473 cg
->hostent
.h_aliases
= NULL
;
474 cg
->hostent
.h_addrtype
= AF_INET
;
475 cg
->hostent
.h_length
= 4;
476 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
477 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
478 cg
->ip_ptrs
[1] = NULL
;
480 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
482 return (&cg
->hostent
);
487 * Use the gethostbyname() function to get the IPv4 address for
491 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
493 return (gethostbyname(name
));
499 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
501 * When "http" points to a connected socket, return the hostname or
502 * address that was used in the call to httpConnect() or httpConnectEncrypt().
503 * Otherwise, return the FQDN for the local system using both gethostname()
504 * and gethostbyname() to get the local hostname with domain.
509 const char * /* O - FQDN for connection or system */
510 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
511 char *s
, /* I - String buffer for name */
512 int slen
) /* I - Size of buffer */
514 struct hostent
*host
; /* Host entry to get FQDN */
522 if (http
->hostname
[0] == '/')
523 strlcpy(s
, "localhost", slen
);
525 strlcpy(s
, http
->hostname
, slen
);
530 * Get the hostname...
533 if (gethostname(s
, slen
) < 0)
534 strlcpy(s
, "localhost", slen
);
539 * The hostname is not a FQDN, so look it up...
542 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
543 strlcpy(s
, host
->h_name
, slen
);
548 * Return the hostname with as much domain info as we have...
556 * End of "$Id: http-addr.c 6649 2007-07-11 21:46:42Z mike $".