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