File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / ntp / libntp / netof.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:08:38 2012 UTC (12 years, 5 months ago) by misho
Branches: ntp, MAIN
CVS tags: v4_2_6p5p0, v4_2_6p5, HEAD
ntp 4.2.6p5

    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: #include <syslog.h>
    7: 
    8: #include "ntp_fp.h"
    9: #include "ntp_net.h"
   10: #include "ntp_stdlib.h"
   11: #include "ntp.h"
   12: 
   13: sockaddr_u *
   14: netof(
   15: 	sockaddr_u *hostaddr
   16: 	)
   17: {
   18: 	static sockaddr_u	netofbuf[8];
   19: 	static int		next_netofbuf;
   20: 	u_int32			netnum;
   21: 	sockaddr_u *		netaddr;
   22: 
   23: 	netaddr = &netofbuf[next_netofbuf];
   24: 	next_netofbuf = (next_netofbuf + 1) % COUNTOF(netofbuf);
   25: 
   26: 	memcpy(netaddr, hostaddr, sizeof(*netaddr));
   27: 
   28: 	if (IS_IPV4(netaddr)) {
   29: 		netnum = SRCADR(netaddr);
   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: 
   41: 		SET_ADDR4(netaddr, netnum);
   42: 
   43: 	} else if (IS_IPV6(netaddr))
   44: 		/* assume the typical /64 subnet size */
   45: 		memset(&NSRCADR6(netaddr)[8], 0, 8);
   46: #ifdef DEBUG
   47: 	else {
   48: 		msyslog(LOG_ERR, "netof unknown AF %d", AF(netaddr));
   49: 		exit(1);
   50: 	}
   51: #endif
   52: 
   53: 	return netaddr;
   54: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>