Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / netof.c
blobc6f91c6da49dfbc234e2e993cf38e481cf18a53e
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>
8 #include <syslog.h>
10 #include "ntp_fp.h"
11 #include "ntp_net.h"
12 #include "ntp_stdlib.h"
13 #include "ntp.h"
15 sockaddr_u *
16 netof(
17 sockaddr_u *hostaddr
20 static sockaddr_u netofbuf[8];
21 static int next_netofbuf;
22 u_int32 netnum;
23 sockaddr_u * netaddr;
25 netaddr = &netofbuf[next_netofbuf];
26 next_netofbuf = (next_netofbuf + 1) % COUNTOF(netofbuf);
28 memcpy(netaddr, hostaddr, sizeof(*netaddr));
30 if (IS_IPV4(netaddr)) {
31 netnum = SRCADR(netaddr);
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;
43 SET_ADDR4(netaddr, netnum);
45 } else if (IS_IPV6(netaddr))
46 /* assume the typical /64 subnet size */
47 memset(&NSRCADR6(netaddr)[8], 0, 8);
48 #ifdef DEBUG
49 else {
50 msyslog(LOG_ERR, "netof unknown AF %d", AF(netaddr));
51 exit(1);
53 #endif
55 return netaddr;