Coverage Report

Created: 2026-09-14 06:46

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