summaryrefslogtreecommitdiff
path: root/dns/edns.py
diff options
context:
space:
mode:
authorpascal.bouchareine <pascal@gandi.net>2016-11-08 11:09:34 -0800
committerpascal.bouchareine <pascal@gandi.net>2016-11-08 11:09:34 -0800
commit2613b441f50ada2b60205ea37c4a8abe9594c6ff (patch)
tree97398dfb0a6204f33d6934a2fa9a96d3f5027459 /dns/edns.py
parent349ddcacba31adda18b8170e2ef76c34537965f1 (diff)
downloaddnspython-2613b441f50ada2b60205ea37c4a8abe9594c6ff.tar.gz
Clarify ceiling, add tests, fix doc, fix python3
- bad class doc - explicit ceil - python3 encoding issue - improve edns testing
Diffstat (limited to 'dns/edns.py')
-rw-r--r--dns/edns.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/dns/edns.py b/dns/edns.py
index 54f4e12..83b9be6 100644
--- a/dns/edns.py
+++ b/dns/edns.py
@@ -17,7 +17,9 @@
from __future__ import absolute_import
+import math
import struct
+import sys
import dns.inet
@@ -145,9 +147,10 @@ class ECSOption(Option):
@ivar srclen: prefix length, leftmost number of bits of the address
to be used for the lookup. Sent by client, mirrored by server in
responses. If not provided at init, will use /24 for v4 and /56 for v6
- @ivar srclen: int
+ @type srclen: int
@ivar scopelen: prefix length, leftmost number of bits of the address
that the response covers. 0 in queries, set by server.
+ @type scopelen: int
"""
super(ECSOption, self).__init__(ECS)
af = dns.inet.af_for_address(address)
@@ -167,13 +170,16 @@ class ECSOption(Option):
self.scopelen = scopelen
self.address = address
- self.addrdata = dns.inet.inet_pton(af, address)
+ addrdata = dns.inet.inet_pton(af, address)
+ nbytes = int(math.ceil(srclen/8.0))
# Truncate to srclen and pad to the end of the last octet needed
# See RFC section 6
- self.addrdata = self.addrdata[:-(-srclen//8)]
- last = ord(self.addrdata[-1:]) & (0xff << srclen % 8)
- self.addrdata = self.addrdata[:-1] + chr(last).encode('latin1')
+ self.addrdata = addrdata[:nbytes]
+ last = chr(ord(self.addrdata[-1:]) & (0xff << srclen % 8))
+ if sys.version_info >= (3,):
+ last = last.encode('latin1')
+ self.addrdata = self.addrdata[:-1] + last
def to_text(self):
return "ECS %s/%s scope/%s" % (self.address, self.srclen,
@@ -191,7 +197,7 @@ class ECSOption(Option):
family, src, scope = struct.unpack('!HBB', wire[cur:cur+4])
cur += 4
- addrlen = -(-src//8)
+ addrlen = int(math.ceil(src/8.0))
if family == 1:
af = dns.inet.AF_INET
@@ -202,7 +208,7 @@ class ECSOption(Option):
else:
raise ValueError('unsupported family')
- addr = dns.inet.inet_ntop(af, wire[cur:cur+addrlen] + '\x00' * pad)
+ addr = dns.inet.inet_ntop(af, wire[cur:cur+addrlen] + b'\x00' * pad)
return cls(addr, src, scope)
def _cmp(self, other):