Module: sip-router
Branch: master
Commit: c1a4788cfd058e3eb52330eac04344be5fc93e35
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=c1a4788…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: Fri Dec 23 09:32:24 2011 +0100
core: function to match ip address to net address with bitmask
---
ip_addr.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ip_addr.h | 7 ++++++-
2 files changed, 63 insertions(+), 1 deletions(-)
diff --git a/ip_addr.c b/ip_addr.c
index 06c57c3..1b15c96 100644
--- a/ip_addr.c
+++ b/ip_addr.c
@@ -354,3 +354,60 @@ char* get_proto_name(unsigned int proto)
return "unknown";
}
}
+
+
+/**
+ * match ip address with net address and bitmask
+ * - return 0 on match, -1 otherwise
+ */
+int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr,
+ int mask)
+{
+ unsigned char c;
+ int i;
+ int mbytes;
+ int mbits;
+
+ if(mask==0)
+ return 0;
+ if(iaddr==NULL || naddr==NULL || mask<0)
+ return -1;
+ if(iaddr->af != naddr->af)
+ return -1;
+
+ if(iaddr->af == AF_INET)
+ {
+ if(mask>32)
+ return -1;
+ if(mask==32)
+ {
+ if(ip_addr_cmp(iaddr, naddr))
+ return 0;
+ return -1;
+ }
+ } else if(iaddr->af == AF_INET6) {
+ if(mask>128)
+ return -1;
+
+ if(mask==128)
+ {
+ if(ip_addr_cmp(iaddr, naddr))
+ return 0;
+ return -1;
+ }
+ }
+
+ mbytes = mask / 8;
+ for(i=0; i<mbytes; i++)
+ {
+ if(iaddr->u.addr[i] != naddr->u.addr[i])
+ return -1;
+ }
+ mbits = mask % 8;
+ if(mbits==0)
+ return 0;
+ c = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1));
+ if((iaddr->u.addr[i] & c) == c)
+ return 0;
+ return -1;
+}
diff --git a/ip_addr.h b/ip_addr.h
index 0c1a129..9bfe670 100644
--- a/ip_addr.h
+++ b/ip_addr.h
@@ -72,7 +72,7 @@ struct ip_addr{
}u;
};
-
+typedef struct ip_addr ip_addr_t;
struct net{
struct ip_addr ip;
@@ -825,5 +825,10 @@ inline static void init_dst_from_rcv(struct dest_info* dst,
#endif
}
+/**
+ * match ip address with net address and bitmask
+ * - return 0 on match, -1 otherwise
+ */
+int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr, int mask);
#endif