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