]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id: http-addrlist.c 4815 2005-10-31 20:40:17Z mike $" | |
3 | * | |
4 | * HTTP address list routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2005 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * httpAddrConnect() - Connect to any of the addresses in the list. | |
27 | * httpAddrFreeList() - Free an address list. | |
28 | * httpAddrGetList() - Get a list of addresses for a hostname. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Include necessary headers... | |
33 | */ | |
34 | ||
35 | #include "http-private.h" | |
36 | #include "globals.h" | |
37 | #include "debug.h" | |
38 | #include <stdlib.h> | |
39 | ||
40 | ||
41 | /* | |
42 | * 'httpAddrConnect()' - Connect to any of the addresses in the list. | |
43 | * | |
44 | * @since CUPS 1.2@ | |
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 | int val; /* Socket option value */ | |
53 | ||
54 | ||
55 | /* | |
56 | * Loop through each address until we connect or run out of addresses... | |
57 | */ | |
58 | ||
59 | while (addrlist) | |
60 | { | |
61 | /* | |
62 | * Create the socket... | |
63 | */ | |
64 | ||
65 | if ((*sock = socket(addrlist->addr.addr.sa_family, SOCK_STREAM, 0)) < 0) | |
66 | { | |
67 | /* | |
68 | * Don't abort yet, as this could just be an issue with the local | |
69 | * system not being configured with IPv4/IPv6/domain socket enabled... | |
70 | */ | |
71 | ||
72 | addrlist = addrlist->next; | |
73 | continue; | |
74 | } | |
75 | ||
76 | /* | |
77 | * Set options... | |
78 | */ | |
79 | ||
80 | val = 1; | |
81 | #ifdef WIN32 | |
82 | setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, | |
83 | sizeof(val)); | |
84 | #else | |
85 | setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); | |
86 | #endif /* WIN32 */ | |
87 | ||
88 | #ifdef SO_REUSEPORT | |
89 | val = 1; | |
90 | setsockopt(*sock, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)); | |
91 | #endif /* SO_REUSEPORT */ | |
92 | ||
93 | /* | |
94 | * Using TCP_NODELAY improves responsiveness, especially on systems | |
95 | * with a slow loopback interface... | |
96 | */ | |
97 | ||
98 | val = 1; | |
99 | #ifdef WIN32 | |
100 | setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&val, | |
101 | sizeof(val)); | |
102 | #else | |
103 | setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); | |
104 | #endif /* WIN32 */ | |
105 | ||
106 | #ifdef FD_CLOEXEC | |
107 | /* | |
108 | * Close this socket when starting another process... | |
109 | */ | |
110 | ||
111 | fcntl(*sock, F_SETFD, FD_CLOEXEC); | |
112 | #endif /* FD_CLOEXEC */ | |
113 | ||
114 | /* | |
115 | * Then connect... | |
116 | */ | |
117 | ||
118 | if (!connect(*sock, &(addrlist->addr.addr), | |
119 | httpAddrLength(&(addrlist->addr)))) | |
120 | break; | |
121 | ||
122 | /* | |
123 | * Close this socket and move to the next address... | |
124 | */ | |
125 | ||
126 | closesocket(*sock); | |
127 | ||
128 | addrlist = addrlist->next; | |
129 | } | |
130 | ||
131 | return (addrlist); | |
132 | } | |
133 | ||
134 | ||
135 | /* | |
136 | * 'httpAddrFreeList()' - Free an address list. | |
137 | * | |
138 | * @since CUPS 1.2@ | |
139 | */ | |
140 | ||
141 | void | |
142 | httpAddrFreeList( | |
143 | http_addrlist_t *addrlist) /* I - Address list to free */ | |
144 | { | |
145 | http_addrlist_t *next; /* Next address in list */ | |
146 | ||
147 | ||
148 | /* | |
149 | * Free each address in the list... | |
150 | */ | |
151 | ||
152 | while (addrlist) | |
153 | { | |
154 | next = addrlist->next; | |
155 | ||
156 | free(addrlist); | |
157 | ||
158 | addrlist = next; | |
159 | } | |
160 | } | |
161 | ||
162 | ||
163 | /* | |
164 | * 'httpAddrGetList()' - Get a list of addresses for a hostname. | |
165 | * | |
166 | * @since CUPS 1.2@ | |
167 | */ | |
168 | ||
169 | http_addrlist_t * /* O - List of addresses or NULL */ | |
170 | httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for passive listen address */ | |
171 | int family, /* I - Address family or AF_UNSPEC */ | |
172 | const char *service) /* I - Service name or port number */ | |
173 | { | |
174 | http_addrlist_t *first, /* First address in list */ | |
175 | *addr, /* Current address in list */ | |
176 | *temp; /* New address */ | |
177 | ||
178 | ||
179 | #ifdef DEBUG | |
180 | printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, service=\"%s\")\n", | |
181 | hostname ? hostname : "(nil)", | |
182 | family == AF_UNSPEC ? "UNSPEC" : | |
183 | # ifdef AF_LOCAL | |
184 | family == AF_LOCAL ? "LOCAL" : | |
185 | # endif /* AF_LOCAL */ | |
186 | # ifdef AF_INET6 | |
187 | family == AF_INET6 ? "INET6" : | |
188 | # endif /* AF_INET6 */ | |
189 | family == AF_INET ? "INET" : "???", service); | |
190 | #endif /* DEBUG */ | |
191 | ||
192 | /* | |
193 | * Lookup the address the best way we can... | |
194 | */ | |
195 | ||
196 | first = addr = NULL; | |
197 | ||
198 | #ifdef AF_LOCAL | |
199 | if (hostname && hostname[0] == '/') | |
200 | { | |
201 | /* | |
202 | * Domain socket address... | |
203 | */ | |
204 | ||
205 | first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
206 | first->addr.un.sun_family = AF_LOCAL; | |
207 | strlcpy(first->addr.un.sun_path, hostname, sizeof(first->addr.un.sun_path)); | |
208 | } | |
209 | else | |
210 | #endif /* AF_LOCAL */ | |
211 | { | |
212 | #ifdef HAVE_GETADDRINFO | |
213 | struct addrinfo hints, /* Address lookup hints */ | |
214 | *results, /* Address lookup results */ | |
215 | *current; /* Current result */ | |
216 | char ipv6[1024], /* IPv6 address */ | |
217 | *ipv6zone; /* Pointer to zone separator */ | |
218 | int ipv6len; /* Length of IPv6 address */ | |
219 | ||
220 | /* | |
221 | * Lookup the address as needed... | |
222 | */ | |
223 | ||
224 | memset(&hints, 0, sizeof(hints)); | |
225 | hints.ai_family = family; | |
226 | hints.ai_flags = hostname ? 0 : AI_PASSIVE; | |
227 | hints.ai_socktype = SOCK_STREAM; | |
228 | ||
229 | if (hostname && *hostname == '[') | |
230 | { | |
231 | /* | |
232 | * Remove brackets from numeric IPv6 address... | |
233 | */ | |
234 | ||
235 | if (!strncmp(hostname, "[v1.", 4)) | |
236 | { | |
237 | /* | |
238 | * Copy the newer address format which supports link-local addresses... | |
239 | */ | |
240 | ||
241 | strlcpy(ipv6, hostname + 4, sizeof(ipv6)); | |
242 | if ((ipv6len = strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']') | |
243 | { | |
244 | ipv6[ipv6len] = '\0'; | |
245 | hostname = ipv6; | |
246 | ||
247 | /* | |
248 | * Convert "+zone" in address to "%zone"... | |
249 | */ | |
250 | ||
251 | if ((ipv6zone = strrchr(ipv6, '+')) != NULL) | |
252 | *ipv6zone = '%'; | |
253 | } | |
254 | } | |
255 | else | |
256 | { | |
257 | /* | |
258 | * Copy the regular non-link-local IPv6 address... | |
259 | */ | |
260 | ||
261 | strlcpy(ipv6, hostname + 1, sizeof(ipv6)); | |
262 | if ((ipv6len = strlen(ipv6) - 1) >= 0 && ipv6[ipv6len] == ']') | |
263 | { | |
264 | ipv6[ipv6len] = '\0'; | |
265 | hostname = ipv6; | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | if (!getaddrinfo(hostname, service, &hints, &results)) | |
271 | { | |
272 | /* | |
273 | * Copy the results to our own address list structure... | |
274 | */ | |
275 | ||
276 | for (current = results; current; current = current->ai_next) | |
277 | if (current->ai_family == AF_INET || current->ai_family == AF_INET6) | |
278 | { | |
279 | /* | |
280 | * Copy the address over... | |
281 | */ | |
282 | ||
283 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
284 | if (!temp) | |
285 | { | |
286 | httpAddrFreeList(first); | |
287 | return (NULL); | |
288 | } | |
289 | ||
290 | if (current->ai_family == AF_INET6) | |
291 | memcpy(&(temp->addr.ipv6), current->ai_addr, | |
292 | sizeof(temp->addr.ipv6)); | |
293 | else | |
294 | memcpy(&(temp->addr.ipv4), current->ai_addr, | |
295 | sizeof(temp->addr.ipv4)); | |
296 | ||
297 | /* | |
298 | * Append the address to the list... | |
299 | */ | |
300 | ||
301 | if (!first) | |
302 | first = temp; | |
303 | ||
304 | if (addr) | |
305 | addr->next = temp; | |
306 | ||
307 | addr = temp; | |
308 | } | |
309 | ||
310 | /* | |
311 | * Free the results from getaddrinfo()... | |
312 | */ | |
313 | ||
314 | freeaddrinfo(results); | |
315 | } | |
316 | #else | |
317 | if (hostname) | |
318 | { | |
319 | int i; /* Looping vars */ | |
320 | unsigned ip[4]; /* IPv4 address components */ | |
321 | const char *ptr; /* Pointer into hostname */ | |
322 | struct hostent *host; /* Result of lookup */ | |
323 | struct servent *port; /* Port number for service */ | |
324 | int portnum; /* Port number */ | |
325 | ||
326 | ||
327 | /* | |
328 | * Lookup the service... | |
329 | */ | |
330 | ||
331 | if (!service) | |
332 | portnum = 0; | |
333 | else if (isdigit(*service & 255)) | |
334 | portnum = atoi(service); | |
335 | else if ((port = getservbyname(service, NULL)) != NULL) | |
336 | portnum = ntohs(port->s_port); | |
337 | else if (!strcmp(service, "http")) | |
338 | portnum = 80; | |
339 | else if (!strcmp(service, "https")) | |
340 | portnum = 443; | |
341 | else if (!strcmp(service, "ipp")) | |
342 | portnum = 631; | |
343 | else if (!strcmp(service, "lpd")) | |
344 | portnum = 515; | |
345 | else if (!strcmp(service, "socket")) | |
346 | portnum = 9100; | |
347 | else | |
348 | return (NULL); | |
349 | ||
350 | /* | |
351 | * This code is needed because some operating systems have a | |
352 | * buggy implementation of gethostbyname() that does not support | |
353 | * IPv4 addresses. If the hostname string is an IPv4 address, then | |
354 | * sscanf() is used to extract the IPv4 components. We then pack | |
355 | * the components into an IPv4 address manually, since the | |
356 | * inet_aton() function is deprecated. We use the htonl() macro | |
357 | * to get the right byte order for the address. | |
358 | */ | |
359 | ||
360 | for (ptr = hostname; isdigit(*ptr & 255) || *ptr == '.'; ptr ++); | |
361 | ||
362 | if (!*ptr) | |
363 | { | |
364 | /* | |
365 | * We have an IPv4 address; break it up and create an IPv4 address... | |
366 | */ | |
367 | ||
368 | if (sscanf(hostname, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) == 4 && | |
369 | ip[0] <= 255 && ip[1] <= 255 && ip[2] <= 255 && ip[3] <= 255) | |
370 | { | |
371 | first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
372 | if (!first) | |
373 | return (NULL); | |
374 | ||
375 | first->addr.ipv4.sin_family = AF_INET; | |
376 | first->addr.ipv4.sin_addr.s_addr = htonl(((((((ip[0] << 8) | | |
377 | ip[1]) << 8) | | |
378 | ip[2]) << 8) | ip[3])); | |
379 | first->addr.ipv4.sin_port = htons(portnum); | |
380 | } | |
381 | } | |
382 | else if ((host = gethostbyname(hostname)) != NULL && | |
383 | # ifdef AF_INET6 | |
384 | (host->h_addrtype == AF_INET || host->h_addrtype == AF_INET6)) | |
385 | # else | |
386 | host->h_addrtype == AF_INET) | |
387 | # endif /* AF_INET6 */ | |
388 | { | |
389 | for (i = 0; host->h_addr_list[i]; i ++) | |
390 | { | |
391 | /* | |
392 | * Copy the address over... | |
393 | */ | |
394 | ||
395 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
396 | if (!temp) | |
397 | { | |
398 | httpAddrFreeList(first); | |
399 | return (NULL); | |
400 | } | |
401 | ||
402 | # ifdef AF_INET6 | |
403 | if (host->h_addrtype == AF_INET6) | |
404 | { | |
405 | first->addr.ipv6.sin6_family = AF_INET6; | |
406 | memcpy(&(temp->addr.ipv6), host->h_addr_list[i], | |
407 | sizeof(temp->addr.ipv6)); | |
408 | temp->addr.ipv6.sin6_port = htons(portnum); | |
409 | } | |
410 | else | |
411 | # endif /* AF_INET6 */ | |
412 | { | |
413 | first->addr.ipv4.sin_family = AF_INET; | |
414 | memcpy(&(temp->addr.ipv4), host->h_addr_list[i], | |
415 | sizeof(temp->addr.ipv4)); | |
416 | temp->addr.ipv4.sin_port = htons(portnum); | |
417 | } | |
418 | ||
419 | /* | |
420 | * Append the address to the list... | |
421 | */ | |
422 | ||
423 | if (!first) | |
424 | first = temp; | |
425 | ||
426 | if (addr) | |
427 | addr->next = temp; | |
428 | ||
429 | addr = temp; | |
430 | } | |
431 | } | |
432 | } | |
433 | #endif /* HAVE_GETADDRINFO */ | |
434 | } | |
435 | ||
436 | /* | |
437 | * Detect some common errors and handle them sanely... | |
438 | */ | |
439 | ||
440 | if (!addr && (!hostname || !strcmp(hostname, "localhost"))) | |
441 | { | |
442 | struct servent *port; /* Port number for service */ | |
443 | int portnum; /* Port number */ | |
444 | ||
445 | ||
446 | /* | |
447 | * Lookup the service... | |
448 | */ | |
449 | ||
450 | if (!service) | |
451 | portnum = 0; | |
452 | else if (isdigit(*service & 255)) | |
453 | portnum = atoi(service); | |
454 | else if ((port = getservbyname(service, NULL)) != NULL) | |
455 | portnum = ntohs(port->s_port); | |
456 | else if (!strcmp(service, "http")) | |
457 | portnum = 80; | |
458 | else if (!strcmp(service, "https")) | |
459 | portnum = 443; | |
460 | else if (!strcmp(service, "ipp")) | |
461 | portnum = 631; | |
462 | else if (!strcmp(service, "lpd")) | |
463 | portnum = 515; | |
464 | else if (!strcmp(service, "socket")) | |
465 | portnum = 9100; | |
466 | else | |
467 | return (NULL); | |
468 | ||
469 | if (hostname && !strcmp(hostname, "localhost")) | |
470 | { | |
471 | /* | |
472 | * Unfortunately, some users ignore all of the warnings in the | |
473 | * /etc/hosts file and delete "localhost" from it. If we get here | |
474 | * then we were unable to resolve the name, so use the IPv6 and/or | |
475 | * IPv4 loopback interface addresses... | |
476 | */ | |
477 | ||
478 | #ifdef AF_INET6 | |
479 | if (family != AF_INET) | |
480 | { | |
481 | /* | |
482 | * Add [::1] to the address list... | |
483 | */ | |
484 | ||
485 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
486 | if (!temp) | |
487 | { | |
488 | httpAddrFreeList(first); | |
489 | return (NULL); | |
490 | } | |
491 | ||
492 | temp->addr.ipv6.sin6_family = AF_INET6; | |
493 | temp->addr.ipv6.sin6_port = htons(portnum); | |
494 | # ifdef WIN32 | |
495 | temp->addr.ipv6.sin6_addr.u.Byte[15] = 1; | |
496 | # else | |
497 | temp->addr.ipv6.sin6_addr.s6_addr32[3] = htonl(1); | |
498 | # endif /* WIN32 */ | |
499 | ||
500 | addr = temp; | |
501 | } | |
502 | ||
503 | if (family != AF_INET6) | |
504 | #endif /* AF_INET6 */ | |
505 | { | |
506 | /* | |
507 | * Add 127.0.0.1 to the address list... | |
508 | */ | |
509 | ||
510 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
511 | if (!temp) | |
512 | { | |
513 | httpAddrFreeList(first); | |
514 | return (NULL); | |
515 | } | |
516 | ||
517 | temp->addr.ipv4.sin_family = AF_INET; | |
518 | temp->addr.ipv4.sin_port = htons(portnum); | |
519 | temp->addr.ipv4.sin_addr.s_addr = htonl(0x7f000001); | |
520 | ||
521 | if (addr) | |
522 | addr->next = temp; | |
523 | else | |
524 | addr = temp; | |
525 | } | |
526 | } | |
527 | else if (!hostname) | |
528 | { | |
529 | /* | |
530 | * Provide one or more passive listening addresses... | |
531 | */ | |
532 | ||
533 | #ifdef AF_INET6 | |
534 | if (family != AF_INET) | |
535 | { | |
536 | /* | |
537 | * Add [::] to the address list... | |
538 | */ | |
539 | ||
540 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
541 | if (!temp) | |
542 | { | |
543 | httpAddrFreeList(first); | |
544 | return (NULL); | |
545 | } | |
546 | ||
547 | temp->addr.ipv6.sin6_family = AF_INET6; | |
548 | temp->addr.ipv6.sin6_port = htons(portnum); | |
549 | ||
550 | addr = temp; | |
551 | } | |
552 | ||
553 | if (family != AF_INET6) | |
554 | #endif /* AF_INET6 */ | |
555 | { | |
556 | /* | |
557 | * Add 0.0.0.0 to the address list... | |
558 | */ | |
559 | ||
560 | temp = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); | |
561 | if (!temp) | |
562 | { | |
563 | httpAddrFreeList(first); | |
564 | return (NULL); | |
565 | } | |
566 | ||
567 | temp->addr.ipv4.sin_family = AF_INET; | |
568 | temp->addr.ipv4.sin_port = htons(portnum); | |
569 | ||
570 | if (addr) | |
571 | addr->next = temp; | |
572 | else | |
573 | addr = temp; | |
574 | } | |
575 | } | |
576 | } | |
577 | ||
578 | /* | |
579 | * Return the address list... | |
580 | */ | |
581 | ||
582 | return (first); | |
583 | } | |
584 | ||
585 | ||
586 | /* | |
587 | * End of "$Id: http-addrlist.c 4815 2005-10-31 20:40:17Z mike $". | |
588 | */ |