154359Sroberto/*
2132451Sroberto * netof - return the net address part of an ip address in a sockaddr_storage structure
354359Sroberto *         (zero out host part)
454359Sroberto */
5290001Sglebius#include <config.h>
654359Sroberto#include <stdio.h>
7290001Sglebius#include <syslog.h>
854359Sroberto
954359Sroberto#include "ntp_fp.h"
10290001Sglebius#include "ntp_net.h"
1154359Sroberto#include "ntp_stdlib.h"
12132451Sroberto#include "ntp.h"
1354359Sroberto
14290001Sglebiussockaddr_u *
1554359Srobertonetof(
16290001Sglebius	sockaddr_u *hostaddr
1754359Sroberto	)
1854359Sroberto{
19290001Sglebius	static sockaddr_u	netofbuf[8];
20290001Sglebius	static int		next_netofbuf;
21290001Sglebius	u_int32			netnum;
22290001Sglebius	sockaddr_u *		netaddr;
2354359Sroberto
24290001Sglebius	netaddr = &netofbuf[next_netofbuf];
25290001Sglebius	next_netofbuf = (next_netofbuf + 1) % COUNTOF(netofbuf);
26132451Sroberto
27290001Sglebius	memcpy(netaddr, hostaddr, sizeof(*netaddr));
28132451Sroberto
29290001Sglebius	if (IS_IPV4(netaddr)) {
30290001Sglebius		netnum = SRCADR(netaddr);
31290001Sglebius
32132451Sroberto		/*
33132451Sroberto		 * We live in a modern CIDR world where the basement nets, which
34132451Sroberto		 * used to be class A, are now probably associated with each
35132451Sroberto		 * host address. So, for class-A nets, all bits are significant.
36132451Sroberto		 */
37290001Sglebius		if (IN_CLASSC(netnum))
38290001Sglebius			netnum &= IN_CLASSC_NET;
39132451Sroberto		else if (IN_CLASSB(netnum))
40290001Sglebius			netnum &= IN_CLASSB_NET;
41132451Sroberto
42290001Sglebius		SET_ADDR4(netaddr, netnum);
43290001Sglebius
44290001Sglebius	} else if (IS_IPV6(netaddr))
45290001Sglebius		/* assume the typical /64 subnet size */
46290001Sglebius		zero_mem(&NSRCADR6(netaddr)[8], 8);
47290001Sglebius#ifdef DEBUG
48290001Sglebius	else {
49290001Sglebius		msyslog(LOG_ERR, "netof unknown AF %d", AF(netaddr));
50290001Sglebius		exit(1);
51290001Sglebius	}
52290001Sglebius#endif
53290001Sglebius
54290001Sglebius	return netaddr;
5554359Sroberto}
56