Skip to content

Instantly share code, notes, and snippets.

@benley
Last active May 4, 2018 18:36
Show Gist options
  • Save benley/f470c5c2fefaa66473114d8b6de9b495 to your computer and use it in GitHub Desktop.
Save benley/f470c5c2fefaa66473114d8b6de9b495 to your computer and use it in GitHub Desktop.
/****
Copyright 2018 Postmates Inc
Copyright 2018 Benjamin Staffin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
****/
local ones(n) = if n == 0 then 0 else (n & 1) + ones(n >> 1);
local ipv4 = {
// ipToInt :: str -> int
// e.g. "255.255.255.0" -> 4294967040
ipToInt(ip)::
local o = [std.parseInt(x) for x in std.split(ip, ".")];
(o[0] * 256 << 16) +
(o[1] * 256 << 8) +
(o[2] * 256) +
o[3]
// intToIp :: int -> str
// e.g. 4294967040 -> "255.255.255.0"
, intToIp(i)::
std.join(".", [ (i >> 24) & 255
, (i >> 16) & 255
, (i >> 8) & 255
, i & 255 ])
// cidrMaskToInt :: int -> int
// e.g. 24 -> 4294967040
, cidrMaskToInt(n)::
(1<<32) - (1 << (32-n))
// cidrToNetwork :: str -> int
// e.g. "192.168.0.1/24" -> "192.168.0.0"
, cidrToNetwork(cidr)::
local ip = $.ipToInt(std.split(cidr, "/")[0]),
mask = $.cidrMaskToInt(std.parseInt(std.split(cidr, "/")[1]));
$.intToIp(ip & mask)
// cidrToNetmask :: str -> str
// e.g. "192.168.0.1/24" -> "255.255.255.0"
, cidrToNetmask(cidr)::
$.intToIp($.cidrMaskToInt(std.parseInt(std.split(cidr, "/")[1])))
, IpAddr:: {
address: std.split(self.cidr, "/")[0],
netmask: ipv4.cidrToNetmask(self.cidr),
network: $.intToIp($.ipToInt(self.address) & $.ipToInt(self.netmask)),
cidr: "%s/%s" % [self.address, ones(ipv4.ipToInt(self.netmask))],
}
};
assert ones(4294967040) == 24;
assert ones(255) == 8;
assert ones(254) == 7;
assert ones(252) == 6;
assert ipv4.ipToInt("255.255.255.0") == 4294967040;
assert ipv4.intToIp(4294967040) == "255.255.255.0";
assert ipv4.cidrMaskToInt(24) == 4294967040;
assert ipv4.cidrToNetwork("192.168.237.52/20") == "192.168.224.0";
assert ipv4.cidrToNetmask("192.168.237.52/20") == "255.255.240.0";
assert (ipv4.IpAddr { address: "192.168.0.1", netmask: "255.255.255.0" }).cidr == "192.168.0.1/24";
assert (ipv4.IpAddr { cidr: "10.2.3.4/16" }).network == "10.2.0.0";
ipv4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment