1/*	$NetBSD$	*/
2
3/*
4 * netof - return the net address part of an ip address in a sockaddr_storage structure
5 *         (zero out host part)
6 */
7#include <stdio.h>
8#include <syslog.h>
9
10#include "ntp_fp.h"
11#include "ntp_net.h"
12#include "ntp_stdlib.h"
13#include "ntp.h"
14
15sockaddr_u *
16netof(
17	sockaddr_u *hostaddr
18	)
19{
20	static sockaddr_u	netofbuf[8];
21	static int		next_netofbuf;
22	u_int32			netnum;
23	sockaddr_u *		netaddr;
24
25	netaddr = &netofbuf[next_netofbuf];
26	next_netofbuf = (next_netofbuf + 1) % COUNTOF(netofbuf);
27
28	memcpy(netaddr, hostaddr, sizeof(*netaddr));
29
30	if (IS_IPV4(netaddr)) {
31		netnum = SRCADR(netaddr);
32
33		/*
34		 * We live in a modern CIDR world where the basement nets, which
35		 * used to be class A, are now probably associated with each
36		 * host address. So, for class-A nets, all bits are significant.
37		 */
38		if (IN_CLASSC(netnum))
39			netnum &= IN_CLASSC_NET;
40		else if (IN_CLASSB(netnum))
41			netnum &= IN_CLASSB_NET;
42
43		SET_ADDR4(netaddr, netnum);
44
45	} else if (IS_IPV6(netaddr))
46		/* assume the typical /64 subnet size */
47		memset(&NSRCADR6(netaddr)[8], 0, 8);
48#ifdef DEBUG
49	else {
50		msyslog(LOG_ERR, "netof unknown AF %d", AF(netaddr));
51		exit(1);
52	}
53#endif
54
55	return netaddr;
56}
57