]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
996acce8 | 4 | * HTTP address routines for CUPS. |
ef416fc2 | 5 | * |
996acce8 MS |
6 | * Copyright 2007-2013 by Apple Inc. |
7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
ef416fc2 | 8 | * |
996acce8 MS |
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/". | |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include "cups-private.h" |
87e98392 | 21 | #include <sys/stat.h> |
49d87452 MS |
22 | #ifdef HAVE_RESOLV_H |
23 | # include <resolv.h> | |
24 | #endif /* HAVE_RESOLV_H */ | |
a29fd7dd | 25 | #ifdef __APPLE__ |
1106b00e | 26 | # include <CoreFoundation/CoreFoundation.h> |
1106b00e | 27 | # include <SystemConfiguration/SystemConfiguration.h> |
a29fd7dd | 28 | #endif /* __APPLE__ */ |
ef416fc2 | 29 | |
30 | ||
31 | /* | |
32 | * 'httpAddrAny()' - Check for the "any" address. | |
33 | * | |
f3c17241 | 34 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 35 | */ |
36 | ||
37 | int /* O - 1 if "any", 0 otherwise */ | |
38 | httpAddrAny(const http_addr_t *addr) /* I - Address to check */ | |
39 | { | |
89d46774 | 40 | if (!addr) |
41 | return (0); | |
42 | ||
ef416fc2 | 43 | #ifdef AF_INET6 |
44 | if (addr->addr.sa_family == AF_INET6 && | |
45 | IN6_IS_ADDR_UNSPECIFIED(&(addr->ipv6.sin6_addr))) | |
46 | return (1); | |
47 | #endif /* AF_INET6 */ | |
48 | ||
49 | if (addr->addr.sa_family == AF_INET && | |
50 | ntohl(addr->ipv4.sin_addr.s_addr) == 0x00000000) | |
51 | return (1); | |
52 | ||
53 | return (0); | |
54 | } | |
55 | ||
56 | ||
87e98392 MS |
57 | /* |
58 | * 'httpAddrClose()' - Close a socket created by @link httpAddrConnect@ or | |
59 | * @link httpAddrListen@. | |
60 | * | |
61 | * Pass @code NULL@ for sockets created with @link httpAddrConnect@ and the | |
62 | * listen address for sockets created with @link httpAddrListen@. This will | |
63 | * ensure that domain sockets are removed when closed. | |
64 | * | |
65 | * @since CUPS 2.0@ | |
66 | */ | |
67 | ||
68 | int /* O - 0 on success, -1 on failure */ | |
69 | httpAddrClose(http_addr_t *addr, /* I - Listen address or @code NULL@ */ | |
70 | int fd) /* I - Socket file descriptor */ | |
71 | { | |
72 | #ifdef WIN32 | |
73 | if (closesocket(fd)) | |
74 | #else | |
75 | if (close(fd)) | |
76 | #endif /* WIN32 */ | |
77 | return (-1); | |
78 | ||
79 | #ifdef AF_LOCAL | |
80 | if (addr && addr->addr.sa_family == AF_LOCAL) | |
81 | return (unlink(addr->un.sun_path)); | |
82 | #endif /* AF_LOCAL */ | |
83 | ||
84 | return (0); | |
85 | } | |
86 | ||
87 | ||
ef416fc2 | 88 | /* |
89 | * 'httpAddrEqual()' - Compare two addresses. | |
90 | * | |
f3c17241 | 91 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 92 | */ |
93 | ||
ecdc0628 | 94 | int /* O - 1 if equal, 0 if not */ |
ef416fc2 | 95 | httpAddrEqual(const http_addr_t *addr1, /* I - First address */ |
96 | const http_addr_t *addr2) /* I - Second address */ | |
97 | { | |
89d46774 | 98 | if (!addr1 && !addr2) |
99 | return (1); | |
100 | ||
101 | if (!addr1 || !addr2) | |
102 | return (0); | |
103 | ||
ef416fc2 | 104 | if (addr1->addr.sa_family != addr2->addr.sa_family) |
105 | return (0); | |
106 | ||
107 | #ifdef AF_LOCAL | |
108 | if (addr1->addr.sa_family == AF_LOCAL) | |
109 | return (!strcmp(addr1->un.sun_path, addr2->un.sun_path)); | |
110 | #endif /* AF_LOCAL */ | |
111 | ||
112 | #ifdef AF_INET6 | |
113 | if (addr1->addr.sa_family == AF_INET6) | |
114 | return (!memcmp(&(addr1->ipv6.sin6_addr), &(addr2->ipv6.sin6_addr), 16)); | |
115 | #endif /* AF_INET6 */ | |
116 | ||
117 | return (addr1->ipv4.sin_addr.s_addr == addr2->ipv4.sin_addr.s_addr); | |
118 | } | |
119 | ||
120 | ||
121 | /* | |
122 | * 'httpAddrLength()' - Return the length of the address in bytes. | |
123 | * | |
f3c17241 | 124 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 125 | */ |
126 | ||
127 | int /* O - Length in bytes */ | |
128 | httpAddrLength(const http_addr_t *addr) /* I - Address */ | |
129 | { | |
89d46774 | 130 | if (!addr) |
131 | return (0); | |
132 | ||
ef416fc2 | 133 | #ifdef AF_INET6 |
134 | if (addr->addr.sa_family == AF_INET6) | |
135 | return (sizeof(addr->ipv6)); | |
136 | else | |
137 | #endif /* AF_INET6 */ | |
138 | #ifdef AF_LOCAL | |
139 | if (addr->addr.sa_family == AF_LOCAL) | |
140 | return (offsetof(struct sockaddr_un, sun_path) + | |
141 | strlen(addr->un.sun_path) + 1); | |
142 | else | |
143 | #endif /* AF_LOCAL */ | |
144 | if (addr->addr.sa_family == AF_INET) | |
145 | return (sizeof(addr->ipv4)); | |
146 | else | |
147 | return (0); | |
148 | ||
149 | } | |
150 | ||
151 | ||
a469f8a5 MS |
152 | /* |
153 | * 'httpAddrListen()' - Create a listening socket bound to the specified | |
154 | * address and port. | |
155 | * | |
9c0e8e5d | 156 | * @since CUPS 1.7/OS X 10.9@ |
a469f8a5 MS |
157 | */ |
158 | ||
159 | int /* O - Socket or -1 on error */ | |
160 | httpAddrListen(http_addr_t *addr, /* I - Address to bind to */ | |
161 | int port) /* I - Port number to bind to */ | |
162 | { | |
163 | int fd = -1, /* Socket */ | |
87e98392 MS |
164 | val, /* Socket value */ |
165 | status; /* Bind status */ | |
a469f8a5 MS |
166 | |
167 | ||
168 | /* | |
169 | * Range check input... | |
170 | */ | |
171 | ||
87e98392 | 172 | if (!addr || port < 0) |
a469f8a5 MS |
173 | return (-1); |
174 | ||
87e98392 MS |
175 | /* |
176 | * Create the socket and set options... | |
177 | */ | |
178 | ||
a469f8a5 MS |
179 | if ((fd = socket(addr->addr.sa_family, SOCK_STREAM, 0)) < 0) |
180 | { | |
181 | _cupsSetHTTPError(HTTP_STATUS_ERROR); | |
182 | return (-1); | |
183 | } | |
184 | ||
185 | val = 1; | |
db8b865d | 186 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, CUPS_SOCAST &val, sizeof(val)); |
a469f8a5 MS |
187 | |
188 | #ifdef IPV6_V6ONLY | |
189 | if (addr->addr.sa_family == AF_INET6) | |
db8b865d | 190 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, CUPS_SOCAST &val, sizeof(val)); |
a469f8a5 MS |
191 | #endif /* IPV6_V6ONLY */ |
192 | ||
87e98392 MS |
193 | /* |
194 | * Bind the socket... | |
195 | */ | |
a469f8a5 | 196 | |
87e98392 MS |
197 | #ifdef AF_LOCAL |
198 | if (addr->addr.sa_family == AF_LOCAL) | |
199 | { | |
200 | mode_t mask; /* Umask setting */ | |
201 | ||
202 | /* | |
203 | * Remove any existing domain socket file... | |
204 | */ | |
205 | ||
206 | unlink(addr->un.sun_path); | |
207 | ||
208 | /* | |
209 | * Save the current umask and set it to 0 so that all users can access | |
210 | * the domain socket... | |
211 | */ | |
212 | ||
213 | mask = umask(0); | |
214 | ||
215 | /* | |
216 | * Bind the domain socket... | |
217 | */ | |
218 | ||
219 | status = bind(fd, (struct sockaddr *)addr, httpAddrLength(addr)); | |
220 | ||
221 | /* | |
222 | * Restore the umask and fix permissions... | |
223 | */ | |
224 | ||
225 | umask(mask); | |
226 | chmod(addr->un.sun_path, 0140777); | |
227 | } | |
228 | else | |
229 | #endif /* AF_LOCAL */ | |
230 | { | |
231 | _httpAddrSetPort(addr, port); | |
232 | ||
233 | status = bind(fd, (struct sockaddr *)addr, httpAddrLength(addr)); | |
234 | } | |
235 | ||
236 | if (status) | |
a469f8a5 MS |
237 | { |
238 | _cupsSetHTTPError(HTTP_STATUS_ERROR); | |
239 | ||
240 | close(fd); | |
241 | ||
242 | return (-1); | |
243 | } | |
244 | ||
87e98392 MS |
245 | /* |
246 | * Listen... | |
247 | */ | |
248 | ||
a469f8a5 MS |
249 | if (listen(fd, 5)) |
250 | { | |
251 | _cupsSetHTTPError(HTTP_STATUS_ERROR); | |
252 | ||
253 | close(fd); | |
254 | ||
255 | return (-1); | |
256 | } | |
257 | ||
87e98392 MS |
258 | /* |
259 | * Close on exec... | |
260 | */ | |
261 | ||
262 | #ifndef WIN32 | |
263 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); | |
264 | #endif /* !WIN32 */ | |
265 | ||
a469f8a5 MS |
266 | #ifdef SO_NOSIGPIPE |
267 | /* | |
268 | * Disable SIGPIPE for this socket. | |
269 | */ | |
270 | ||
271 | val = 1; | |
db8b865d | 272 | setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val)); |
a469f8a5 MS |
273 | #endif /* SO_NOSIGPIPE */ |
274 | ||
275 | return (fd); | |
276 | } | |
277 | ||
278 | ||
ef416fc2 | 279 | /* |
280 | * 'httpAddrLocalhost()' - Check for the local loopback address. | |
281 | * | |
f3c17241 | 282 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 283 | */ |
284 | ||
285 | int /* O - 1 if local host, 0 otherwise */ | |
286 | httpAddrLocalhost( | |
287 | const http_addr_t *addr) /* I - Address to check */ | |
288 | { | |
89d46774 | 289 | if (!addr) |
290 | return (1); | |
291 | ||
ef416fc2 | 292 | #ifdef AF_INET6 |
293 | if (addr->addr.sa_family == AF_INET6 && | |
fa73b229 | 294 | IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr))) |
ef416fc2 | 295 | return (1); |
296 | #endif /* AF_INET6 */ | |
297 | ||
298 | #ifdef AF_LOCAL | |
299 | if (addr->addr.sa_family == AF_LOCAL) | |
300 | return (1); | |
301 | #endif /* AF_LOCAL */ | |
302 | ||
303 | if (addr->addr.sa_family == AF_INET && | |
e07d4801 | 304 | (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000) |
ef416fc2 | 305 | return (1); |
306 | ||
307 | return (0); | |
308 | } | |
309 | ||
310 | ||
ef416fc2 | 311 | /* |
312 | * 'httpAddrLookup()' - Lookup the hostname associated with the address. | |
313 | * | |
f3c17241 | 314 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 315 | */ |
316 | ||
ecdc0628 | 317 | char * /* O - Host name */ |
318 | httpAddrLookup( | |
319 | const http_addr_t *addr, /* I - Address to lookup */ | |
320 | char *name, /* I - Host name buffer */ | |
321 | int namelen) /* I - Size of name buffer */ | |
ef416fc2 | 322 | { |
49d87452 MS |
323 | _cups_globals_t *cg = _cupsGlobals(); |
324 | /* Global data */ | |
325 | ||
326 | ||
e07d4801 MS |
327 | DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name, |
328 | namelen)); | |
ef416fc2 | 329 | |
330 | /* | |
331 | * Range check input... | |
332 | */ | |
333 | ||
334 | if (!addr || !name || namelen <= 2) | |
335 | { | |
336 | if (name && namelen >= 1) | |
337 | *name = '\0'; | |
338 | ||
339 | return (NULL); | |
340 | } | |
341 | ||
342 | #ifdef AF_LOCAL | |
343 | if (addr->addr.sa_family == AF_LOCAL) | |
49d87452 | 344 | { |
ef416fc2 | 345 | strlcpy(name, addr->un.sun_path, namelen); |
49d87452 MS |
346 | return (name); |
347 | } | |
ef416fc2 | 348 | #endif /* AF_LOCAL */ |
49d87452 | 349 | |
d2354e63 MS |
350 | /* |
351 | * Optimize lookups for localhost/loopback addresses... | |
352 | */ | |
353 | ||
354 | if (httpAddrLocalhost(addr)) | |
355 | { | |
356 | strlcpy(name, "localhost", namelen); | |
357 | return (name); | |
358 | } | |
359 | ||
49d87452 MS |
360 | #ifdef HAVE_RES_INIT |
361 | /* | |
362 | * STR #2920: Initialize resolver after failure in cups-polld | |
363 | * | |
364 | * If the previous lookup failed, re-initialize the resolver to prevent | |
365 | * temporary network errors from persisting. This *should* be handled by | |
366 | * the resolver libraries, but apparently the glibc folks do not agree. | |
367 | * | |
368 | * We set a flag at the end of this function if we encounter an error that | |
369 | * requires reinitialization of the resolver functions. We then call | |
370 | * res_init() if the flag is set on the next call here or in httpAddrLookup(). | |
371 | */ | |
372 | ||
373 | if (cg->need_res_init) | |
374 | { | |
375 | res_init(); | |
376 | ||
377 | cg->need_res_init = 0; | |
378 | } | |
379 | #endif /* HAVE_RES_INIT */ | |
380 | ||
ef416fc2 | 381 | #ifdef HAVE_GETNAMEINFO |
382 | { | |
db1f069b MS |
383 | /* |
384 | * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN | |
385 | * | |
386 | * FWIW, I think this is really a bug in the implementation of | |
387 | * getnameinfo(), but falling back on httpAddrString() is easy to | |
388 | * do... | |
389 | */ | |
ef416fc2 | 390 | |
49d87452 MS |
391 | int error = getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen, |
392 | NULL, 0, 0); | |
393 | ||
394 | if (error) | |
395 | { | |
396 | if (error == EAI_FAIL) | |
397 | cg->need_res_init = 1; | |
398 | ||
db1f069b | 399 | return (httpAddrString(addr, name, namelen)); |
49d87452 | 400 | } |
ef416fc2 | 401 | } |
402 | #else | |
403 | { | |
404 | struct hostent *host; /* Host from name service */ | |
405 | ||
406 | ||
407 | # ifdef AF_INET6 | |
408 | if (addr->addr.sa_family == AF_INET6) | |
3dd9c340 | 409 | host = gethostbyaddr((char *)&(addr->ipv6.sin6_addr), |
ef416fc2 | 410 | sizeof(struct in_addr), AF_INET6); |
411 | else | |
412 | # endif /* AF_INET6 */ | |
3dd9c340 | 413 | host = gethostbyaddr((char *)&(addr->ipv4.sin_addr), |
ef416fc2 | 414 | sizeof(struct in_addr), AF_INET); |
415 | ||
416 | if (host == NULL) | |
417 | { | |
418 | /* | |
419 | * No hostname, so return the raw address... | |
420 | */ | |
421 | ||
49d87452 MS |
422 | if (h_errno == NO_RECOVERY) |
423 | cg->need_res_init = 1; | |
424 | ||
425 | return (httpAddrString(addr, name, namelen)); | |
ef416fc2 | 426 | } |
427 | ||
428 | strlcpy(name, host->h_name, namelen); | |
429 | } | |
430 | #endif /* HAVE_GETNAMEINFO */ | |
431 | ||
e07d4801 MS |
432 | DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name)); |
433 | ||
ef416fc2 | 434 | return (name); |
435 | } | |
436 | ||
437 | ||
5ec1fd3d MS |
438 | /* |
439 | * 'httpAddrFamily()' - Get the address family of an address. | |
440 | */ | |
441 | ||
442 | int /* O - Address family */ | |
443 | httpAddrFamily(http_addr_t *addr) /* I - Address */ | |
444 | { | |
445 | if (addr) | |
446 | return (addr->addr.sa_family); | |
447 | else | |
448 | return (0); | |
449 | } | |
450 | ||
451 | ||
1ff0402e | 452 | /* |
a469f8a5 MS |
453 | * 'httpAddrPort()' - Get the port number associated with an address. |
454 | * | |
9c0e8e5d | 455 | * @since CUPS 1.7/OS X 10.9@ |
1ff0402e MS |
456 | */ |
457 | ||
458 | int /* O - Port number */ | |
a469f8a5 | 459 | httpAddrPort(http_addr_t *addr) /* I - Address */ |
1ff0402e | 460 | { |
5f64df29 | 461 | if (!addr) |
5ec1fd3d | 462 | return (-1); |
1ff0402e | 463 | #ifdef AF_INET6 |
5f64df29 | 464 | else if (addr->addr.sa_family == AF_INET6) |
1ff0402e | 465 | return (ntohs(addr->ipv6.sin6_port)); |
1ff0402e | 466 | #endif /* AF_INET6 */ |
5f64df29 | 467 | else if (addr->addr.sa_family == AF_INET) |
1ff0402e MS |
468 | return (ntohs(addr->ipv4.sin_port)); |
469 | else | |
5ec1fd3d | 470 | return (0); |
1ff0402e MS |
471 | } |
472 | ||
473 | ||
22c9029b MS |
474 | /* |
475 | * '_httpAddrSetPort()' - Set the port number associated with an address. | |
476 | */ | |
477 | ||
478 | void | |
479 | _httpAddrSetPort(http_addr_t *addr, /* I - Address */ | |
480 | int port) /* I - Port */ | |
481 | { | |
482 | if (!addr || port <= 0) | |
483 | return; | |
484 | ||
485 | #ifdef AF_INET6 | |
486 | if (addr->addr.sa_family == AF_INET6) | |
487 | addr->ipv6.sin6_port = htons(port); | |
488 | else | |
489 | #endif /* AF_INET6 */ | |
490 | if (addr->addr.sa_family == AF_INET) | |
491 | addr->ipv4.sin_port = htons(port); | |
492 | } | |
493 | ||
494 | ||
ef416fc2 | 495 | /* |
ecdc0628 | 496 | * 'httpAddrString()' - Convert an address to a numeric string. |
ef416fc2 | 497 | * |
f3c17241 | 498 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 499 | */ |
500 | ||
ecdc0628 | 501 | char * /* O - Numeric address string */ |
502 | httpAddrString(const http_addr_t *addr, /* I - Address to convert */ | |
503 | char *s, /* I - String buffer */ | |
504 | int slen) /* I - Length of string */ | |
ef416fc2 | 505 | { |
e07d4801 | 506 | DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen)); |
ef416fc2 | 507 | |
508 | /* | |
509 | * Range check input... | |
510 | */ | |
511 | ||
512 | if (!addr || !s || slen <= 2) | |
513 | { | |
514 | if (s && slen >= 1) | |
515 | *s = '\0'; | |
516 | ||
517 | return (NULL); | |
518 | } | |
519 | ||
520 | #ifdef AF_LOCAL | |
521 | if (addr->addr.sa_family == AF_LOCAL) | |
771bd8cb MS |
522 | { |
523 | if (addr->un.sun_path[0] == '/') | |
524 | strlcpy(s, addr->un.sun_path, slen); | |
525 | else | |
526 | strlcpy(s, "localhost", slen); | |
527 | } | |
ef416fc2 | 528 | else |
529 | #endif /* AF_LOCAL */ | |
b423cd4c | 530 | if (addr->addr.sa_family == AF_INET) |
ef416fc2 | 531 | { |
b423cd4c | 532 | unsigned temp; /* Temporary address */ |
ef416fc2 | 533 | |
534 | ||
b423cd4c | 535 | temp = ntohl(addr->ipv4.sin_addr.s_addr); |
536 | ||
537 | snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255, | |
538 | (temp >> 16) & 255, (temp >> 8) & 255, temp & 255); | |
539 | } | |
540 | #ifdef AF_INET6 | |
541 | else if (addr->addr.sa_family == AF_INET6) | |
542 | { | |
82f97232 MS |
543 | char *sptr, /* Pointer into string */ |
544 | temps[64]; /* Temporary string for address */ | |
545 | ||
b423cd4c | 546 | # ifdef HAVE_GETNAMEINFO |
82f97232 | 547 | if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps), |
b423cd4c | 548 | NULL, 0, NI_NUMERICHOST)) |
ef416fc2 | 549 | { |
550 | /* | |
551 | * If we get an error back, then the address type is not supported | |
552 | * and we should zero out the buffer... | |
553 | */ | |
554 | ||
555 | s[0] = '\0'; | |
556 | ||
557 | return (NULL); | |
558 | } | |
82f97232 MS |
559 | else if ((sptr = strchr(temps, '%')) != NULL) |
560 | { | |
561 | /* | |
562 | * Convert "%zone" to "+zone" to match URI form... | |
563 | */ | |
564 | ||
565 | *sptr = '+'; | |
566 | } | |
567 | ||
b423cd4c | 568 | # else |
b423cd4c | 569 | int i; /* Looping var */ |
570 | unsigned temp; /* Current value */ | |
571 | const char *prefix; /* Prefix for address */ | |
572 | ||
573 | ||
574 | prefix = ""; | |
82f97232 | 575 | for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++) |
ef416fc2 | 576 | { |
b423cd4c | 577 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 578 | |
82f97232 MS |
579 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, |
580 | (temp >> 16) & 0xffff); | |
b423cd4c | 581 | prefix = ":"; |
b423cd4c | 582 | sptr += strlen(sptr); |
ef416fc2 | 583 | |
b423cd4c | 584 | temp &= 0xffff; |
ef416fc2 | 585 | |
b423cd4c | 586 | if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1]) |
587 | { | |
82f97232 | 588 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp); |
ef416fc2 | 589 | sptr += strlen(sptr); |
ef416fc2 | 590 | } |
b423cd4c | 591 | } |
592 | ||
593 | if (i < 4) | |
594 | { | |
595 | while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i]) | |
596 | i ++; | |
ef416fc2 | 597 | |
598 | if (i < 4) | |
599 | { | |
82f97232 | 600 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix); |
b423cd4c | 601 | prefix = ":"; |
b423cd4c | 602 | sptr += strlen(sptr); |
ef416fc2 | 603 | |
b423cd4c | 604 | for (; i < 4; i ++) |
ef416fc2 | 605 | { |
b423cd4c | 606 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 607 | |
82f97232 MS |
608 | if ((temp & 0xffff0000) || |
609 | (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1])) | |
ef416fc2 | 610 | { |
82f97232 MS |
611 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, |
612 | (temp >> 16) & 0xffff); | |
ef416fc2 | 613 | sptr += strlen(sptr); |
b423cd4c | 614 | } |
ef416fc2 | 615 | |
82f97232 MS |
616 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, |
617 | temp & 0xffff); | |
b423cd4c | 618 | sptr += strlen(sptr); |
ef416fc2 | 619 | } |
620 | } | |
b423cd4c | 621 | else if (sptr == s) |
622 | { | |
623 | /* | |
624 | * Empty address... | |
625 | */ | |
ef416fc2 | 626 | |
82f97232 | 627 | strlcpy(temps, "::", sizeof(temps)); |
b423cd4c | 628 | } |
629 | else | |
630 | { | |
631 | /* | |
632 | * Empty at end... | |
633 | */ | |
ef416fc2 | 634 | |
82f97232 | 635 | strlcpy(sptr, "::", sizeof(temps) - (sptr - temps)); |
b423cd4c | 636 | } |
ef416fc2 | 637 | } |
b423cd4c | 638 | # endif /* HAVE_GETNAMEINFO */ |
82f97232 MS |
639 | |
640 | /* | |
641 | * Add "[v1." and "]" around IPv6 address to convert to URI form. | |
642 | */ | |
643 | ||
644 | snprintf(s, slen, "[v1.%s]", temps); | |
ef416fc2 | 645 | } |
b423cd4c | 646 | #endif /* AF_INET6 */ |
647 | else | |
648 | strlcpy(s, "UNKNOWN", slen); | |
ef416fc2 | 649 | |
e07d4801 | 650 | DEBUG_printf(("1httpAddrString: returning \"%s\"...", s)); |
ef416fc2 | 651 | |
652 | return (s); | |
653 | } | |
654 | ||
655 | ||
996acce8 MS |
656 | /* |
657 | * 'httpGetAddress()' - Get the address of the connected peer of a connection. | |
658 | * | |
659 | * Returns @code NULL@ if the socket is currently unconnected. | |
660 | * | |
661 | * @since CUPS 2.0@ | |
662 | */ | |
663 | ||
664 | http_addr_t * /* O - Connected address or @code NULL@ */ | |
665 | httpGetAddress(http_t *http) /* I - HTTP connection */ | |
666 | { | |
667 | if (http) | |
668 | return (http->hostaddr); | |
669 | else | |
670 | return (NULL); | |
671 | } | |
672 | ||
673 | ||
ef416fc2 | 674 | /* |
675 | * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return | |
676 | * address records for the specified name. | |
677 | * | |
678 | * @deprecated@ | |
679 | */ | |
680 | ||
681 | struct hostent * /* O - Host entry */ | |
682 | httpGetHostByName(const char *name) /* I - Hostname or IP address */ | |
683 | { | |
684 | const char *nameptr; /* Pointer into name */ | |
685 | unsigned ip[4]; /* IP address components */ | |
686 | _cups_globals_t *cg = _cupsGlobals(); | |
687 | /* Pointer to library globals */ | |
688 | ||
689 | ||
e07d4801 | 690 | DEBUG_printf(("httpGetHostByName(name=\"%s\")", name)); |
ef416fc2 | 691 | |
692 | /* | |
693 | * Avoid lookup delays and configuration problems when connecting | |
694 | * to the localhost address... | |
695 | */ | |
696 | ||
697 | if (!strcmp(name, "localhost")) | |
698 | name = "127.0.0.1"; | |
699 | ||
700 | /* | |
701 | * This function is needed because some operating systems have a | |
702 | * buggy implementation of gethostbyname() that does not support | |
703 | * IP addresses. If the first character of the name string is a | |
704 | * number, then sscanf() is used to extract the IP components. | |
705 | * We then pack the components into an IPv4 address manually, | |
706 | * since the inet_aton() function is deprecated. We use the | |
707 | * htonl() macro to get the right byte order for the address. | |
708 | * | |
709 | * We also support domain sockets when supported by the underlying | |
710 | * OS... | |
711 | */ | |
712 | ||
713 | #ifdef AF_LOCAL | |
714 | if (name[0] == '/') | |
715 | { | |
716 | /* | |
717 | * A domain socket address, so make an AF_LOCAL entry and return it... | |
718 | */ | |
719 | ||
720 | cg->hostent.h_name = (char *)name; | |
721 | cg->hostent.h_aliases = NULL; | |
722 | cg->hostent.h_addrtype = AF_LOCAL; | |
723 | cg->hostent.h_length = strlen(name) + 1; | |
724 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
725 | cg->ip_ptrs[0] = (char *)name; | |
726 | cg->ip_ptrs[1] = NULL; | |
727 | ||
e07d4801 | 728 | DEBUG_puts("1httpGetHostByName: returning domain socket address..."); |
ef416fc2 | 729 | |
730 | return (&cg->hostent); | |
731 | } | |
732 | #endif /* AF_LOCAL */ | |
733 | ||
734 | for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++); | |
735 | ||
736 | if (!*nameptr) | |
737 | { | |
738 | /* | |
739 | * We have an IPv4 address; break it up and provide the host entry | |
740 | * to the caller. | |
741 | */ | |
742 | ||
743 | if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) | |
744 | return (NULL); /* Must have 4 numbers */ | |
745 | ||
746 | if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255) | |
747 | return (NULL); /* Invalid byte ranges! */ | |
748 | ||
da003234 MS |
749 | cg->ip_addr = htonl((((((((unsigned)ip[0] << 8) | (unsigned)ip[1]) << 8) | |
750 | (unsigned)ip[2]) << 8) | | |
751 | (unsigned)ip[3])); | |
ef416fc2 | 752 | |
753 | /* | |
754 | * Fill in the host entry and return it... | |
755 | */ | |
756 | ||
757 | cg->hostent.h_name = (char *)name; | |
758 | cg->hostent.h_aliases = NULL; | |
759 | cg->hostent.h_addrtype = AF_INET; | |
760 | cg->hostent.h_length = 4; | |
761 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
762 | cg->ip_ptrs[0] = (char *)&(cg->ip_addr); | |
763 | cg->ip_ptrs[1] = NULL; | |
764 | ||
e07d4801 | 765 | DEBUG_puts("1httpGetHostByName: returning IPv4 address..."); |
ef416fc2 | 766 | |
767 | return (&cg->hostent); | |
768 | } | |
769 | else | |
770 | { | |
771 | /* | |
772 | * Use the gethostbyname() function to get the IPv4 address for | |
773 | * the name... | |
774 | */ | |
775 | ||
e07d4801 | 776 | DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)..."); |
ef416fc2 | 777 | |
778 | return (gethostbyname(name)); | |
779 | } | |
780 | } | |
781 | ||
782 | ||
783 | /* | |
757d2cad | 784 | * 'httpGetHostname()' - Get the FQDN for the connection or local system. |
ef416fc2 | 785 | * |
757d2cad | 786 | * When "http" points to a connected socket, return the hostname or |
48bd1142 MS |
787 | * address that was used in the call to httpConnect() or httpConnectEncrypt(), |
788 | * or the address of the client for the connection from httpAcceptConnection(). | |
757d2cad | 789 | * Otherwise, return the FQDN for the local system using both gethostname() |
790 | * and gethostbyname() to get the local hostname with domain. | |
ef416fc2 | 791 | * |
f3c17241 | 792 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 793 | */ |
794 | ||
757d2cad | 795 | const char * /* O - FQDN for connection or system */ |
796 | httpGetHostname(http_t *http, /* I - HTTP connection or NULL */ | |
797 | char *s, /* I - String buffer for name */ | |
798 | int slen) /* I - Size of buffer */ | |
ef416fc2 | 799 | { |
757d2cad | 800 | if (http) |
480ef0fe | 801 | { |
5ec1fd3d MS |
802 | if (!s || slen <= 1) |
803 | { | |
804 | if (http->hostname[0] == '/') | |
805 | return ("localhost"); | |
806 | else | |
807 | return (http->hostname); | |
808 | } | |
809 | else if (http->hostname[0] == '/') | |
480ef0fe | 810 | strlcpy(s, "localhost", slen); |
811 | else | |
812 | strlcpy(s, http->hostname, slen); | |
813 | } | |
757d2cad | 814 | else |
ef416fc2 | 815 | { |
816 | /* | |
757d2cad | 817 | * Get the hostname... |
ef416fc2 | 818 | */ |
819 | ||
5ec1fd3d MS |
820 | if (!s || slen <= 1) |
821 | return (NULL); | |
da003234 | 822 | |
89d46774 | 823 | if (gethostname(s, slen) < 0) |
824 | strlcpy(s, "localhost", slen); | |
757d2cad | 825 | |
826 | if (!strchr(s, '.')) | |
827 | { | |
0837b7e8 | 828 | #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME |
1106b00e MS |
829 | /* |
830 | * The hostname is not a FQDN, so use the local hostname from the | |
831 | * SystemConfiguration framework... | |
832 | */ | |
833 | ||
834 | SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault, | |
835 | CFSTR("libcups"), NULL, NULL); | |
836 | /* System configuration data */ | |
837 | CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL; | |
838 | /* Local host name */ | |
839 | char localStr[1024]; /* Local host name C string */ | |
840 | ||
841 | if (local && CFStringGetCString(local, localStr, sizeof(localStr), | |
842 | kCFStringEncodingUTF8)) | |
843 | { | |
844 | /* | |
845 | * Append ".local." to the hostname we get... | |
846 | */ | |
847 | ||
848 | snprintf(s, slen, "%s.local.", localStr); | |
849 | } | |
850 | ||
851 | if (local) | |
852 | CFRelease(local); | |
853 | if (sc) | |
854 | CFRelease(sc); | |
855 | ||
856 | #else | |
757d2cad | 857 | /* |
858 | * The hostname is not a FQDN, so look it up... | |
859 | */ | |
860 | ||
1106b00e MS |
861 | struct hostent *host; /* Host entry to get FQDN */ |
862 | ||
89d46774 | 863 | if ((host = gethostbyname(s)) != NULL && host->h_name) |
1106b00e MS |
864 | { |
865 | /* | |
866 | * Use the resolved hostname... | |
867 | */ | |
868 | ||
757d2cad | 869 | strlcpy(s, host->h_name, slen); |
1106b00e | 870 | } |
0837b7e8 | 871 | #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */ |
757d2cad | 872 | } |
ef416fc2 | 873 | } |
874 | ||
875 | /* | |
876 | * Return the hostname with as much domain info as we have... | |
877 | */ | |
878 | ||
879 | return (s); | |
880 | } | |
881 | ||
882 | ||
48bd1142 MS |
883 | /* |
884 | * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection | |
885 | * address. | |
886 | * | |
887 | * @since CUPS 2.0@ | |
888 | */ | |
889 | ||
890 | const char * /* O - Resolved hostname or @code NULL@ */ | |
891 | httpResolveHostname(http_t *http, /* I - HTTP connection */ | |
892 | char *buffer, /* I - Hostname buffer */ | |
893 | size_t bufsize) /* I - Size of buffer */ | |
894 | { | |
895 | if (!http) | |
896 | return (NULL); | |
897 | ||
898 | if (isdigit(http->hostname[0] & 255) || http->hostname[0] == '[') | |
899 | { | |
900 | char temp[1024]; /* Temporary string */ | |
901 | ||
902 | if (httpAddrLookup(http->hostaddr, temp, sizeof(temp))) | |
903 | strlcpy(http->hostname, temp, sizeof(http->hostname)); | |
904 | else | |
905 | return (NULL); | |
906 | } | |
907 | ||
908 | if (buffer) | |
909 | { | |
910 | if (http->hostname[0] == '/') | |
911 | strlcpy(buffer, "localhost", bufsize); | |
912 | else | |
913 | strlcpy(buffer, http->hostname, bufsize); | |
914 | ||
915 | return (buffer); | |
916 | } | |
917 | else if (http->hostname[0] == '/') | |
918 | return ("localhost"); | |
919 | else | |
920 | return (http->hostname); | |
921 | } | |
922 | ||
923 | ||
ef416fc2 | 924 | /* |
f2d18633 | 925 | * End of "$Id$". |
ef416fc2 | 926 | */ |