2 * socktoa.c socktoa(), sockporttoa(), and sock_hash()
10 #ifdef HAVE_SYS_SOCKET_H
11 #include <sys/socket.h>
13 #ifdef HAVE_NETINET_IN_H
14 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <isc/result.h>
20 #include <isc/netaddr.h>
21 #include <isc/sockaddr.h>
24 #include "lib_strbuf.h"
25 #include "ntp_stdlib.h"
29 * socktoa - return a numeric host name from a sockaddr_storage structure
33 const sockaddr_u *sock
41 saved_errno = socket_errno();
45 strlcpy(res, "(null)", LIB_BUFLENGTH);
51 inet_ntop(AF_INET, PSOCK_ADDR4(sock), res,
56 inet_ntop(AF_INET6, PSOCK_ADDR6(sock), res,
58 scope = SCOPE_VAR(sock);
59 if (0 != scope && !strchr(res, '%')) {
62 snprintf(res, LIB_BUFLENGTH, "%s%%%lu",
64 res[LIB_BUFLENGTH - 1] = '\0';
69 snprintf(res, LIB_BUFLENGTH,
70 "(socktoa unknown family %d)",
82 const sockaddr_u *sock
89 saved_errno = socket_errno();
90 atext = socktoa(sock);
92 snprintf(buf, LIB_BUFLENGTH,
96 atext, SRCPORT(sock));
104 * sock_hash - hash a sockaddr_u structure
108 const sockaddr_u *addr
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.
124 pch = (const void *)&AF(addr);
125 hashVal = 37 * hashVal + *pch;
126 if (sizeof(AF(addr)) > 1) {
128 hashVal = 37 * hashVal + *pch;
132 pch = (const void *)&SOCK_ADDR4(addr);
133 len = sizeof(SOCK_ADDR4(addr));
137 pch = (const void *)&SOCK_ADDR6(addr);
138 len = sizeof(SOCK_ADDR6(addr));
142 for (j = 0; j < len ; j++)
143 hashVal = 37 * hashVal + pch[j];
145 return (u_short)(hashVal & USHRT_MAX);
150 sockaddr_masktoprefixlen(
151 const sockaddr_u * psa
154 isc_netaddr_t isc_na;
155 isc_sockaddr_t isc_sa;
161 memcpy(&isc_sa.type, psa,
162 min(sizeof(isc_sa.type), sizeof(*psa)));
163 isc_netaddr_fromsockaddr(&isc_na, &isc_sa);
164 result = isc_netaddr_masktoprefixlen(&isc_na, &pfxlen);
165 rc = (ISC_R_SUCCESS == result)