No empty .Rs/.Re
[netbsd-mini2440.git] / dist / ntp / libntp / netof.c
blobe22fbd16ee5757acfc822b8530c0753d6052cb1e
1 /* $NetBSD$ */
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>
9 #include "ntp_fp.h"
10 #include "ntp_stdlib.h"
11 #include "ntp.h"
13 #define NUM_NETOF_BUFS 10
14 static struct sockaddr_storage ssbuf[NUM_NETOF_BUFS];
15 static int next_ssbuf = 0;
17 struct sockaddr_storage*
18 netof(
19 struct sockaddr_storage* hostaddr
22 register u_int32 netnum;
23 struct sockaddr_storage *netaddr;
25 netaddr = &ssbuf[next_ssbuf++];
26 if (next_ssbuf == NUM_NETOF_BUFS)
27 next_ssbuf = 0;
28 memcpy(netaddr, hostaddr, sizeof(struct sockaddr_storage));
30 if(netaddr->ss_family == AF_INET) {
31 netnum = ((struct sockaddr_in*)netaddr)->sin_addr.s_addr;
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.
38 if(IN_CLASSC(netnum))
39 netnum &= IN_CLASSC_NET;
40 else if (IN_CLASSB(netnum))
41 netnum &= IN_CLASSB_NET;
42 ((struct sockaddr_in*)netaddr)->sin_addr.s_addr = netnum;
44 else if(netaddr->ss_family == AF_INET6) {
45 /* Here we put 0 at the local link address so we get net address */
46 memset(&((struct sockaddr_in6*)netaddr)->sin6_addr.s6_addr[8], 0, 8*sizeof(u_char));
49 return netaddr;