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