summaryrefslogtreecommitdiff
path: root/src/libstddjb/ip6_netmask.c
blob: 5d8b13e1873d842251e7639b74ad6adb065cfa1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* ISC license. */

#include <string.h>

#include <skalibs/uint64.h>
#include <skalibs/fmtscan.h>

static void apply (char *half, uint8_t netmask)
{
  uint64_t u ;
  uint64_unpack_big(half, &u) ;
  u &= netmask ? ~((1ull << 64 - netmask) - 1) : 0 ;
  uint64_pack_big(half, u) ;
}

int ip6_netmask (char *ip, uint8_t netmask)
{
  if (netmask > 128) return 0 ;
  if (netmask <= 64)
  {
    apply(ip, netmask) ;
    memset(ip + 8, 0, 8) ;
  }
  else apply(ip + 8, netmask - 64) ;
  return 1 ;
}