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