@@ -97,19 +97,23 @@ def pf_query_nat(family, proto, src_ip, src_port, dst_ip, dst_port):
97
97
[proto , family , src_port , dst_port ] = [
98
98
int (v ) for v in [proto , family , src_port , dst_port ]]
99
99
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 )
101
105
102
106
pnl = pfioc_natlook ()
103
107
pnl .proto = proto
104
108
pnl .direction = PF_OUT
105
109
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 )
107
111
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 )
109
113
pnl .dxport .port = socket .htons (dst_port )
110
114
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 )))
113
117
114
118
ip = socket .inet_ntop (
115
119
pnl .af , (c_char * length ).from_address (addressof (pnl .rdaddr )).raw )
0 commit comments