Coverage Report

Created: 2023-03-26 07:11

/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
151
{
36
151
  int   saved_errno;
37
151
  char *    res;
38
151
  char *    addr;
39
151
  u_long    scope;
40
41
151
  saved_errno = socket_errno();
42
151
  LIB_GETBUF(res);
43
44
151
  if (NULL == sock) {
45
0
    strlcpy(res, "(null)", LIB_BUFLENGTH);
46
151
  } else {
47
151
    switch(AF(sock)) {
48
49
43
    case AF_INET:
50
43
    case AF_UNSPEC:
51
43
      inet_ntop(AF_INET, PSOCK_ADDR4(sock), res,
52
43
          LIB_BUFLENGTH);
53
43
      break;
54
55
108
    case AF_INET6:
56
108
      inet_ntop(AF_INET6, PSOCK_ADDR6(sock), res,
57
108
          LIB_BUFLENGTH);
58
108
      scope = SCOPE_VAR(sock);
59
108
      if (0 != scope && !strchr(res, '%')) {
60
67
        addr = res;
61
67
        LIB_GETBUF(res);
62
67
        snprintf(res, LIB_BUFLENGTH, "%s%%%lu",
63
67
           addr, scope);
64
67
        res[LIB_BUFLENGTH - 1] = '\0';
65
67
      }
66
108
      break;
67
68
0
    default:
69
0
      snprintf(res, LIB_BUFLENGTH, 
70
151
         "(socktoa unknown family %d)", 
71
151
         AF(sock));
72
151
    }
73
151
  }
74
151
  errno = saved_errno;
75
76
151
  return res;
77
151
}
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
246
{
111
246
  u_int hashVal;
112
246
  u_int j;
113
246
  size_t len;
114
246
  const u_char *pch;
115
116
246
  hashVal = 0;
117
246
  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
246
  pch = (const void *)&AF(addr);
125
246
  hashVal = 37 * hashVal + *pch;
126
246
  if (sizeof(AF(addr)) > 1) {
127
246
    pch++;
128
246
    hashVal = 37 * hashVal + *pch;
129
246
  }
130
246
  switch(AF(addr)) {
131
84
  case AF_INET:
132
84
    pch = (const void *)&SOCK_ADDR4(addr);
133
84
    len = sizeof(SOCK_ADDR4(addr));
134
84
    break;
135
136
162
  case AF_INET6:
137
162
    pch = (const void *)&SOCK_ADDR6(addr);
138
162
    len = sizeof(SOCK_ADDR6(addr));
139
162
    break;
140
246
  }
141
142
3.17k
  for (j = 0; j < len ; j++)
143
2.92k
    hashVal = 37 * hashVal + pch[j];
144
145
246
  return (u_short)(hashVal & USHRT_MAX);
146
246
}
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
}