Skip to content

Commit b9675b9

Browse files
committed
add other filters
1 parent de0f229 commit b9675b9

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

src/netom/filters.py

+77-3
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,89 @@ def ip_address(value):
8383
return ipaddress.ip_interface(value).ip
8484

8585

86-
# XXX use this
8786
def ip_interface(value):
8887
"""
89-
Returns ip_interface of passed value.
88+
Returns ip_interface of passed IP interface.
9089
"""
9190
return ipaddress.ip_interface(value)
9291

92+
def ip_prefixlen(addr):
93+
"""
94+
Returns the prefix length for passed IP interface.
95+
"""
96+
return ipaddress.ip_interface(addr).prefixlen
97+
98+
def ip_netmask(addr):
99+
"""
100+
Returns a subnet mask for passed IP interface.
101+
"""
102+
return ipaddress.ip_interface(addr).netmask
103+
104+
def ip_hostmask(addr):
105+
"""
106+
Returns a wilcard or hostmask for passed IP interface.
107+
108+
Example:
109+
ip_netmask('10.10.10.5/24')
110+
> 0.0.0.255
111+
"""
112+
return ipaddress.ip_interface(addr).hostmask
113+
114+
def ip_network(addr):
115+
"""
116+
Returns a network address for a combination of IP address and subnet mask
117+
118+
Example:
119+
ip_network('10.10.10.5/24')
120+
> 10.10.10.0
121+
"""
122+
return ipaddress.ip_network(addr).network
123+
124+
125+
def ip_broadcast(addr):
126+
"""
127+
Returns a broadcast address for a combination of IP address and subnet mask
128+
129+
Example:
130+
ip_network('10.10.10.5/24')
131+
> 10.10.10.255
132+
"""
133+
return ipaddress.ip_network(addr).broadcast
134+
135+
def ip_network_hosts_size(addr):
136+
"""
137+
Returns the size of the subnet for a combination of IP address and subnet mask
138+
139+
Example:
140+
ip_network_hosts_size('10.10.10.5/24')
141+
> 253
142+
"""
143+
return ipaddress.ip_network(addr).size
144+
145+
146+
def ip_network_first(addr):
147+
"""
148+
Returns the first usable address in network address for a combination of IP address and subnet mask
149+
150+
Example:
151+
ip_network('10.10.10.5/24')
152+
> 10.10.10.1
153+
"""
154+
net = ipaddress.ip_network(addr)
155+
return ipaddress.ip_address(net[1]).__str__()
156+
157+
158+
def ip_network_last(addr):
159+
"""
160+
Returns the last usable address in network address for a combination of IP address and subnet mask
161+
162+
Example:
163+
ip_network('10.10.10.5/24')
164+
> 10.10.10.254
165+
"""
166+
return ipaddress.ip_address(ipaddress.ip_network(addr)[-2]).__str__()
167+
93168

94-
# XXX when was interface added?
95169
def ip_version(value):
96170
"""
97171
Returns version of passed IP address.

tests/test_filters.py

+19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
address_to_wildcard,
44
line_to_mask,
55
line_to_wildcard,
6+
ip_network_first,
7+
ip_network_last,
68
)
9+
import pytest
710

811

912
def test_address_to_mask():
@@ -106,3 +109,19 @@ def test_line_to_wildcard():
106109
line_to_wildcard("permit tcp 192.168.0.0/32 any eq 22")
107110
== "permit tcp 192.168.0.0 0.0.0.0 any eq 22"
108111
)
112+
113+
114+
def test_ip_network_first():
115+
assert ip_network_first("192.168.0.0/24") == "192.168.0.1"
116+
assert ip_network_first("10.0.0.0/8") == "10.0.0.1"
117+
118+
119+
def test_ip_network_last():
120+
assert ip_network_last("192.168.0.0/24") == "192.168.0.254"
121+
assert ip_network_last("10.0.0.0/8") == "10.255.255.254"
122+
assert ip_network_last("172.16.0.0/16") == "172.16.255.254"
123+
assert ip_network_last("192.168.1.0/30") == "192.168.1.2"
124+
# XXX assert ip_network_last("192.168.1.0/31") == "192.168.1.1"
125+
126+
with pytest.raises(Exception):
127+
ip_network_last("192.168.1.0/32")

0 commit comments

Comments
 (0)