PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/ntp/libntp/netof.c

https://bitbucket.org/freebsd/freebsd-head/
C | 48 lines | 31 code | 7 blank | 10 comment | 10 complexity | ff7aab6a269406e0197662693ff4c2f2 MD5 | raw file
 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
12static struct sockaddr_storage ssbuf[NUM_NETOF_BUFS];
13static int next_ssbuf = 0;
14
15struct sockaddr_storage*
16netof(
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}