@@ -848,8 +848,25 @@ class IPField(Field[Union[str, Net], bytes]):
848
848
def __init__ (self , name , default ):
849
849
# type: (str, Optional[str]) -> None
850
850
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 ):
853
870
# type: (Optional[Packet], Union[AnyStr, List[AnyStr]]) -> Any
854
871
if isinstance (x , bytes ):
855
872
x = plain_str (x ) # type: ignore
@@ -858,7 +875,7 @@ def h2i(self, pkt, x):
858
875
elif isinstance (x , str ):
859
876
x = ScopedIP (x )
860
877
try :
861
- inet_aton (x )
878
+ self . _concat_ip (x )
862
879
except socket .error :
863
880
return Net (x )
864
881
elif isinstance (x , tuple ):
@@ -889,7 +906,7 @@ def i2m(self, pkt, x):
889
906
# type: (Optional[Packet], Optional[Union[str, Net]]) -> bytes
890
907
if x is None :
891
908
return b'\x00 \x00 \x00 \x00 '
892
- return inet_aton (plain_str (x ))
909
+ return self . _concat_ip (plain_str (x ))
893
910
894
911
def m2i (self , pkt , x ):
895
912
# type: (Optional[Packet], bytes) -> str
0 commit comments