]>
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 */
36 # include <sys/fcntl.h>
41 * 'httpAddrConnect()' - Connect to any of the addresses in the list.
43 * @since CUPS 1.2/Mac OS X 10.5@
46 http_addrlist_t
* /* O - Connected address or NULL on failure */
48 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
49 int *sock
) /* O - Socket */
51 DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)", addrlist
, sock
));
53 return (httpAddrConnect2(addrlist
, sock
, 30000, NULL
));
58 * 'httpAddrConnect2()' - Connect to any of the addresses in the list with a
59 * timeout and optional cancel.
64 http_addrlist_t
* /* O - Connected address or NULL on failure */
66 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
67 int *sock
, /* O - Socket */
68 int msec
, /* I - Timeout in milliseconds */
69 int *cancel
) /* I - Pointer to "cancel" variable */
71 int val
; /* Socket option value */
73 socklen_t len
; /* Length of value */
74 http_addr_t peer
; /* Peer address */
75 int flags
, /* Socket flags */
76 remaining
; /* Remaining timeout */
78 struct pollfd pfd
; /* Polled file descriptor */
80 fd_set input_set
, /* select() input set */
81 output_set
; /* select() output set */
82 struct timeval timeout
; /* Timeout */
83 # endif /* HAVE_POLL */
84 int nfds
; /* Result from select()/poll() */
85 #endif /* O_NONBLOCK */
87 char temp
[256]; /* Temporary address string */
91 DEBUG_printf(("httpAddrConnect2(addrlist=%p, sock=%p, msec=%d, cancel=%p)",
92 addrlist
, sock
, msec
, cancel
));
97 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
101 if (cancel
&& *cancel
)
104 if (msec
<= 0 || getenv("CUPS_DISABLE_ASYNC_CONNECT"))
108 * Loop through each address until we connect or run out of addresses...
113 if (cancel
&& *cancel
)
117 * Create the socket...
120 DEBUG_printf(("2httpAddrConnect2: Trying %s:%d...",
121 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
122 _httpAddrPort(&(addrlist
->addr
))));
124 if ((*sock
= (int)socket(_httpAddrFamily(&(addrlist
->addr
)), SOCK_STREAM
,
128 * Don't abort yet, as this could just be an issue with the local
129 * system not being configured with IPv4/IPv6/domain socket enabled...
132 addrlist
= addrlist
->next
;
142 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
,
145 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
150 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
151 #endif /* SO_REUSEPORT */
155 setsockopt(*sock
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof(val
));
156 #endif /* SO_NOSIGPIPE */
159 * Using TCP_NODELAY improves responsiveness, especially on systems
160 * with a slow loopback interface...
165 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, (const char *)&val
,
168 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
173 * Close this socket when starting another process...
176 fcntl(*sock
, F_SETFD
, FD_CLOEXEC
);
177 #endif /* FD_CLOEXEC */
181 * Do an asynchronous connect by setting the socket non-blocking...
184 DEBUG_printf(("httpAddrConnect2: Setting non-blocking connect()"));
186 flags
= fcntl(*sock
, F_GETFL
, 0);
189 DEBUG_puts("httpAddrConnect2: Setting non-blocking connect()");
191 fcntl(*sock
, F_SETFL
, flags
| O_NONBLOCK
);
193 #endif /* O_NONBLOCK */
199 if (!connect(*sock
, &(addrlist
->addr
.addr
),
200 httpAddrLength(&(addrlist
->addr
))))
202 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...",
203 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
204 _httpAddrPort(&(addrlist
->addr
))));
207 fcntl(*sock
, F_SETFL
, flags
);
208 #endif /* O_NONBLOCK */
215 if (WSAGetLastError() == WSAEINPROGRESS
||
216 WSAGetLastError() == WSAEWOULDBLOCK
)
218 if (errno
== EINPROGRESS
|| errno
== EWOULDBLOCK
)
221 DEBUG_puts("1httpAddrConnect2: Finishing async connect()");
223 fcntl(*sock
, F_SETFL
, flags
);
225 for (remaining
= msec
; remaining
> 0; remaining
-= 250)
229 if (cancel
&& *cancel
)
232 * Close this socket and return...
235 DEBUG_puts("1httpAddrConnect2: Canceled connect()");
250 pfd
.events
= POLLIN
| POLLOUT
;
252 nfds
= poll(&pfd
, 1, remaining
> 250 ? 250 : remaining
);
254 DEBUG_printf(("1httpAddrConnect2: poll() returned %d (%d)", nfds
,
259 FD_SET(*sock
, &input_set
);
260 output_set
= input_set
;
263 timeout
.tv_usec
= (remaining
> 250 ? 250 : remaining
) * 1000;
265 nfds
= select(*sock
+ 1, &input_set
, &output_set
, NULL
, &timeout
);
267 DEBUG_printf(("1httpAddrConnect2: select() returned %d (%d)", nfds
,
269 # endif /* HAVE_POLL */
272 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
273 WSAGetLastError() == WSAEWOULDBLOCK
));
275 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
281 if (!getpeername(*sock
, (struct sockaddr
*)&peer
, &len
))
283 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...",
284 httpAddrString(&peer
, temp
, sizeof(temp
)),
285 _httpAddrPort(&peer
)));
294 #endif /* O_NONBLOCK */
296 DEBUG_printf(("1httpAddrConnect2: Unable to connect to %s:%d: %s",
297 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
298 _httpAddrPort(&(addrlist
->addr
)), strerror(errno
)));
301 if (errno
== EINPROGRESS
)
306 * Close this socket and move to the next address...
316 addrlist
= addrlist
->next
;
321 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, "Connection failed", 0);
323 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
), 0);
331 * 'httpAddrFreeList()' - Free an address list.
333 * @since CUPS 1.2/Mac OS X 10.5@
338 http_addrlist_t
*addrlist
) /* I - Address list to free */
340 http_addrlist_t
*next
; /* Next address in list */
344 * Free each address in the list...
349 next
= addrlist
->next
;
359 * 'httpAddrGetList()' - Get a list of addresses for a hostname.
361 * @since CUPS 1.2/Mac OS X 10.5@
364 http_addrlist_t
* /* O - List of addresses or NULL */
365 httpAddrGetList(const char *hostname
, /* I - Hostname, IP address, or NULL for passive listen address */
366 int family
, /* I - Address family or AF_UNSPEC */
367 const char *service
) /* I - Service name or port number */
369 http_addrlist_t
*first
, /* First address in list */
370 *addr
, /* Current address in list */
371 *temp
; /* New address */
372 _cups_globals_t
*cg
= _cupsGlobals();
377 _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, "
379 hostname
? hostname
: "(nil)",
380 family
== AF_UNSPEC
? "UNSPEC" :
382 family
== AF_LOCAL
? "LOCAL" :
383 # endif /* AF_LOCAL */
385 family
== AF_INET6
? "INET6" :
386 # endif /* AF_INET6 */
387 family
== AF_INET
? "INET" : "???", service
);
392 * STR #2920: Initialize resolver after failure in cups-polld
394 * If the previous lookup failed, re-initialize the resolver to prevent
395 * temporary network errors from persisting. This *should* be handled by
396 * the resolver libraries, but apparently the glibc folks do not agree.
398 * We set a flag at the end of this function if we encounter an error that
399 * requires reinitialization of the resolver functions. We then call
400 * res_init() if the flag is set on the next call here or in httpAddrLookup().
403 if (cg
->need_res_init
)
407 cg
->need_res_init
= 0;
409 #endif /* HAVE_RES_INIT */
412 * Lookup the address the best way we can...
418 if (hostname
&& hostname
[0] == '/')
421 * Domain socket address...
424 if ((first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
))) != NULL
)
426 first
->addr
.un
.sun_family
= AF_LOCAL
;
427 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
431 #endif /* AF_LOCAL */
432 if (!hostname
|| _cups_strcasecmp(hostname
, "localhost"))
434 #ifdef HAVE_GETADDRINFO
435 struct addrinfo hints
, /* Address lookup hints */
436 *results
, /* Address lookup results */
437 *current
; /* Current result */
438 char ipv6
[64], /* IPv6 address */
439 *ipv6zone
; /* Pointer to zone separator */
440 int ipv6len
; /* Length of IPv6 address */
441 int error
; /* getaddrinfo() error */
445 * Lookup the address as needed...
448 memset(&hints
, 0, sizeof(hints
));
449 hints
.ai_family
= family
;
450 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
451 hints
.ai_socktype
= SOCK_STREAM
;
453 if (hostname
&& *hostname
== '[')
456 * Remove brackets from numeric IPv6 address...
459 if (!strncmp(hostname
, "[v1.", 4))
462 * Copy the newer address format which supports link-local addresses...
465 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
466 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
468 ipv6
[ipv6len
] = '\0';
472 * Convert "+zone" in address to "%zone"...
475 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
482 * Copy the regular non-link-local IPv6 address...
485 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
486 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
488 ipv6
[ipv6len
] = '\0';
494 if ((error
= getaddrinfo(hostname
, service
, &hints
, &results
)) == 0)
497 * Copy the results to our own address list structure...
500 for (current
= results
; current
; current
= current
->ai_next
)
501 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
504 * Copy the address over...
507 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
510 httpAddrFreeList(first
);
511 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
515 if (current
->ai_family
== AF_INET6
)
516 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
517 sizeof(temp
->addr
.ipv6
));
519 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
520 sizeof(temp
->addr
.ipv4
));
523 * Append the address to the list...
536 * Free the results from getaddrinfo()...
539 freeaddrinfo(results
);
543 if (error
== EAI_FAIL
)
544 cg
->need_res_init
= 1;
546 _cupsSetError(IPP_INTERNAL_ERROR
, gai_strerror(error
), 0);
552 int i
; /* Looping vars */
553 unsigned ip
[4]; /* IPv4 address components */
554 const char *ptr
; /* Pointer into hostname */
555 struct hostent
*host
; /* Result of lookup */
556 struct servent
*port
; /* Port number for service */
557 int portnum
; /* Port number */
561 * Lookup the service...
566 else if (isdigit(*service
& 255))
567 portnum
= atoi(service
);
568 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
569 portnum
= ntohs(port
->s_port
);
570 else if (!strcmp(service
, "http"))
572 else if (!strcmp(service
, "https"))
574 else if (!strcmp(service
, "ipp") || !strcmp(service
, "ipps"))
576 else if (!strcmp(service
, "lpd"))
578 else if (!strcmp(service
, "socket"))
584 * This code is needed because some operating systems have a
585 * buggy implementation of gethostbyname() that does not support
586 * IPv4 addresses. If the hostname string is an IPv4 address, then
587 * sscanf() is used to extract the IPv4 components. We then pack
588 * the components into an IPv4 address manually, since the
589 * inet_aton() function is deprecated. We use the htonl() macro
590 * to get the right byte order for the address.
593 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
598 * We have an IPv4 address; break it up and create an IPv4 address...
601 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
602 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
604 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
608 first
->addr
.ipv4
.sin_family
= AF_INET
;
609 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
611 ip
[2]) << 8) | ip
[3]));
612 first
->addr
.ipv4
.sin_port
= htons(portnum
);
615 else if ((host
= gethostbyname(hostname
)) != NULL
&&
617 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
619 host
->h_addrtype
== AF_INET
)
620 # endif /* AF_INET6 */
622 for (i
= 0; host
->h_addr_list
[i
]; i
++)
625 * Copy the address over...
628 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
631 httpAddrFreeList(first
);
636 if (host
->h_addrtype
== AF_INET6
)
638 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
639 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
640 sizeof(temp
->addr
.ipv6
));
641 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
644 # endif /* AF_INET6 */
646 temp
->addr
.ipv4
.sin_family
= AF_INET
;
647 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
648 sizeof(temp
->addr
.ipv4
));
649 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
653 * Append the address to the list...
667 if (h_errno
== NO_RECOVERY
)
668 cg
->need_res_init
= 1;
670 _cupsSetError(IPP_INTERNAL_ERROR
, hstrerror(h_errno
), 0);
673 #endif /* HAVE_GETADDRINFO */
677 * Detect some common errors and handle them sanely...
680 if (!addr
&& (!hostname
|| !_cups_strcasecmp(hostname
, "localhost")))
682 struct servent
*port
; /* Port number for service */
683 int portnum
; /* Port number */
687 * Lookup the service...
692 else if (isdigit(*service
& 255))
693 portnum
= atoi(service
);
694 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
695 portnum
= ntohs(port
->s_port
);
696 else if (!strcmp(service
, "http"))
698 else if (!strcmp(service
, "https"))
700 else if (!strcmp(service
, "ipp") || !strcmp(service
, "ipps"))
702 else if (!strcmp(service
, "lpd"))
704 else if (!strcmp(service
, "socket"))
708 httpAddrFreeList(first
);
710 _cupsSetError(IPP_INTERNAL_ERROR
, _("Unknown service name."), 1);
714 if (hostname
&& !_cups_strcasecmp(hostname
, "localhost"))
717 * Unfortunately, some users ignore all of the warnings in the
718 * /etc/hosts file and delete "localhost" from it. If we get here
719 * then we were unable to resolve the name, so use the IPv6 and/or
720 * IPv4 loopback interface addresses...
724 if (family
!= AF_INET
)
727 * Add [::1] to the address list...
730 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
733 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
734 httpAddrFreeList(first
);
738 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
739 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
741 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
743 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
752 if (family
!= AF_INET6
)
753 #endif /* AF_INET6 */
756 * Add 127.0.0.1 to the address list...
759 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
762 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
763 httpAddrFreeList(first
);
767 temp
->addr
.ipv4
.sin_family
= AF_INET
;
768 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
769 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
781 * Provide one or more passive listening addresses...
785 if (family
!= AF_INET
)
788 * Add [::] to the address list...
791 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
794 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
795 httpAddrFreeList(first
);
799 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
800 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
808 if (family
!= AF_INET6
)
809 #endif /* AF_INET6 */
812 * Add 0.0.0.0 to the address list...
815 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
818 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
819 httpAddrFreeList(first
);
823 temp
->addr
.ipv4
.sin_family
= AF_INET
;
824 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
836 * Return the address list...
844 * End of "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $".