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