Coverage Report

Created: 2023-05-19 06:16

/src/ntp-dev/libntp/netof.c
Line
Count
Source (jump to first uncovered line)
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 <config.h>
6
#include <stdio.h>
7
#include <syslog.h>
8
9
#include "ntp_fp.h"
10
#include "ntp_net.h"
11
#include "ntp_stdlib.h"
12
#include "ntp.h"
13
14
sockaddr_u *
15
netof(
16
  sockaddr_u *hostaddr
17
  )
18
0
{
19
0
  static sockaddr_u netofbuf[8];
20
0
  static int    next_netofbuf;
21
0
  u_int32     netnum;
22
0
  sockaddr_u *    netaddr;
23
24
0
  netaddr = &netofbuf[next_netofbuf];
25
0
  next_netofbuf = (next_netofbuf + 1) % COUNTOF(netofbuf);
26
27
0
  memcpy(netaddr, hostaddr, sizeof(*netaddr));
28
29
0
  if (IS_IPV4(netaddr)) {
30
0
    netnum = SRCADR(netaddr);
31
32
    /*
33
     * We live in a modern CIDR world where the basement nets, which
34
     * used to be class A, are now probably associated with each
35
     * host address. So, for class-A nets, all bits are significant.
36
     */
37
0
    if (IN_CLASSC(netnum))
38
0
      netnum &= IN_CLASSC_NET;
39
0
    else if (IN_CLASSB(netnum))
40
0
      netnum &= IN_CLASSB_NET;
41
42
0
    SET_ADDR4(netaddr, netnum);
43
44
0
  } else if (IS_IPV6(netaddr))
45
    /* assume the typical /64 subnet size */
46
0
    zero_mem(&NSRCADR6(netaddr)[8], 8);
47
0
#ifdef DEBUG
48
0
  else {
49
0
    msyslog(LOG_ERR, "netof unknown AF %d", AF(netaddr));
50
0
    exit(1);
51
0
  }
52
0
#endif
53
54
0
  return netaddr;
55
0
}