]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
4dd9064eab4b96dd4afbc5882b4d8c93b0af266e
2 * "$Id: http-addr.c 5138 2006-02-21 10:49:06Z mike $"
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 */
56 if (addr
->addr
.sa_family
== AF_INET6
&&
57 IN6_IS_ADDR_UNSPECIFIED(&(addr
->ipv6
.sin6_addr
)))
61 if (addr
->addr
.sa_family
== AF_INET
&&
62 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x00000000)
70 * 'httpAddrEqual()' - Compare two addresses.
75 int /* O - 1 if equal, 0 if not */
76 httpAddrEqual(const http_addr_t
*addr1
, /* I - First address */
77 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 */
106 if (addr
->addr
.sa_family
== AF_INET6
)
107 return (sizeof(addr
->ipv6
));
109 #endif /* AF_INET6 */
111 if (addr
->addr
.sa_family
== AF_LOCAL
)
112 return (offsetof(struct sockaddr_un
, sun_path
) +
113 strlen(addr
->un
.sun_path
) + 1);
115 #endif /* AF_LOCAL */
116 if (addr
->addr
.sa_family
== AF_INET
)
117 return (sizeof(addr
->ipv4
));
125 * 'httpAddrLocalhost()' - Check for the local loopback address.
130 int /* O - 1 if local host, 0 otherwise */
132 const http_addr_t
*addr
) /* I - Address to check */
135 if (addr
->addr
.sa_family
== AF_INET6
&&
136 IN6_IS_ADDR_LOOPBACK(&(addr
->ipv6
.sin6_addr
)))
138 #endif /* AF_INET6 */
141 if (addr
->addr
.sa_family
== AF_LOCAL
)
143 #endif /* AF_LOCAL */
145 if (addr
->addr
.sa_family
== AF_INET
&&
146 ntohl(addr
->ipv4
.sin_addr
.s_addr
) == 0x7f000001)
154 # define ADDR_CAST (struct sockaddr *)
156 # define ADDR_CAST (char *)
161 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
166 char * /* O - Host name */
168 const http_addr_t
*addr
, /* I - Address to lookup */
169 char *name
, /* I - Host name buffer */
170 int namelen
) /* I - Size of name buffer */
172 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)\n",
173 addr
, name
, namelen
));
176 * Range check input...
179 if (!addr
|| !name
|| namelen
<= 2)
181 if (name
&& namelen
>= 1)
188 if (addr
->addr
.sa_family
== AF_LOCAL
)
189 strlcpy(name
, addr
->un
.sun_path
, namelen
);
191 #endif /* AF_LOCAL */
192 #ifdef HAVE_GETNAMEINFO
194 char servname
[1024]; /* Service name (not used) */
197 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
198 servname
, sizeof(servname
), 0))
201 * If we get an error back, then the address type is not supported
202 * and we should zero out the buffer...
212 struct hostent
*host
; /* Host from name service */
216 if (addr
->addr
.sa_family
== AF_INET6
)
217 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
218 sizeof(struct in_addr
), AF_INET6
);
220 # endif /* AF_INET6 */
221 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
222 sizeof(struct in_addr
), AF_INET
);
227 * No hostname, so return the raw address...
230 httpAddrString(addr
, name
, namelen
);
234 strlcpy(name
, host
->h_name
, namelen
);
236 #endif /* HAVE_GETNAMEINFO */
243 * 'httpAddrString()' - Convert an address to a numeric string.
248 char * /* O - Numeric address string */
249 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
250 char *s
, /* I - String buffer */
251 int slen
) /* I - Length of string */
253 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
257 * Range check input...
260 if (!addr
|| !s
|| slen
<= 2)
269 if (addr
->addr
.sa_family
== AF_LOCAL
)
270 strlcpy(s
, addr
->un
.sun_path
, slen
);
272 #endif /* AF_LOCAL */
273 #ifdef HAVE_GETNAMEINFO
275 char servname
[1024]; /* Service name (not used) */
278 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
279 servname
, sizeof(servname
), NI_NUMERICHOST
))
282 * If we get an error back, then the address type is not supported
283 * and we should zero out the buffer...
294 if (addr
->addr
.sa_family
== AF_INET6
)
296 char *sptr
; /* Pointer into string */
297 int i
; /* Looping var */
298 unsigned temp
; /* Current value */
299 const char *prefix
; /* Prefix for address */
303 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
305 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
307 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
309 slen
-= strlen(sptr
);
310 sptr
+= strlen(sptr
);
314 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
316 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
317 slen
-= strlen(sptr
);
318 sptr
+= strlen(sptr
);
324 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
329 snprintf(sptr
, slen
, "%s:", prefix
);
331 slen
-= strlen(sptr
);
332 sptr
+= strlen(sptr
);
336 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
338 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
340 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
341 slen
-= strlen(sptr
);
342 sptr
+= strlen(sptr
);
345 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
346 slen
-= strlen(sptr
);
347 sptr
+= strlen(sptr
);
356 strlcpy(s
, "::", slen
);
366 strlcpy(sptr
, "::", slen
);
373 #endif /* AF_INET6 */
374 if (addr
->addr
.sa_family
== AF_INET
)
376 unsigned temp
; /* Temporary address */
379 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
381 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
382 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
385 strlcpy(s
, "UNKNOWN", slen
);
387 #endif /* HAVE_GETNAMEINFO */
389 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
396 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
397 * address records for the specified name.
402 struct hostent
* /* O - Host entry */
403 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
405 const char *nameptr
; /* Pointer into name */
406 unsigned ip
[4]; /* IP address components */
407 _cups_globals_t
*cg
= _cupsGlobals();
408 /* Pointer to library globals */
411 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
414 * Avoid lookup delays and configuration problems when connecting
415 * to the localhost address...
418 if (!strcmp(name
, "localhost"))
422 * This function is needed because some operating systems have a
423 * buggy implementation of gethostbyname() that does not support
424 * IP addresses. If the first character of the name string is a
425 * number, then sscanf() is used to extract the IP components.
426 * We then pack the components into an IPv4 address manually,
427 * since the inet_aton() function is deprecated. We use the
428 * htonl() macro to get the right byte order for the address.
430 * We also support domain sockets when supported by the underlying
438 * A domain socket address, so make an AF_LOCAL entry and return it...
441 cg
->hostent
.h_name
= (char *)name
;
442 cg
->hostent
.h_aliases
= NULL
;
443 cg
->hostent
.h_addrtype
= AF_LOCAL
;
444 cg
->hostent
.h_length
= strlen(name
) + 1;
445 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
446 cg
->ip_ptrs
[0] = (char *)name
;
447 cg
->ip_ptrs
[1] = NULL
;
449 DEBUG_puts("httpGetHostByName: returning domain socket address...");
451 return (&cg
->hostent
);
453 #endif /* AF_LOCAL */
455 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
460 * We have an IPv4 address; break it up and provide the host entry
464 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
465 return (NULL
); /* Must have 4 numbers */
467 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
468 return (NULL
); /* Invalid byte ranges! */
470 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
474 * Fill in the host entry and return it...
477 cg
->hostent
.h_name
= (char *)name
;
478 cg
->hostent
.h_aliases
= NULL
;
479 cg
->hostent
.h_addrtype
= AF_INET
;
480 cg
->hostent
.h_length
= 4;
481 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
482 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
483 cg
->ip_ptrs
[1] = NULL
;
485 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
487 return (&cg
->hostent
);
492 * Use the gethostbyname() function to get the IPv4 address for
496 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
498 return (gethostbyname(name
));
504 * 'httpGetHostname()' - Get the FQDN for the local system.
506 * This function uses both gethostname() and gethostbyname() to
507 * get the local hostname with domain.
512 const char * /* O - FQDN for this system */
513 httpGetHostname(char *s
, /* I - String buffer for name */
514 int slen
) /* I - Size of buffer */
516 struct hostent
*host
; /* Host entry to get FQDN */
520 * Get the hostname...
523 gethostname(s
, slen
);
528 * The hostname is not a FQDN, so look it up...
531 if ((host
= gethostbyname(s
)) != NULL
)
532 strlcpy(s
, host
->h_name
, slen
);
536 * Return the hostname with as much domain info as we have...
544 * End of "$Id: http-addr.c 5138 2006-02-21 10:49:06Z mike $".