]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addr.c
2 * "$Id: http-addr.c 5251 2006-03-08 18:34:07Z 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 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), name
, namelen
,
198 * If we get an error back, then the address type is not supported
199 * and we should zero out the buffer...
209 struct hostent
*host
; /* Host from name service */
213 if (addr
->addr
.sa_family
== AF_INET6
)
214 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv6
.sin6_addr
),
215 sizeof(struct in_addr
), AF_INET6
);
217 # endif /* AF_INET6 */
218 host
= gethostbyaddr(ADDR_CAST
&(addr
->ipv4
.sin_addr
),
219 sizeof(struct in_addr
), AF_INET
);
224 * No hostname, so return the raw address...
227 httpAddrString(addr
, name
, namelen
);
231 strlcpy(name
, host
->h_name
, namelen
);
233 #endif /* HAVE_GETNAMEINFO */
240 * 'httpAddrString()' - Convert an address to a numeric string.
245 char * /* O - Numeric address string */
246 httpAddrString(const http_addr_t
*addr
, /* I - Address to convert */
247 char *s
, /* I - String buffer */
248 int slen
) /* I - Length of string */
250 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n",
254 * Range check input...
257 if (!addr
|| !s
|| slen
<= 2)
266 if (addr
->addr
.sa_family
== AF_LOCAL
)
267 strlcpy(s
, addr
->un
.sun_path
, slen
);
269 #endif /* AF_LOCAL */
270 if (addr
->addr
.sa_family
== AF_INET
)
272 unsigned temp
; /* Temporary address */
275 temp
= ntohl(addr
->ipv4
.sin_addr
.s_addr
);
277 snprintf(s
, slen
, "%d.%d.%d.%d", (temp
>> 24) & 255,
278 (temp
>> 16) & 255, (temp
>> 8) & 255, temp
& 255);
281 else if (addr
->addr
.sa_family
== AF_INET6
)
283 # ifdef HAVE_GETNAMEINFO
284 if (getnameinfo(&addr
->addr
, httpAddrLength(addr
), s
, slen
,
285 NULL
, 0, NI_NUMERICHOST
))
288 * If we get an error back, then the address type is not supported
289 * and we should zero out the buffer...
297 char *sptr
; /* Pointer into string */
298 int i
; /* Looping var */
299 unsigned temp
; /* Current value */
300 const char *prefix
; /* Prefix for address */
304 for (sptr
= s
, i
= 0; i
< 4 && addr
->ipv6
.sin6_addr
.s6_addr32
[i
]; i
++)
306 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
308 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
310 slen
-= strlen(sptr
);
311 sptr
+= strlen(sptr
);
315 if (temp
|| i
== 3 || addr
->ipv6
.sin6_addr
.s6_addr32
[i
+ 1])
317 snprintf(sptr
, slen
, "%s%x", prefix
, temp
);
318 slen
-= strlen(sptr
);
319 sptr
+= strlen(sptr
);
325 while (i
< 4 && !addr
->ipv6
.sin6_addr
.s6_addr32
[i
])
330 snprintf(sptr
, slen
, "%s:", prefix
);
332 slen
-= strlen(sptr
);
333 sptr
+= strlen(sptr
);
337 temp
= ntohl(addr
->ipv6
.sin6_addr
.s6_addr32
[i
]);
339 if ((temp
& 0xffff0000) || addr
->ipv6
.sin6_addr
.s6_addr32
[i
- 1])
341 snprintf(sptr
, slen
, "%s%x", prefix
, (temp
>> 16) & 0xffff);
342 slen
-= strlen(sptr
);
343 sptr
+= strlen(sptr
);
346 snprintf(sptr
, slen
, "%s%x", prefix
, temp
& 0xffff);
347 slen
-= strlen(sptr
);
348 sptr
+= strlen(sptr
);
357 strlcpy(s
, "::", slen
);
367 strlcpy(sptr
, "::", slen
);
372 # endif /* HAVE_GETNAMEINFO */
374 #endif /* AF_INET6 */
376 strlcpy(s
, "UNKNOWN", slen
);
378 DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s
));
385 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
386 * address records for the specified name.
391 struct hostent
* /* O - Host entry */
392 httpGetHostByName(const char *name
) /* I - Hostname or IP address */
394 const char *nameptr
; /* Pointer into name */
395 unsigned ip
[4]; /* IP address components */
396 _cups_globals_t
*cg
= _cupsGlobals();
397 /* Pointer to library globals */
400 DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name
));
403 * Avoid lookup delays and configuration problems when connecting
404 * to the localhost address...
407 if (!strcmp(name
, "localhost"))
411 * This function is needed because some operating systems have a
412 * buggy implementation of gethostbyname() that does not support
413 * IP addresses. If the first character of the name string is a
414 * number, then sscanf() is used to extract the IP components.
415 * We then pack the components into an IPv4 address manually,
416 * since the inet_aton() function is deprecated. We use the
417 * htonl() macro to get the right byte order for the address.
419 * We also support domain sockets when supported by the underlying
427 * A domain socket address, so make an AF_LOCAL entry and return it...
430 cg
->hostent
.h_name
= (char *)name
;
431 cg
->hostent
.h_aliases
= NULL
;
432 cg
->hostent
.h_addrtype
= AF_LOCAL
;
433 cg
->hostent
.h_length
= strlen(name
) + 1;
434 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
435 cg
->ip_ptrs
[0] = (char *)name
;
436 cg
->ip_ptrs
[1] = NULL
;
438 DEBUG_puts("httpGetHostByName: returning domain socket address...");
440 return (&cg
->hostent
);
442 #endif /* AF_LOCAL */
444 for (nameptr
= name
; isdigit(*nameptr
& 255) || *nameptr
== '.'; nameptr
++);
449 * We have an IPv4 address; break it up and provide the host entry
453 if (sscanf(name
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) != 4)
454 return (NULL
); /* Must have 4 numbers */
456 if (ip
[0] > 255 || ip
[1] > 255 || ip
[2] > 255 || ip
[3] > 255)
457 return (NULL
); /* Invalid byte ranges! */
459 cg
->ip_addr
= htonl(((((((ip
[0] << 8) | ip
[1]) << 8) | ip
[2]) << 8) |
463 * Fill in the host entry and return it...
466 cg
->hostent
.h_name
= (char *)name
;
467 cg
->hostent
.h_aliases
= NULL
;
468 cg
->hostent
.h_addrtype
= AF_INET
;
469 cg
->hostent
.h_length
= 4;
470 cg
->hostent
.h_addr_list
= cg
->ip_ptrs
;
471 cg
->ip_ptrs
[0] = (char *)&(cg
->ip_addr
);
472 cg
->ip_ptrs
[1] = NULL
;
474 DEBUG_puts("httpGetHostByName: returning IPv4 address...");
476 return (&cg
->hostent
);
481 * Use the gethostbyname() function to get the IPv4 address for
485 DEBUG_puts("httpGetHostByName: returning domain lookup address(es)...");
487 return (gethostbyname(name
));
493 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
495 * When "http" points to a connected socket, return the hostname or
496 * address that was used in the call to httpConnect() or httpConnectEncrypt().
497 * Otherwise, return the FQDN for the local system using both gethostname()
498 * and gethostbyname() to get the local hostname with domain.
503 const char * /* O - FQDN for connection or system */
504 httpGetHostname(http_t
*http
, /* I - HTTP connection or NULL */
505 char *s
, /* I - String buffer for name */
506 int slen
) /* I - Size of buffer */
508 struct hostent
*host
; /* Host entry to get FQDN */
513 if (http
->hostname
[0] == '/')
514 strlcpy(s
, "localhost", slen
);
516 strlcpy(s
, http
->hostname
, slen
);
521 * Get the hostname...
524 gethostname(s
, slen
);
529 * The hostname is not a FQDN, so look it up...
532 if ((host
= gethostbyname(s
)) != NULL
)
533 strlcpy(s
, host
->h_name
, slen
);
538 * Return the hostname with as much domain info as we have...
546 * End of "$Id: http-addr.c 5251 2006-03-08 18:34:07Z mike $".