xref: /freebsd/contrib/ntp/libntp/netof.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * netof - return the net address part of an ip address in a sockaddr_storage structure
3  *         (zero out host part)
4  */
5 #include <stdio.h>
6 
7 #include "ntp_fp.h"
8 #include "ntp_stdlib.h"
9 #include "ntp.h"
10 
11 #define NUM_NETOF_BUFS	10
12 static struct sockaddr_storage ssbuf[NUM_NETOF_BUFS];
13 static int next_ssbuf = 0;
14 
15 struct sockaddr_storage*
16 netof(
17         struct sockaddr_storage* hostaddr
18 	)
19 {
20 	register u_int32 netnum;
21         struct sockaddr_storage *netaddr;
22 
23 	netaddr = &ssbuf[next_ssbuf++];
24 	if (next_ssbuf == NUM_NETOF_BUFS)
25 		next_ssbuf = 0;
26         memcpy(netaddr, hostaddr, sizeof(struct sockaddr_storage));
27 
28         if(netaddr->ss_family == AF_INET) {
29                 netnum = ((struct sockaddr_in*)netaddr)->sin_addr.s_addr;
30 
31 		/*
32 		 * We live in a modern CIDR world where the basement nets, which
33 		 * used to be class A, are now probably associated with each
34 		 * host address. So, for class-A nets, all bits are significant.
35 		 */
36 		if(IN_CLASSC(netnum))
37 		    netnum &= IN_CLASSC_NET;
38 		else if (IN_CLASSB(netnum))
39 		    netnum &= IN_CLASSB_NET;
40 			((struct sockaddr_in*)netaddr)->sin_addr.s_addr = netnum;
41 		 }
42          else if(netaddr->ss_family == AF_INET6) {
43 		/* Here we put 0 at the local link address so we get net address */
44 		  memset(&((struct sockaddr_in6*)netaddr)->sin6_addr.s6_addr[8], 0, 8*sizeof(u_char));
45          }
46 
47          return netaddr;
48 }
49