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