]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/http-addr.c
Save work; public accessors for more stuff, continue transition away from private
[thirdparty/cups.git] / cups / http-addr.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
996acce8 4 * HTTP address routines for CUPS.
ef416fc2 5 *
996acce8
MS
6 * Copyright 2007-2013 by Apple Inc.
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"
49d87452
MS
21#ifdef HAVE_RESOLV_H
22# include <resolv.h>
23#endif /* HAVE_RESOLV_H */
a29fd7dd 24#ifdef __APPLE__
1106b00e 25# include <CoreFoundation/CoreFoundation.h>
1106b00e 26# include <SystemConfiguration/SystemConfiguration.h>
a29fd7dd 27#endif /* __APPLE__ */
ef416fc2 28
29
30/*
31 * 'httpAddrAny()' - Check for the "any" address.
32 *
f3c17241 33 * @since CUPS 1.2/OS X 10.5@
ef416fc2 34 */
35
36int /* O - 1 if "any", 0 otherwise */
37httpAddrAny(const http_addr_t *addr) /* I - Address to check */
38{
89d46774 39 if (!addr)
40 return (0);
41
ef416fc2 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 *
f3c17241 59 * @since CUPS 1.2/OS X 10.5@
ef416fc2 60 */
61
ecdc0628 62int /* O - 1 if equal, 0 if not */
ef416fc2 63httpAddrEqual(const http_addr_t *addr1, /* I - First address */
64 const http_addr_t *addr2) /* I - Second address */
65{
89d46774 66 if (!addr1 && !addr2)
67 return (1);
68
69 if (!addr1 || !addr2)
70 return (0);
71
ef416fc2 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 *
f3c17241 92 * @since CUPS 1.2/OS X 10.5@
ef416fc2 93 */
94
95int /* O - Length in bytes */
96httpAddrLength(const http_addr_t *addr) /* I - Address */
97{
89d46774 98 if (!addr)
99 return (0);
100
ef416fc2 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
a469f8a5
MS
120/*
121 * 'httpAddrListen()' - Create a listening socket bound to the specified
122 * address and port.
123 *
9c0e8e5d 124 * @since CUPS 1.7/OS X 10.9@
a469f8a5
MS
125 */
126
127int /* O - Socket or -1 on error */
128httpAddrListen(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;
db8b865d 149 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, CUPS_SOCAST &val, sizeof(val));
a469f8a5
MS
150
151#ifdef IPV6_V6ONLY
152 if (addr->addr.sa_family == AF_INET6)
db8b865d 153 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, CUPS_SOCAST &val, sizeof(val));
a469f8a5
MS
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;
db8b865d 182 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val));
a469f8a5
MS
183#endif /* SO_NOSIGPIPE */
184
185 return (fd);
186}
187
188
ef416fc2 189/*
190 * 'httpAddrLocalhost()' - Check for the local loopback address.
191 *
f3c17241 192 * @since CUPS 1.2/OS X 10.5@
ef416fc2 193 */
194
195int /* O - 1 if local host, 0 otherwise */
196httpAddrLocalhost(
197 const http_addr_t *addr) /* I - Address to check */
198{
89d46774 199 if (!addr)
200 return (1);
201
ef416fc2 202#ifdef AF_INET6
203 if (addr->addr.sa_family == AF_INET6 &&
fa73b229 204 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
ef416fc2 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 &&
e07d4801 214 (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000)
ef416fc2 215 return (1);
216
217 return (0);
218}
219
220
ef416fc2 221/*
222 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
223 *
f3c17241 224 * @since CUPS 1.2/OS X 10.5@
ef416fc2 225 */
226
ecdc0628 227char * /* O - Host name */
228httpAddrLookup(
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 */
ef416fc2 232{
49d87452
MS
233 _cups_globals_t *cg = _cupsGlobals();
234 /* Global data */
235
236
e07d4801
MS
237 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name,
238 namelen));
ef416fc2 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)
49d87452 254 {
ef416fc2 255 strlcpy(name, addr->un.sun_path, namelen);
49d87452
MS
256 return (name);
257 }
ef416fc2 258#endif /* AF_LOCAL */
49d87452 259
d2354e63
MS
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
49d87452
MS
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
ef416fc2 291#ifdef HAVE_GETNAMEINFO
292 {
db1f069b
MS
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 */
ef416fc2 300
49d87452
MS
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
db1f069b 309 return (httpAddrString(addr, name, namelen));
49d87452 310 }
ef416fc2 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)
3dd9c340 319 host = gethostbyaddr((char *)&(addr->ipv6.sin6_addr),
ef416fc2 320 sizeof(struct in_addr), AF_INET6);
321 else
322# endif /* AF_INET6 */
3dd9c340 323 host = gethostbyaddr((char *)&(addr->ipv4.sin_addr),
ef416fc2 324 sizeof(struct in_addr), AF_INET);
325
326 if (host == NULL)
327 {
328 /*
329 * No hostname, so return the raw address...
330 */
331
49d87452
MS
332 if (h_errno == NO_RECOVERY)
333 cg->need_res_init = 1;
334
335 return (httpAddrString(addr, name, namelen));
ef416fc2 336 }
337
338 strlcpy(name, host->h_name, namelen);
339 }
340#endif /* HAVE_GETNAMEINFO */
341
e07d4801
MS
342 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name));
343
ef416fc2 344 return (name);
345}
346
347
5ec1fd3d
MS
348/*
349 * 'httpAddrFamily()' - Get the address family of an address.
350 */
351
352int /* O - Address family */
353httpAddrFamily(http_addr_t *addr) /* I - Address */
354{
355 if (addr)
356 return (addr->addr.sa_family);
357 else
358 return (0);
359}
360
361
1ff0402e 362/*
a469f8a5
MS
363 * 'httpAddrPort()' - Get the port number associated with an address.
364 *
9c0e8e5d 365 * @since CUPS 1.7/OS X 10.9@
1ff0402e
MS
366 */
367
368int /* O - Port number */
a469f8a5 369httpAddrPort(http_addr_t *addr) /* I - Address */
1ff0402e 370{
5f64df29 371 if (!addr)
5ec1fd3d 372 return (-1);
1ff0402e 373#ifdef AF_INET6
5f64df29 374 else if (addr->addr.sa_family == AF_INET6)
1ff0402e 375 return (ntohs(addr->ipv6.sin6_port));
1ff0402e 376#endif /* AF_INET6 */
5f64df29 377 else if (addr->addr.sa_family == AF_INET)
1ff0402e
MS
378 return (ntohs(addr->ipv4.sin_port));
379 else
5ec1fd3d 380 return (0);
1ff0402e
MS
381}
382
c1420c87
MS
383/* For OS X 10.8 and earlier */
384int _httpAddrPort(http_addr_t *addr) { return (httpAddrPort(addr)); }
385
1ff0402e 386
22c9029b
MS
387/*
388 * '_httpAddrSetPort()' - Set the port number associated with an address.
389 */
390
391void
392_httpAddrSetPort(http_addr_t *addr, /* I - Address */
393 int port) /* I - Port */
394{
395 if (!addr || port <= 0)
396 return;
397
398#ifdef AF_INET6
399 if (addr->addr.sa_family == AF_INET6)
400 addr->ipv6.sin6_port = htons(port);
401 else
402#endif /* AF_INET6 */
403 if (addr->addr.sa_family == AF_INET)
404 addr->ipv4.sin_port = htons(port);
405}
406
407
ef416fc2 408/*
ecdc0628 409 * 'httpAddrString()' - Convert an address to a numeric string.
ef416fc2 410 *
f3c17241 411 * @since CUPS 1.2/OS X 10.5@
ef416fc2 412 */
413
ecdc0628 414char * /* O - Numeric address string */
415httpAddrString(const http_addr_t *addr, /* I - Address to convert */
416 char *s, /* I - String buffer */
417 int slen) /* I - Length of string */
ef416fc2 418{
e07d4801 419 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen));
ef416fc2 420
421 /*
422 * Range check input...
423 */
424
425 if (!addr || !s || slen <= 2)
426 {
427 if (s && slen >= 1)
428 *s = '\0';
429
430 return (NULL);
431 }
432
433#ifdef AF_LOCAL
434 if (addr->addr.sa_family == AF_LOCAL)
771bd8cb
MS
435 {
436 if (addr->un.sun_path[0] == '/')
437 strlcpy(s, addr->un.sun_path, slen);
438 else
439 strlcpy(s, "localhost", slen);
440 }
ef416fc2 441 else
442#endif /* AF_LOCAL */
b423cd4c 443 if (addr->addr.sa_family == AF_INET)
ef416fc2 444 {
b423cd4c 445 unsigned temp; /* Temporary address */
ef416fc2 446
447
b423cd4c 448 temp = ntohl(addr->ipv4.sin_addr.s_addr);
449
450 snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255,
451 (temp >> 16) & 255, (temp >> 8) & 255, temp & 255);
452 }
453#ifdef AF_INET6
454 else if (addr->addr.sa_family == AF_INET6)
455 {
82f97232
MS
456 char *sptr, /* Pointer into string */
457 temps[64]; /* Temporary string for address */
458
b423cd4c 459# ifdef HAVE_GETNAMEINFO
82f97232 460 if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps),
b423cd4c 461 NULL, 0, NI_NUMERICHOST))
ef416fc2 462 {
463 /*
464 * If we get an error back, then the address type is not supported
465 * and we should zero out the buffer...
466 */
467
468 s[0] = '\0';
469
470 return (NULL);
471 }
82f97232
MS
472 else if ((sptr = strchr(temps, '%')) != NULL)
473 {
474 /*
475 * Convert "%zone" to "+zone" to match URI form...
476 */
477
478 *sptr = '+';
479 }
480
b423cd4c 481# else
b423cd4c 482 int i; /* Looping var */
483 unsigned temp; /* Current value */
484 const char *prefix; /* Prefix for address */
485
486
487 prefix = "";
82f97232 488 for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
ef416fc2 489 {
b423cd4c 490 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
ef416fc2 491
82f97232
MS
492 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
493 (temp >> 16) & 0xffff);
b423cd4c 494 prefix = ":";
b423cd4c 495 sptr += strlen(sptr);
ef416fc2 496
b423cd4c 497 temp &= 0xffff;
ef416fc2 498
b423cd4c 499 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
500 {
82f97232 501 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp);
ef416fc2 502 sptr += strlen(sptr);
ef416fc2 503 }
b423cd4c 504 }
505
506 if (i < 4)
507 {
508 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
509 i ++;
ef416fc2 510
511 if (i < 4)
512 {
82f97232 513 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix);
b423cd4c 514 prefix = ":";
b423cd4c 515 sptr += strlen(sptr);
ef416fc2 516
b423cd4c 517 for (; i < 4; i ++)
ef416fc2 518 {
b423cd4c 519 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
ef416fc2 520
82f97232
MS
521 if ((temp & 0xffff0000) ||
522 (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1]))
ef416fc2 523 {
82f97232
MS
524 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
525 (temp >> 16) & 0xffff);
ef416fc2 526 sptr += strlen(sptr);
b423cd4c 527 }
ef416fc2 528
82f97232
MS
529 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
530 temp & 0xffff);
b423cd4c 531 sptr += strlen(sptr);
ef416fc2 532 }
533 }
b423cd4c 534 else if (sptr == s)
535 {
536 /*
537 * Empty address...
538 */
ef416fc2 539
82f97232 540 strlcpy(temps, "::", sizeof(temps));
b423cd4c 541 }
542 else
543 {
544 /*
545 * Empty at end...
546 */
ef416fc2 547
82f97232 548 strlcpy(sptr, "::", sizeof(temps) - (sptr - temps));
b423cd4c 549 }
ef416fc2 550 }
b423cd4c 551# endif /* HAVE_GETNAMEINFO */
82f97232
MS
552
553 /*
554 * Add "[v1." and "]" around IPv6 address to convert to URI form.
555 */
556
557 snprintf(s, slen, "[v1.%s]", temps);
ef416fc2 558 }
b423cd4c 559#endif /* AF_INET6 */
560 else
561 strlcpy(s, "UNKNOWN", slen);
ef416fc2 562
e07d4801 563 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s));
ef416fc2 564
565 return (s);
566}
567
568
996acce8
MS
569/*
570 * 'httpGetAddress()' - Get the address of the connected peer of a connection.
571 *
572 * Returns @code NULL@ if the socket is currently unconnected.
573 *
574 * @since CUPS 2.0@
575 */
576
577http_addr_t * /* O - Connected address or @code NULL@ */
578httpGetAddress(http_t *http) /* I - HTTP connection */
579{
580 if (http)
581 return (http->hostaddr);
582 else
583 return (NULL);
584}
585
586
ef416fc2 587/*
588 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
589 * address records for the specified name.
590 *
591 * @deprecated@
592 */
593
594struct hostent * /* O - Host entry */
595httpGetHostByName(const char *name) /* I - Hostname or IP address */
596{
597 const char *nameptr; /* Pointer into name */
598 unsigned ip[4]; /* IP address components */
599 _cups_globals_t *cg = _cupsGlobals();
600 /* Pointer to library globals */
601
602
e07d4801 603 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name));
ef416fc2 604
605 /*
606 * Avoid lookup delays and configuration problems when connecting
607 * to the localhost address...
608 */
609
610 if (!strcmp(name, "localhost"))
611 name = "127.0.0.1";
612
613 /*
614 * This function is needed because some operating systems have a
615 * buggy implementation of gethostbyname() that does not support
616 * IP addresses. If the first character of the name string is a
617 * number, then sscanf() is used to extract the IP components.
618 * We then pack the components into an IPv4 address manually,
619 * since the inet_aton() function is deprecated. We use the
620 * htonl() macro to get the right byte order for the address.
621 *
622 * We also support domain sockets when supported by the underlying
623 * OS...
624 */
625
626#ifdef AF_LOCAL
627 if (name[0] == '/')
628 {
629 /*
630 * A domain socket address, so make an AF_LOCAL entry and return it...
631 */
632
633 cg->hostent.h_name = (char *)name;
634 cg->hostent.h_aliases = NULL;
635 cg->hostent.h_addrtype = AF_LOCAL;
636 cg->hostent.h_length = strlen(name) + 1;
637 cg->hostent.h_addr_list = cg->ip_ptrs;
638 cg->ip_ptrs[0] = (char *)name;
639 cg->ip_ptrs[1] = NULL;
640
e07d4801 641 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
ef416fc2 642
643 return (&cg->hostent);
644 }
645#endif /* AF_LOCAL */
646
647 for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++);
648
649 if (!*nameptr)
650 {
651 /*
652 * We have an IPv4 address; break it up and provide the host entry
653 * to the caller.
654 */
655
656 if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4)
657 return (NULL); /* Must have 4 numbers */
658
659 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
660 return (NULL); /* Invalid byte ranges! */
661
662 cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) |
663 ip[3]));
664
665 /*
666 * Fill in the host entry and return it...
667 */
668
669 cg->hostent.h_name = (char *)name;
670 cg->hostent.h_aliases = NULL;
671 cg->hostent.h_addrtype = AF_INET;
672 cg->hostent.h_length = 4;
673 cg->hostent.h_addr_list = cg->ip_ptrs;
674 cg->ip_ptrs[0] = (char *)&(cg->ip_addr);
675 cg->ip_ptrs[1] = NULL;
676
e07d4801 677 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
ef416fc2 678
679 return (&cg->hostent);
680 }
681 else
682 {
683 /*
684 * Use the gethostbyname() function to get the IPv4 address for
685 * the name...
686 */
687
e07d4801 688 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
ef416fc2 689
690 return (gethostbyname(name));
691 }
692}
693
694
695/*
757d2cad 696 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
ef416fc2 697 *
757d2cad 698 * When "http" points to a connected socket, return the hostname or
699 * address that was used in the call to httpConnect() or httpConnectEncrypt().
700 * Otherwise, return the FQDN for the local system using both gethostname()
701 * and gethostbyname() to get the local hostname with domain.
ef416fc2 702 *
f3c17241 703 * @since CUPS 1.2/OS X 10.5@
ef416fc2 704 */
705
757d2cad 706const char * /* O - FQDN for connection or system */
707httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
708 char *s, /* I - String buffer for name */
709 int slen) /* I - Size of buffer */
ef416fc2 710{
757d2cad 711 if (http)
480ef0fe 712 {
5ec1fd3d
MS
713 if (!s || slen <= 1)
714 {
715 if (http->hostname[0] == '/')
716 return ("localhost");
717 else
718 return (http->hostname);
719 }
720 else if (http->hostname[0] == '/')
480ef0fe 721 strlcpy(s, "localhost", slen);
722 else
723 strlcpy(s, http->hostname, slen);
724 }
757d2cad 725 else
ef416fc2 726 {
727 /*
757d2cad 728 * Get the hostname...
ef416fc2 729 */
730
5ec1fd3d
MS
731 if (!s || slen <= 1)
732 return (NULL);
733
89d46774 734 if (gethostname(s, slen) < 0)
735 strlcpy(s, "localhost", slen);
757d2cad 736
737 if (!strchr(s, '.'))
738 {
0837b7e8 739#ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
1106b00e
MS
740 /*
741 * The hostname is not a FQDN, so use the local hostname from the
742 * SystemConfiguration framework...
743 */
744
745 SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault,
746 CFSTR("libcups"), NULL, NULL);
747 /* System configuration data */
748 CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL;
749 /* Local host name */
750 char localStr[1024]; /* Local host name C string */
751
752 if (local && CFStringGetCString(local, localStr, sizeof(localStr),
753 kCFStringEncodingUTF8))
754 {
755 /*
756 * Append ".local." to the hostname we get...
757 */
758
759 snprintf(s, slen, "%s.local.", localStr);
760 }
761
762 if (local)
763 CFRelease(local);
764 if (sc)
765 CFRelease(sc);
766
767#else
757d2cad 768 /*
769 * The hostname is not a FQDN, so look it up...
770 */
771
1106b00e
MS
772 struct hostent *host; /* Host entry to get FQDN */
773
89d46774 774 if ((host = gethostbyname(s)) != NULL && host->h_name)
1106b00e
MS
775 {
776 /*
777 * Use the resolved hostname...
778 */
779
757d2cad 780 strlcpy(s, host->h_name, slen);
1106b00e 781 }
0837b7e8 782#endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
757d2cad 783 }
ef416fc2 784 }
785
786 /*
787 * Return the hostname with as much domain info as we have...
788 */
789
790 return (s);
791}
792
793
794/*
f2d18633 795 * End of "$Id$".
ef416fc2 796 */