Coverage Report

Created: 2026-01-09 07:09

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
354
{
38
354
  int   saved_errno;
39
354
  unsigned long scope;
40
41
354
  saved_errno = errno;
42
43
354
  if (NULL == sock) {
44
0
    strlcpy(buf, "(null)", buflen);
45
354
  } else {
46
354
    switch(AF(sock)) {
47
48
354
    case AF_INET:
49
354
    case AF_UNSPEC:
50
354
      inet_ntop(AF_INET, PSOCK_ADDR4(sock), buf, buflen);
51
354
      break;
52
53
0
    case AF_INET6:
54
0
      inet_ntop(AF_INET6, PSOCK_ADDR6(sock), buf, buflen);
55
0
      scope = SCOPE_VAR(sock);
56
0
      if (0 != scope && !strchr(buf, '%')) {
57
0
        char buf2[LIB_BUFLENGTH];
58
0
        snprintf(buf2, sizeof(buf2), "%s%%%lu",
59
0
           buf, scope);
60
0
        buf2[LIB_BUFLENGTH - 1] = '\0';
61
0
        strlcpy(buf, buf2, buflen);
62
0
      }
63
0
      break;
64
65
0
    default:
66
0
      snprintf(buf, buflen,
67
0
         "(socktoa unknown family %d)",
68
0
         AF(sock));
69
354
    }
70
354
  }
71
354
  errno = saved_errno;
72
73
354
  return buf;
74
354
}
75
76
77
const char *
78
sockporttoa(
79
  const sockaddr_u *sock
80
  )
81
0
{
82
0
  char *buf = lib_getbuf();
83
0
  sockporttoa_r(sock, buf, LIB_BUFLENGTH);
84
0
  return buf;
85
0
}
86
87
const char *
88
sockporttoa_r(
89
  const sockaddr_u *sock, char *buf, size_t buflen
90
  )
91
0
{
92
0
  int saved_errno;
93
0
        char buf2[LIB_BUFLENGTH];
94
95
0
  saved_errno = errno;
96
0
  socktoa_r(sock, buf2, sizeof(buf2));
97
0
  snprintf(buf, buflen,
98
0
     (IS_IPV6(sock))
99
0
         ? "[%s]:%hu"
100
0
         : "%s:%hu",
101
0
     buf2, (unsigned short)SRCPORT(sock));
102
0
  errno = saved_errno;
103
104
0
  return buf;
105
0
}
106
107
108
/*
109
 * sock_hash - hash a sockaddr_u structure
110
 */
111
unsigned int
112
sock_hash(
113
  const sockaddr_u *addr
114
  )
115
0
{
116
0
  unsigned int hashVal;
117
0
  size_t len;
118
0
  const uint8_t *pch;
119
120
0
  hashVal = 0;
121
0
  len = 0;
122
123
  /*
124
   * We can't just hash the whole thing because there are hidden
125
   * fields in sockaddr_in6 that might be filled in by recvfrom(),
126
   * so just use the family, port and address.
127
   */
128
0
  pch = (const void *)&AF(addr);
129
0
  hashVal = 37 * hashVal + *pch;
130
0
  if (sizeof(AF(addr)) > 1) {
131
0
    pch++;
132
0
    hashVal = 37 * hashVal + *pch;
133
0
  }
134
0
  switch(AF(addr)) {
135
0
  case AF_INET:
136
0
    pch = (const void *)&SOCK_ADDR4(addr);
137
0
    len = sizeof(SOCK_ADDR4(addr));
138
0
    break;
139
140
0
  case AF_INET6:
141
0
    pch = (const void *)&SOCK_ADDR6(addr);
142
0
    len = sizeof(SOCK_ADDR6(addr));
143
0
    break;
144
0
        default:
145
                /* huh? */
146
0
                break;
147
0
  }
148
149
0
  for (unsigned int j = 0; j < len ; j++) {
150
0
    hashVal = 37 * hashVal + pch[j];
151
0
  }
152
153
0
  return hashVal;
154
0
}