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