]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addrlist.c
effe2e6a80ea11e54546f6a4e98a605d34de84e1
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 * 'httpAddrConnect()' - Connect to any of the addresses in the list.
38 http_addrlist_t
* /* O - Connected address or NULL on failure */
40 http_addrlist_t
*addrlist
, /* I - List of potential addresses */
41 int *sock
) /* O - Socket */
43 int val
; /* Socket option value */
47 * Loop through each address until we connect or run out of addresses...
53 * Create the socket...
56 if ((*sock
= (int)socket(addrlist
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
59 * Don't abort yet, as this could just be an issue with the local
60 * system not being configured with IPv4/IPv6/domain socket enabled...
63 addrlist
= addrlist
->next
;
73 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
,
76 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
81 setsockopt(*sock
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
));
82 #endif /* SO_REUSEPORT */
86 setsockopt(*sock
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof(val
));
87 #endif /* SO_NOSIGPIPE */
90 * Using TCP_NODELAY improves responsiveness, especially on systems
91 * with a slow loopback interface...
96 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, (const char *)&val
,
99 setsockopt(*sock
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
104 * Close this socket when starting another process...
107 fcntl(*sock
, F_SETFD
, FD_CLOEXEC
);
108 #endif /* FD_CLOEXEC */
114 if (!connect(*sock
, &(addrlist
->addr
.addr
),
115 httpAddrLength(&(addrlist
->addr
))))
119 * Close this socket and move to the next address...
128 addrlist
= addrlist
->next
;
136 * 'httpAddrFreeList()' - Free an address list.
143 http_addrlist_t
*addrlist
) /* I - Address list to free */
145 http_addrlist_t
*next
; /* Next address in list */
149 * Free each address in the list...
154 next
= addrlist
->next
;
164 * 'httpAddrGetList()' - Get a list of addresses for a hostname.
169 http_addrlist_t
* /* O - List of addresses or NULL */
170 httpAddrGetList(const char *hostname
, /* I - Hostname, IP address, or NULL for passive listen address */
171 int family
, /* I - Address family or AF_UNSPEC */
172 const char *service
) /* I - Service name or port number */
174 http_addrlist_t
*first
, /* First address in list */
175 *addr
, /* Current address in list */
176 *temp
; /* New address */
180 _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, "
182 hostname
? hostname
: "(nil)",
183 family
== AF_UNSPEC
? "UNSPEC" :
185 family
== AF_LOCAL
? "LOCAL" :
186 # endif /* AF_LOCAL */
188 family
== AF_INET6
? "INET6" :
189 # endif /* AF_INET6 */
190 family
== AF_INET
? "INET" : "???", service
);
194 * Lookup the address the best way we can...
200 if (hostname
&& hostname
[0] == '/')
203 * Domain socket address...
206 if ((first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
))) != NULL
)
208 first
->addr
.un
.sun_family
= AF_LOCAL
;
209 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
213 #endif /* AF_LOCAL */
215 #ifdef HAVE_GETADDRINFO
216 struct addrinfo hints
, /* Address lookup hints */
217 *results
, /* Address lookup results */
218 *current
; /* Current result */
219 char ipv6
[1024], /* IPv6 address */
220 *ipv6zone
; /* Pointer to zone separator */
221 int ipv6len
; /* Length of IPv6 address */
224 * Lookup the address as needed...
227 memset(&hints
, 0, sizeof(hints
));
228 hints
.ai_family
= family
;
229 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
230 hints
.ai_socktype
= SOCK_STREAM
;
232 if (hostname
&& *hostname
== '[')
235 * Remove brackets from numeric IPv6 address...
238 if (!strncmp(hostname
, "[v1.", 4))
241 * Copy the newer address format which supports link-local addresses...
244 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
245 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
247 ipv6
[ipv6len
] = '\0';
251 * Convert "+zone" in address to "%zone"...
254 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
261 * Copy the regular non-link-local IPv6 address...
264 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
265 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
267 ipv6
[ipv6len
] = '\0';
273 if (!getaddrinfo(hostname
, service
, &hints
, &results
))
276 * Copy the results to our own address list structure...
279 for (current
= results
; current
; current
= current
->ai_next
)
280 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
283 * Copy the address over...
286 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
289 httpAddrFreeList(first
);
293 if (current
->ai_family
== AF_INET6
)
294 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
295 sizeof(temp
->addr
.ipv6
));
297 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
298 sizeof(temp
->addr
.ipv4
));
301 * Append the address to the list...
314 * Free the results from getaddrinfo()...
317 freeaddrinfo(results
);
322 int i
; /* Looping vars */
323 unsigned ip
[4]; /* IPv4 address components */
324 const char *ptr
; /* Pointer into hostname */
325 struct hostent
*host
; /* Result of lookup */
326 struct servent
*port
; /* Port number for service */
327 int portnum
; /* Port number */
331 * Lookup the service...
336 else if (isdigit(*service
& 255))
337 portnum
= atoi(service
);
338 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
339 portnum
= ntohs(port
->s_port
);
340 else if (!strcmp(service
, "http"))
342 else if (!strcmp(service
, "https"))
344 else if (!strcmp(service
, "ipp"))
346 else if (!strcmp(service
, "lpd"))
348 else if (!strcmp(service
, "socket"))
354 * This code is needed because some operating systems have a
355 * buggy implementation of gethostbyname() that does not support
356 * IPv4 addresses. If the hostname string is an IPv4 address, then
357 * sscanf() is used to extract the IPv4 components. We then pack
358 * the components into an IPv4 address manually, since the
359 * inet_aton() function is deprecated. We use the htonl() macro
360 * to get the right byte order for the address.
363 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
368 * We have an IPv4 address; break it up and create an IPv4 address...
371 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
372 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
374 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
378 first
->addr
.ipv4
.sin_family
= AF_INET
;
379 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
381 ip
[2]) << 8) | ip
[3]));
382 first
->addr
.ipv4
.sin_port
= htons(portnum
);
385 else if ((host
= gethostbyname(hostname
)) != NULL
&&
387 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
389 host
->h_addrtype
== AF_INET
)
390 # endif /* AF_INET6 */
392 for (i
= 0; host
->h_addr_list
[i
]; i
++)
395 * Copy the address over...
398 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
401 httpAddrFreeList(first
);
406 if (host
->h_addrtype
== AF_INET6
)
408 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
409 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
410 sizeof(temp
->addr
.ipv6
));
411 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
414 # endif /* AF_INET6 */
416 temp
->addr
.ipv4
.sin_family
= AF_INET
;
417 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
418 sizeof(temp
->addr
.ipv4
));
419 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
423 * Append the address to the list...
436 #endif /* HAVE_GETADDRINFO */
440 * Detect some common errors and handle them sanely...
443 if (!addr
&& (!hostname
|| !strcmp(hostname
, "localhost")))
445 struct servent
*port
; /* Port number for service */
446 int portnum
; /* Port number */
450 * Lookup the service...
455 else if (isdigit(*service
& 255))
456 portnum
= atoi(service
);
457 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
458 portnum
= ntohs(port
->s_port
);
459 else if (!strcmp(service
, "http"))
461 else if (!strcmp(service
, "https"))
463 else if (!strcmp(service
, "ipp"))
465 else if (!strcmp(service
, "lpd"))
467 else if (!strcmp(service
, "socket"))
472 if (hostname
&& !strcmp(hostname
, "localhost"))
475 * Unfortunately, some users ignore all of the warnings in the
476 * /etc/hosts file and delete "localhost" from it. If we get here
477 * then we were unable to resolve the name, so use the IPv6 and/or
478 * IPv4 loopback interface addresses...
482 if (family
!= AF_INET
)
485 * Add [::1] to the address list...
488 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
491 httpAddrFreeList(first
);
495 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
496 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
498 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
500 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
509 if (family
!= AF_INET6
)
510 #endif /* AF_INET6 */
513 * Add 127.0.0.1 to the address list...
516 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
519 httpAddrFreeList(first
);
523 temp
->addr
.ipv4
.sin_family
= AF_INET
;
524 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
525 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
539 * Provide one or more passive listening addresses...
543 if (family
!= AF_INET
)
546 * Add [::] 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
);
565 if (family
!= AF_INET6
)
566 #endif /* AF_INET6 */
569 * Add 0.0.0.0 to the address list...
572 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
575 httpAddrFreeList(first
);
579 temp
->addr
.ipv4
.sin_family
= AF_INET
;
580 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
594 * Return the address list...
602 * End of "$Id: http-addrlist.c 7460 2008-04-16 02:19:54Z mike $".