]>
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 */
38 * 'httpAddrConnect()' - Connect to any of the addresses in the list.
40 * @since CUPS 1.2/Mac OS X 10.5@
43 http_addrlist_t
* /* O - Connected address or NULL on failure */
45 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
46 int *sock
) /* O - Socket */
48 DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)", addrlist
, sock
));
50 return (httpAddrConnect2(addrlist
, sock
, 30000, NULL
));
55 * 'httpAddrConnect2()' - Connect to any of the addresses in the list with a
56 * timeout and optional cancel.
61 http_addrlist_t
* /* O - Connected address or NULL on failure */
63 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
64 int *sock
, /* O - Socket */
65 int msec
, /* I - Timeout in milliseconds */
66 int *cancel
) /* I - Pointer to "cancel" variable */
68 int val
; /* Socket option value */
70 socklen_t len
; /* Length of value */
71 http_addr_t peer
; /* Peer address */
72 int flags
, /* Socket flags */
73 remaining
; /* Remaining timeout */
75 struct pollfd pfd
; /* Polled file descriptor */
77 fd_set input_set
, /* select() input set */
78 output_set
; /* select() output set */
79 struct timeval timeout
; /* Timeout */
80 # endif /* HAVE_POLL */
81 int nfds
; /* Result from select()/poll() */
82 #endif /* O_NONBLOCK */
84 char temp
[256]; /* Temporary address string */
88 DEBUG_printf(("httpAddrConnect2(addrlist=%p, sock=%p, msec=%d, cancel=%p)",
89 addrlist
, sock
, msec
, cancel
));
94 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
98 if (cancel
&& *cancel
)
101 if (msec
<= 0 || getenv("CUPS_DISABLE_ASYNC_CONNECT"))
105 * Loop through each address until we connect or run out of addresses...
110 if (cancel
&& *cancel
)
114 * Create the socket...
117 DEBUG_printf(("2httpAddrConnect2: Trying %s:%d...",
118 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
119 _httpAddrPort(&(addrlist
->addr
))));
121 if ((*sock
= (int)socket(_httpAddrFamily(&(addrlist
->addr
)), SOCK_STREAM
,
125 * Don't abort yet, as this could just be an issue with the local
126 * system not being configured with IPv4/IPv6/domain socket enabled...
129 addrlist
= addrlist
->next
;
139 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
,
142 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
147 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
148 #endif /* SO_REUSEPORT */
152 setsockopt(*sock
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof(val
));
153 #endif /* SO_NOSIGPIPE */
156 * Using TCP_NODELAY improves responsiveness, especially on systems
157 * with a slow loopback interface...
162 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, (const char *)&val
,
165 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
170 * Close this socket when starting another process...
173 fcntl(*sock
, F_SETFD
, FD_CLOEXEC
);
174 #endif /* FD_CLOEXEC */
178 * Do an asynchronous connect by setting the socket non-blocking...
181 flags
= fcntl(*sock
, F_GETFL
, 0);
184 DEBUG_puts("httpAddrConnect2: Setting non-blocking connect()");
186 fcntl(*sock
, F_SETFL
, flags
| O_NONBLOCK
);
188 #endif /* O_NONBLOCK */
194 if (!connect(*sock
, &(addrlist
->addr
.addr
),
195 httpAddrLength(&(addrlist
->addr
))))
197 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...",
198 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
199 _httpAddrPort(&(addrlist
->addr
))));
202 fcntl(*sock
, F_SETFL
, flags
);
203 #endif /* O_NONBLOCK */
210 if (WSAGetLastError() == WSAEINPROGRESS
||
211 WSAGetLastError() == WSAEWOULDBLOCK
)
213 if (errno
== EINPROGRESS
|| errno
== EWOULDBLOCK
)
216 DEBUG_puts("1httpAddrConnect2: Finishing async connect()");
218 fcntl(*sock
, F_SETFL
, flags
);
220 for (remaining
= msec
; remaining
> 0; remaining
-= 250)
224 if (cancel
&& *cancel
)
227 * Close this socket and return...
230 DEBUG_puts("1httpAddrConnect2: Canceled connect()");
245 pfd
.events
= POLLIN
| POLLOUT
;
247 nfds
= poll(&pfd
, 1, remaining
> 250 ? 250 : remaining
);
249 DEBUG_printf(("1httpAddrConnect2: poll() returned %d (%d)", nfds
,
254 FD_SET(*sock
, &input_set
);
255 output_set
= input_set
;
258 timeout
.tv_usec
= (remaining
> 250 ? 250 : remaining
) * 1000;
260 nfds
= select(*sock
+ 1, &input_set
, &output_set
, NULL
, &timeout
);
262 DEBUG_printf(("1httpAddrConnect2: select() returned %d (%d)", nfds
,
264 # endif /* HAVE_POLL */
267 while (nfds
< 0 && (WSAGetLastError() == WSAEINTR
||
268 WSAGetLastError() == WSAEWOULDBLOCK
));
270 while (nfds
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
276 if (!getpeername(*sock
, (struct sockaddr
*)&peer
, &len
))
278 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...",
279 httpAddrString(&peer
, temp
, sizeof(temp
)),
280 _httpAddrPort(&peer
)));
289 #endif /* O_NONBLOCK */
291 DEBUG_printf(("1httpAddrConnect2: Unable to connect to %s:%d: %s",
292 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
293 _httpAddrPort(&(addrlist
->addr
)), strerror(errno
)));
296 if (errno
== EINPROGRESS
)
301 * Close this socket and move to the next address...
311 addrlist
= addrlist
->next
;
316 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, "Connection failed", 0);
318 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, strerror(errno
), 0);
326 * 'httpAddrFreeList()' - Free an address list.
328 * @since CUPS 1.2/Mac OS X 10.5@
333 http_addrlist_t
*addrlist
) /* I - Address list to free */
335 http_addrlist_t
*next
; /* Next address in list */
339 * Free each address in the list...
344 next
= addrlist
->next
;
354 * 'httpAddrGetList()' - Get a list of addresses for a hostname.
356 * @since CUPS 1.2/Mac OS X 10.5@
359 http_addrlist_t
* /* O - List of addresses or NULL */
360 httpAddrGetList(const char *hostname
, /* I - Hostname, IP address, or NULL for passive listen address */
361 int family
, /* I - Address family or AF_UNSPEC */
362 const char *service
) /* I - Service name or port number */
364 http_addrlist_t
*first
, /* First address in list */
365 *addr
, /* Current address in list */
366 *temp
; /* New address */
367 _cups_globals_t
*cg
= _cupsGlobals();
372 _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, "
374 hostname
? hostname
: "(nil)",
375 family
== AF_UNSPEC
? "UNSPEC" :
377 family
== AF_LOCAL
? "LOCAL" :
378 # endif /* AF_LOCAL */
380 family
== AF_INET6
? "INET6" :
381 # endif /* AF_INET6 */
382 family
== AF_INET
? "INET" : "???", service
);
387 * STR #2920: Initialize resolver after failure in cups-polld
389 * If the previous lookup failed, re-initialize the resolver to prevent
390 * temporary network errors from persisting. This *should* be handled by
391 * the resolver libraries, but apparently the glibc folks do not agree.
393 * We set a flag at the end of this function if we encounter an error that
394 * requires reinitialization of the resolver functions. We then call
395 * res_init() if the flag is set on the next call here or in httpAddrLookup().
398 if (cg
->need_res_init
)
402 cg
->need_res_init
= 0;
404 #endif /* HAVE_RES_INIT */
407 * Lookup the address the best way we can...
413 if (hostname
&& hostname
[0] == '/')
416 * Domain socket address...
419 if ((first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
))) != NULL
)
421 first
->addr
.un
.sun_family
= AF_LOCAL
;
422 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
426 #endif /* AF_LOCAL */
427 if (!hostname
|| _cups_strcasecmp(hostname
, "localhost"))
429 #ifdef HAVE_GETADDRINFO
430 struct addrinfo hints
, /* Address lookup hints */
431 *results
, /* Address lookup results */
432 *current
; /* Current result */
433 char ipv6
[64], /* IPv6 address */
434 *ipv6zone
; /* Pointer to zone separator */
435 int ipv6len
; /* Length of IPv6 address */
436 int error
; /* getaddrinfo() error */
440 * Lookup the address as needed...
443 memset(&hints
, 0, sizeof(hints
));
444 hints
.ai_family
= family
;
445 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
446 hints
.ai_socktype
= SOCK_STREAM
;
448 if (hostname
&& *hostname
== '[')
451 * Remove brackets from numeric IPv6 address...
454 if (!strncmp(hostname
, "[v1.", 4))
457 * Copy the newer address format which supports link-local addresses...
460 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
461 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
463 ipv6
[ipv6len
] = '\0';
467 * Convert "+zone" in address to "%zone"...
470 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
477 * Copy the regular non-link-local IPv6 address...
480 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
481 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
483 ipv6
[ipv6len
] = '\0';
489 if ((error
= getaddrinfo(hostname
, service
, &hints
, &results
)) == 0)
492 * Copy the results to our own address list structure...
495 for (current
= results
; current
; current
= current
->ai_next
)
496 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
499 * Copy the address over...
502 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
505 httpAddrFreeList(first
);
506 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
510 if (current
->ai_family
== AF_INET6
)
511 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
512 sizeof(temp
->addr
.ipv6
));
514 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
515 sizeof(temp
->addr
.ipv4
));
518 * Append the address to the list...
531 * Free the results from getaddrinfo()...
534 freeaddrinfo(results
);
538 if (error
== EAI_FAIL
)
539 cg
->need_res_init
= 1;
541 _cupsSetError(IPP_INTERNAL_ERROR
, gai_strerror(error
), 0);
547 int i
; /* Looping vars */
548 unsigned ip
[4]; /* IPv4 address components */
549 const char *ptr
; /* Pointer into hostname */
550 struct hostent
*host
; /* Result of lookup */
551 struct servent
*port
; /* Port number for service */
552 int portnum
; /* Port number */
556 * Lookup the service...
561 else if (isdigit(*service
& 255))
562 portnum
= atoi(service
);
563 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
564 portnum
= ntohs(port
->s_port
);
565 else if (!strcmp(service
, "http"))
567 else if (!strcmp(service
, "https"))
569 else if (!strcmp(service
, "ipp") || !strcmp(service
, "ipps"))
571 else if (!strcmp(service
, "lpd"))
573 else if (!strcmp(service
, "socket"))
579 * This code is needed because some operating systems have a
580 * buggy implementation of gethostbyname() that does not support
581 * IPv4 addresses. If the hostname string is an IPv4 address, then
582 * sscanf() is used to extract the IPv4 components. We then pack
583 * the components into an IPv4 address manually, since the
584 * inet_aton() function is deprecated. We use the htonl() macro
585 * to get the right byte order for the address.
588 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
593 * We have an IPv4 address; break it up and create an IPv4 address...
596 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
597 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
599 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
603 first
->addr
.ipv4
.sin_family
= AF_INET
;
604 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
606 ip
[2]) << 8) | ip
[3]));
607 first
->addr
.ipv4
.sin_port
= htons(portnum
);
610 else if ((host
= gethostbyname(hostname
)) != NULL
&&
612 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
614 host
->h_addrtype
== AF_INET
)
615 # endif /* AF_INET6 */
617 for (i
= 0; host
->h_addr_list
[i
]; i
++)
620 * Copy the address over...
623 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
626 httpAddrFreeList(first
);
631 if (host
->h_addrtype
== AF_INET6
)
633 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
634 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
635 sizeof(temp
->addr
.ipv6
));
636 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
639 # endif /* AF_INET6 */
641 temp
->addr
.ipv4
.sin_family
= AF_INET
;
642 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
643 sizeof(temp
->addr
.ipv4
));
644 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
648 * Append the address to the list...
662 if (h_errno
== NO_RECOVERY
)
663 cg
->need_res_init
= 1;
665 _cupsSetError(IPP_INTERNAL_ERROR
, hstrerror(h_errno
), 0);
668 #endif /* HAVE_GETADDRINFO */
672 * Detect some common errors and handle them sanely...
675 if (!addr
&& (!hostname
|| !_cups_strcasecmp(hostname
, "localhost")))
677 struct servent
*port
; /* Port number for service */
678 int portnum
; /* Port number */
682 * Lookup the service...
687 else if (isdigit(*service
& 255))
688 portnum
= atoi(service
);
689 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
690 portnum
= ntohs(port
->s_port
);
691 else if (!strcmp(service
, "http"))
693 else if (!strcmp(service
, "https"))
695 else if (!strcmp(service
, "ipp") || !strcmp(service
, "ipps"))
697 else if (!strcmp(service
, "lpd"))
699 else if (!strcmp(service
, "socket"))
703 httpAddrFreeList(first
);
705 _cupsSetError(IPP_INTERNAL_ERROR
, _("Unknown service name."), 1);
709 if (hostname
&& !_cups_strcasecmp(hostname
, "localhost"))
712 * Unfortunately, some users ignore all of the warnings in the
713 * /etc/hosts file and delete "localhost" from it. If we get here
714 * then we were unable to resolve the name, so use the IPv6 and/or
715 * IPv4 loopback interface addresses...
719 if (family
!= AF_INET
)
722 * Add [::1] to the address list...
725 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
728 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
729 httpAddrFreeList(first
);
733 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
734 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
736 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
738 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
747 if (family
!= AF_INET6
)
748 #endif /* AF_INET6 */
751 * Add 127.0.0.1 to the address list...
754 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
757 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
758 httpAddrFreeList(first
);
762 temp
->addr
.ipv4
.sin_family
= AF_INET
;
763 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
764 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
776 * Provide one or more passive listening addresses...
780 if (family
!= AF_INET
)
783 * Add [::] to the address list...
786 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
789 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
790 httpAddrFreeList(first
);
794 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
795 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
803 if (family
!= AF_INET6
)
804 #endif /* AF_INET6 */
807 * Add 0.0.0.0 to the address list...
810 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
813 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
814 httpAddrFreeList(first
);
818 temp
->addr
.ipv4
.sin_family
= AF_INET
;
819 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
831 * Return the address list...
839 * End of "$Id: http-addrlist.c 7910 2008-09-06 00:25:17Z mike $".