]>
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"
34 * 'httpAddrConnect()' - Connect to any of the addresses in the list.
39 http_addrlist_t
* /* O - Connected address or NULL on failure */
41 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
42 int *sock
) /* O - Socket */
44 int val
; /* Socket option value */
46 char temp
[256]; /* Temporary address string */
50 DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)\n", addrlist
, sock
));
59 * Loop through each address until we connect or run out of addresses...
65 * Create the socket...
68 DEBUG_printf(("httpAddrConnect: Trying %s:%d...\n",
69 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
70 _httpAddrPort(&(addrlist
->addr
))));
72 if ((*sock
= (int)socket(addrlist
->addr
.addr
.sa_family
, SOCK_STREAM
,
76 * Don't abort yet, as this could just be an issue with the local
77 * system not being configured with IPv4/IPv6/domain socket enabled...
80 addrlist
= addrlist
->next
;
90 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
,
93 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
98 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
99 #endif /* SO_REUSEPORT */
103 setsockopt(*sock
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof(val
));
104 #endif /* SO_NOSIGPIPE */
107 * Using TCP_NODELAY improves responsiveness, especially on systems
108 * with a slow loopback interface...
113 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, (const char *)&val
,
116 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
121 * Close this socket when starting another process...
124 fcntl(*sock
, F_SETFD
, FD_CLOEXEC
);
125 #endif /* FD_CLOEXEC */
131 if (!connect(*sock
, &(addrlist
->addr
.addr
),
132 httpAddrLength(&(addrlist
->addr
))))
134 DEBUG_printf(("httpAddrConnect: Connected to %s:%d...\n",
135 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
136 _httpAddrPort(&(addrlist
->addr
))));
140 DEBUG_printf(("httpAddrConnect: Unable to connect to %s:%d: %s\n",
141 httpAddrString(&(addrlist
->addr
), temp
, sizeof(temp
)),
142 _httpAddrPort(&(addrlist
->addr
)), strerror(errno
)));
145 * Close this socket and move to the next address...
155 addrlist
= addrlist
->next
;
163 * 'httpAddrFreeList()' - Free an address list.
170 http_addrlist_t
*addrlist
) /* I - Address list to free */
172 http_addrlist_t
*next
; /* Next address in list */
176 * Free each address in the list...
181 next
= addrlist
->next
;
191 * 'httpAddrGetList()' - Get a list of addresses for a hostname.
196 http_addrlist_t
* /* O - List of addresses or NULL */
197 httpAddrGetList(const char *hostname
, /* I - Hostname, IP address, or NULL for passive listen address */
198 int family
, /* I - Address family or AF_UNSPEC */
199 const char *service
) /* I - Service name or port number */
201 http_addrlist_t
*first
, /* First address in list */
202 *addr
, /* Current address in list */
203 *temp
; /* New address */
207 _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, "
209 hostname
? hostname
: "(nil)",
210 family
== AF_UNSPEC
? "UNSPEC" :
212 family
== AF_LOCAL
? "LOCAL" :
213 # endif /* AF_LOCAL */
215 family
== AF_INET6
? "INET6" :
216 # endif /* AF_INET6 */
217 family
== AF_INET
? "INET" : "???", service
);
221 * Lookup the address the best way we can...
227 if (hostname
&& hostname
[0] == '/')
230 * Domain socket address...
233 if ((first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
))) != NULL
)
235 first
->addr
.un
.sun_family
= AF_LOCAL
;
236 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
240 #endif /* AF_LOCAL */
242 #ifdef HAVE_GETADDRINFO
243 struct addrinfo hints
, /* Address lookup hints */
244 *results
, /* Address lookup results */
245 *current
; /* Current result */
246 char ipv6
[1024], /* IPv6 address */
247 *ipv6zone
; /* Pointer to zone separator */
248 int ipv6len
; /* Length of IPv6 address */
251 * Lookup the address as needed...
254 memset(&hints
, 0, sizeof(hints
));
255 hints
.ai_family
= family
;
256 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
257 hints
.ai_socktype
= SOCK_STREAM
;
259 if (hostname
&& *hostname
== '[')
262 * Remove brackets from numeric IPv6 address...
265 if (!strncmp(hostname
, "[v1.", 4))
268 * Copy the newer address format which supports link-local addresses...
271 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
272 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
274 ipv6
[ipv6len
] = '\0';
278 * Convert "+zone" in address to "%zone"...
281 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
288 * Copy the regular non-link-local IPv6 address...
291 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
292 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
294 ipv6
[ipv6len
] = '\0';
300 if (!getaddrinfo(hostname
, service
, &hints
, &results
))
303 * Copy the results to our own address list structure...
306 for (current
= results
; current
; current
= current
->ai_next
)
307 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
310 * Copy the address over...
313 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
316 httpAddrFreeList(first
);
320 if (current
->ai_family
== AF_INET6
)
321 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
322 sizeof(temp
->addr
.ipv6
));
324 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
325 sizeof(temp
->addr
.ipv4
));
328 * Append the address to the list...
341 * Free the results from getaddrinfo()...
344 freeaddrinfo(results
);
349 int i
; /* Looping vars */
350 unsigned ip
[4]; /* IPv4 address components */
351 const char *ptr
; /* Pointer into hostname */
352 struct hostent
*host
; /* Result of lookup */
353 struct servent
*port
; /* Port number for service */
354 int portnum
; /* Port number */
358 * Lookup the service...
363 else if (isdigit(*service
& 255))
364 portnum
= atoi(service
);
365 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
366 portnum
= ntohs(port
->s_port
);
367 else if (!strcmp(service
, "http"))
369 else if (!strcmp(service
, "https"))
371 else if (!strcmp(service
, "ipp"))
373 else if (!strcmp(service
, "lpd"))
375 else if (!strcmp(service
, "socket"))
381 * This code is needed because some operating systems have a
382 * buggy implementation of gethostbyname() that does not support
383 * IPv4 addresses. If the hostname string is an IPv4 address, then
384 * sscanf() is used to extract the IPv4 components. We then pack
385 * the components into an IPv4 address manually, since the
386 * inet_aton() function is deprecated. We use the htonl() macro
387 * to get the right byte order for the address.
390 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
395 * We have an IPv4 address; break it up and create an IPv4 address...
398 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
399 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
401 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
405 first
->addr
.ipv4
.sin_family
= AF_INET
;
406 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
408 ip
[2]) << 8) | ip
[3]));
409 first
->addr
.ipv4
.sin_port
= htons(portnum
);
412 else if ((host
= gethostbyname(hostname
)) != NULL
&&
414 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
416 host
->h_addrtype
== AF_INET
)
417 # endif /* AF_INET6 */
419 for (i
= 0; host
->h_addr_list
[i
]; i
++)
422 * Copy the address over...
425 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
428 httpAddrFreeList(first
);
433 if (host
->h_addrtype
== AF_INET6
)
435 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
436 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
437 sizeof(temp
->addr
.ipv6
));
438 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
441 # endif /* AF_INET6 */
443 temp
->addr
.ipv4
.sin_family
= AF_INET
;
444 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
445 sizeof(temp
->addr
.ipv4
));
446 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
450 * Append the address to the list...
463 #endif /* HAVE_GETADDRINFO */
467 * Detect some common errors and handle them sanely...
470 if (!addr
&& (!hostname
|| !strcmp(hostname
, "localhost")))
472 struct servent
*port
; /* Port number for service */
473 int portnum
; /* Port number */
477 * Lookup the service...
482 else if (isdigit(*service
& 255))
483 portnum
= atoi(service
);
484 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
485 portnum
= ntohs(port
->s_port
);
486 else if (!strcmp(service
, "http"))
488 else if (!strcmp(service
, "https"))
490 else if (!strcmp(service
, "ipp"))
492 else if (!strcmp(service
, "lpd"))
494 else if (!strcmp(service
, "socket"))
499 if (hostname
&& !strcmp(hostname
, "localhost"))
502 * Unfortunately, some users ignore all of the warnings in the
503 * /etc/hosts file and delete "localhost" from it. If we get here
504 * then we were unable to resolve the name, so use the IPv6 and/or
505 * IPv4 loopback interface addresses...
509 if (family
!= AF_INET
)
512 * Add [::1] to the address list...
515 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
518 httpAddrFreeList(first
);
522 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
523 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
525 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
527 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
536 if (family
!= AF_INET6
)
537 #endif /* AF_INET6 */
540 * Add 127.0.0.1 to the address list...
543 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
546 httpAddrFreeList(first
);
550 temp
->addr
.ipv4
.sin_family
= AF_INET
;
551 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
552 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
564 * Provide one or more passive listening addresses...
568 if (family
!= AF_INET
)
571 * Add [::] to the address list...
574 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
577 httpAddrFreeList(first
);
581 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
582 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
590 if (family
!= AF_INET6
)
591 #endif /* AF_INET6 */
594 * Add 0.0.0.0 to the address list...
597 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
600 httpAddrFreeList(first
);
604 temp
->addr
.ipv4
.sin_family
= AF_INET
;
605 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
617 * Return the address list...
625 * End of "$Id: http-addrlist.c 7460 2008-04-16 02:19:54Z mike $".