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