]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
170558e73f8052aa768dafc471401a7d8470f842
4 * HTTP address routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * httpAddrAny() - Check for the "any" address.
27 * httpAddrEqual() - Compare two addresses.
28 * httpAddrLocalhost() - Check for the local loopback address.
29 * httpAddrLookup() - Lookup the hostname associated with the address.
30 * httpAddrString() - Convert an IP address to a dotted string.
31 * httpGetHostByName() - Lookup a hostname or IP address, and return
32 * address records for the specified name.
33 * httpGetHostname() - Get the FQDN for the local system.
37 * Include necessary headers...
47 * 'httpAddrAny()' - Check for the "any" address.
52 int /* O - 1 if "any", 0 otherwise */
53 httpAddrAny(const http_addr_t
*addr
) /* I - Address to check */
59 if (addr
->addr
.sa_family
== AF_INET6
&&
60 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
64 if (addr
->addr
.sa_family
== AF_INET
&&
65 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
73 * 'httpAddrEqual()' - Compare two addresses.
78 int /* O - 1 if equal, 0 if not */
79 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
80 const http_addr_t
*addr2
) /* I - Second address */
88 if (addr1
->addr
.sa_family
!= addr2
->addr
.sa_family
)
92 if (addr1
->addr
.sa_family
== AF_LOCAL
)
93 return (!strcmp(addr1
->un
.sun_path
, addr2
->un
.sun_path
));
97 if (addr1
->addr
.sa_family
== AF_INET6
)
98 return (!memcmp(&(addr1
->ipv6
.sin6_addr
), &(addr2
->ipv6
.sin6_addr
), 16));
101 return (addr1
->ipv4
.sin_addr
.s_addr
== addr2
->ipv4
.sin_addr
.s_addr
);
106 * 'httpAddrLength()' - Return the length of the address in bytes.
111 int /* O - Length in bytes */
112 httpAddrLength(const http_addr_t
*addr
) /* I - Address */
118 if (addr
->addr
.sa_family
== AF_INET6
)
119 return (sizeof(addr
->ipv6
));
121 #endif /* AF_INET6 */
123 if (addr
->addr
.sa_family
== AF_LOCAL
)
124 return (offsetof(struct sockaddr_un
, sun_path
) +
125 strlen(addr
->un
.sun_path
) + 1);
127 #endif /* AF_LOCAL */
128 if (addr
->addr
.sa_family
== AF_INET
)
129 return (sizeof(addr
->ipv4
));
137 * 'httpAddrLocalhost()' - Check for the local loopback address.
142 int /* O - 1 if local host, 0 otherwise */
144 const http_addr_t
*addr
) /* I - Address to check */
150 if (addr
->addr
.sa_family
== AF_INET6
&&
151 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
153 #endif /* AF_INET6 */
156 if (addr
->addr
.sa_family
== AF_LOCAL
)
158 #endif /* AF_LOCAL */
160 if (addr
->addr
.sa_family
== AF_INET
&&
161 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x7f000001)
169 # define ADDR_CAST (struct sockaddr *)
171 # define ADDR_CAST (char *)
176 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
181 char * /* O - Host name */
183 const http_addr_t
*addr
, /* I - Address to lookup */
184 char *name
, /* I - Host name buffer */
185 int namelen
) /* I - Size of name buffer */
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
)
204 strlcpy(name
, addr
->un
.sun_path
, namelen
);
206 #endif /* AF_LOCAL */
207 #ifdef HAVE_GETNAMEINFO
209 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
213 * If we get an error back, then the address type is not supported
214 * and we should zero out the buffer...
224 struct hostent
*host
; /* Host from name service */
228 if (addr
->addr
.sa_family
== AF_INET6
)
229 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
230 sizeof(struct in_addr
), AF_INET6
);
232 # endif /* AF_INET6 */
233 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
234 sizeof(struct in_addr
), AF_INET
);
239 * No hostname, so return the raw address...
242 httpAddrString(addr
, name
, namelen
);
246 strlcpy(name
, host
->h_name
, namelen
);
248 #endif /* HAVE_GETNAMEINFO */
255 * 'httpAddrString()' - Convert an address to a numeric string.
260 char * /* O - Numeric address string */
261 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
262 char *s
, /* I - String buffer */
263 int slen
) /* I - Length of string */
265 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
269 * Range check input...
272 if (!addr
|| !s
|| slen
<= 2)
281 if (addr
->addr
.sa_family
== AF_LOCAL
)
282 strlcpy(s
, addr
->un
.sun_path
, slen
);
284 #endif /* AF_LOCAL */
285 if (addr
->addr
.sa_family
== AF_INET
)
287 unsigned temp
; /* Temporary address */
290 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
292 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
293 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
296 else if (addr
->addr
.sa_family
== AF_INET6
)
298 # ifdef HAVE_GETNAMEINFO
299 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
300 NULL
, 0, NI_NUMERICHOST
))
303 * If we get an error back, then the address type is not supported
304 * and we should zero out the buffer...
312 char *sptr
; /* Pointer into string */
313 int i
; /* Looping var */
314 unsigned temp
; /* Current value */
315 const char *prefix
; /* Prefix for address */
319 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
321 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
323 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
325 slen
-= strlen(sptr
);
326 sptr
+= strlen(sptr
);
330 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
332 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
333 slen
-= strlen(sptr
);
334 sptr
+= strlen(sptr
);
340 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
345 snprintf(sptr
, slen
, "%s:", prefix
);
347 slen
-= strlen(sptr
);
348 sptr
+= strlen(sptr
);
352 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
354 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
356 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
357 slen
-= strlen(sptr
);
358 sptr
+= strlen(sptr
);
361 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
362 slen
-= strlen(sptr
);
363 sptr
+= strlen(sptr
);
372 strlcpy(s
, "::", slen
);
382 strlcpy(sptr
, "::", slen
);
387 # endif /* HAVE_GETNAMEINFO */
389 #endif /* AF_INET6 */
391 strlcpy(s
, "UNKNOWN", slen
);
393 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
400 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
401 * address records for the specified name.
406 struct hostent
* /* O - Host entry */
407 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
409 const char *nameptr
; /* Pointer into name */
410 unsigned ip
[4]; /* IP address components */
411 _cups_globals_t
*cg
= _cupsGlobals();
412 /* Pointer to library globals */
415 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
418 * Avoid lookup delays and configuration problems when connecting
419 * to the localhost address...
422 if (!strcmp(name
, "localhost"))
426 * This function is needed because some operating systems have a
427 * buggy implementation of gethostbyname() that does not support
428 * IP addresses. If the first character of the name string is a
429 * number, then sscanf() is used to extract the IP components.
430 * We then pack the components into an IPv4 address manually,
431 * since the inet_aton() function is deprecated. We use the
432 * htonl() macro to get the right byte order for the address.
434 * We also support domain sockets when supported by the underlying
442 * A domain socket address, so make an AF_LOCAL entry and return it...
445 cg
->hostent
.h_name
= (char *)name
;
446 cg
->hostent
.h_aliases
= NULL
;
447 cg
->hostent
.h_addrtype
= AF_LOCAL
;
448 cg
->hostent
.h_length
= strlen(name
) + 1;
449 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
450 cg
->ip_ptrs
[0] = (char *)name
;
451 cg
->ip_ptrs
[1] = NULL
;
453 DEBUG_puts("httpGetHostByName: returning domain socket address...");
455 return (&cg
->hostent
);
457 #endif /* AF_LOCAL */
459 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
464 * We have an IPv4 address; break it up and provide the host entry
468 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
469 return (NULL
); /* Must have 4 numbers */
471 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
472 return (NULL
); /* Invalid byte ranges! */
474 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
478 * Fill in the host entry and return it...
481 cg
->hostent
.h_name
= (char *)name
;
482 cg
->hostent
.h_aliases
= NULL
;
483 cg
->hostent
.h_addrtype
= AF_INET
;
484 cg
->hostent
.h_length
= 4;
485 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
486 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
487 cg
->ip_ptrs
[1] = NULL
;
489 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
491 return (&cg
->hostent
);
496 * Use the gethostbyname() function to get the IPv4 address for
500 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
502 return (gethostbyname(name
));
508 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
510 * When "http" points to a connected socket, return the hostname or
511 * address that was used in the call to httpConnect() or httpConnectEncrypt().
512 * Otherwise, return the FQDN for the local system using both gethostname()
513 * and gethostbyname() to get the local hostname with domain.
518 const char * /* O - FQDN for connection or system */
519 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
520 char *s
, /* I - String buffer for name */
521 int slen
) /* I - Size of buffer */
523 struct hostent
*host
; /* Host entry to get FQDN */
531 if (http
->hostname
[0] == '/')
532 strlcpy(s
, "localhost", slen
);
534 strlcpy(s
, http
->hostname
, slen
);
539 * Get the hostname...
542 if (gethostname(s
, slen
) < 0)
543 strlcpy(s
, "localhost", slen
);
548 * The hostname is not a FQDN, so look it up...
551 if ((host
= gethostbyname(s
)) != NULL
&& host
->h_name
)
552 strlcpy(s
, host
->h_name
, slen
);
557 * Return the hostname with as much domain info as we have...