summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Modules1
-rw-r--r--lib/net.c60
-rw-r--r--lib/net.h5
3 files changed, 62 insertions, 4 deletions
diff --git a/lib/Modules b/lib/Modules
index 745306d9..21830875 100644
--- a/lib/Modules
+++ b/lib/Modules
@@ -31,3 +31,4 @@ event.h
checksum.c
checksum.h
alloca.h
+net.c
diff --git a/lib/net.c b/lib/net.c
index 87d4aa16..df833e0f 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -8,9 +8,9 @@ const u16 net_addr_length[] = {
[NET_IP6] = sizeof(net_addr_ip6),
[NET_VPN4] = sizeof(net_addr_vpn4),
[NET_VPN6] = sizeof(net_addr_vpn6)
-}
+};
-char *
+int
net_format(const net_addr *N, char *buf, int buflen)
{
net_addr_union *n = (void *) N;
@@ -27,6 +27,58 @@ net_format(const net_addr *N, char *buf, int buflen)
case NET_VPN6:
return bsnprintf(buf, buflen, "%u:%u %I/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
}
+
+ return 0;
+}
+
+
+ip_addr
+net_pxmask(const net_addr *a)
+{
+ switch (a->type)
+ {
+ case NET_IP4:
+ case NET_VPN4:
+ return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
+
+ case NET_IP6:
+ case NET_VPN6:
+ return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
+
+ default:
+ return IPA_NONE;
+ }
+}
+
+
+static inline int net_validate_ip4(const net_addr_ip4 *n)
+{
+ return (n->pxlen <= IP4_MAX_PREFIX_LENGTH) &&
+ ip4_zero(ip4_and(n->prefix, ip4_not(ip4_mkmask(n->pxlen))));
+}
+
+static inline int net_validate_ip6(const net_addr_ip6 *n)
+{
+ return (n->pxlen <= IP6_MAX_PREFIX_LENGTH) &&
+ ip6_zero(ip6_and(n->prefix, ip6_not(ip6_mkmask(n->pxlen))));
+}
+
+int
+net_validate(const net_addr *N)
+{
+ switch (a->type)
+ {
+ case NET_IP4:
+ case NET_VPN4:
+ return net_validate_ip4((net_addr_ip4 *) N);
+
+ case NET_IP6:
+ case NET_VPN6:
+ return net_validate_ip6((net_addr_ip6 *) N);
+
+ default:
+ return 0;
+ }
}
int
@@ -42,6 +94,8 @@ net_classify(const net_addr *N)
case NET_IP6:
case NET_VPN6:
- return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(n->ip6.prefix);
+ return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
}
+
+ return 0;
}
diff --git a/lib/net.h b/lib/net.h
index 378703f7..33686fb5 100644
--- a/lib/net.h
+++ b/lib/net.h
@@ -131,6 +131,8 @@ static inline uint net6_pxlen(const net_addr *a)
static inline uint net_pxlen(const net_addr *a)
{ return a->pxlen; }
+ip_addr net_pxmask(const net_addr *a);
+
static inline int net_equal(const net_addr *a, const net_addr *b)
{ return (a->length == b->length) && !memcmp(a, b, a->length); }
@@ -203,8 +205,9 @@ static inline void net_normalize_ip6(net_addr_ip6 *n)
void net_normalize(net_addr *N);
int net_validate(const net_addr *N);
+
int net_classify(const net_addr *N);
-char * net_format(const net_addr *N, char *buf, int buflen);
+int net_format(const net_addr *N, char *buf, int buflen);
int ipa_in_netX(const ip_addr A, const net_addr *N);