]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
da003234 | 2 | * HTTP address list routines for CUPS. |
ef416fc2 | 3 | * |
1a3ff20f | 4 | * Copyright 2007-2018 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 | |
57b7b66b | 11 | * 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 | * | |
53af7f21 | 35 | * @since CUPS 1.2/macOS 10.5@ @exclude all@ |
ef416fc2 | 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 */ | |
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 | ||
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 */ | |
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 */ | |
7b0a28e3 | 68 | int i, j, /* Looping vars */ |
d5cc05c9 MS |
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 | { | |
0ca77b3e MS |
307 | http_addrlist_t *connaddr = NULL; /* Connected address, if any */ |
308 | ||
d5cc05c9 MS |
309 | for (i = 0; i < nfds; i ++) |
310 | { | |
311 | # ifdef HAVE_POLL | |
6fb588e0 | 312 | DEBUG_printf(("pfds[%d].revents=%x\n", i, pfds[i].revents)); |
4c3f8a9b | 313 | if (pfds[i].revents && !(pfds[i].revents & (POLLERR | POLLHUP))) |
d5cc05c9 | 314 | # else |
c7505715 | 315 | if (FD_ISSET(fds[i], &input_set) && !FD_ISSET(fds[i], &error_set)) |
d5cc05c9 | 316 | # endif /* HAVE_POLL */ |
6fb588e0 MS |
317 | { |
318 | *sock = fds[i]; | |
0ca77b3e | 319 | connaddr = addrs[i]; |
6fb588e0 MS |
320 | |
321 | # ifdef DEBUG | |
d5cc05c9 MS |
322 | len = sizeof(peer); |
323 | if (!getpeername(fds[i], (struct sockaddr *)&peer, &len)) | |
d5cc05c9 | 324 | DEBUG_printf(("1httpAddrConnect2: Connected to %s:%d...", httpAddrString(&peer, temp, sizeof(temp)), httpAddrPort(&peer))); |
6fb588e0 | 325 | # endif /* DEBUG */ |
7b0a28e3 MS |
326 | |
327 | break; | |
d5cc05c9 | 328 | } |
0ca77b3e MS |
329 | # ifdef HAVE_POLL |
330 | else if (pfds[i].revents & (POLLERR | POLLHUP)) | |
331 | # else | |
c7505715 | 332 | else if (FD_ISSET(fds[i], &error_set)) |
0ca77b3e MS |
333 | # endif /* HAVE_POLL */ |
334 | { | |
335 | /* | |
336 | * Error on socket, remove from the "pool"... | |
337 | */ | |
338 | ||
d5cc05c9 | 339 | httpAddrClose(NULL, fds[i]); |
0ca77b3e MS |
340 | nfds --; |
341 | if (i < nfds) | |
342 | { | |
343 | memmove(fds + i, fds + i + 1, (size_t)(nfds - i) * (sizeof(fds[0]))); | |
344 | memmove(addrs + i, addrs + i + 1, (size_t)(nfds - i) * (sizeof(addrs[0]))); | |
345 | } | |
346 | i --; | |
347 | } | |
dcb445bc | 348 | } |
d5cc05c9 | 349 | |
0ca77b3e | 350 | if (connaddr) |
7b0a28e3 MS |
351 | { |
352 | /* | |
353 | * Connected on one address, close all of the other sockets we have so | |
354 | * far and return... | |
355 | */ | |
356 | ||
357 | for (j = 0; j < i; j ++) | |
358 | httpAddrClose(NULL, fds[j]); | |
359 | ||
360 | for (j ++; j < nfds; j ++) | |
361 | httpAddrClose(NULL, fds[j]); | |
362 | ||
0ca77b3e | 363 | return (connaddr); |
7b0a28e3 | 364 | } |
1ff0402e | 365 | } |
dcb445bc | 366 | #endif /* O_NONBLOCK */ |
1ff0402e | 367 | |
6fb588e0 MS |
368 | if (addrlist) |
369 | remaining -= 100; | |
370 | else | |
371 | remaining -= 250; | |
372 | } | |
373 | ||
d5cc05c9 MS |
374 | while (nfds > 0) |
375 | { | |
376 | nfds --; | |
377 | httpAddrClose(NULL, fds[nfds]); | |
ef416fc2 | 378 | } |
379 | ||
12f89d24 | 380 | #ifdef WIN32 |
d5cc05c9 | 381 | _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, "Connection failed", 0); |
12f89d24 | 382 | #else |
d5cc05c9 | 383 | _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, strerror(errno), 0); |
12f89d24 | 384 | #endif /* WIN32 */ |
7cf5915e | 385 | |
d5cc05c9 | 386 | return (NULL); |
ef416fc2 | 387 | } |
388 | ||
389 | ||
a469f8a5 MS |
390 | /* |
391 | * 'httpAddrCopyList()' - Copy an address list. | |
392 | * | |
8072030b | 393 | * @since CUPS 1.7/macOS 10.9@ |
a469f8a5 MS |
394 | */ |
395 | ||
396 | http_addrlist_t * /* O - New address list or @code NULL@ on error */ | |
397 | httpAddrCopyList( | |
398 | http_addrlist_t *src) /* I - Source address list */ | |
399 | { | |
400 | http_addrlist_t *dst = NULL, /* First list entry */ | |
401 | *prev = NULL, /* Previous list entry */ | |
402 | *current = NULL;/* Current list entry */ | |
403 | ||
404 | ||
405 | while (src) | |
406 | { | |
407 | if ((current = malloc(sizeof(http_addrlist_t))) == NULL) | |
408 | { | |
409 | current = dst; | |
410 | ||
411 | while (current) | |
412 | { | |
413 | prev = current; | |
414 | current = current->next; | |
415 | ||
416 | free(prev); | |
417 | } | |
418 | ||
419 | return (NULL); | |
420 | } | |
421 | ||
422 | memcpy(current, src, sizeof(http_addrlist_t)); | |
423 | ||
424 | current->next = NULL; | |
425 | ||
426 | if (prev) | |
427 | prev->next = current; | |
428 | else | |
429 | dst = current; | |
430 | ||
db8b865d MS |
431 | prev = current; |
432 | src = src->next; | |
a469f8a5 MS |
433 | } |
434 | ||
435 | return (dst); | |
436 | } | |
437 | ||
438 | ||
ef416fc2 | 439 | /* |
440 | * 'httpAddrFreeList()' - Free an address list. | |
441 | * | |
8072030b | 442 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 443 | */ |
444 | ||
445 | void | |
446 | httpAddrFreeList( | |
447 | http_addrlist_t *addrlist) /* I - Address list to free */ | |
448 | { | |
449 | http_addrlist_t *next; /* Next address in list */ | |
450 | ||
451 | ||
452 | /* | |
453 | * Free each address in the list... | |
454 | */ | |
455 | ||
456 | while (addrlist) | |
457 | { | |
458 | next = addrlist->next; | |
459 | ||
460 | free(addrlist); | |
461 | ||
462 | addrlist = next; | |
463 | } | |
464 | } | |
465 | ||
466 | ||
467 | /* | |
468 | * 'httpAddrGetList()' - Get a list of addresses for a hostname. | |
469 | * | |
8072030b | 470 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 471 | */ |
472 | ||
473 | http_addrlist_t * /* O - List of addresses or NULL */ | |
474 | httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for passive listen address */ | |
475 | int family, /* I - Address family or AF_UNSPEC */ | |
476 | const char *service) /* I - Service name or port number */ | |
477 | { | |
478 | http_addrlist_t *first, /* First address in list */ | |
479 | *addr, /* Current address in list */ | |
480 | *temp; /* New address */ | |
49d87452 MS |
481 | _cups_globals_t *cg = _cupsGlobals(); |
482 | /* Global data */ | |
ef416fc2 | 483 | |
484 | ||
485 | #ifdef DEBUG | |
ae71f5de MS |
486 | _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, " |
487 | "service=\"%s\")\n", | |
488 | hostname ? hostname : "(nil)", | |
489 | family == AF_UNSPEC ? "UNSPEC" : | |
ef416fc2 | 490 | # ifdef AF_LOCAL |
ae71f5de | 491 | family == AF_LOCAL ? "LOCAL" : |
ef416fc2 | 492 | # endif /* AF_LOCAL */ |
493 | # ifdef AF_INET6 | |
ae71f5de | 494 | family == AF_INET6 ? "INET6" : |
ef416fc2 | 495 | # endif /* AF_INET6 */ |
ae71f5de | 496 | family == AF_INET ? "INET" : "???", service); |
ef416fc2 | 497 | #endif /* DEBUG */ |
498 | ||
49d87452 MS |
499 | #ifdef HAVE_RES_INIT |
500 | /* | |
501 | * STR #2920: Initialize resolver after failure in cups-polld | |
502 | * | |
503 | * If the previous lookup failed, re-initialize the resolver to prevent | |
504 | * temporary network errors from persisting. This *should* be handled by | |
505 | * the resolver libraries, but apparently the glibc folks do not agree. | |
506 | * | |
507 | * We set a flag at the end of this function if we encounter an error that | |
508 | * requires reinitialization of the resolver functions. We then call | |
509 | * res_init() if the flag is set on the next call here or in httpAddrLookup(). | |
510 | */ | |
511 | ||
512 | if (cg->need_res_init) | |
513 | { | |
514 | res_init(); | |
515 | ||
516 | cg->need_res_init = 0; | |
517 | } | |
518 | #endif /* HAVE_RES_INIT */ | |
519 | ||
ef416fc2 | 520 | /* |
521 | * Lookup the address the best way we can... | |
522 | */ | |
523 | ||
524 | first = addr = NULL; | |
525 | ||
526 | #ifdef AF_LOCAL | |
527 | if (hostname && hostname[0] == '/') | |
528 | { | |
529 | /* | |
530 | * Domain socket address... | |
531 | */ | |
532 | ||
91c84a35 MS |
533 | if ((first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t))) != NULL) |
534 | { | |
b1564bae | 535 | addr = first; |
91c84a35 MS |
536 | first->addr.un.sun_family = AF_LOCAL; |
537 | strlcpy(first->addr.un.sun_path, hostname, sizeof(first->addr.un.sun_path)); | |
538 | } | |
ef416fc2 | 539 | } |
540 | else | |
541 | #endif /* AF_LOCAL */ | |
88f9aafc | 542 | if (!hostname || _cups_strcasecmp(hostname, "localhost")) |
ef416fc2 | 543 | { |
544 | #ifdef HAVE_GETADDRINFO | |
545 | struct addrinfo hints, /* Address lookup hints */ | |
546 | *results, /* Address lookup results */ | |
547 | *current; /* Current result */ | |
82f97232 | 548 | char ipv6[64], /* IPv6 address */ |
ef416fc2 | 549 | *ipv6zone; /* Pointer to zone separator */ |
550 | int ipv6len; /* Length of IPv6 address */ | |
49d87452 MS |
551 | int error; /* getaddrinfo() error */ |
552 | ||
ef416fc2 | 553 | |
554 | /* | |
555 | * Lookup the address as needed... | |
556 | */ | |
557 | ||
558 | memset(&hints, 0, sizeof(hints)); | |
559 | hints.ai_family = family; | |
560 | hints.ai_flags = hostname ? 0 : AI_PASSIVE; | |
561 | hints.ai_socktype = SOCK_STREAM; | |
562 | ||
563 | if (hostname && *hostname == '[') | |
564 | { | |
565 | /* | |
566 | * Remove brackets from numeric IPv6 address... | |
567 | */ | |
568 | ||
569 | if (!strncmp(hostname, "[v1.", 4)) | |
570 | { | |
571 | /* | |
572 | * Copy the newer address format which supports link-local addresses... | |
573 | */ | |
574 | ||
575 | strlcpy(ipv6, hostname + 4, sizeof(ipv6)); | |
b86bc4cf | 576 | if ((ipv6len = (int)strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']') |
ef416fc2 | 577 | { |
578 | ipv6[ipv6len] = '\0'; | |
579 | hostname = ipv6; | |
580 | ||
581 | /* | |
582 | * Convert "+zone" in address to "%zone"... | |
583 | */ | |
584 | ||
585 | if ((ipv6zone = strrchr(ipv6, '+')) != NULL) | |
586 | *ipv6zone = '%'; | |
587 | } | |
588 | } | |
589 | else | |
590 | { | |
591 | /* | |
592 | * Copy the regular non-link-local IPv6 address... | |
593 | */ | |
594 | ||
595 | strlcpy(ipv6, hostname + 1, sizeof(ipv6)); | |
b86bc4cf | 596 | if ((ipv6len = (int)strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']') |
ef416fc2 | 597 | { |
598 | ipv6[ipv6len] = '\0'; | |
599 | hostname = ipv6; | |
600 | } | |
601 | } | |
602 | } | |
603 | ||
49d87452 | 604 | if ((error = getaddrinfo(hostname, service, &hints, &results)) == 0) |
ef416fc2 | 605 | { |
606 | /* | |
607 | * Copy the results to our own address list structure... | |
608 | */ | |
609 | ||
610 | for (current = results; current; current = current->ai_next) | |
611 | if (current->ai_family == AF_INET || current->ai_family == AF_INET6) | |
612 | { | |
613 | /* | |
614 | * Copy the address over... | |
615 | */ | |
616 | ||
617 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
618 | if (!temp) | |
619 | { | |
620 | httpAddrFreeList(first); | |
1a3ff20f | 621 | freeaddrinfo(results); |
cb7f98ee | 622 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); |
ef416fc2 | 623 | return (NULL); |
624 | } | |
625 | ||
626 | if (current->ai_family == AF_INET6) | |
627 | memcpy(&(temp->addr.ipv6), current->ai_addr, | |
628 | sizeof(temp->addr.ipv6)); | |
629 | else | |
630 | memcpy(&(temp->addr.ipv4), current->ai_addr, | |
631 | sizeof(temp->addr.ipv4)); | |
632 | ||
633 | /* | |
634 | * Append the address to the list... | |
635 | */ | |
636 | ||
637 | if (!first) | |
638 | first = temp; | |
639 | ||
640 | if (addr) | |
641 | addr->next = temp; | |
642 | ||
643 | addr = temp; | |
644 | } | |
645 | ||
646 | /* | |
647 | * Free the results from getaddrinfo()... | |
648 | */ | |
649 | ||
650 | freeaddrinfo(results); | |
651 | } | |
dcb445bc MS |
652 | else |
653 | { | |
654 | if (error == EAI_FAIL) | |
655 | cg->need_res_init = 1; | |
656 | ||
cb7f98ee | 657 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, gai_strerror(error), 0); |
dcb445bc | 658 | } |
49d87452 | 659 | |
ef416fc2 | 660 | #else |
661 | if (hostname) | |
662 | { | |
663 | int i; /* Looping vars */ | |
664 | unsigned ip[4]; /* IPv4 address components */ | |
665 | const char *ptr; /* Pointer into hostname */ | |
666 | struct hostent *host; /* Result of lookup */ | |
667 | struct servent *port; /* Port number for service */ | |
668 | int portnum; /* Port number */ | |
669 | ||
670 | ||
671 | /* | |
672 | * Lookup the service... | |
673 | */ | |
674 | ||
675 | if (!service) | |
676 | portnum = 0; | |
677 | else if (isdigit(*service & 255)) | |
678 | portnum = atoi(service); | |
679 | else if ((port = getservbyname(service, NULL)) != NULL) | |
680 | portnum = ntohs(port->s_port); | |
681 | else if (!strcmp(service, "http")) | |
682 | portnum = 80; | |
683 | else if (!strcmp(service, "https")) | |
684 | portnum = 443; | |
321d8d57 | 685 | else if (!strcmp(service, "ipp") || !strcmp(service, "ipps")) |
ef416fc2 | 686 | portnum = 631; |
687 | else if (!strcmp(service, "lpd")) | |
688 | portnum = 515; | |
689 | else if (!strcmp(service, "socket")) | |
690 | portnum = 9100; | |
691 | else | |
692 | return (NULL); | |
693 | ||
694 | /* | |
695 | * This code is needed because some operating systems have a | |
696 | * buggy implementation of gethostbyname() that does not support | |
697 | * IPv4 addresses. If the hostname string is an IPv4 address, then | |
698 | * sscanf() is used to extract the IPv4 components. We then pack | |
699 | * the components into an IPv4 address manually, since the | |
700 | * inet_aton() function is deprecated. We use the htonl() macro | |
701 | * to get the right byte order for the address. | |
702 | */ | |
703 | ||
704 | for (ptr = hostname; isdigit(*ptr & 255) || *ptr == '.'; ptr ++); | |
705 | ||
706 | if (!*ptr) | |
707 | { | |
708 | /* | |
709 | * We have an IPv4 address; break it up and create an IPv4 address... | |
710 | */ | |
711 | ||
712 | if (sscanf(hostname, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) == 4 && | |
713 | ip[0] <= 255 && ip[1] <= 255 && ip[2] <= 255 && ip[3] <= 255) | |
714 | { | |
715 | first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
716 | if (!first) | |
717 | return (NULL); | |
718 | ||
719 | first->addr.ipv4.sin_family = AF_INET; | |
da003234 MS |
720 | first->addr.ipv4.sin_addr.s_addr = htonl((((((((unsigned)ip[0] << 8) | |
721 | (unsigned)ip[1]) << 8) | | |
722 | (unsigned)ip[2]) << 8) | | |
723 | (unsigned)ip[3])); | |
ef416fc2 | 724 | first->addr.ipv4.sin_port = htons(portnum); |
725 | } | |
726 | } | |
727 | else if ((host = gethostbyname(hostname)) != NULL && | |
728 | # ifdef AF_INET6 | |
729 | (host->h_addrtype == AF_INET || host->h_addrtype == AF_INET6)) | |
730 | # else | |
731 | host->h_addrtype == AF_INET) | |
732 | # endif /* AF_INET6 */ | |
733 | { | |
734 | for (i = 0; host->h_addr_list[i]; i ++) | |
735 | { | |
736 | /* | |
737 | * Copy the address over... | |
738 | */ | |
739 | ||
740 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
741 | if (!temp) | |
742 | { | |
743 | httpAddrFreeList(first); | |
744 | return (NULL); | |
745 | } | |
746 | ||
747 | # ifdef AF_INET6 | |
748 | if (host->h_addrtype == AF_INET6) | |
749 | { | |
d6ae789d | 750 | temp->addr.ipv6.sin6_family = AF_INET6; |
ed486911 | 751 | memcpy(&(temp->addr.ipv6.sin6_addr), host->h_addr_list[i], |
ef416fc2 | 752 | sizeof(temp->addr.ipv6)); |
753 | temp->addr.ipv6.sin6_port = htons(portnum); | |
754 | } | |
755 | else | |
756 | # endif /* AF_INET6 */ | |
757 | { | |
d6ae789d | 758 | temp->addr.ipv4.sin_family = AF_INET; |
ed486911 | 759 | memcpy(&(temp->addr.ipv4.sin_addr), host->h_addr_list[i], |
ef416fc2 | 760 | sizeof(temp->addr.ipv4)); |
761 | temp->addr.ipv4.sin_port = htons(portnum); | |
762 | } | |
763 | ||
764 | /* | |
765 | * Append the address to the list... | |
766 | */ | |
767 | ||
768 | if (!first) | |
769 | first = temp; | |
770 | ||
771 | if (addr) | |
772 | addr->next = temp; | |
773 | ||
774 | addr = temp; | |
775 | } | |
776 | } | |
dcb445bc MS |
777 | else |
778 | { | |
779 | if (h_errno == NO_RECOVERY) | |
780 | cg->need_res_init = 1; | |
781 | ||
cb7f98ee | 782 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, hstrerror(h_errno), 0); |
dcb445bc | 783 | } |
ef416fc2 | 784 | } |
785 | #endif /* HAVE_GETADDRINFO */ | |
786 | } | |
787 | ||
788 | /* | |
789 | * Detect some common errors and handle them sanely... | |
790 | */ | |
791 | ||
88f9aafc | 792 | if (!addr && (!hostname || !_cups_strcasecmp(hostname, "localhost"))) |
ef416fc2 | 793 | { |
794 | struct servent *port; /* Port number for service */ | |
795 | int portnum; /* Port number */ | |
796 | ||
797 | ||
798 | /* | |
799 | * Lookup the service... | |
800 | */ | |
801 | ||
802 | if (!service) | |
803 | portnum = 0; | |
804 | else if (isdigit(*service & 255)) | |
805 | portnum = atoi(service); | |
806 | else if ((port = getservbyname(service, NULL)) != NULL) | |
807 | portnum = ntohs(port->s_port); | |
808 | else if (!strcmp(service, "http")) | |
809 | portnum = 80; | |
810 | else if (!strcmp(service, "https")) | |
811 | portnum = 443; | |
321d8d57 | 812 | else if (!strcmp(service, "ipp") || !strcmp(service, "ipps")) |
ef416fc2 | 813 | portnum = 631; |
814 | else if (!strcmp(service, "lpd")) | |
815 | portnum = 515; | |
816 | else if (!strcmp(service, "socket")) | |
817 | portnum = 9100; | |
818 | else | |
321d8d57 MS |
819 | { |
820 | httpAddrFreeList(first); | |
dcb445bc | 821 | |
cb7f98ee | 822 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown service name."), 1); |
ef416fc2 | 823 | return (NULL); |
321d8d57 | 824 | } |
ef416fc2 | 825 | |
88f9aafc | 826 | if (hostname && !_cups_strcasecmp(hostname, "localhost")) |
ef416fc2 | 827 | { |
828 | /* | |
829 | * Unfortunately, some users ignore all of the warnings in the | |
830 | * /etc/hosts file and delete "localhost" from it. If we get here | |
831 | * then we were unable to resolve the name, so use the IPv6 and/or | |
832 | * IPv4 loopback interface addresses... | |
833 | */ | |
834 | ||
835 | #ifdef AF_INET6 | |
836 | if (family != AF_INET) | |
837 | { | |
838 | /* | |
839 | * Add [::1] to the address list... | |
840 | */ | |
841 | ||
842 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
843 | if (!temp) | |
844 | { | |
cb7f98ee | 845 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); |
ef416fc2 | 846 | httpAddrFreeList(first); |
847 | return (NULL); | |
848 | } | |
849 | ||
850 | temp->addr.ipv6.sin6_family = AF_INET6; | |
851 | temp->addr.ipv6.sin6_port = htons(portnum); | |
852 | # ifdef WIN32 | |
853 | temp->addr.ipv6.sin6_addr.u.Byte[15] = 1; | |
854 | # else | |
855 | temp->addr.ipv6.sin6_addr.s6_addr32[3] = htonl(1); | |
856 | # endif /* WIN32 */ | |
857 | ||
ed486911 | 858 | if (!first) |
859 | first = temp; | |
860 | ||
ef416fc2 | 861 | addr = temp; |
862 | } | |
863 | ||
864 | if (family != AF_INET6) | |
865 | #endif /* AF_INET6 */ | |
866 | { | |
867 | /* | |
868 | * Add 127.0.0.1 to the address list... | |
869 | */ | |
870 | ||
871 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
872 | if (!temp) | |
873 | { | |
cb7f98ee | 874 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); |
ef416fc2 | 875 | httpAddrFreeList(first); |
876 | return (NULL); | |
877 | } | |
878 | ||
879 | temp->addr.ipv4.sin_family = AF_INET; | |
880 | temp->addr.ipv4.sin_port = htons(portnum); | |
881 | temp->addr.ipv4.sin_addr.s_addr = htonl(0x7f000001); | |
882 | ||
ed486911 | 883 | if (!first) |
884 | first = temp; | |
885 | ||
ef416fc2 | 886 | if (addr) |
887 | addr->next = temp; | |
ef416fc2 | 888 | } |
889 | } | |
890 | else if (!hostname) | |
891 | { | |
892 | /* | |
893 | * Provide one or more passive listening addresses... | |
894 | */ | |
895 | ||
896 | #ifdef AF_INET6 | |
897 | if (family != AF_INET) | |
898 | { | |
899 | /* | |
900 | * Add [::] to the address list... | |
901 | */ | |
902 | ||
903 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
904 | if (!temp) | |
905 | { | |
cb7f98ee | 906 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); |
ef416fc2 | 907 | httpAddrFreeList(first); |
908 | return (NULL); | |
909 | } | |
910 | ||
911 | temp->addr.ipv6.sin6_family = AF_INET6; | |
912 | temp->addr.ipv6.sin6_port = htons(portnum); | |
913 | ||
ed486911 | 914 | if (!first) |
915 | first = temp; | |
916 | ||
ef416fc2 | 917 | addr = temp; |
918 | } | |
919 | ||
920 | if (family != AF_INET6) | |
921 | #endif /* AF_INET6 */ | |
922 | { | |
923 | /* | |
924 | * Add 0.0.0.0 to the address list... | |
925 | */ | |
926 | ||
927 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
928 | if (!temp) | |
929 | { | |
cb7f98ee | 930 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); |
ef416fc2 | 931 | httpAddrFreeList(first); |
932 | return (NULL); | |
933 | } | |
934 | ||
935 | temp->addr.ipv4.sin_family = AF_INET; | |
936 | temp->addr.ipv4.sin_port = htons(portnum); | |
937 | ||
ed486911 | 938 | if (!first) |
939 | first = temp; | |
940 | ||
ef416fc2 | 941 | if (addr) |
942 | addr->next = temp; | |
ef416fc2 | 943 | } |
944 | } | |
945 | } | |
946 | ||
947 | /* | |
948 | * Return the address list... | |
949 | */ | |
950 | ||
951 | return (first); | |
952 | } |