summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/ipcalc.lua
diff options
context:
space:
mode:
Diffstat (limited to 'misc/freeswitch/scripts/common/ipcalc.lua')
-rw-r--r--misc/freeswitch/scripts/common/ipcalc.lua27
1 files changed, 27 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/ipcalc.lua b/misc/freeswitch/scripts/common/ipcalc.lua
new file mode 100644
index 0000000..5c19d20
--- /dev/null
+++ b/misc/freeswitch/scripts/common/ipcalc.lua
@@ -0,0 +1,27 @@
+-- Gemeinschaft 5 module: ip calculation functions
+-- (c) AMOOMA GmbH 2012
+--
+
+module(...,package.seeall)
+
+function ipv4_to_i(ip_address_str)
+ local octet4, octet3, octet2, octet1 = ip_address_str:match('(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)');
+ if octet4 and octet3 and octet2 and octet1 then
+ return (2^24*octet4 + 2^16*octet3 + 2^8*octet2 + octet1);
+ end
+end
+
+function ipv4_to_network_netmask(ip_address_str)
+ local octet4, octet3, octet2, octet1, netmask = ip_address_str:match('(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)/(%d%d?)');
+ if octet4 and octet3 and octet2 and octet1 and netmask then
+ return (2^24*octet4 + 2^16*octet3 + 2^8*octet2 + octet1), tonumber(netmask);
+ end
+end
+
+function ipv4_network(ip_address, netmask)
+ return math.floor(ip_address / 2^(32-netmask));
+end
+
+function ipv4_in_network(ip_address, network, netmask)
+ return ipv4_network(ip_address, netmask) == ipv4_network(network, netmask);
+end