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