s = curio.socket.socket(af, socktype, proto)
try:
if source:
- s.bind(_lltuple(af, source))
+ s.bind(_lltuple(source, af))
except Exception:
await s.close()
raise
return DatagramSocket(s)
elif socktype == socket.SOCK_STREAM:
if source:
- source_addr = (_lltuple(af, source))
+ source_addr = _lltuple(source, af)
else:
source_addr = None
async with _maybe_timeout(timeout):
stream = None
try:
if source:
- await s.bind(_lltuple(af, source))
+ await s.bind(_lltuple(source, af))
if socktype == socket.SOCK_STREAM:
with _maybe_timeout(timeout):
- await s.connect(_lltuple(af, destination))
+ await s.connect(_lltuple(destination, af))
except Exception:
s.close()
raise
af = dns.inet.af_for_address(where)
stuple = _source_tuple(af, source, source_port)
s = await backend.make_socket(af, socket.SOCK_DGRAM, 0, stuple)
- destination = _lltuple(af, (where, port))
+ destination = _lltuple((where, port), af)
await send_udp(s, wire, destination, expiration)
(r, received_time) = await receive_udp(s, destination, expiration,
ignore_unexpected,
return False
-def low_level_address_tuple(af, high_tuple):
- """Given an address family and a "high-level" address tuple, i.e.
+def low_level_address_tuple(high_tuple, af=None):
+ """Given a "high-level" address tuple, i.e.
an (address, port) return the appropriate "low-level" address tuple
suitable for use in socket calls.
+
+ If an *af* other than ``None`` is provided, it is assumed the
+ address in the high-level tuple is valid and has that af. If af
+ is ``None``, then af_for_address will be called.
+
"""
address, port = high_tuple
- if af == dns.inet.AF_INET:
+ if af is None:
+ af = af_for_address(address)
+ if af == AF_INET:
return (address, port)
- elif af == dns.inet.AF_INET6:
+ elif af == AF_INET6:
ai_flags = socket.AI_NUMERICHOST
((*_, tup), *_) = socket.getaddrinfo(address, port, flags=ai_flags)
return tup
import unittest
import binascii
+import socket
import dns.exception
import dns.ipv4
('2001:db8:0:1:1:1:1:q1', False)]:
self.assertEqual(dns.inet.is_address(t), e)
+ def test_low_level_address_tuple(self):
+ t = dns.inet.low_level_address_tuple(('1.2.3.4', 53))
+ self.assertEqual(t, ('1.2.3.4', 53))
+ t = dns.inet.low_level_address_tuple(('2600::1', 53))
+ self.assertEqual(t, ('2600::1', 53, 0, 0))
+ t = dns.inet.low_level_address_tuple(('1.2.3.4', 53), socket.AF_INET)
+ self.assertEqual(t, ('1.2.3.4', 53))
+ t = dns.inet.low_level_address_tuple(('2600::1', 53), socket.AF_INET6)
+ self.assertEqual(t, ('2600::1', 53, 0, 0))
+ def bad():
+ bogus = socket.AF_INET + socket.AF_INET6 + 1
+ t = dns.inet.low_level_address_tuple(('2600::1', 53), bogus)
+ self.assertRaises(NotImplementedError, bad)
+
if __name__ == '__main__':
unittest.main()