Skip to content

Commit

Permalink
add other filters
Browse files Browse the repository at this point in the history
  • Loading branch information
grizz committed Oct 10, 2024
1 parent de0f229 commit b9675b9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
80 changes: 77 additions & 3 deletions src/netom/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,89 @@ def ip_address(value):
return ipaddress.ip_interface(value).ip


# XXX use this
def ip_interface(value):
"""
Returns ip_interface of passed value.
Returns ip_interface of passed IP interface.
"""
return ipaddress.ip_interface(value)

def ip_prefixlen(addr):
"""
Returns the prefix length for passed IP interface.
"""
return ipaddress.ip_interface(addr).prefixlen

def ip_netmask(addr):
"""
Returns a subnet mask for passed IP interface.
"""
return ipaddress.ip_interface(addr).netmask

def ip_hostmask(addr):
"""
Returns a wilcard or hostmask for passed IP interface.
Example:
ip_netmask('10.10.10.5/24')
> 0.0.0.255
"""
return ipaddress.ip_interface(addr).hostmask

def ip_network(addr):
"""
Returns a network address for a combination of IP address and subnet mask
Example:
ip_network('10.10.10.5/24')
> 10.10.10.0
"""
return ipaddress.ip_network(addr).network


def ip_broadcast(addr):
"""
Returns a broadcast address for a combination of IP address and subnet mask
Example:
ip_network('10.10.10.5/24')
> 10.10.10.255
"""
return ipaddress.ip_network(addr).broadcast

def ip_network_hosts_size(addr):
"""
Returns the size of the subnet for a combination of IP address and subnet mask
Example:
ip_network_hosts_size('10.10.10.5/24')
> 253
"""
return ipaddress.ip_network(addr).size


def ip_network_first(addr):
"""
Returns the first usable address in network address for a combination of IP address and subnet mask
Example:
ip_network('10.10.10.5/24')
> 10.10.10.1
"""
net = ipaddress.ip_network(addr)
return ipaddress.ip_address(net[1]).__str__()


def ip_network_last(addr):
"""
Returns the last usable address in network address for a combination of IP address and subnet mask
Example:
ip_network('10.10.10.5/24')
> 10.10.10.254
"""
return ipaddress.ip_address(ipaddress.ip_network(addr)[-2]).__str__()


# XXX when was interface added?
def ip_version(value):
"""
Returns version of passed IP address.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
address_to_wildcard,
line_to_mask,
line_to_wildcard,
ip_network_first,
ip_network_last,
)
import pytest


def test_address_to_mask():
Expand Down Expand Up @@ -106,3 +109,19 @@ def test_line_to_wildcard():
line_to_wildcard("permit tcp 192.168.0.0/32 any eq 22")
== "permit tcp 192.168.0.0 0.0.0.0 any eq 22"
)


def test_ip_network_first():
assert ip_network_first("192.168.0.0/24") == "192.168.0.1"
assert ip_network_first("10.0.0.0/8") == "10.0.0.1"


def test_ip_network_last():
assert ip_network_last("192.168.0.0/24") == "192.168.0.254"
assert ip_network_last("10.0.0.0/8") == "10.255.255.254"
assert ip_network_last("172.16.0.0/16") == "172.16.255.254"
assert ip_network_last("192.168.1.0/30") == "192.168.1.2"
# XXX assert ip_network_last("192.168.1.0/31") == "192.168.1.1"

with pytest.raises(Exception):
ip_network_last("192.168.1.0/32")

0 comments on commit b9675b9

Please sign in to comment.