Coverage Report

Created: 2023-05-19 06:16

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