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