]>
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-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 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 if ((first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
))) != NULL
)
207 first
->addr
.un
.sun_family
= AF_LOCAL
;
208 strlcpy(first
->addr
.un
.sun_path
, hostname
, sizeof(first
->addr
.un
.sun_path
));
212 #endif /* AF_LOCAL */
214 #ifdef HAVE_GETADDRINFO
215 struct addrinfo hints
, /* Address lookup hints */
216 *results
, /* Address lookup results */
217 *current
; /* Current result */
218 char ipv6
[1024], /* IPv6 address */
219 *ipv6zone
; /* Pointer to zone separator */
220 int ipv6len
; /* Length of IPv6 address */
223 * Lookup the address as needed...
226 memset(&hints
, 0, sizeof(hints
));
227 hints
.ai_family
= family
;
228 hints
.ai_flags
= hostname
? 0 : AI_PASSIVE
;
229 hints
.ai_socktype
= SOCK_STREAM
;
231 if (hostname
&& *hostname
== '[')
234 * Remove brackets from numeric IPv6 address...
237 if (!strncmp(hostname
, "[v1.", 4))
240 * Copy the newer address format which supports link-local addresses...
243 strlcpy(ipv6
, hostname
+ 4, sizeof(ipv6
));
244 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
246 ipv6
[ipv6len
] = '\0';
250 * Convert "+zone" in address to "%zone"...
253 if ((ipv6zone
= strrchr(ipv6
, '+')) != NULL
)
260 * Copy the regular non-link-local IPv6 address...
263 strlcpy(ipv6
, hostname
+ 1, sizeof(ipv6
));
264 if ((ipv6len
= (int)strlen(ipv6
) - 1) >= 0 && ipv6
[ipv6len
] == ']')
266 ipv6
[ipv6len
] = '\0';
272 if (!getaddrinfo(hostname
, service
, &hints
, &results
))
275 * Copy the results to our own address list structure...
278 for (current
= results
; current
; current
= current
->ai_next
)
279 if (current
->ai_family
== AF_INET
|| current
->ai_family
== AF_INET6
)
282 * Copy the address over...
285 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
288 httpAddrFreeList(first
);
292 if (current
->ai_family
== AF_INET6
)
293 memcpy(&(temp
->addr
.ipv6
), current
->ai_addr
,
294 sizeof(temp
->addr
.ipv6
));
296 memcpy(&(temp
->addr
.ipv4
), current
->ai_addr
,
297 sizeof(temp
->addr
.ipv4
));
300 * Append the address to the list...
313 * Free the results from getaddrinfo()...
316 freeaddrinfo(results
);
321 int i
; /* Looping vars */
322 unsigned ip
[4]; /* IPv4 address components */
323 const char *ptr
; /* Pointer into hostname */
324 struct hostent
*host
; /* Result of lookup */
325 struct servent
*port
; /* Port number for service */
326 int portnum
; /* Port number */
330 * Lookup the service...
335 else if (isdigit(*service
& 255))
336 portnum
= atoi(service
);
337 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
338 portnum
= ntohs(port
->s_port
);
339 else if (!strcmp(service
, "http"))
341 else if (!strcmp(service
, "https"))
343 else if (!strcmp(service
, "ipp"))
345 else if (!strcmp(service
, "lpd"))
347 else if (!strcmp(service
, "socket"))
353 * This code is needed because some operating systems have a
354 * buggy implementation of gethostbyname() that does not support
355 * IPv4 addresses. If the hostname string is an IPv4 address, then
356 * sscanf() is used to extract the IPv4 components. We then pack
357 * the components into an IPv4 address manually, since the
358 * inet_aton() function is deprecated. We use the htonl() macro
359 * to get the right byte order for the address.
362 for (ptr
= hostname
; isdigit(*ptr
& 255) || *ptr
== '.'; ptr
++);
367 * We have an IPv4 address; break it up and create an IPv4 address...
370 if (sscanf(hostname
, "%u.%u.%u.%u", ip
, ip
+ 1, ip
+ 2, ip
+ 3) == 4 &&
371 ip
[0] <= 255 && ip
[1] <= 255 && ip
[2] <= 255 && ip
[3] <= 255)
373 first
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
377 first
->addr
.ipv4
.sin_family
= AF_INET
;
378 first
->addr
.ipv4
.sin_addr
.s_addr
= htonl(((((((ip
[0] << 8) |
380 ip
[2]) << 8) | ip
[3]));
381 first
->addr
.ipv4
.sin_port
= htons(portnum
);
384 else if ((host
= gethostbyname(hostname
)) != NULL
&&
386 (host
->h_addrtype
== AF_INET
|| host
->h_addrtype
== AF_INET6
))
388 host
->h_addrtype
== AF_INET
)
389 # endif /* AF_INET6 */
391 for (i
= 0; host
->h_addr_list
[i
]; i
++)
394 * Copy the address over...
397 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
400 httpAddrFreeList(first
);
405 if (host
->h_addrtype
== AF_INET6
)
407 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
408 memcpy(&(temp
->addr
.ipv6
.sin6_addr
), host
->h_addr_list
[i
],
409 sizeof(temp
->addr
.ipv6
));
410 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
413 # endif /* AF_INET6 */
415 temp
->addr
.ipv4
.sin_family
= AF_INET
;
416 memcpy(&(temp
->addr
.ipv4
.sin_addr
), host
->h_addr_list
[i
],
417 sizeof(temp
->addr
.ipv4
));
418 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
422 * Append the address to the list...
435 #endif /* HAVE_GETADDRINFO */
439 * Detect some common errors and handle them sanely...
442 if (!addr
&& (!hostname
|| !strcmp(hostname
, "localhost")))
444 struct servent
*port
; /* Port number for service */
445 int portnum
; /* Port number */
449 * Lookup the service...
454 else if (isdigit(*service
& 255))
455 portnum
= atoi(service
);
456 else if ((port
= getservbyname(service
, NULL
)) != NULL
)
457 portnum
= ntohs(port
->s_port
);
458 else if (!strcmp(service
, "http"))
460 else if (!strcmp(service
, "https"))
462 else if (!strcmp(service
, "ipp"))
464 else if (!strcmp(service
, "lpd"))
466 else if (!strcmp(service
, "socket"))
471 if (hostname
&& !strcmp(hostname
, "localhost"))
474 * Unfortunately, some users ignore all of the warnings in the
475 * /etc/hosts file and delete "localhost" from it. If we get here
476 * then we were unable to resolve the name, so use the IPv6 and/or
477 * IPv4 loopback interface addresses...
481 if (family
!= AF_INET
)
484 * Add [::1] to the address list...
487 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
490 httpAddrFreeList(first
);
494 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
495 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
497 temp
->addr
.ipv6
.sin6_addr
.u
.Byte
[15] = 1;
499 temp
->addr
.ipv6
.sin6_addr
.s6_addr32
[3] = htonl(1);
508 if (family
!= AF_INET6
)
509 #endif /* AF_INET6 */
512 * Add 127.0.0.1 to the address list...
515 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
518 httpAddrFreeList(first
);
522 temp
->addr
.ipv4
.sin_family
= AF_INET
;
523 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
524 temp
->addr
.ipv4
.sin_addr
.s_addr
= htonl(0x7f000001);
538 * Provide one or more passive listening addresses...
542 if (family
!= AF_INET
)
545 * Add [::] to the address list...
548 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
551 httpAddrFreeList(first
);
555 temp
->addr
.ipv6
.sin6_family
= AF_INET6
;
556 temp
->addr
.ipv6
.sin6_port
= htons(portnum
);
564 if (family
!= AF_INET6
)
565 #endif /* AF_INET6 */
568 * Add 0.0.0.0 to the address list...
571 temp
= (http_addrlist_t
*)calloc(1, sizeof(http_addrlist_t
));
574 httpAddrFreeList(first
);
578 temp
->addr
.ipv4
.sin_family
= AF_INET
;
579 temp
->addr
.ipv4
.sin_port
= htons(portnum
);
593 * Return the address list...
601 * End of "$Id: http-addrlist.c 6649 2007-07-11 21:46:42Z mike $".