]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addrlist.c
2 * "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $"
4 * HTTP address list routines for CUPS.
6 * Copyright 2007-2012 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 * httpAddrConnect2() - Connect to any of the addresses in the list with a
19 * timeout and optional cancel.
20 * httpAddrFreeList() - Free an address list.
21 * httpAddrGetList() - Get a list of addresses for a hostname.
25 * Include necessary headers...
28 #include "cups-private.h"
31 #endif /* HAVE_RESOLV_H */
34 #endif /* HAVE_POLL */
35 #include <sys/fcntl.h>
39 * 'httpAddrConnect()' - Connect to any of the addresses in the list.
41 * @since CUPS 1.2/Mac OS X 10.5@
44 http_addrlist_t
* /* O - Connected address or NULL on failure */
46 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
47 int *sock
) /* O - Socket */
49 DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)", addrlist
, sock
));
51 return (httpAddrConnect2(addrlist
, sock
, 30000, NULL
));
56 * 'httpAddrConnect2()' - Connect to any of the addresses in the list with a
57 * timeout and optional cancel.
62 http_addrlist_t
* /* O - Connected address or NULL on failure */
64 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
65 int *sock
, /* O - Socket */
66 int msec
, /* I - Timeout in milliseconds */
67 int *cancel
) /* I - Pointer to "cancel" variable */
69 int val
; /* Socket option value */
71 socklen_t len
; /* Length of value */
72 http_addr_t peer
; /* Peer address */
73 int flags
, /* Socket flags */
74 remaining
; /* Remaining timeout */
76 struct pollfd pfd
; /* Polled file descriptor */
78 fd_set input_set
, /* select() input set */
79 output_set
; /* select() output set */
80 struct timeval timeout
; /* Timeout */
81 # endif /* HAVE_POLL */
82 int nfds
; /* Result from select()/poll() */
83 #endif /* O_NONBLOCK */
85 char temp
[256]; /* Temporary address string */
89 DEBUG_printf(("httpAddrConnect2(addrlist=%p, sock=%p, msec=%d, cancel=%p)",
90 addrlist
, sock
, msec
, cancel
));
95 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
99 if (cancel
&& *cancel
)
102 if (msec
<= 0 || getenv("CUPS_DISABLE_ASYNC_CONNECT"))
106 * Loop through each address until we connect or run out of addresses...
111 if (cancel
&& *cancel
)
115 * Create the socket...
118 DEBUG_printf(("2httpAddrConnect2: Trying %s:%d...",
119 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
120 _httpAddrPort(&(addrlist
->addr
))));
122 if ((*sock
= (int)socket(_httpAddrFamily(&(addrlist
->addr
)), SOCK_STREAM
,
126 * Don't abort yet, as this could just be an issue with the local
127 * system not being configured with IPv4/IPv6/domain socket enabled...
130 addrlist
= addrlist
->next
;
140 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
,
143 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
148 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
149 #endif /* SO_REUSEPORT */
153 setsockopt(*sock
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof(val
));
154 #endif /* SO_NOSIGPIPE */
157 * Using TCP_NODELAY improves responsiveness, especially on systems
158 * with a slow loopback interface...
163 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, (const char *)&val
,
166 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
171 * Close this socket when starting another process...
174 fcntl(*sock
, F_SETFD
, FD_CLOEXEC
);
175 #endif /* FD_CLOEXEC */
179 * Do an asynchronous connect by setting the socket non-blocking...
182 DEBUG_printf(("httpAddrConnect2: Setting non-blocking connect()"));
184 flags
= fcntl(*sock
, F_GETFL
, 0);
187 DEBUG_puts("httpAddrConnect2: Setting non-blocking connect()");
189 fcntl(*sock
, F_SETFL
, flags
| O_NONBLOCK
);
191 #endif /* O_NONBLOCK */
197 if (!connect(*sock
, &(addrlist
->addr
.addr
),
198 httpAddrLength(&(addrlist
->addr
))))
200 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...",
201 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
202 _httpAddrPort(&(addrlist
->addr
))));
205 fcntl(*sock
, F_SETFL
, flags
);
206 #endif /* O_NONBLOCK */
213 if (WSAGetLastError() == WSAEINPROGRESS
||
214 WSAGetLastError() == WSAEWOULDBLOCK
)
216 if (errno
== EINPROGRESS
|| errno
== EWOULDBLOCK
)
219 DEBUG_puts("1httpAddrConnect2: Finishing async connect()");
221 fcntl(*sock
, F_SETFL
, flags
);
223 for (remaining
= msec
; remaining
> 0; remaining
-= 250)
227 if (cancel
&& *cancel
)
230 * Close this socket and return...
233 DEBUG_puts("1httpAddrConnect2: Canceled connect()");
248 pfd
.events
= POLLIN
| POLLOUT
;
250 nfds
= poll(&pfd
, 1, remaining
> 250 ? 250 : remaining
);
252 DEBUG_printf(("1httpAddrConnect2: poll() returned %d (%d)", nfds
,
257 FD_SET(*sock
, &input_set
);
258 output_set
= input_set
;
261 timeout
.tv_usec
= (remaining
> 250 ? 250 : remaining
) * 1000;
263 nfds
= select(*sock
+ 1, &input_set
, &output_set
, NULL
, &timeout
);
265 DEBUG_printf(("1httpAddrConnect2: select() returned %d (%d)", nfds
,
267 # endif /* HAVE_POLL */
270 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
271 WSAGetLastError() == WSAEWOULDBLOCK
));
273 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
279 if (!getpeername(*sock
, (struct sockaddr
*)&peer
, &len
))
281 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...",
282 httpAddrString(&peer
, temp
, sizeof(temp
)),
283 _httpAddrPort(&peer
)));
292 #endif /* O_NONBLOCK */
294 DEBUG_printf(("1httpAddrConnect2: Unable to connect to %s:%d: %s",
295 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
296 _httpAddrPort(&(addrlist
->addr
)), strerror(errno
)));
299 if (errno
== EINPROGRESS
)
304 * Close this socket and move to the next address...
314 addrlist
= addrlist
->next
;
319 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, "Connection failed", 0);
321 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
), 0);
329 * 'httpAddrFreeList()' - Free an address list.
331 * @since CUPS 1.2/Mac OS X 10.5@
336 http_addrlist_t
*addrlist
) /* I - Address list to free */
338 http_addrlist_t
*next
; /* Next address in list */
342 * Free each address in the list...
347 next
= addrlist
->next
;
357 * 'httpAddrGetList()' - Get a list of addresses for a hostname.
359 * @since CUPS 1.2/Mac OS X 10.5@
362 http_addrlist_t
* /* O - List of addresses or NULL */
363 httpAddrGetList(const char *hostname
, /* I - Hostname, IP address, or NULL for passive listen address */
364 int family
, /* I - Address family or AF_UNSPEC */
365 const char *service
) /* I - Service name or port number */
367 http_addrlist_t
*first
, /* First address in list */
368 *addr
, /* Current address in list */
369 *temp
; /* New address */
370 _cups_globals_t
*cg
= _cupsGlobals();
375 _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, "
377 hostname
? hostname
: "(nil)",
378 family
== AF_UNSPEC
? "UNSPEC" :
380 family
== AF_LOCAL
? "LOCAL" :
381 # endif /* AF_LOCAL */
383 family
== AF_INET6
? "INET6" :
384 # endif /* AF_INET6 */
385 family
== AF_INET
? "INET" : "???", service
);
390 * STR #2920: Initialize resolver after failure in cups-polld
392 * If the previous lookup failed, re-initialize the resolver to prevent
393 * temporary network errors from persisting. This *should* be handled by
394 * the resolver libraries, but apparently the glibc folks do not agree.
396 * We set a flag at the end of this function if we encounter an error that
397 * requires reinitialization of the resolver functions. We then call
398 * res_init() if the flag is set on the next call here or in httpAddrLookup().
401 if (cg
->need_res_init
)
405 cg
->need_res_init
= 0;
407 #endif /* HAVE_RES_INIT */
410 * Lookup the address the best way we can...
416 if (hostname
&& hostname
[0] == '/')
419 * Domain socket address...
422 if ((first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
))) != NULL
)
424 first
->addr
.un
.sun_family
= AF_LOCAL
;
425 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
429 #endif /* AF_LOCAL */
430 if (!hostname
|| _cups_strcasecmp(hostname
, "localhost"))
432 #ifdef HAVE_GETADDRINFO
433 struct addrinfo hints
, /* Address lookup hints */
434 *results
, /* Address lookup results */
435 *current
; /* Current result */
436 char ipv6
[64], /* IPv6 address */
437 *ipv6zone
; /* Pointer to zone separator */
438 int ipv6len
; /* Length of IPv6 address */
439 int error
; /* getaddrinfo() error */
443 * Lookup the address as needed...
446 memset(&hints
, 0, sizeof(hints
));
447 hints
.ai_family
= family
;
448 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
449 hints
.ai_socktype
= SOCK_STREAM
;
451 if (hostname
&& *hostname
== '[')
454 * Remove brackets from numeric IPv6 address...
457 if (!strncmp(hostname
, "[v1.", 4))
460 * Copy the newer address format which supports link-local addresses...
463 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
464 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
466 ipv6
[ipv6len
] = '\0';
470 * Convert "+zone" in address to "%zone"...
473 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
480 * Copy the regular non-link-local IPv6 address...
483 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
484 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
486 ipv6
[ipv6len
] = '\0';
492 if ((error
= getaddrinfo(hostname
, service
, &hints
, &results
)) == 0)
495 * Copy the results to our own address list structure...
498 for (current
= results
; current
; current
= current
->ai_next
)
499 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
502 * Copy the address over...
505 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
508 httpAddrFreeList(first
);
509 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
513 if (current
->ai_family
== AF_INET6
)
514 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
515 sizeof(temp
->addr
.ipv6
));
517 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
518 sizeof(temp
->addr
.ipv4
));
521 * Append the address to the list...
534 * Free the results from getaddrinfo()...
537 freeaddrinfo(results
);
541 if (error
== EAI_FAIL
)
542 cg
->need_res_init
= 1;
544 _cupsSetError(IPP_INTERNAL_ERROR
, gai_strerror(error
), 0);
550 int i
; /* Looping vars */
551 unsigned ip
[4]; /* IPv4 address components */
552 const char *ptr
; /* Pointer into hostname */
553 struct hostent
*host
; /* Result of lookup */
554 struct servent
*port
; /* Port number for service */
555 int portnum
; /* Port number */
559 * Lookup the service...
564 else if (isdigit(*service
& 255))
565 portnum
= atoi(service
);
566 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
567 portnum
= ntohs(port
->s_port
);
568 else if (!strcmp(service
, "http"))
570 else if (!strcmp(service
, "https"))
572 else if (!strcmp(service
, "ipp") || !strcmp(service
, "ipps"))
574 else if (!strcmp(service
, "lpd"))
576 else if (!strcmp(service
, "socket"))
582 * This code is needed because some operating systems have a
583 * buggy implementation of gethostbyname() that does not support
584 * IPv4 addresses. If the hostname string is an IPv4 address, then
585 * sscanf() is used to extract the IPv4 components. We then pack
586 * the components into an IPv4 address manually, since the
587 * inet_aton() function is deprecated. We use the htonl() macro
588 * to get the right byte order for the address.
591 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
596 * We have an IPv4 address; break it up and create an IPv4 address...
599 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
600 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
602 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
606 first
->addr
.ipv4
.sin_family
= AF_INET
;
607 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
609 ip
[2]) << 8) | ip
[3]));
610 first
->addr
.ipv4
.sin_port
= htons(portnum
);
613 else if ((host
= gethostbyname(hostname
)) != NULL
&&
615 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
617 host
->h_addrtype
== AF_INET
)
618 # endif /* AF_INET6 */
620 for (i
= 0; host
->h_addr_list
[i
]; i
++)
623 * Copy the address over...
626 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
629 httpAddrFreeList(first
);
634 if (host
->h_addrtype
== AF_INET6
)
636 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
637 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
638 sizeof(temp
->addr
.ipv6
));
639 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
642 # endif /* AF_INET6 */
644 temp
->addr
.ipv4
.sin_family
= AF_INET
;
645 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
646 sizeof(temp
->addr
.ipv4
));
647 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
651 * Append the address to the list...
665 if (h_errno
== NO_RECOVERY
)
666 cg
->need_res_init
= 1;
668 _cupsSetError(IPP_INTERNAL_ERROR
, hstrerror(h_errno
), 0);
671 #endif /* HAVE_GETADDRINFO */
675 * Detect some common errors and handle them sanely...
678 if (!addr
&& (!hostname
|| !_cups_strcasecmp(hostname
, "localhost")))
680 struct servent
*port
; /* Port number for service */
681 int portnum
; /* Port number */
685 * Lookup the service...
690 else if (isdigit(*service
& 255))
691 portnum
= atoi(service
);
692 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
693 portnum
= ntohs(port
->s_port
);
694 else if (!strcmp(service
, "http"))
696 else if (!strcmp(service
, "https"))
698 else if (!strcmp(service
, "ipp") || !strcmp(service
, "ipps"))
700 else if (!strcmp(service
, "lpd"))
702 else if (!strcmp(service
, "socket"))
706 httpAddrFreeList(first
);
708 _cupsSetError(IPP_INTERNAL_ERROR
, _("Unknown service name."), 1);
712 if (hostname
&& !_cups_strcasecmp(hostname
, "localhost"))
715 * Unfortunately, some users ignore all of the warnings in the
716 * /etc/hosts file and delete "localhost" from it. If we get here
717 * then we were unable to resolve the name, so use the IPv6 and/or
718 * IPv4 loopback interface addresses...
722 if (family
!= AF_INET
)
725 * Add [::1] to the address list...
728 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
731 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
732 httpAddrFreeList(first
);
736 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
737 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
739 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
741 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
750 if (family
!= AF_INET6
)
751 #endif /* AF_INET6 */
754 * Add 127.0.0.1 to the address list...
757 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
760 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
761 httpAddrFreeList(first
);
765 temp
->addr
.ipv4
.sin_family
= AF_INET
;
766 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
767 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
779 * Provide one or more passive listening addresses...
783 if (family
!= AF_INET
)
786 * Add [::] to the address list...
789 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
792 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
793 httpAddrFreeList(first
);
797 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
798 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
806 if (family
!= AF_INET6
)
807 #endif /* AF_INET6 */
810 * Add 0.0.0.0 to the address list...
813 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
816 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
817 httpAddrFreeList(first
);
821 temp
->addr
.ipv4
.sin_family
= AF_INET
;
822 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
834 * Return the address list...
842 * End of "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $".