]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * "$Id: http-addr.c 4950 2006-01-19 16:07:57Z mike $"
4 * HTTP address routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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 != */
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 */
167 httpAddrLookup(const http_addr_t
*addr
, /* I - Address to lookup */
168 char *name
, /* I - Host name buffer */
169 int namelen
) /* I - Size of name buffer */
171 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)\n",
172 addr
, name
, namelen
));
175 * Range check input...
178 if (!addr
|| !name
|| namelen
<= 2)
180 if (name
&& namelen
>= 1)
187 if (addr
->addr
.sa_family
== AF_LOCAL
)
188 strlcpy(name
, addr
->un
.sun_path
, namelen
);
190 #endif /* AF_LOCAL */
191 #ifdef HAVE_GETNAMEINFO
193 char servname
[1024]; /* Service name (not used) */
196 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
197 servname
, sizeof(servname
), 0))
200 * If we get an error back, then the address type is not supported
201 * and we should zero out the buffer...
211 struct hostent
*host
; /* Host from name service */
215 if (addr
->addr
.sa_family
== AF_INET6
)
216 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
217 sizeof(struct in_addr
), AF_INET6
);
219 # endif /* AF_INET6 */
220 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
221 sizeof(struct in_addr
), AF_INET
);
226 * No hostname, so return the raw address...
229 httpAddrString(addr
, name
, namelen
);
233 strlcpy(name
, host
->h_name
, namelen
);
235 #endif /* HAVE_GETNAMEINFO */
242 * 'httpAddrString()' - Convert an IP address to a dotted string.
247 char * /* O - IP string */
248 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
249 char *s
, /* I - String buffer */
250 int slen
) /* I - Length of string */
252 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
256 * Range check input...
259 if (!addr
|| !s
|| slen
<= 2)
268 if (addr
->addr
.sa_family
== AF_LOCAL
)
269 strlcpy(s
, addr
->un
.sun_path
, slen
);
271 #endif /* AF_LOCAL */
272 #ifdef HAVE_GETNAMEINFO
274 char servname
[1024]; /* Service name (not used) */
277 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
278 servname
, sizeof(servname
), NI_NUMERICHOST
))
281 * If we get an error back, then the address type is not supported
282 * and we should zero out the buffer...
293 if (addr
->addr
.sa_family
== AF_INET6
)
295 char *sptr
; /* Pointer into string */
296 int i
; /* Looping var */
297 unsigned temp
; /* Current value */
298 const char *prefix
; /* Prefix for address */
302 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
304 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
306 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
308 slen
-= strlen(sptr
);
309 sptr
+= strlen(sptr
);
313 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
315 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
316 slen
-= strlen(sptr
);
317 sptr
+= strlen(sptr
);
323 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
328 snprintf(sptr
, slen
, "%s:", prefix
);
330 slen
-= strlen(sptr
);
331 sptr
+= strlen(sptr
);
335 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
337 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
339 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
340 slen
-= strlen(sptr
);
341 sptr
+= strlen(sptr
);
344 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
345 slen
-= strlen(sptr
);
346 sptr
+= strlen(sptr
);
355 strlcpy(s
, "::", slen
);
365 strlcpy(sptr
, "::", slen
);
372 #endif /* AF_INET6 */
373 if (addr
->addr
.sa_family
== AF_INET
)
375 unsigned temp
; /* Temporary address */
378 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
380 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
381 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
384 strlcpy(s
, "UNKNOWN", slen
);
386 #endif /* HAVE_GETNAMEINFO */
388 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
395 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
396 * address records for the specified name.
401 struct hostent
* /* O - Host entry */
402 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
404 const char *nameptr
; /* Pointer into name */
405 unsigned ip
[4]; /* IP address components */
406 _cups_globals_t
*cg
= _cupsGlobals();
407 /* Pointer to library globals */
410 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
413 * Avoid lookup delays and configuration problems when connecting
414 * to the localhost address...
417 if (!strcmp(name
, "localhost"))
421 * This function is needed because some operating systems have a
422 * buggy implementation of gethostbyname() that does not support
423 * IP addresses. If the first character of the name string is a
424 * number, then sscanf() is used to extract the IP components.
425 * We then pack the components into an IPv4 address manually,
426 * since the inet_aton() function is deprecated. We use the
427 * htonl() macro to get the right byte order for the address.
429 * We also support domain sockets when supported by the underlying
437 * A domain socket address, so make an AF_LOCAL entry and return it...
440 cg
->hostent
.h_name
= (char *)name
;
441 cg
->hostent
.h_aliases
= NULL
;
442 cg
->hostent
.h_addrtype
= AF_LOCAL
;
443 cg
->hostent
.h_length
= strlen(name
) + 1;
444 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
445 cg
->ip_ptrs
[0] = (char *)name
;
446 cg
->ip_ptrs
[1] = NULL
;
448 DEBUG_puts("httpGetHostByName: returning domain socket address...");
450 return (&cg
->hostent
);
452 #endif /* AF_LOCAL */
454 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
459 * We have an IPv4 address; break it up and provide the host entry
463 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
464 return (NULL
); /* Must have 4 numbers */
466 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
467 return (NULL
); /* Invalid byte ranges! */
469 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
473 * Fill in the host entry and return it...
476 cg
->hostent
.h_name
= (char *)name
;
477 cg
->hostent
.h_aliases
= NULL
;
478 cg
->hostent
.h_addrtype
= AF_INET
;
479 cg
->hostent
.h_length
= 4;
480 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
481 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
482 cg
->ip_ptrs
[1] = NULL
;
484 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
486 return (&cg
->hostent
);
491 * Use the gethostbyname() function to get the IPv4 address for
495 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
497 return (gethostbyname(name
));
503 * 'httpGetHostname()' - Get the FQDN for the local system.
505 * This function uses both gethostname() and gethostbyname() to
506 * get the local hostname with domain.
511 const char * /* O - FQDN for this system */
512 httpGetHostname(char *s
, /* I - String buffer for name */
513 int slen
) /* I - Size of buffer */
515 struct hostent
*host
; /* Host entry to get FQDN */
519 * Get the hostname...
522 gethostname(s
, slen
);
527 * The hostname is not a FQDN, so look it up...
530 if ((host
= gethostbyname(s
)) != NULL
)
531 strlcpy(s
, host
->h_name
, slen
);
535 * Return the hostname with as much domain info as we have...
543 * End of "$Id: http-addr.c 4950 2006-01-19 16:07:57Z mike $".