Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions scapy/layers/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,34 +558,31 @@ def m2i(self, pkt, x):
x = x[: operator.floordiv(self.af_length, 8)]
return inet_ntop(self.af_familly, x)

def _pack_subnet(self, subnet):
# type: (bytes) -> bytes
def _pack_subnet(self, subnet, pkt):
# type: (bytes, Packet) -> bytes
packed_subnet = inet_pton(self.af_familly, plain_str(subnet))
for i in list(range(operator.floordiv(self.af_length, 8)))[::-1]:
if packed_subnet[i] != 0:
i += 1
break
return packed_subnet[:i]
sz = operator.floordiv(self.length_from(pkt) + 7, 8)
return packed_subnet[:sz]

def i2m(self, pkt, x):
# type: (Optional[Packet], Optional[Union[str, Net]]) -> bytes
if x is None:
return self.af_default
try:
return self._pack_subnet(x)
return self._pack_subnet(x, pkt)
except (OSError, socket.error):
pkt.family = 2
return ClientSubnetv6("", "")._pack_subnet(x)
return ClientSubnetv6("", "")._pack_subnet(x, pkt)

def i2len(self, pkt, x):
# type: (Packet, Any) -> int
if x is None:
return 1
try:
return len(self._pack_subnet(x))
return len(self._pack_subnet(x, pkt))
except (OSError, socket.error):
pkt.family = 2
return len(ClientSubnetv6("", "")._pack_subnet(x))
return len(ClientSubnetv6("", "")._pack_subnet(x, pkt))


class ClientSubnetv6(ClientSubnetv4):
Expand Down
3 changes: 3 additions & 0 deletions test/scapy/layers/dns.uts
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,6 @@ for i in pkt:
b = b'\x00\x08\x00\x0c\x00\x02=\x00+\xaf\xa3\xc4\xed\xeeW\xb8'
p = EDNS0ClientSubnet(b)
assert p.source_plen == 61 and p.address == '2baf:a3c4:edee:57b8::'

b = EDNS0ClientSubnet(source_plen=23, address='101.132.0.0')
assert bytes(b) == b'\x00\x08\x00\x07\x00\x01\x17\x00e\x84\x00'
Loading