Coverage Report

Created: 2026-02-26 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntp-dev/libntp/socktoa.c
Line
Count
Source
1
/*
2
 * socktoa.c  socktoa(), sockporttoa(), and sock_hash()
3
 */
4
5
#ifdef HAVE_CONFIG_H
6
#include <config.h>
7
#endif
8
9
#include <sys/types.h>
10
#ifdef HAVE_SYS_SOCKET_H
11
#include <sys/socket.h>
12
#endif
13
#ifdef HAVE_NETINET_IN_H
14
#include <netinet/in.h>
15
#endif
16
17
#include <stdio.h>
18
#include <arpa/inet.h>
19
#include <isc/result.h>
20
#include <isc/netaddr.h>
21
#include <isc/sockaddr.h>
22
23
#include "ntp_fp.h"
24
#include "ntp_stdlib.h"
25
#include "ntp.h"
26
27
/*
28
 * socktoa - return a numeric host name from a sockaddr_storage structure
29
 */
30
const char *
31
socktoa(
32
  const sockaddr_u *sock
33
  )
34
4
{
35
4
  int   saved_errno;
36
4
  char *    res;
37
4
  char *    addr;
38
4
  u_long    scope;
39
40
4
  saved_errno = socket_errno();
41
4
  LIB_GETBUF(res);
42
43
4
  if (NULL == sock) {
44
0
    strlcpy(res, "(null)", LIB_BUFLENGTH);
45
4
  } else {
46
4
    switch(AF(sock)) {
47
48
3
    case AF_INET:
49
3
    case AF_UNSPEC:
50
3
      inet_ntop(AF_INET, PSOCK_ADDR4(sock), res,
51
3
          LIB_BUFLENGTH);
52
3
      break;
53
54
1
    case AF_INET6:
55
1
      inet_ntop(AF_INET6, PSOCK_ADDR6(sock), res,
56
1
          LIB_BUFLENGTH);
57
1
      scope = SCOPE_VAR(sock);
58
1
      if (0 != scope && !strchr(res, '%')) {
59
0
        addr = res;
60
0
        LIB_GETBUF(res);
61
0
        snprintf(res, LIB_BUFLENGTH, "%s%%%lu",
62
0
           addr, scope);
63
0
        res[LIB_BUFLENGTH - 1] = '\0';
64
0
      }
65
1
      break;
66
67
0
    default:
68
0
      snprintf(res, LIB_BUFLENGTH, 
69
4
         "(socktoa unknown family %d)", 
70
4
         AF(sock));
71
4
    }
72
4
  }
73
4
  errno = saved_errno;
74
75
4
  return res;
76
4
}
77
78
79
const char *
80
sockporttoa(
81
  const sockaddr_u *sock
82
  )
83
4
{
84
4
  int   saved_errno;
85
4
  const char *  atext;
86
4
  char *    buf;
87
88
4
  saved_errno = socket_errno();
89
4
  atext = socktoa(sock);
90
4
  LIB_GETBUF(buf);
91
4
  snprintf(buf, LIB_BUFLENGTH,
92
4
     (IS_IPV6(sock))
93
4
         ? "[%s]:%hu"
94
4
         : "%s:%hu",
95
4
     atext, SRCPORT(sock));
96
4
  errno = saved_errno;
97
98
4
  return buf;
99
4
}
100
101
102
/*
103
 * sock_hash - hash a sockaddr_u structure
104
 */
105
u_short
106
sock_hash(
107
  const sockaddr_u *addr
108
  )
109
0
{
110
0
  u_int hashVal;
111
0
  u_int j;
112
0
  size_t len;
113
0
  const u_char *pch;
114
115
0
  hashVal = 0;
116
0
  len = 0;
117
118
  /*
119
   * We can't just hash the whole thing because there are hidden
120
   * fields in sockaddr_in6 that might be filled in by recvfrom(),
121
   * so just use the family and address.
122
   */
123
0
  pch = (const void *)&AF(addr);
124
0
  hashVal = 37 * hashVal + *pch;
125
0
  if (sizeof(AF(addr)) > 1) {
126
0
    pch++;
127
0
    hashVal = 37 * hashVal + *pch;
128
0
  }
129
0
  switch(AF(addr)) {
130
0
  case AF_INET:
131
0
    pch = (const void *)&SOCK_ADDR4(addr);
132
0
    len = sizeof(SOCK_ADDR4(addr));
133
0
    break;
134
135
0
  case AF_INET6:
136
0
    pch = (const void *)&SOCK_ADDR6(addr);
137
0
    len = sizeof(SOCK_ADDR6(addr));
138
0
    break;
139
0
  }
140
141
0
  for (j = 0; j < len ; j++)
142
0
    hashVal = 37 * hashVal + pch[j];
143
144
0
  return (u_short)(hashVal & USHRT_MAX);
145
0
}
146
147
148
int
149
sockaddr_masktoprefixlen(
150
  const sockaddr_u *  psa
151
  )
152
0
{
153
0
  isc_netaddr_t isc_na;
154
0
  isc_sockaddr_t  isc_sa;
155
0
  u_int   pfxlen;
156
0
  isc_result_t  result;
157
0
  int   rc;
158
159
0
  ZERO(isc_sa);
160
0
  memcpy(&isc_sa.type, psa,
161
0
         min(sizeof(isc_sa.type), sizeof(*psa)));
162
0
  isc_netaddr_fromsockaddr(&isc_na, &isc_sa);
163
0
  result = isc_netaddr_masktoprefixlen(&isc_na, &pfxlen);
164
0
  rc = (ISC_R_SUCCESS == result)
165
0
     ? (int)pfxlen
166
0
     : -1;
167
168
0
  return rc;
169
0
}