freebsd-nq/libntp/netof.c

55 lines
1.2 KiB
C
Raw Normal View History

1999-12-09 13:01:21 +00:00
/*
2004-07-20 15:01:56 +00:00
* netof - return the net address part of an ip address in a sockaddr_storage structure
1999-12-09 13:01:21 +00:00
* (zero out host part)
*/
#include <stdio.h>
#include <syslog.h>
1999-12-09 13:01:21 +00:00
#include "ntp_fp.h"
#include "ntp_net.h"
1999-12-09 13:01:21 +00:00
#include "ntp_stdlib.h"
2004-07-20 15:01:56 +00:00
#include "ntp.h"
1999-12-09 13:01:21 +00:00
sockaddr_u *
1999-12-09 13:01:21 +00:00
netof(
sockaddr_u *hostaddr
1999-12-09 13:01:21 +00:00
)
{
static sockaddr_u netofbuf[8];
static int next_netofbuf;
u_int32 netnum;
sockaddr_u * netaddr;
netaddr = &netofbuf[next_netofbuf];
next_netofbuf = (next_netofbuf + 1) % COUNTOF(netofbuf);
2004-07-20 15:01:56 +00:00
memcpy(netaddr, hostaddr, sizeof(*netaddr));
2004-07-20 15:01:56 +00:00
if (IS_IPV4(netaddr)) {
netnum = SRCADR(netaddr);
2004-07-20 15:01:56 +00:00
/*
* We live in a modern CIDR world where the basement nets, which
* used to be class A, are now probably associated with each
* host address. So, for class-A nets, all bits are significant.
*/
if (IN_CLASSC(netnum))
netnum &= IN_CLASSC_NET;
2004-07-20 15:01:56 +00:00
else if (IN_CLASSB(netnum))
netnum &= IN_CLASSB_NET;
SET_ADDR4(netaddr, netnum);
} else if (IS_IPV6(netaddr))
/* assume the typical /64 subnet size */
memset(&NSRCADR6(netaddr)[8], 0, 8);
#ifdef DEBUG
else {
msyslog(LOG_ERR, "netof unknown AF %d", AF(netaddr));
exit(1);
}
#endif
return netaddr;
1999-12-09 13:01:21 +00:00
}