"""EDNS Options"""
import math
+import socket
import struct
import dns.enum
super().__init__(OptionType.ECS)
af = dns.inet.af_for_address(address)
- if af == dns.inet.AF_INET6:
+ if af == socket.AF_INET6:
self.family = 2
if srclen is None:
srclen = 56
- elif af == dns.inet.AF_INET:
+ elif af == socket.AF_INET:
self.family = 1
if srclen is None:
srclen = 24
import dns.ipv4
import dns.ipv6
-# We assume that AF_INET is always defined.
+# We assume that AF_INET and AF_INET6 are always defined. We keep
+# these here for the benefit of any old code (unlikely though that
+# is!).
AF_INET = socket.AF_INET
-
-# AF_INET6 might not be defined in the socket module, but we need it.
-# We'll try to use the socket module's value, and if it doesn't work,
-# we'll use our own value.
-
-try:
- AF_INET6 = socket.AF_INET6
-except AttributeError:
- AF_INET6 = 9999 # type: ignore
+AF_INET6 = socket.AF_INET6
def inet_pton(family, text):
# Caller has specified a source_port but not an address, so we
# need to return a source, and we need to use the appropriate
# wildcard address as the address.
- if af == dns.inet.AF_INET:
+ if af == socket.AF_INET:
source = '0.0.0.0'
- elif af == dns.inet.AF_INET6:
+ elif af == socket.AF_INET6:
source = '::'
else:
raise ValueError('source_port specified but address family is '
}
try:
where_af = dns.inet.af_for_address(where)
- if where_af == dns.inet.AF_INET:
+ if where_af == socket.AF_INET:
url = 'https://{}:{}{}'.format(where, port, path)
- elif where_af == dns.inet.AF_INET6:
+ elif where_af == socket.AF_INET6:
url = 'https://[{}]:{}{}'.format(where, port, path)
except ValueError:
if bootstrap_address is not None:
self.assertRaises(dns.exception.SyntaxError, bad)
def test_ptontop(self):
- for (af, a) in [(dns.inet.AF_INET, '1.2.3.4'),
- (dns.inet.AF_INET6, '2001:db8:0:1:1:1:1:1')]:
+ for (af, a) in [(socket.AF_INET, '1.2.3.4'),
+ (socket.AF_INET6, '2001:db8:0:1:1:1:1:1')]:
self.assertEqual(dns.inet.inet_ntop(af, dns.inet.inet_pton(af, a)),
a)
def test_af_inferred_from_where(self):
(af, d, s) = _d_and_s('1.2.3.4', 53, None, 0)
- self.assertEqual(af, dns.inet.AF_INET)
+ self.assertEqual(af, socket.AF_INET)
def test_af_inferred_from_where(self):
(af, d, s) = _d_and_s('1::2', 53, None, 0)
- self.assertEqual(af, dns.inet.AF_INET6)
+ self.assertEqual(af, socket.AF_INET6)
def test_af_inferred_from_source(self):
(af, d, s) = _d_and_s('https://example/dns-query', 443,
'1.2.3.4', 0, False)
- self.assertEqual(af, dns.inet.AF_INET)
+ self.assertEqual(af, socket.AF_INET)
def test_af_mismatch(self):
def bad():