]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addrlist.c
2 * "$Id: http-addrlist.c 7460 2008-04-16 02:19:54Z mike $"
4 * HTTP address list routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 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 * httpAddrConnect() - Connect to any of the addresses in the list.
18 * httpAddrFreeList() - Free an address list.
19 * httpAddrGetList() - Get a list of addresses for a hostname.
23 * Include necessary headers...
26 #include "http-private.h"
33 #endif /* HAVE_RESOLV_H */
37 * 'httpAddrConnect()' - Connect to any of the addresses in the list.
42 http_addrlist_t
* /* O - Connected address or NULL on failure */
44 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
45 int *sock
) /* O - Socket */
47 int val
; /* Socket option value */
49 char temp
[256]; /* Temporary address string */
53 DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)\n", addrlist
, sock
));
62 * Loop through each address until we connect or run out of addresses...
68 * Create the socket...
71 DEBUG_printf(("httpAddrConnect: Trying %s:%d...\n",
72 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
73 _httpAddrPort(&(addrlist
->addr
))));
75 if ((*sock
= (int)socket(addrlist
->addr
.addr
.sa_family
, SOCK_STREAM
,
79 * Don't abort yet, as this could just be an issue with the local
80 * system not being configured with IPv4/IPv6/domain socket enabled...
83 addrlist
= addrlist
->next
;
93 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
,
96 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
101 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
102 #endif /* SO_REUSEPORT */
106 setsockopt(*sock
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof(val
));
107 #endif /* SO_NOSIGPIPE */
110 * Using TCP_NODELAY improves responsiveness, especially on systems
111 * with a slow loopback interface...
116 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, (const char *)&val
,
119 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
124 * Close this socket when starting another process...
127 fcntl(*sock
, F_SETFD
, FD_CLOEXEC
);
128 #endif /* FD_CLOEXEC */
134 if (!connect(*sock
, &(addrlist
->addr
.addr
),
135 httpAddrLength(&(addrlist
->addr
))))
137 DEBUG_printf(("httpAddrConnect: Connected to %s:%d...\n",
138 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
139 _httpAddrPort(&(addrlist
->addr
))));
143 DEBUG_printf(("httpAddrConnect: Unable to connect to %s:%d: %s\n",
144 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
145 _httpAddrPort(&(addrlist
->addr
)), strerror(errno
)));
148 * Close this socket and move to the next address...
158 addrlist
= addrlist
->next
;
166 * 'httpAddrFreeList()' - Free an address list.
173 http_addrlist_t
*addrlist
) /* I - Address list to free */
175 http_addrlist_t
*next
; /* Next address in list */
179 * Free each address in the list...
184 next
= addrlist
->next
;
194 * 'httpAddrGetList()' - Get a list of addresses for a hostname.
199 http_addrlist_t
* /* O - List of addresses or NULL */
200 httpAddrGetList(const char *hostname
, /* I - Hostname, IP address, or NULL for passive listen address */
201 int family
, /* I - Address family or AF_UNSPEC */
202 const char *service
) /* I - Service name or port number */
204 http_addrlist_t
*first
, /* First address in list */
205 *addr
, /* Current address in list */
206 *temp
; /* New address */
207 _cups_globals_t
*cg
= _cupsGlobals();
212 _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, "
214 hostname
? hostname
: "(nil)",
215 family
== AF_UNSPEC
? "UNSPEC" :
217 family
== AF_LOCAL
? "LOCAL" :
218 # endif /* AF_LOCAL */
220 family
== AF_INET6
? "INET6" :
221 # endif /* AF_INET6 */
222 family
== AF_INET
? "INET" : "???", service
);
227 * STR #2920: Initialize resolver after failure in cups-polld
229 * If the previous lookup failed, re-initialize the resolver to prevent
230 * temporary network errors from persisting. This *should* be handled by
231 * the resolver libraries, but apparently the glibc folks do not agree.
233 * We set a flag at the end of this function if we encounter an error that
234 * requires reinitialization of the resolver functions. We then call
235 * res_init() if the flag is set on the next call here or in httpAddrLookup().
238 if (cg
->need_res_init
)
242 cg
->need_res_init
= 0;
244 #endif /* HAVE_RES_INIT */
248 * Lookup the address the best way we can...
254 if (hostname
&& hostname
[0] == '/')
257 * Domain socket address...
260 if ((first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
))) != NULL
)
262 first
->addr
.un
.sun_family
= AF_LOCAL
;
263 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
267 #endif /* AF_LOCAL */
269 #ifdef HAVE_GETADDRINFO
270 struct addrinfo hints
, /* Address lookup hints */
271 *results
, /* Address lookup results */
272 *current
; /* Current result */
273 char ipv6
[1024], /* IPv6 address */
274 *ipv6zone
; /* Pointer to zone separator */
275 int ipv6len
; /* Length of IPv6 address */
276 int error
; /* getaddrinfo() error */
280 * Lookup the address as needed...
283 memset(&hints
, 0, sizeof(hints
));
284 hints
.ai_family
= family
;
285 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
286 hints
.ai_socktype
= SOCK_STREAM
;
288 if (hostname
&& *hostname
== '[')
291 * Remove brackets from numeric IPv6 address...
294 if (!strncmp(hostname
, "[v1.", 4))
297 * Copy the newer address format which supports link-local addresses...
300 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
301 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
303 ipv6
[ipv6len
] = '\0';
307 * Convert "+zone" in address to "%zone"...
310 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
317 * Copy the regular non-link-local IPv6 address...
320 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
321 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
323 ipv6
[ipv6len
] = '\0';
329 if ((error
= getaddrinfo(hostname
, service
, &hints
, &results
)) == 0)
332 * Copy the results to our own address list structure...
335 for (current
= results
; current
; current
= current
->ai_next
)
336 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
339 * Copy the address over...
342 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
345 httpAddrFreeList(first
);
349 if (current
->ai_family
== AF_INET6
)
350 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
351 sizeof(temp
->addr
.ipv6
));
353 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
354 sizeof(temp
->addr
.ipv4
));
357 * Append the address to the list...
370 * Free the results from getaddrinfo()...
373 freeaddrinfo(results
);
375 else if (error
== EAI_FAIL
)
376 cg
->need_res_init
= 1;
381 int i
; /* Looping vars */
382 unsigned ip
[4]; /* IPv4 address components */
383 const char *ptr
; /* Pointer into hostname */
384 struct hostent
*host
; /* Result of lookup */
385 struct servent
*port
; /* Port number for service */
386 int portnum
; /* Port number */
390 * Lookup the service...
395 else if (isdigit(*service
& 255))
396 portnum
= atoi(service
);
397 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
398 portnum
= ntohs(port
->s_port
);
399 else if (!strcmp(service
, "http"))
401 else if (!strcmp(service
, "https"))
403 else if (!strcmp(service
, "ipp"))
405 else if (!strcmp(service
, "lpd"))
407 else if (!strcmp(service
, "socket"))
413 * This code is needed because some operating systems have a
414 * buggy implementation of gethostbyname() that does not support
415 * IPv4 addresses. If the hostname string is an IPv4 address, then
416 * sscanf() is used to extract the IPv4 components. We then pack
417 * the components into an IPv4 address manually, since the
418 * inet_aton() function is deprecated. We use the htonl() macro
419 * to get the right byte order for the address.
422 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
427 * We have an IPv4 address; break it up and create an IPv4 address...
430 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
431 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
433 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
437 first
->addr
.ipv4
.sin_family
= AF_INET
;
438 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
440 ip
[2]) << 8) | ip
[3]));
441 first
->addr
.ipv4
.sin_port
= htons(portnum
);
444 else if ((host
= gethostbyname(hostname
)) != NULL
&&
446 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
448 host
->h_addrtype
== AF_INET
)
449 # endif /* AF_INET6 */
451 for (i
= 0; host
->h_addr_list
[i
]; i
++)
454 * Copy the address over...
457 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
460 httpAddrFreeList(first
);
465 if (host
->h_addrtype
== AF_INET6
)
467 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
468 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
469 sizeof(temp
->addr
.ipv6
));
470 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
473 # endif /* AF_INET6 */
475 temp
->addr
.ipv4
.sin_family
= AF_INET
;
476 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
477 sizeof(temp
->addr
.ipv4
));
478 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
482 * Append the address to the list...
494 else if (h_errno
== NO_RECOVERY
)
495 cg
->need_res_init
= 1;
497 #endif /* HAVE_GETADDRINFO */
501 * Detect some common errors and handle them sanely...
504 if (!addr
&& (!hostname
|| !strcmp(hostname
, "localhost")))
506 struct servent
*port
; /* Port number for service */
507 int portnum
; /* Port number */
511 * Lookup the service...
516 else if (isdigit(*service
& 255))
517 portnum
= atoi(service
);
518 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
519 portnum
= ntohs(port
->s_port
);
520 else if (!strcmp(service
, "http"))
522 else if (!strcmp(service
, "https"))
524 else if (!strcmp(service
, "ipp"))
526 else if (!strcmp(service
, "lpd"))
528 else if (!strcmp(service
, "socket"))
533 if (hostname
&& !strcmp(hostname
, "localhost"))
536 * Unfortunately, some users ignore all of the warnings in the
537 * /etc/hosts file and delete "localhost" from it. If we get here
538 * then we were unable to resolve the name, so use the IPv6 and/or
539 * IPv4 loopback interface addresses...
543 if (family
!= AF_INET
)
546 * Add [::1] to the address list...
549 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
552 httpAddrFreeList(first
);
556 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
557 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
559 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
561 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
570 if (family
!= AF_INET6
)
571 #endif /* AF_INET6 */
574 * Add 127.0.0.1 to the address list...
577 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
580 httpAddrFreeList(first
);
584 temp
->addr
.ipv4
.sin_family
= AF_INET
;
585 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
586 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
598 * Provide one or more passive listening addresses...
602 if (family
!= AF_INET
)
605 * Add [::] to the address list...
608 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
611 httpAddrFreeList(first
);
615 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
616 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
624 if (family
!= AF_INET6
)
625 #endif /* AF_INET6 */
628 * Add 0.0.0.0 to the address list...
631 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
634 httpAddrFreeList(first
);
638 temp
->addr
.ipv4
.sin_family
= AF_INET
;
639 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
651 * Return the address list...
659 * End of "$Id: http-addrlist.c 7460 2008-04-16 02:19:54Z mike $".