Coverage Report

Created: 2026-02-26 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntp-dev/libntp/numtoa.c
Line
Count
Source
1
/*
2
 * numtoa - return asciized network numbers store in local array space
3
 */
4
#include <config.h>
5
6
#include <sys/types.h>
7
#ifdef HAVE_NETINET_IN_H
8
#include <netinet/in.h>   /* ntohl */
9
#endif
10
11
#include <stdio.h>
12
#include <ctype.h>
13
14
#include "ntp_fp.h"
15
#include "ntp_stdlib.h"
16
17
char *
18
numtoa(
19
  u_int32 num
20
  )
21
0
{
22
0
  register u_int32 netnum;
23
0
  register char *buf;
24
25
0
  netnum = ntohl(num);
26
0
  LIB_GETBUF(buf);
27
0
  snprintf(buf, LIB_BUFLENGTH, "%lu.%lu.%lu.%lu",
28
0
     ((u_long)netnum >> 24) & 0xff,
29
0
     ((u_long)netnum >> 16) & 0xff,
30
0
     ((u_long)netnum >> 8) & 0xff,
31
0
     (u_long)netnum & 0xff);
32
0
  return buf;
33
0
}
34
35
36
/* 
37
 * Convert a refid & stratum to a string.  If stratum is negative and the
38
 * refid consists entirely of graphic chars, up to an optional
39
 * terminating zero, display as text similar to stratum 0 & 1.
40
 */
41
const char *
42
refid_str(
43
  u_int32 refid,
44
  int stratum
45
  )
46
0
{
47
0
  char *  text;
48
0
  size_t  tlen;
49
0
  char *  cp;
50
0
  int printable;
51
52
  /*
53
   * ntpd can have stratum = 0 and refid 127.0.0.1 in orphan mode.
54
   * https://bugs.ntp.org/3854.  Mirror the refid logic in timer().
55
   */
56
0
  if (0 == stratum && LOOPBACKADR_N == refid) {
57
0
    return ".ORPH.";
58
0
  }
59
0
  printable = FALSE;
60
0
  if (stratum < 2) {
61
0
    text = lib_getbuf();
62
0
    text[0] = '.';
63
0
    memcpy(&text[1], &refid, sizeof(refid));
64
0
    text[1 + sizeof(refid)] = '\0';
65
0
    tlen = strlen(text);
66
0
    text[tlen] = '.';
67
0
    text[tlen + 1] = '\0';
68
    /*
69
     * Now make sure the contents are 'graphic'.
70
     *
71
     * This refid is expected to be up to 4 printable ASCII.
72
     * isgraph() is similar to isprint() but excludes space.
73
     * If any character is not graphic, replace it with a '?'.
74
     * This will at least alert the viewer of a problem.
75
     */
76
0
    for (cp = text + 1; '\0' != *cp; ++cp) {
77
0
      if (!isgraph((int)*cp)) {
78
0
        printable = FALSE;
79
0
        *cp = '?';
80
0
      }
81
0
    }
82
0
    if (   (stratum < 0 && printable)
83
0
        || stratum < 2) {
84
0
      return text;
85
0
    }
86
0
  }
87
0
  return numtoa(refid);
88
0
}
89