Skip to content

Commit

Permalink
Support range format in IP(v6) fields (#4284)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpotter2 committed Feb 26, 2024
1 parent 7cb28cf commit 1354e4b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions scapy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,10 @@ def h2i(self, pkt, x):
inet_aton(x)
except socket.error:
return Net(x)
elif isinstance(x, tuple):
if len(x) != 2:
raise ValueError("Invalid IP format")
return Net(*x)
elif isinstance(x, list):
return [self.h2i(pkt, n) for n in x]
return x
Expand Down Expand Up @@ -949,6 +953,10 @@ def h2i(self, pkt, x):
x = in6_ptop(x)
except socket.error:
return Net6(x) # type: ignore
elif isinstance(x, tuple):
if len(x) != 2:
raise ValueError("Invalid IPv6 format")
return Net6(*x)
elif isinstance(x, list):
x = [self.h2i(pkt, n) for n in x]
return x # type: ignore
Expand Down
8 changes: 8 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -3890,6 +3890,10 @@ n1 = ip.dst
assert isinstance(n1, Net)
ip.show()

= Net using implicit format in IP

assert len(list(IP(dst=("192.168.0.100", "192.168.0.199")))) == 100

= Multiple IP addresses test
~ netaccess

Expand Down Expand Up @@ -3945,6 +3949,10 @@ ip.show()
ip = IPv6(dst="www.yahoo.com")
assert IPv6(raw(ip)).dst == [p.dst for p in ip][0]

= Net6 using implicit format in IPv6

assert len(list(IPv6(dst=("fe80::1", "fe80::1f")))) == 31

= Multiple IPv6 addresses test
~ netaccess ipv6

Expand Down

0 comments on commit 1354e4b

Please sign in to comment.