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