NetBSD/dist/ntp/libntp/netof.c

51 lines
1.4 KiB
C
Raw Normal View History

2003-12-04 19:05:14 +03:00
/* $NetBSD: netof.c,v 1.1.1.2 2003/12/04 16:05:24 drochner Exp $ */
2000-03-29 16:38:44 +04:00
/*
2003-12-04 19:05:14 +03:00
* netof - return the net address part of an ip address in a sockaddr_storage structure
2000-03-29 16:38:44 +04:00
* (zero out host part)
*/
#include <stdio.h>
#include "ntp_fp.h"
#include "ntp_stdlib.h"
2003-12-04 19:05:14 +03:00
#include "ntp.h"
2000-03-29 16:38:44 +04:00
2003-12-04 19:05:14 +03:00
#define NUM_NETOF_BUFS 10
static struct sockaddr_storage ssbuf[NUM_NETOF_BUFS];
static int next_ssbuf = 0;
struct sockaddr_storage*
2000-03-29 16:38:44 +04:00
netof(
2003-12-04 19:05:14 +03:00
struct sockaddr_storage* hostaddr
2000-03-29 16:38:44 +04:00
)
{
register u_int32 netnum;
2003-12-04 19:05:14 +03:00
struct sockaddr_storage *netaddr;
netaddr = &ssbuf[next_ssbuf++];
if (next_ssbuf == NUM_NETOF_BUFS)
next_ssbuf = 0;
memcpy(netaddr, hostaddr, sizeof(struct sockaddr_storage));
if(netaddr->ss_family == AF_INET) {
netnum = ((struct sockaddr_in*)netaddr)->sin_addr.s_addr;
/*
* 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;
else if (IN_CLASSB(netnum))
netnum &= IN_CLASSB_NET;
((struct sockaddr_in*)netaddr)->sin_addr.s_addr = netnum;
}
else if(netaddr->ss_family == AF_INET6) {
/* Here we put 0 at the local link address so we get net address */
memset(&((struct sockaddr_in6*)netaddr)->sin6_addr.s6_addr[8], 0, 8*sizeof(u_char));
}
2000-03-29 16:38:44 +04:00
2003-12-04 19:05:14 +03:00
return netaddr;
2000-03-29 16:38:44 +04:00
}