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
29 changes: 15 additions & 14 deletions scapy/layers/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,34 +558,35 @@ 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("", "", lambda p: p.source_plen or 32). \
_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(
"", "", lambda p: p.source_plen or 32
)._pack_subnet(x, pkt))


class ClientSubnetv6(ClientSubnetv4):
Expand All @@ -607,13 +608,13 @@ class EDNS0ClientSubnet(_EDNS0Dummy):
ByteField("scope_plen", 0),
MultipleTypeField(
[(ClientSubnetv4("address", "192.168.0.0",
length_from=lambda p: p.source_plen),
length_from=lambda p: p.source_plen or 16),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value of 16 here is to maintain backward compatibility: when scope_plen is None, the original _pack_subnet method computes 16 bits from the default address 192.168.0.0.

lambda pkt: pkt.family == 1),
(ClientSubnetv6("address", "2001:db8::",
length_from=lambda p: p.source_plen),
length_from=lambda p: p.source_plen or 32),
lambda pkt: pkt.family == 2)],
ClientSubnetv4("address", "192.168.0.0",
length_from=lambda p: p.source_plen))]
length_from=lambda p: p.source_plen or 32))]


class EDNS0COOKIE(_EDNS0Dummy):
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