Coverage Report

Created: 2026-04-09 07:06

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