@@ -97,19 +97,23 @@ def pf_query_nat(family, proto, src_ip, src_port, dst_ip, dst_port):
9797 [proto , family , src_port , dst_port ] = [
9898 int (v ) for v in [proto , family , src_port , dst_port ]]
9999
100- length = 4 if family == socket .AF_INET else 16
100+ packed_src_ip = socket .inet_pton (family , src_ip )
101+ packed_dst_ip = socket .inet_pton (family , dst_ip )
102+
103+ assert len (packed_src_ip ) == len (packed_dst_ip )
104+ length = len (packed_src_ip )
101105
102106 pnl = pfioc_natlook ()
103107 pnl .proto = proto
104108 pnl .direction = PF_OUT
105109 pnl .af = family
106- memmove (addressof (pnl .saddr ), socket . inet_pton ( pnl . af , src_ip ) , length )
110+ memmove (addressof (pnl .saddr ), packed_src_ip , length )
107111 pnl .sxport .port = socket .htons (src_port )
108- memmove (addressof (pnl .daddr ), socket . inet_pton ( pnl . af , dst_ip ) , length )
112+ memmove (addressof (pnl .daddr ), packed_dst_ip , length )
109113 pnl .dxport .port = socket .htons (dst_port )
110114
111- ioctl (pf_get_dev (), DIOCNATLOOK , (
112- c_char * sizeof (pnl )).from_address (addressof (pnl )))
115+ ioctl (pf_get_dev (), DIOCNATLOOK ,
116+ ( c_char * sizeof (pnl )).from_address (addressof (pnl )))
113117
114118 ip = socket .inet_ntop (
115119 pnl .af , (c_char * length ).from_address (addressof (pnl .rdaddr )).raw )
0 commit comments