Statistics
| Branch: | Revision:

iof-bird-daemon / lib / net.c @ 5e173e9f

History | View | Annotate | Download (2.48 KB)

1 fe9f1a6d Ondrej Zajicek (work)
2
#include "nest/bird.h"
3
#include "lib/ip.h"
4
#include "lib/net.h"
5
6 d7661fbe Jan Moskyto Matejka
7 fe9f1a6d Ondrej Zajicek (work)
const u16 net_addr_length[] = {
8
  [NET_IP4] = sizeof(net_addr_ip4),
9
  [NET_IP6] = sizeof(net_addr_ip6),
10
  [NET_VPN4] = sizeof(net_addr_vpn4),
11
  [NET_VPN6] = sizeof(net_addr_vpn6)
12 9b136840 Jan Moskyto Matejka
};
13 fe9f1a6d Ondrej Zajicek (work)
14 d7661fbe Jan Moskyto Matejka
const u8 net_max_prefix_length[] = {
15
  [NET_IP4] = IP4_MAX_PREFIX_LENGTH,
16
  [NET_IP6] = IP6_MAX_PREFIX_LENGTH,
17
  [NET_VPN4] = IP4_MAX_PREFIX_LENGTH,
18
  [NET_VPN6] = IP4_MAX_PREFIX_LENGTH
19
};
20
21
22 9b136840 Jan Moskyto Matejka
int
23 fe9f1a6d Ondrej Zajicek (work)
net_format(const net_addr *N, char *buf, int buflen)
24
{
25
  net_addr_union *n = (void *) N;
26
27
  /* FIXME: quick hack */
28
  switch (n->n.type)
29
  {
30
  case NET_IP4:
31
    return bsnprintf(buf, buflen, "%I/%d", n->ip4.prefix, n->ip4.pxlen);
32
  case NET_IP6:
33
    return bsnprintf(buf, buflen, "%I/%d", n->ip6.prefix, n->ip6.pxlen);
34
  case NET_VPN4:
35
    return bsnprintf(buf, buflen, "%u:%u %I/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
36
  case NET_VPN6:
37
    return bsnprintf(buf, buflen, "%u:%u %I/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
38
  }
39 9b136840 Jan Moskyto Matejka
40
  return 0;
41
}
42
43
ip_addr
44
net_pxmask(const net_addr *a)
45
{
46
  switch (a->type)
47
  {
48
  case NET_IP4:
49
  case NET_VPN4:
50
    return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
51
52
  case NET_IP6:
53
  case NET_VPN6:
54
    return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
55
56
  default:
57
    return IPA_NONE;
58
  }
59
}
60
61 5e173e9f Jan Moskyto Matejka
int
62
net_compare(const net_addr *a, const net_addr *b)
63 9b136840 Jan Moskyto Matejka
{
64 5e173e9f Jan Moskyto Matejka
  if (a->type != b->type)
65
    return uint_cmp(a->type, b->type);
66 9b136840 Jan Moskyto Matejka
67 5e173e9f Jan Moskyto Matejka
  switch (a->type)
68
  {
69
  case NET_IP4:
70
    return net_compare_ip4((const net_addr_ip4 *) a, (const net_addr_ip4 *) b);
71
  case NET_IP6:
72
    return net_compare_ip6((const net_addr_ip6 *) a, (const net_addr_ip6 *) b);
73
  case NET_VPN4:
74
    return net_compare_vpn4((const net_addr_vpn4 *) a, (const net_addr_vpn4 *) b);
75
  case NET_VPN6:
76
    return net_compare_vpn6((const net_addr_vpn6 *) a, (const net_addr_vpn6 *) b);
77
  }
78
  return 0;
79 9b136840 Jan Moskyto Matejka
}
80
81
int
82
net_validate(const net_addr *N)
83
{
84 5e173e9f Jan Moskyto Matejka
  switch (N->type)
85 9b136840 Jan Moskyto Matejka
  {
86
  case NET_IP4:
87
  case NET_VPN4:
88
    return net_validate_ip4((net_addr_ip4 *) N);
89
90
  case NET_IP6:
91
  case NET_VPN6:
92
    return net_validate_ip6((net_addr_ip6 *) N);
93
94
  default:
95
    return 0;
96
  }
97 fe9f1a6d Ondrej Zajicek (work)
}
98
99
int
100
net_classify(const net_addr *N)
101
{
102
  net_addr_union *n = (void *) N;
103
104
  switch (n->n.type)
105
  {
106
  case NET_IP4:
107
  case NET_VPN4:
108
    return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);
109
110
  case NET_IP6:
111
  case NET_VPN6:
112 9b136840 Jan Moskyto Matejka
    return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
113 fe9f1a6d Ondrej Zajicek (work)
  }
114 9b136840 Jan Moskyto Matejka
115
  return 0;
116 fe9f1a6d Ondrej Zajicek (work)
}