]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/http-addrlist.c
Update changelog.
[thirdparty/cups.git] / cups / http-addrlist.c
CommitLineData
ef416fc2 1/*
da003234 2 * HTTP address list routines for CUPS.
ef416fc2 3 *
6fb588e0 4 * Copyright 2007-2016 by Apple Inc.
da003234 5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 6 *
da003234
MS
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
6539a0af
MS
12 *
13 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 14 */
15
16/*
17 * Include necessary headers...
18 */
19
71e16022 20#include "cups-private.h"
49d87452
MS
21#ifdef HAVE_RESOLV_H
22# include <resolv.h>
23#endif /* HAVE_RESOLV_H */
dcb445bc
MS
24#ifdef HAVE_POLL
25# include <poll.h>
26#endif /* HAVE_POLL */
82cc1f9a 27#ifndef WIN32
6961465f 28# include <fcntl.h>
82cc1f9a 29#endif /* WIN32 */
ef416fc2 30
31
32/*
33 * 'httpAddrConnect()' - Connect to any of the addresses in the list.
34 *
8072030b 35 * @since CUPS 1.2/macOS 10.5@
ef416fc2 36 */
37
38http_addrlist_t * /* O - Connected address or NULL on failure */
39httpAddrConnect(
40 http_addrlist_t *addrlist, /* I - List of potential addresses */
41 int *sock) /* O - Socket */
dcb445bc 42{
807315e6 43 DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)", (void *)addrlist, (void *)sock));
dcb445bc
MS
44
45 return (httpAddrConnect2(addrlist, sock, 30000, NULL));
46}
47
48
49/*
50 * 'httpAddrConnect2()' - Connect to any of the addresses in the list with a
51 * timeout and optional cancel.
52 *
8072030b 53 * @since CUPS 1.7/macOS 10.9@
dcb445bc
MS
54 */
55
56http_addrlist_t * /* O - Connected address or NULL on failure */
57httpAddrConnect2(
58 http_addrlist_t *addrlist, /* I - List of potential addresses */
59 int *sock, /* O - Socket */
60 int msec, /* I - Timeout in milliseconds */
61 int *cancel) /* I - Pointer to "cancel" variable */
ef416fc2 62{
10d09e33 63 int val; /* Socket option value */
21aa3f37
MS
64#ifndef WIN32
65 int flags; /* Socket flags */
66#endif /* !WIN32 */
67 int remaining; /* Remaining timeout */
d5cc05c9
MS
68 int i, /* Looping var */
69 nfds, /* Number of file descriptors */
70 fds[100], /* Socket file descriptors */
71 result; /* Result from select() or poll() */
72 http_addrlist_t *addrs[100]; /* Addresses */
21aa3f37
MS
73#ifndef HAVE_POLL
74 int max_fd = -1; /* Highest file descriptor */
75#endif /* !HAVE_POLL */
76#ifdef O_NONBLOCK
12f89d24 77# ifdef HAVE_POLL
d5cc05c9 78 struct pollfd pfds[100]; /* Polled file descriptors */
12f89d24 79# else
dcb445bc 80 fd_set input_set, /* select() input set */
4c3f8a9b
MS
81 output_set, /* select() output set */
82 error_set; /* select() error set */
dcb445bc 83 struct timeval timeout; /* Timeout */
12f89d24 84# endif /* HAVE_POLL */
12f89d24 85#endif /* O_NONBLOCK */
1ff0402e 86#ifdef DEBUG
6fb588e0
MS
87 socklen_t len; /* Length of value */
88 http_addr_t peer; /* Peer address */
10d09e33 89 char temp[256]; /* Temporary address string */
1ff0402e 90#endif /* DEBUG */
ef416fc2 91
92
807315e6 93 DEBUG_printf(("httpAddrConnect2(addrlist=%p, sock=%p, msec=%d, cancel=%p)", (void *)addrlist, (void *)sock, msec, (void *)cancel));
1ff0402e
MS
94
95 if (!sock)
96 {
97 errno = EINVAL;
cb7f98ee 98 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
1ff0402e
MS
99 return (NULL);
100 }
101
dcb445bc
MS
102 if (cancel && *cancel)
103 return (NULL);
104
d5cc05c9 105 if (msec <= 0)
dcb445bc
MS
106 msec = INT_MAX;
107
ef416fc2 108 /*
109 * Loop through each address until we connect or run out of addresses...
110 */
111
6fb588e0
MS
112 nfds = 0;
113 remaining = msec;
114
115 while (remaining > 0)
ef416fc2 116 {
12f89d24 117 if (cancel && *cancel)
d5cc05c9
MS
118 {
119 while (nfds > 0)
120 {
121 nfds --;
122 httpAddrClose(NULL, fds[nfds]);
123 }
124
12f89d24 125 return (NULL);
d5cc05c9 126 }
12f89d24 127
6fb588e0 128 if (addrlist && nfds < (int)(sizeof(fds) / sizeof(fds[0])))
ef416fc2 129 {
130 /*
6fb588e0 131 * Create the socket...
ef416fc2 132 */
133
6fb588e0 134 DEBUG_printf(("2httpAddrConnect2: Trying %s:%d...", httpAddrString(&(addrlist->addr), temp, sizeof(temp)), httpAddrPort(&(addrlist->addr))));
ef416fc2 135
6fb588e0
MS
136 if ((fds[nfds] = (int)socket(httpAddrFamily(&(addrlist->addr)), SOCK_STREAM, 0)) < 0)
137 {
138 /*
139 * Don't abort yet, as this could just be an issue with the local
34016d2a
MS
140 * system not being configured with IPv4/IPv6/domain socket enabled.
141 *
142 * Just skip this address...
6fb588e0 143 */
ef416fc2 144
34016d2a 145 addrlist = addrlist->next;
6fb588e0
MS
146 continue;
147 }
148
149 /*
150 * Set options...
151 */
152
153 val = 1;
154 setsockopt(fds[nfds], SOL_SOCKET, SO_REUSEADDR, CUPS_SOCAST &val, sizeof(val));
ef416fc2 155
156#ifdef SO_REUSEPORT
6fb588e0
MS
157 val = 1;
158 setsockopt(fds[nfds], SOL_SOCKET, SO_REUSEPORT, CUPS_SOCAST &val, sizeof(val));
ef416fc2 159#endif /* SO_REUSEPORT */
160
fa73b229 161#ifdef SO_NOSIGPIPE
6fb588e0
MS
162 val = 1;
163 setsockopt(fds[nfds], SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val));
fa73b229 164#endif /* SO_NOSIGPIPE */
165
6fb588e0
MS
166 /*
167 * Using TCP_NODELAY improves responsiveness, especially on systems
168 * with a slow loopback interface...
169 */
ef416fc2 170
6fb588e0
MS
171 val = 1;
172 setsockopt(fds[nfds], IPPROTO_TCP, TCP_NODELAY, CUPS_SOCAST &val, sizeof(val));
ef416fc2 173
174#ifdef FD_CLOEXEC
6fb588e0
MS
175 /*
176 * Close this socket when starting another process...
177 */
ef416fc2 178
6fb588e0 179 fcntl(fds[nfds], F_SETFD, FD_CLOEXEC);
ef416fc2 180#endif /* FD_CLOEXEC */
181
dcb445bc 182#ifdef O_NONBLOCK
6fb588e0
MS
183 /*
184 * Do an asynchronous connect by setting the socket non-blocking...
185 */
dcb445bc 186
6fb588e0 187 DEBUG_printf(("httpAddrConnect2: Setting non-blocking connect()"));
bb0d23b2 188
6fb588e0
MS
189 flags = fcntl(fds[nfds], F_GETFL, 0);
190 fcntl(fds[nfds], F_SETFL, flags | O_NONBLOCK);
dcb445bc
MS
191#endif /* O_NONBLOCK */
192
6fb588e0
MS
193 /*
194 * Then connect...
195 */
ef416fc2 196
6fb588e0
MS
197 if (!connect(fds[nfds], &(addrlist->addr.addr), (socklen_t)httpAddrLength(&(addrlist->addr))))
198 {
199 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...", httpAddrString(&(addrlist->addr), temp, sizeof(temp)), httpAddrPort(&(addrlist->addr))));
dcb445bc
MS
200
201#ifdef O_NONBLOCK
6fb588e0 202 fcntl(fds[nfds], F_SETFL, flags);
dcb445bc
MS
203#endif /* O_NONBLOCK */
204
6fb588e0 205 *sock = fds[nfds];
d5cc05c9 206
6fb588e0
MS
207 while (nfds > 0)
208 {
209 nfds --;
210 httpAddrClose(NULL, fds[nfds]);
211 }
d5cc05c9 212
6fb588e0
MS
213 return (addrlist);
214 }
dcb445bc 215
d5cc05c9 216#ifdef WIN32
6fb588e0 217 if (WSAGetLastError() != WSAEINPROGRESS && WSAGetLastError() != WSAEWOULDBLOCK)
d5cc05c9 218#else
6fb588e0 219 if (errno != EINPROGRESS && errno != EWOULDBLOCK)
d5cc05c9 220#endif /* WIN32 */
6fb588e0
MS
221 {
222 DEBUG_printf(("1httpAddrConnect2: Unable to connect to %s:%d: %s", httpAddrString(&(addrlist->addr), temp, sizeof(temp)), httpAddrPort(&(addrlist->addr)), strerror(errno)));
223 httpAddrClose(NULL, fds[nfds]);
34016d2a 224 addrlist = addrlist->next;
6fb588e0
MS
225 continue;
226 }
dcb445bc 227
21aa3f37 228#ifndef WIN32
6fb588e0 229 fcntl(fds[nfds], F_SETFL, flags);
21aa3f37 230#endif /* !WIN32 */
dcb445bc 231
d5cc05c9 232#ifndef HAVE_POLL
6fb588e0
MS
233 if (fds[nfds] > max_fd)
234 max_fd = fds[nfds];
d5cc05c9
MS
235#endif /* !HAVE_POLL */
236
6fb588e0
MS
237 addrs[nfds] = addrlist;
238 nfds ++;
239 addrlist = addrlist->next;
240 }
241
e8916f41
MS
242 if (!addrlist && nfds == 0)
243 break;
244
6fb588e0
MS
245 /*
246 * See if we can connect to any of the addresses so far...
247 */
d5cc05c9
MS
248
249#ifdef O_NONBLOCK
6fb588e0 250 DEBUG_puts("1httpAddrConnect2: Finishing async connect()");
d5cc05c9 251
d5cc05c9
MS
252 do
253 {
254 if (cancel && *cancel)
12f89d24 255 {
d5cc05c9
MS
256 /*
257 * Close this socket and return...
258 */
dcb445bc 259
d5cc05c9 260 DEBUG_puts("1httpAddrConnect2: Canceled connect()");
12f89d24 261
d5cc05c9
MS
262 while (nfds > 0)
263 {
264 nfds --;
265 httpAddrClose(NULL, fds[nfds]);
266 }
dcb445bc 267
d5cc05c9 268 *sock = -1;
dcb445bc 269
d5cc05c9
MS
270 return (NULL);
271 }
12f89d24
MS
272
273# ifdef HAVE_POLL
d5cc05c9
MS
274 for (i = 0; i < nfds; i ++)
275 {
6fb588e0
MS
276 pfds[i].fd = fds[i];
277 pfds[i].events = POLLIN | POLLOUT;
d5cc05c9 278 }
12f89d24 279
6fb588e0 280 result = poll(pfds, (nfds_t)nfds, addrlist ? 100 : remaining > 250 ? 250 : remaining);
12f89d24 281
d5cc05c9 282 DEBUG_printf(("1httpAddrConnect2: poll() returned %d (%d)", result, errno));
dcb445bc 283
12f89d24 284# else
d5cc05c9
MS
285 FD_ZERO(&input_set);
286 for (i = 0; i < nfds; i ++)
6fb588e0 287 FD_SET(fds[i], &input_set);
d5cc05c9 288 output_set = input_set;
4c3f8a9b 289 error_set = input_set;
dcb445bc 290
d5cc05c9 291 timeout.tv_sec = 0;
6fb588e0 292 timeout.tv_usec = (addrlist ? 100 : remaining > 250 ? 250 : remaining) * 1000;
dcb445bc 293
4c3f8a9b 294 result = select(max_fd + 1, &input_set, &output_set, &error_set, &timeout);
12f89d24 295
d5cc05c9 296 DEBUG_printf(("1httpAddrConnect2: select() returned %d (%d)", result, errno));
12f89d24 297# endif /* HAVE_POLL */
d5cc05c9 298 }
12f89d24 299# ifdef WIN32
d5cc05c9 300 while (result < 0 && (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK));
12f89d24 301# else
d5cc05c9 302 while (result < 0 && (errno == EINTR || errno == EAGAIN));
12f89d24 303# endif /* WIN32 */
dcb445bc 304
d5cc05c9
MS
305 if (result > 0)
306 {
307 for (i = 0; i < nfds; i ++)
308 {
309# ifdef HAVE_POLL
6fb588e0 310 DEBUG_printf(("pfds[%d].revents=%x\n", i, pfds[i].revents));
4c3f8a9b 311 if (pfds[i].revents && !(pfds[i].revents & (POLLERR | POLLHUP)))
d5cc05c9 312# else
4c3f8a9b 313 if (FD_ISSET(fds[i], &input) && !FD_ISSET(fds[i], &error))
d5cc05c9 314# endif /* HAVE_POLL */
6fb588e0
MS
315 {
316 *sock = fds[i];
317 addrlist = addrs[i];
318
319# ifdef DEBUG
d5cc05c9
MS
320 len = sizeof(peer);
321 if (!getpeername(fds[i], (struct sockaddr *)&peer, &len))
d5cc05c9 322 DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...", httpAddrString(&peer, temp, sizeof(temp)), httpAddrPort(&peer)));
6fb588e0 323# endif /* DEBUG */
d5cc05c9
MS
324 }
325 else
326 httpAddrClose(NULL, fds[i]);
dcb445bc 327 }
d5cc05c9
MS
328
329 return (addrlist);
1ff0402e 330 }
dcb445bc 331#endif /* O_NONBLOCK */
1ff0402e 332
6fb588e0
MS
333 if (addrlist)
334 remaining -= 100;
335 else
336 remaining -= 250;
337 }
338
d5cc05c9
MS
339 while (nfds > 0)
340 {
341 nfds --;
342 httpAddrClose(NULL, fds[nfds]);
ef416fc2 343 }
344
12f89d24 345#ifdef WIN32
d5cc05c9 346 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, "Connection failed", 0);
12f89d24 347#else
d5cc05c9 348 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, strerror(errno), 0);
12f89d24 349#endif /* WIN32 */
7cf5915e 350
d5cc05c9 351 return (NULL);
ef416fc2 352}
353
354
a469f8a5
MS
355/*
356 * 'httpAddrCopyList()' - Copy an address list.
357 *
8072030b 358 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
359 */
360
361http_addrlist_t * /* O - New address list or @code NULL@ on error */
362httpAddrCopyList(
363 http_addrlist_t *src) /* I - Source address list */
364{
365 http_addrlist_t *dst = NULL, /* First list entry */
366 *prev = NULL, /* Previous list entry */
367 *current = NULL;/* Current list entry */
368
369
370 while (src)
371 {
372 if ((current = malloc(sizeof(http_addrlist_t))) == NULL)
373 {
374 current = dst;
375
376 while (current)
377 {
378 prev = current;
379 current = current->next;
380
381 free(prev);
382 }
383
384 return (NULL);
385 }
386
387 memcpy(current, src, sizeof(http_addrlist_t));
388
389 current->next = NULL;
390
391 if (prev)
392 prev->next = current;
393 else
394 dst = current;
395
db8b865d
MS
396 prev = current;
397 src = src->next;
a469f8a5
MS
398 }
399
400 return (dst);
401}
402
403
ef416fc2 404/*
405 * 'httpAddrFreeList()' - Free an address list.
406 *
8072030b 407 * @since CUPS 1.2/macOS 10.5@
ef416fc2 408 */
409
410void
411httpAddrFreeList(
412 http_addrlist_t *addrlist) /* I - Address list to free */
413{
414 http_addrlist_t *next; /* Next address in list */
415
416
417 /*
418 * Free each address in the list...
419 */
420
421 while (addrlist)
422 {
423 next = addrlist->next;
424
425 free(addrlist);
426
427 addrlist = next;
428 }
429}
430
431
432/*
433 * 'httpAddrGetList()' - Get a list of addresses for a hostname.
434 *
8072030b 435 * @since CUPS 1.2/macOS 10.5@
ef416fc2 436 */
437
438http_addrlist_t * /* O - List of addresses or NULL */
439httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for passive listen address */
440 int family, /* I - Address family or AF_UNSPEC */
441 const char *service) /* I - Service name or port number */
442{
443 http_addrlist_t *first, /* First address in list */
444 *addr, /* Current address in list */
445 *temp; /* New address */
49d87452
MS
446 _cups_globals_t *cg = _cupsGlobals();
447 /* Global data */
ef416fc2 448
449
450#ifdef DEBUG
ae71f5de
MS
451 _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, "
452 "service=\"%s\")\n",
453 hostname ? hostname : "(nil)",
454 family == AF_UNSPEC ? "UNSPEC" :
ef416fc2 455# ifdef AF_LOCAL
ae71f5de 456 family == AF_LOCAL ? "LOCAL" :
ef416fc2 457# endif /* AF_LOCAL */
458# ifdef AF_INET6
ae71f5de 459 family == AF_INET6 ? "INET6" :
ef416fc2 460# endif /* AF_INET6 */
ae71f5de 461 family == AF_INET ? "INET" : "???", service);
ef416fc2 462#endif /* DEBUG */
463
49d87452
MS
464#ifdef HAVE_RES_INIT
465 /*
466 * STR #2920: Initialize resolver after failure in cups-polld
467 *
468 * If the previous lookup failed, re-initialize the resolver to prevent
469 * temporary network errors from persisting. This *should* be handled by
470 * the resolver libraries, but apparently the glibc folks do not agree.
471 *
472 * We set a flag at the end of this function if we encounter an error that
473 * requires reinitialization of the resolver functions. We then call
474 * res_init() if the flag is set on the next call here or in httpAddrLookup().
475 */
476
477 if (cg->need_res_init)
478 {
479 res_init();
480
481 cg->need_res_init = 0;
482 }
483#endif /* HAVE_RES_INIT */
484
ef416fc2 485 /*
486 * Lookup the address the best way we can...
487 */
488
489 first = addr = NULL;
490
491#ifdef AF_LOCAL
492 if (hostname && hostname[0] == '/')
493 {
494 /*
495 * Domain socket address...
496 */
497
91c84a35
MS
498 if ((first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t))) != NULL)
499 {
b1564bae 500 addr = first;
91c84a35
MS
501 first->addr.un.sun_family = AF_LOCAL;
502 strlcpy(first->addr.un.sun_path, hostname, sizeof(first->addr.un.sun_path));
503 }
ef416fc2 504 }
505 else
506#endif /* AF_LOCAL */
88f9aafc 507 if (!hostname || _cups_strcasecmp(hostname, "localhost"))
ef416fc2 508 {
509#ifdef HAVE_GETADDRINFO
510 struct addrinfo hints, /* Address lookup hints */
511 *results, /* Address lookup results */
512 *current; /* Current result */
82f97232 513 char ipv6[64], /* IPv6 address */
ef416fc2 514 *ipv6zone; /* Pointer to zone separator */
515 int ipv6len; /* Length of IPv6 address */
49d87452
MS
516 int error; /* getaddrinfo() error */
517
ef416fc2 518
519 /*
520 * Lookup the address as needed...
521 */
522
523 memset(&hints, 0, sizeof(hints));
524 hints.ai_family = family;
525 hints.ai_flags = hostname ? 0 : AI_PASSIVE;
526 hints.ai_socktype = SOCK_STREAM;
527
528 if (hostname && *hostname == '[')
529 {
530 /*
531 * Remove brackets from numeric IPv6 address...
532 */
533
534 if (!strncmp(hostname, "[v1.", 4))
535 {
536 /*
537 * Copy the newer address format which supports link-local addresses...
538 */
539
540 strlcpy(ipv6, hostname + 4, sizeof(ipv6));
b86bc4cf 541 if ((ipv6len = (int)strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']')
ef416fc2 542 {
543 ipv6[ipv6len] = '\0';
544 hostname = ipv6;
545
546 /*
547 * Convert "+zone" in address to "%zone"...
548 */
549
550 if ((ipv6zone = strrchr(ipv6, '+')) != NULL)
551 *ipv6zone = '%';
552 }
553 }
554 else
555 {
556 /*
557 * Copy the regular non-link-local IPv6 address...
558 */
559
560 strlcpy(ipv6, hostname + 1, sizeof(ipv6));
b86bc4cf 561 if ((ipv6len = (int)strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']')
ef416fc2 562 {
563 ipv6[ipv6len] = '\0';
564 hostname = ipv6;
565 }
566 }
567 }
568
49d87452 569 if ((error = getaddrinfo(hostname, service, &hints, &results)) == 0)
ef416fc2 570 {
571 /*
572 * Copy the results to our own address list structure...
573 */
574
575 for (current = results; current; current = current->ai_next)
576 if (current->ai_family == AF_INET || current->ai_family == AF_INET6)
577 {
578 /*
579 * Copy the address over...
580 */
581
582 temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
583 if (!temp)
584 {
585 httpAddrFreeList(first);
cb7f98ee 586 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
ef416fc2 587 return (NULL);
588 }
589
590 if (current->ai_family == AF_INET6)
591 memcpy(&(temp->addr.ipv6), current->ai_addr,
592 sizeof(temp->addr.ipv6));
593 else
594 memcpy(&(temp->addr.ipv4), current->ai_addr,
595 sizeof(temp->addr.ipv4));
596
597 /*
598 * Append the address to the list...
599 */
600
601 if (!first)
602 first = temp;
603
604 if (addr)
605 addr->next = temp;
606
607 addr = temp;
608 }
609
610 /*
611 * Free the results from getaddrinfo()...
612 */
613
614 freeaddrinfo(results);
615 }
dcb445bc
MS
616 else
617 {
618 if (error == EAI_FAIL)
619 cg->need_res_init = 1;
620
cb7f98ee 621 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, gai_strerror(error), 0);
dcb445bc 622 }
49d87452 623
ef416fc2 624#else
625 if (hostname)
626 {
627 int i; /* Looping vars */
628 unsigned ip[4]; /* IPv4 address components */
629 const char *ptr; /* Pointer into hostname */
630 struct hostent *host; /* Result of lookup */
631 struct servent *port; /* Port number for service */
632 int portnum; /* Port number */
633
634
635 /*
636 * Lookup the service...
637 */
638
639 if (!service)
640 portnum = 0;
641 else if (isdigit(*service & 255))
642 portnum = atoi(service);
643 else if ((port = getservbyname(service, NULL)) != NULL)
644 portnum = ntohs(port->s_port);
645 else if (!strcmp(service, "http"))
646 portnum = 80;
647 else if (!strcmp(service, "https"))
648 portnum = 443;
321d8d57 649 else if (!strcmp(service, "ipp") || !strcmp(service, "ipps"))
ef416fc2 650 portnum = 631;
651 else if (!strcmp(service, "lpd"))
652 portnum = 515;
653 else if (!strcmp(service, "socket"))
654 portnum = 9100;
655 else
656 return (NULL);
657
658 /*
659 * This code is needed because some operating systems have a
660 * buggy implementation of gethostbyname() that does not support
661 * IPv4 addresses. If the hostname string is an IPv4 address, then
662 * sscanf() is used to extract the IPv4 components. We then pack
663 * the components into an IPv4 address manually, since the
664 * inet_aton() function is deprecated. We use the htonl() macro
665 * to get the right byte order for the address.
666 */
667
668 for (ptr = hostname; isdigit(*ptr & 255) || *ptr == '.'; ptr ++);
669
670 if (!*ptr)
671 {
672 /*
673 * We have an IPv4 address; break it up and create an IPv4 address...
674 */
675
676 if (sscanf(hostname, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) == 4 &&
677 ip[0] <= 255 && ip[1] <= 255 && ip[2] <= 255 && ip[3] <= 255)
678 {
679 first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
680 if (!first)
681 return (NULL);
682
683 first->addr.ipv4.sin_family = AF_INET;
da003234
MS
684 first->addr.ipv4.sin_addr.s_addr = htonl((((((((unsigned)ip[0] << 8) |
685 (unsigned)ip[1]) << 8) |
686 (unsigned)ip[2]) << 8) |
687 (unsigned)ip[3]));
ef416fc2 688 first->addr.ipv4.sin_port = htons(portnum);
689 }
690 }
691 else if ((host = gethostbyname(hostname)) != NULL &&
692# ifdef AF_INET6
693 (host->h_addrtype == AF_INET || host->h_addrtype == AF_INET6))
694# else
695 host->h_addrtype == AF_INET)
696# endif /* AF_INET6 */
697 {
698 for (i = 0; host->h_addr_list[i]; i ++)
699 {
700 /*
701 * Copy the address over...
702 */
703
704 temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
705 if (!temp)
706 {
707 httpAddrFreeList(first);
708 return (NULL);
709 }
710
711# ifdef AF_INET6
712 if (host->h_addrtype == AF_INET6)
713 {
d6ae789d 714 temp->addr.ipv6.sin6_family = AF_INET6;
ed486911 715 memcpy(&(temp->addr.ipv6.sin6_addr), host->h_addr_list[i],
ef416fc2 716 sizeof(temp->addr.ipv6));
717 temp->addr.ipv6.sin6_port = htons(portnum);
718 }
719 else
720# endif /* AF_INET6 */
721 {
d6ae789d 722 temp->addr.ipv4.sin_family = AF_INET;
ed486911 723 memcpy(&(temp->addr.ipv4.sin_addr), host->h_addr_list[i],
ef416fc2 724 sizeof(temp->addr.ipv4));
725 temp->addr.ipv4.sin_port = htons(portnum);
726 }
727
728 /*
729 * Append the address to the list...
730 */
731
732 if (!first)
733 first = temp;
734
735 if (addr)
736 addr->next = temp;
737
738 addr = temp;
739 }
740 }
dcb445bc
MS
741 else
742 {
743 if (h_errno == NO_RECOVERY)
744 cg->need_res_init = 1;
745
cb7f98ee 746 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, hstrerror(h_errno), 0);
dcb445bc 747 }
ef416fc2 748 }
749#endif /* HAVE_GETADDRINFO */
750 }
751
752 /*
753 * Detect some common errors and handle them sanely...
754 */
755
88f9aafc 756 if (!addr && (!hostname || !_cups_strcasecmp(hostname, "localhost")))
ef416fc2 757 {
758 struct servent *port; /* Port number for service */
759 int portnum; /* Port number */
760
761
762 /*
763 * Lookup the service...
764 */
765
766 if (!service)
767 portnum = 0;
768 else if (isdigit(*service & 255))
769 portnum = atoi(service);
770 else if ((port = getservbyname(service, NULL)) != NULL)
771 portnum = ntohs(port->s_port);
772 else if (!strcmp(service, "http"))
773 portnum = 80;
774 else if (!strcmp(service, "https"))
775 portnum = 443;
321d8d57 776 else if (!strcmp(service, "ipp") || !strcmp(service, "ipps"))
ef416fc2 777 portnum = 631;
778 else if (!strcmp(service, "lpd"))
779 portnum = 515;
780 else if (!strcmp(service, "socket"))
781 portnum = 9100;
782 else
321d8d57
MS
783 {
784 httpAddrFreeList(first);
dcb445bc 785
cb7f98ee 786 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown service name."), 1);
ef416fc2 787 return (NULL);
321d8d57 788 }
ef416fc2 789
88f9aafc 790 if (hostname && !_cups_strcasecmp(hostname, "localhost"))
ef416fc2 791 {
792 /*
793 * Unfortunately, some users ignore all of the warnings in the
794 * /etc/hosts file and delete "localhost" from it. If we get here
795 * then we were unable to resolve the name, so use the IPv6 and/or
796 * IPv4 loopback interface addresses...
797 */
798
799#ifdef AF_INET6
800 if (family != AF_INET)
801 {
802 /*
803 * Add [::1] to the address list...
804 */
805
806 temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
807 if (!temp)
808 {
cb7f98ee 809 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
ef416fc2 810 httpAddrFreeList(first);
811 return (NULL);
812 }
813
814 temp->addr.ipv6.sin6_family = AF_INET6;
815 temp->addr.ipv6.sin6_port = htons(portnum);
816# ifdef WIN32
817 temp->addr.ipv6.sin6_addr.u.Byte[15] = 1;
818# else
819 temp->addr.ipv6.sin6_addr.s6_addr32[3] = htonl(1);
820# endif /* WIN32 */
821
ed486911 822 if (!first)
823 first = temp;
824
ef416fc2 825 addr = temp;
826 }
827
828 if (family != AF_INET6)
829#endif /* AF_INET6 */
830 {
831 /*
832 * Add 127.0.0.1 to the address list...
833 */
834
835 temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
836 if (!temp)
837 {
cb7f98ee 838 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
ef416fc2 839 httpAddrFreeList(first);
840 return (NULL);
841 }
842
843 temp->addr.ipv4.sin_family = AF_INET;
844 temp->addr.ipv4.sin_port = htons(portnum);
845 temp->addr.ipv4.sin_addr.s_addr = htonl(0x7f000001);
846
ed486911 847 if (!first)
848 first = temp;
849
ef416fc2 850 if (addr)
851 addr->next = temp;
ef416fc2 852 }
853 }
854 else if (!hostname)
855 {
856 /*
857 * Provide one or more passive listening addresses...
858 */
859
860#ifdef AF_INET6
861 if (family != AF_INET)
862 {
863 /*
864 * Add [::] to the address list...
865 */
866
867 temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
868 if (!temp)
869 {
cb7f98ee 870 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
ef416fc2 871 httpAddrFreeList(first);
872 return (NULL);
873 }
874
875 temp->addr.ipv6.sin6_family = AF_INET6;
876 temp->addr.ipv6.sin6_port = htons(portnum);
877
ed486911 878 if (!first)
879 first = temp;
880
ef416fc2 881 addr = temp;
882 }
883
884 if (family != AF_INET6)
885#endif /* AF_INET6 */
886 {
887 /*
888 * Add 0.0.0.0 to the address list...
889 */
890
891 temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
892 if (!temp)
893 {
cb7f98ee 894 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
ef416fc2 895 httpAddrFreeList(first);
896 return (NULL);
897 }
898
899 temp->addr.ipv4.sin_family = AF_INET;
900 temp->addr.ipv4.sin_port = htons(portnum);
901
ed486911 902 if (!first)
903 first = temp;
904
ef416fc2 905 if (addr)
906 addr->next = temp;
ef416fc2 907 }
908 }
909 }
910
911 /*
912 * Return the address list...
913 */
914
915 return (first);
916}