]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-addrlist.c
2 * "$Id: http-addrlist.c 6649 2007-07-11 21:46:42Z mike $"
4 * HTTP address list routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, service=\"%s\")\n",
181 hostname
? hostname
: "(nil)",
182 family
== AF_UNSPEC
? "UNSPEC" :
184 family
== AF_LOCAL
? "LOCAL" :
185 # endif /* AF_LOCAL */
187 family
== AF_INET6
? "INET6" :
188 # endif /* AF_INET6 */
189 family
== AF_INET
? "INET" : "???", service
);
193 * Lookup the address the best way we can...
199 if (hostname
&& hostname
[0] == '/')
202 * Domain socket address...
205 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
206 first
->addr
.un
.sun_family
= AF_LOCAL
;
207 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
210 #endif /* AF_LOCAL */
212 #ifdef HAVE_GETADDRINFO
213 struct addrinfo hints
, /* Address lookup hints */
214 *results
, /* Address lookup results */
215 *current
; /* Current result */
216 char ipv6
[1024], /* IPv6 address */
217 *ipv6zone
; /* Pointer to zone separator */
218 int ipv6len
; /* Length of IPv6 address */
221 * Lookup the address as needed...
224 memset(&hints
, 0, sizeof(hints
));
225 hints
.ai_family
= family
;
226 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
227 hints
.ai_socktype
= SOCK_STREAM
;
229 if (hostname
&& *hostname
== '[')
232 * Remove brackets from numeric IPv6 address...
235 if (!strncmp(hostname
, "[v1.", 4))
238 * Copy the newer address format which supports link-local addresses...
241 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
242 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
244 ipv6
[ipv6len
] = '\0';
248 * Convert "+zone" in address to "%zone"...
251 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
258 * Copy the regular non-link-local IPv6 address...
261 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
262 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
264 ipv6
[ipv6len
] = '\0';
270 if (!getaddrinfo(hostname
, service
, &hints
, &results
))
273 * Copy the results to our own address list structure...
276 for (current
= results
; current
; current
= current
->ai_next
)
277 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
280 * Copy the address over...
283 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
286 httpAddrFreeList(first
);
290 if (current
->ai_family
== AF_INET6
)
291 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
292 sizeof(temp
->addr
.ipv6
));
294 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
295 sizeof(temp
->addr
.ipv4
));
298 * Append the address to the list...
311 * Free the results from getaddrinfo()...
314 freeaddrinfo(results
);
319 int i
; /* Looping vars */
320 unsigned ip
[4]; /* IPv4 address components */
321 const char *ptr
; /* Pointer into hostname */
322 struct hostent
*host
; /* Result of lookup */
323 struct servent
*port
; /* Port number for service */
324 int portnum
; /* Port number */
328 * Lookup the service...
333 else if (isdigit(*service
& 255))
334 portnum
= atoi(service
);
335 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
336 portnum
= ntohs(port
->s_port
);
337 else if (!strcmp(service
, "http"))
339 else if (!strcmp(service
, "https"))
341 else if (!strcmp(service
, "ipp"))
343 else if (!strcmp(service
, "lpd"))
345 else if (!strcmp(service
, "socket"))
351 * This code is needed because some operating systems have a
352 * buggy implementation of gethostbyname() that does not support
353 * IPv4 addresses. If the hostname string is an IPv4 address, then
354 * sscanf() is used to extract the IPv4 components. We then pack
355 * the components into an IPv4 address manually, since the
356 * inet_aton() function is deprecated. We use the htonl() macro
357 * to get the right byte order for the address.
360 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
365 * We have an IPv4 address; break it up and create an IPv4 address...
368 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
369 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
371 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
375 first
->addr
.ipv4
.sin_family
= AF_INET
;
376 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
378 ip
[2]) << 8) | ip
[3]));
379 first
->addr
.ipv4
.sin_port
= htons(portnum
);
382 else if ((host
= gethostbyname(hostname
)) != NULL
&&
384 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
386 host
->h_addrtype
== AF_INET
)
387 # endif /* AF_INET6 */
389 for (i
= 0; host
->h_addr_list
[i
]; i
++)
392 * Copy the address over...
395 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
398 httpAddrFreeList(first
);
403 if (host
->h_addrtype
== AF_INET6
)
405 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
406 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
407 sizeof(temp
->addr
.ipv6
));
408 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
411 # endif /* AF_INET6 */
413 temp
->addr
.ipv4
.sin_family
= AF_INET
;
414 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
415 sizeof(temp
->addr
.ipv4
));
416 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
420 * Append the address to the list...
433 #endif /* HAVE_GETADDRINFO */
437 * Detect some common errors and handle them sanely...
440 if (!addr
&& (!hostname
|| !strcmp(hostname
, "localhost")))
442 struct servent
*port
; /* Port number for service */
443 int portnum
; /* Port number */
447 * Lookup the service...
452 else if (isdigit(*service
& 255))
453 portnum
= atoi(service
);
454 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
455 portnum
= ntohs(port
->s_port
);
456 else if (!strcmp(service
, "http"))
458 else if (!strcmp(service
, "https"))
460 else if (!strcmp(service
, "ipp"))
462 else if (!strcmp(service
, "lpd"))
464 else if (!strcmp(service
, "socket"))
469 if (hostname
&& !strcmp(hostname
, "localhost"))
472 * Unfortunately, some users ignore all of the warnings in the
473 * /etc/hosts file and delete "localhost" from it. If we get here
474 * then we were unable to resolve the name, so use the IPv6 and/or
475 * IPv4 loopback interface addresses...
479 if (family
!= AF_INET
)
482 * Add [::1] to the address list...
485 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
488 httpAddrFreeList(first
);
492 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
493 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
495 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
497 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
506 if (family
!= AF_INET6
)
507 #endif /* AF_INET6 */
510 * Add 127.0.0.1 to the address list...
513 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
516 httpAddrFreeList(first
);
520 temp
->addr
.ipv4
.sin_family
= AF_INET
;
521 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
522 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
536 * Provide one or more passive listening addresses...
540 if (family
!= AF_INET
)
543 * Add [::] to the address list...
546 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
549 httpAddrFreeList(first
);
553 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
554 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
562 if (family
!= AF_INET6
)
563 #endif /* AF_INET6 */
566 * Add 0.0.0.0 to the address list...
569 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
572 httpAddrFreeList(first
);
576 temp
->addr
.ipv4
.sin_family
= AF_INET
;
577 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
591 * Return the address list...
599 * End of "$Id: http-addrlist.c 6649 2007-07-11 21:46:42Z mike $".