]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/http-addr.c
Remove old private API.
[thirdparty/cups.git] / cups / http-addr.c
CommitLineData
e92958ad 1/*
c9d3f842 2 * "$Id$"
e92958ad 3 *
3d94661a 4 * HTTP address routines for CUPS.
e92958ad 5 *
c45401bb 6 * Copyright 2007-2012 by Apple Inc.
7fbf7fc4 7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
e92958ad 8 *
9 * These coded instructions, statements, and computer programs are the
4e8d321f 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/".
e92958ad 14 *
15 * Contents:
16 *
2d3aac27 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.
e92958ad 22 * httpAddrLocalhost() - Check for the local loopback address.
2d3aac27 23 * httpAddrLookup() - Lookup the hostname associated with the address.
24 * httpAddrPort() - Get the port number associated with an address.
f0aa54a1 25 * _httpAddrSetPort() - Set the port number associated with an address.
2d3aac27 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.
e92958ad 30 */
31
32/*
33 * Include necessary headers...
34 */
35
3d94661a 36#include "cups-private.h"
36b81b47 37#ifdef HAVE_RESOLV_H
38# include <resolv.h>
39#endif /* HAVE_RESOLV_H */
4f131de2 40#ifdef __APPLE__
d60acc51 41# include <CoreFoundation/CoreFoundation.h>
d60acc51 42# include <SystemConfiguration/SystemConfiguration.h>
4f131de2 43#endif /* __APPLE__ */
e92958ad 44
45
0f71c41e 46/*
47 * 'httpAddrAny()' - Check for the "any" address.
086c584d 48 *
c45401bb 49 * @since CUPS 1.2/OS X 10.5@
0f71c41e 50 */
51
52int /* O - 1 if "any", 0 otherwise */
53httpAddrAny(const http_addr_t *addr) /* I - Address to check */
54{
e699e7b0 55 if (!addr)
56 return (0);
57
0f71c41e 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
e92958ad 72/*
73 * 'httpAddrEqual()' - Compare two addresses.
086c584d 74 *
c45401bb 75 * @since CUPS 1.2/OS X 10.5@
e92958ad 76 */
77
7fbf7fc4 78int /* O - 1 if equal, 0 if not */
e92958ad 79httpAddrEqual(const http_addr_t *addr1, /* I - First address */
80 const http_addr_t *addr2) /* I - Second address */
81{
e699e7b0 82 if (!addr1 && !addr2)
83 return (1);
84
85 if (!addr1 || !addr2)
86 return (0);
87
e92958ad 88 if (addr1->addr.sa_family != addr2->addr.sa_family)
89 return (0);
90
928c0377 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
e92958ad 96#ifdef AF_INET6
97 if (addr1->addr.sa_family == AF_INET6)
2ccffa6a 98 return (!memcmp(&(addr1->ipv6.sin6_addr), &(addr2->ipv6.sin6_addr), 16));
e92958ad 99#endif /* AF_INET6 */
100
101 return (addr1->ipv4.sin_addr.s_addr == addr2->ipv4.sin_addr.s_addr);
102}
103
104
a36f3f3d 105/*
106 * 'httpAddrLength()' - Return the length of the address in bytes.
086c584d 107 *
c45401bb 108 * @since CUPS 1.2/OS X 10.5@
a36f3f3d 109 */
110
111int /* O - Length in bytes */
112httpAddrLength(const http_addr_t *addr) /* I - Address */
113{
e699e7b0 114 if (!addr)
115 return (0);
116
a36f3f3d 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)
c94f4aa9 124 return (offsetof(struct sockaddr_un, sun_path) +
125 strlen(addr->un.sun_path) + 1);
a36f3f3d 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
2d3aac27 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{
68d05bd3 147 int fd = -1, /* Socket */
148 val; /* Socket value */
149
150
e35b1e23 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 }
2d3aac27 191
68d05bd3 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);
2d3aac27 202}
203
204
e92958ad 205/*
206 * 'httpAddrLocalhost()' - Check for the local loopback address.
086c584d 207 *
c45401bb 208 * @since CUPS 1.2/OS X 10.5@
e92958ad 209 */
210
0f71c41e 211int /* O - 1 if local host, 0 otherwise */
086c584d 212httpAddrLocalhost(
213 const http_addr_t *addr) /* I - Address to check */
e92958ad 214{
e699e7b0 215 if (!addr)
216 return (1);
217
e92958ad 218#ifdef AF_INET6
219 if (addr->addr.sa_family == AF_INET6 &&
1879529a 220 IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr)))
e92958ad 221 return (1);
222#endif /* AF_INET6 */
223
8eba4f2f 224#ifdef AF_LOCAL
c6075312 225 if (addr->addr.sa_family == AF_LOCAL)
226 return (1);
8eba4f2f 227#endif /* AF_LOCAL */
c6075312 228
e92958ad 229 if (addr->addr.sa_family == AF_INET &&
96f8a53a 230 (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000)
e92958ad 231 return (1);
232
233 return (0);
234}
235
236
e92958ad 237/*
238 * 'httpAddrLookup()' - Lookup the hostname associated with the address.
086c584d 239 *
c45401bb 240 * @since CUPS 1.2/OS X 10.5@
e92958ad 241 */
242
7fbf7fc4 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 */
e92958ad 248{
36b81b47 249 _cups_globals_t *cg = _cupsGlobals();
250 /* Global data */
251
252
571af0ea 253 DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name,
254 namelen));
8eba4f2f 255
c94f4aa9 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
1fcc120e 268#ifdef AF_LOCAL
269 if (addr->addr.sa_family == AF_LOCAL)
36b81b47 270 {
1fcc120e 271 strlcpy(name, addr->un.sun_path, namelen);
36b81b47 272 return (name);
273 }
1fcc120e 274#endif /* AF_LOCAL */
36b81b47 275
da94e03d 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
36b81b47 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
c94f4aa9 307#ifdef HAVE_GETNAMEINFO
e92958ad 308 {
c65b193c 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 */
c94f4aa9 316
36b81b47 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
c65b193c 325 return (httpAddrString(addr, name, namelen));
36b81b47 326 }
e92958ad 327 }
c94f4aa9 328#else
329 {
330 struct hostent *host; /* Host from name service */
331
e92958ad 332
c94f4aa9 333# ifdef AF_INET6
334 if (addr->addr.sa_family == AF_INET6)
bb2fc480 335 host = gethostbyaddr((char *)&(addr->ipv6.sin6_addr),
c94f4aa9 336 sizeof(struct in_addr), AF_INET6);
337 else
338# endif /* AF_INET6 */
bb2fc480 339 host = gethostbyaddr((char *)&(addr->ipv4.sin_addr),
c94f4aa9 340 sizeof(struct in_addr), AF_INET);
341
342 if (host == NULL)
343 {
344 /*
345 * No hostname, so return the raw address...
346 */
347
36b81b47 348 if (h_errno == NO_RECOVERY)
349 cg->need_res_init = 1;
350
351 return (httpAddrString(addr, name, namelen));
c94f4aa9 352 }
353
354 strlcpy(name, host->h_name, namelen);
355 }
356#endif /* HAVE_GETNAMEINFO */
e92958ad 357
571af0ea 358 DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name));
359
e92958ad 360 return (name);
361}
362
363
bb235d9a 364/*
2d3aac27 365 * 'httpAddrPort()' - Get the port number associated with an address.
366 *
367 * @since CUPS 1.7@
bb235d9a 368 */
369
370int /* O - Port number */
2d3aac27 371httpAddrPort(http_addr_t *addr) /* I - Address */
bb235d9a 372{
eb1af811 373 if (!addr)
374 return (ippPort());
bb235d9a 375#ifdef AF_INET6
eb1af811 376 else if (addr->addr.sa_family == AF_INET6)
bb235d9a 377 return (ntohs(addr->ipv6.sin6_port));
bb235d9a 378#endif /* AF_INET6 */
eb1af811 379 else if (addr->addr.sa_family == AF_INET)
bb235d9a 380 return (ntohs(addr->ipv4.sin_port));
381 else
382 return (ippPort());
383}
384
385
f0aa54a1 386/*
387 * '_httpAddrSetPort()' - Set the port number associated with an address.
388 */
389
390void
391_httpAddrSetPort(http_addr_t *addr, /* I - Address */
392 int port) /* I - Port */
393{
394 if (!addr || port <= 0)
395 return;
396
397#ifdef AF_INET6
398 if (addr->addr.sa_family == AF_INET6)
399 addr->ipv6.sin6_port = htons(port);
400 else
401#endif /* AF_INET6 */
402 if (addr->addr.sa_family == AF_INET)
403 addr->ipv4.sin_port = htons(port);
404}
405
406
e92958ad 407/*
7fbf7fc4 408 * 'httpAddrString()' - Convert an address to a numeric string.
086c584d 409 *
c45401bb 410 * @since CUPS 1.2/OS X 10.5@
e92958ad 411 */
412
7fbf7fc4 413char * /* O - Numeric address string */
414httpAddrString(const http_addr_t *addr, /* I - Address to convert */
415 char *s, /* I - String buffer */
416 int slen) /* I - Length of string */
e92958ad 417{
571af0ea 418 DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen));
8eba4f2f 419
c94f4aa9 420 /*
421 * Range check input...
422 */
423
424 if (!addr || !s || slen <= 2)
425 {
426 if (s && slen >= 1)
427 *s = '\0';
428
429 return (NULL);
430 }
431
c6075312 432#ifdef AF_LOCAL
433 if (addr->addr.sa_family == AF_LOCAL)
50441edb 434 {
435 if (addr->un.sun_path[0] == '/')
436 strlcpy(s, addr->un.sun_path, slen);
437 else
438 strlcpy(s, "localhost", slen);
439 }
c6075312 440 else
441#endif /* AF_LOCAL */
680610ca 442 if (addr->addr.sa_family == AF_INET)
e92958ad 443 {
680610ca 444 unsigned temp; /* Temporary address */
e92958ad 445
446
680610ca 447 temp = ntohl(addr->ipv4.sin_addr.s_addr);
448
449 snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255,
450 (temp >> 16) & 255, (temp >> 8) & 255, temp & 255);
451 }
452#ifdef AF_INET6
453 else if (addr->addr.sa_family == AF_INET6)
454 {
9d4cf357 455 char *sptr, /* Pointer into string */
456 temps[64]; /* Temporary string for address */
457
680610ca 458# ifdef HAVE_GETNAMEINFO
9d4cf357 459 if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps),
680610ca 460 NULL, 0, NI_NUMERICHOST))
c94f4aa9 461 {
462 /*
463 * If we get an error back, then the address type is not supported
464 * and we should zero out the buffer...
465 */
e92958ad 466
c94f4aa9 467 s[0] = '\0';
468
469 return (NULL);
470 }
9d4cf357 471 else if ((sptr = strchr(temps, '%')) != NULL)
472 {
473 /*
474 * Convert "%zone" to "+zone" to match URI form...
475 */
476
477 *sptr = '+';
478 }
479
680610ca 480# else
680610ca 481 int i; /* Looping var */
482 unsigned temp; /* Current value */
483 const char *prefix; /* Prefix for address */
484
485
486 prefix = "";
9d4cf357 487 for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++)
c94f4aa9 488 {
680610ca 489 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
c94f4aa9 490
9d4cf357 491 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
492 (temp >> 16) & 0xffff);
680610ca 493 prefix = ":";
680610ca 494 sptr += strlen(sptr);
c94f4aa9 495
680610ca 496 temp &= 0xffff;
c94f4aa9 497
680610ca 498 if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1])
499 {
9d4cf357 500 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp);
c94f4aa9 501 sptr += strlen(sptr);
c94f4aa9 502 }
680610ca 503 }
504
505 if (i < 4)
506 {
507 while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i])
508 i ++;
c94f4aa9 509
510 if (i < 4)
511 {
9d4cf357 512 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix);
680610ca 513 prefix = ":";
680610ca 514 sptr += strlen(sptr);
c94f4aa9 515
680610ca 516 for (; i < 4; i ++)
c94f4aa9 517 {
680610ca 518 temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]);
c94f4aa9 519
9d4cf357 520 if ((temp & 0xffff0000) ||
521 (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1]))
c94f4aa9 522 {
9d4cf357 523 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
524 (temp >> 16) & 0xffff);
c94f4aa9 525 sptr += strlen(sptr);
680610ca 526 }
c94f4aa9 527
9d4cf357 528 snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix,
529 temp & 0xffff);
680610ca 530 sptr += strlen(sptr);
c94f4aa9 531 }
532 }
680610ca 533 else if (sptr == s)
534 {
535 /*
536 * Empty address...
537 */
c94f4aa9 538
9d4cf357 539 strlcpy(temps, "::", sizeof(temps));
680610ca 540 }
541 else
542 {
543 /*
544 * Empty at end...
545 */
c94f4aa9 546
9d4cf357 547 strlcpy(sptr, "::", sizeof(temps) - (sptr - temps));
680610ca 548 }
c94f4aa9 549 }
680610ca 550# endif /* HAVE_GETNAMEINFO */
9d4cf357 551
552 /*
553 * Add "[v1." and "]" around IPv6 address to convert to URI form.
554 */
555
556 snprintf(s, slen, "[v1.%s]", temps);
c94f4aa9 557 }
680610ca 558#endif /* AF_INET6 */
559 else
560 strlcpy(s, "UNKNOWN", slen);
e92958ad 561
571af0ea 562 DEBUG_printf(("1httpAddrString: returning \"%s\"...", s));
8eba4f2f 563
e92958ad 564 return (s);
565}
566
567
568/*
086c584d 569 * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return
365cfbcc 570 * address records for the specified name.
086c584d 571 *
572 * @deprecated@
365cfbcc 573 */
574
575struct hostent * /* O - Host entry */
576httpGetHostByName(const char *name) /* I - Hostname or IP address */
577{
578 const char *nameptr; /* Pointer into name */
579 unsigned ip[4]; /* IP address components */
2625096f 580 _cups_globals_t *cg = _cupsGlobals();
03f61bf3 581 /* Pointer to library globals */
365cfbcc 582
6db7190f 583
571af0ea 584 DEBUG_printf(("httpGetHostByName(name=\"%s\")", name));
8eba4f2f 585
03f61bf3 586 /*
587 * Avoid lookup delays and configuration problems when connecting
588 * to the localhost address...
589 */
590
7c298ddc 591 if (!strcmp(name, "localhost"))
03f61bf3 592 name = "127.0.0.1";
365cfbcc 593
594 /*
595 * This function is needed because some operating systems have a
596 * buggy implementation of gethostbyname() that does not support
597 * IP addresses. If the first character of the name string is a
598 * number, then sscanf() is used to extract the IP components.
599 * We then pack the components into an IPv4 address manually,
600 * since the inet_aton() function is deprecated. We use the
601 * htonl() macro to get the right byte order for the address.
1fcc120e 602 *
603 * We also support domain sockets when supported by the underlying
604 * OS...
365cfbcc 605 */
606
1fcc120e 607#ifdef AF_LOCAL
608 if (name[0] == '/')
609 {
610 /*
611 * A domain socket address, so make an AF_LOCAL entry and return it...
612 */
613
03f61bf3 614 cg->hostent.h_name = (char *)name;
615 cg->hostent.h_aliases = NULL;
616 cg->hostent.h_addrtype = AF_LOCAL;
617 cg->hostent.h_length = strlen(name) + 1;
618 cg->hostent.h_addr_list = cg->ip_ptrs;
619 cg->ip_ptrs[0] = (char *)name;
620 cg->ip_ptrs[1] = NULL;
1fcc120e 621
571af0ea 622 DEBUG_puts("1httpGetHostByName: returning domain socket address...");
8eba4f2f 623
03f61bf3 624 return (&cg->hostent);
1fcc120e 625 }
626#endif /* AF_LOCAL */
627
64bbab09 628 for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++);
365cfbcc 629
630 if (!*nameptr)
631 {
632 /*
7c298ddc 633 * We have an IPv4 address; break it up and provide the host entry
634 * to the caller.
365cfbcc 635 */
636
637 if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4)
c5b73e9f 638 return (NULL); /* Must have 4 numbers */
639
640 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
641 return (NULL); /* Invalid byte ranges! */
365cfbcc 642
086c584d 643 cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) |
644 ip[3]));
365cfbcc 645
646 /*
647 * Fill in the host entry and return it...
648 */
649
03f61bf3 650 cg->hostent.h_name = (char *)name;
651 cg->hostent.h_aliases = NULL;
652 cg->hostent.h_addrtype = AF_INET;
653 cg->hostent.h_length = 4;
654 cg->hostent.h_addr_list = cg->ip_ptrs;
086c584d 655 cg->ip_ptrs[0] = (char *)&(cg->ip_addr);
656 cg->ip_ptrs[1] = NULL;
365cfbcc 657
571af0ea 658 DEBUG_puts("1httpGetHostByName: returning IPv4 address...");
8eba4f2f 659
03f61bf3 660 return (&cg->hostent);
365cfbcc 661 }
662 else
7c298ddc 663 {
664 /*
086c584d 665 * Use the gethostbyname() function to get the IPv4 address for
365cfbcc 666 * the name...
667 */
668
571af0ea 669 DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)...");
8eba4f2f 670
365cfbcc 671 return (gethostbyname(name));
672 }
673}
674
675
0a283777 676/*
7701ffe8 677 * 'httpGetHostname()' - Get the FQDN for the connection or local system.
0a283777 678 *
7701ffe8 679 * When "http" points to a connected socket, return the hostname or
680 * address that was used in the call to httpConnect() or httpConnectEncrypt().
681 * Otherwise, return the FQDN for the local system using both gethostname()
682 * and gethostbyname() to get the local hostname with domain.
086c584d 683 *
c45401bb 684 * @since CUPS 1.2/OS X 10.5@
0a283777 685 */
686
7701ffe8 687const char * /* O - FQDN for connection or system */
688httpGetHostname(http_t *http, /* I - HTTP connection or NULL */
689 char *s, /* I - String buffer for name */
690 int slen) /* I - Size of buffer */
0a283777 691{
da16e37a 692 if (!s || slen <= 1)
693 return (NULL);
694
7701ffe8 695 if (http)
da2337b1 696 {
697 if (http->hostname[0] == '/')
698 strlcpy(s, "localhost", slen);
699 else
700 strlcpy(s, http->hostname, slen);
701 }
7701ffe8 702 else
0a283777 703 {
704 /*
7701ffe8 705 * Get the hostname...
0a283777 706 */
707
da16e37a 708 if (gethostname(s, slen) < 0)
709 strlcpy(s, "localhost", slen);
7701ffe8 710
711 if (!strchr(s, '.'))
712 {
a1a74874 713#ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
d60acc51 714 /*
715 * The hostname is not a FQDN, so use the local hostname from the
716 * SystemConfiguration framework...
717 */
718
719 SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault,
720 CFSTR("libcups"), NULL, NULL);
721 /* System configuration data */
722 CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL;
723 /* Local host name */
724 char localStr[1024]; /* Local host name C string */
725
726 if (local && CFStringGetCString(local, localStr, sizeof(localStr),
727 kCFStringEncodingUTF8))
728 {
729 /*
730 * Append ".local." to the hostname we get...
731 */
732
733 snprintf(s, slen, "%s.local.", localStr);
734 }
735
736 if (local)
737 CFRelease(local);
738 if (sc)
739 CFRelease(sc);
740
741#else
7701ffe8 742 /*
743 * The hostname is not a FQDN, so look it up...
744 */
745
d60acc51 746 struct hostent *host; /* Host entry to get FQDN */
747
da16e37a 748 if ((host = gethostbyname(s)) != NULL && host->h_name)
d60acc51 749 {
750 /*
751 * Use the resolved hostname...
752 */
753
7701ffe8 754 strlcpy(s, host->h_name, slen);
d60acc51 755 }
a1a74874 756#endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
7701ffe8 757 }
0a283777 758 }
759
760 /*
761 * Return the hostname with as much domain info as we have...
762 */
763
764 return (s);
765}
766
767
365cfbcc 768/*
c9d3f842 769 * End of "$Id$".
e92958ad 770 */