Skip to content

Commit e2ce319

Browse files
committed
IPField: Add concat method for IP handling.
1 parent 92925da commit e2ce319

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

scapy/fields.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,25 @@ class IPField(Field[Union[str, Net], bytes]):
848848
def __init__(self, name, default):
849849
# type: (str, Optional[str]) -> None
850850
Field.__init__(self, name, default, "4s")
851-
852-
def h2i(self, pkt, x):
851+
852+
def _concat_ip(self, ip_string):
853+
"""Concat a IP str of form IP,IP,IP and returns it as bytes
854+
Backcompatible if IP is a single IP.
855+
:param ip_string: String of the IP to be packed"""
856+
ip_string = ip_string.replace(" ", "")
857+
858+
# Split IPs by commas and filter out empty strings
859+
ip_list = [ip.strip() for ip in ip_string.split(',') if ip.strip()]
860+
861+
# Convert each IP to packed format
862+
packed_ips = []
863+
for ip in ip_list:
864+
packed_ips.append(socket.inet_aton(ip))
865+
866+
# Concatenate packed IPs into a single byte string
867+
return b''.join(packed_ips)
868+
869+
def h2i(self, pkt, x, multiple=False):
853870
# type: (Optional[Packet], Union[AnyStr, List[AnyStr]]) -> Any
854871
if isinstance(x, bytes):
855872
x = plain_str(x) # type: ignore
@@ -858,7 +875,7 @@ def h2i(self, pkt, x):
858875
elif isinstance(x, str):
859876
x = ScopedIP(x)
860877
try:
861-
inet_aton(x)
878+
self._concat_ip(x)
862879
except socket.error:
863880
return Net(x)
864881
elif isinstance(x, tuple):
@@ -889,7 +906,7 @@ def i2m(self, pkt, x):
889906
# type: (Optional[Packet], Optional[Union[str, Net]]) -> bytes
890907
if x is None:
891908
return b'\x00\x00\x00\x00'
892-
return inet_aton(plain_str(x))
909+
return self._concat_ip(plain_str(x))
893910

894911
def m2i(self, pkt, x):
895912
# type: (Optional[Packet], bytes) -> str

0 commit comments

Comments
 (0)