1
2from ._cares import ffi as _ffi, lib as _lib
3import _cffi_backend # hint for bundler tools
4
5if _lib.ARES_SUCCESS != _lib.ares_library_init(_lib.ARES_LIB_INIT_ALL):
6 raise RuntimeError('Could not initialize c-ares')
7
8from . import errno
9from .utils import ascii_bytes, maybe_str, parse_name
10from ._version import __version__
11
12import socket
13import math
14import functools
15import sys
16from collections.abc import Callable, Iterable
17from typing import Any, Optional, Union
18
19IP4 = tuple[str, int]
20IP6 = tuple[str, int, int, int]
21
22# Flag values
23ARES_FLAG_USEVC = _lib.ARES_FLAG_USEVC
24ARES_FLAG_PRIMARY = _lib.ARES_FLAG_PRIMARY
25ARES_FLAG_IGNTC = _lib.ARES_FLAG_IGNTC
26ARES_FLAG_NORECURSE = _lib.ARES_FLAG_NORECURSE
27ARES_FLAG_STAYOPEN = _lib.ARES_FLAG_STAYOPEN
28ARES_FLAG_NOSEARCH = _lib.ARES_FLAG_NOSEARCH
29ARES_FLAG_NOALIASES = _lib.ARES_FLAG_NOALIASES
30ARES_FLAG_NOCHECKRESP = _lib.ARES_FLAG_NOCHECKRESP
31ARES_FLAG_EDNS = _lib.ARES_FLAG_EDNS
32ARES_FLAG_NO_DFLT_SVR = _lib.ARES_FLAG_NO_DFLT_SVR
33
34# Nameinfo flag values
35ARES_NI_NOFQDN = _lib.ARES_NI_NOFQDN
36ARES_NI_NUMERICHOST = _lib.ARES_NI_NUMERICHOST
37ARES_NI_NAMEREQD = _lib.ARES_NI_NAMEREQD
38ARES_NI_NUMERICSERV = _lib.ARES_NI_NUMERICSERV
39ARES_NI_DGRAM = _lib.ARES_NI_DGRAM
40ARES_NI_TCP = _lib.ARES_NI_TCP
41ARES_NI_UDP = _lib.ARES_NI_UDP
42ARES_NI_SCTP = _lib.ARES_NI_SCTP
43ARES_NI_DCCP = _lib.ARES_NI_DCCP
44ARES_NI_NUMERICSCOPE = _lib.ARES_NI_NUMERICSCOPE
45ARES_NI_LOOKUPHOST = _lib.ARES_NI_LOOKUPHOST
46ARES_NI_LOOKUPSERVICE = _lib.ARES_NI_LOOKUPSERVICE
47ARES_NI_IDN = _lib.ARES_NI_IDN
48ARES_NI_IDN_ALLOW_UNASSIGNED = _lib.ARES_NI_IDN_ALLOW_UNASSIGNED
49ARES_NI_IDN_USE_STD3_ASCII_RULES = _lib.ARES_NI_IDN_USE_STD3_ASCII_RULES
50
51# Bad socket
52ARES_SOCKET_BAD = _lib.ARES_SOCKET_BAD
53
54# Query types
55QUERY_TYPE_A = _lib.T_A
56QUERY_TYPE_AAAA = _lib.T_AAAA
57QUERY_TYPE_ANY = _lib.T_ANY
58QUERY_TYPE_CAA = _lib.T_CAA
59QUERY_TYPE_CNAME = _lib.T_CNAME
60QUERY_TYPE_MX = _lib.T_MX
61QUERY_TYPE_NAPTR = _lib.T_NAPTR
62QUERY_TYPE_NS = _lib.T_NS
63QUERY_TYPE_PTR = _lib.T_PTR
64QUERY_TYPE_SOA = _lib.T_SOA
65QUERY_TYPE_SRV = _lib.T_SRV
66QUERY_TYPE_TXT = _lib.T_TXT
67
68# Query classes
69QUERY_CLASS_IN = _lib.C_IN
70QUERY_CLASS_CHAOS = _lib.C_CHAOS
71QUERY_CLASS_HS = _lib.C_HS
72QUERY_CLASS_NONE = _lib.C_NONE
73QUERY_CLASS_ANY = _lib.C_ANY
74
75ARES_VERSION = maybe_str(_ffi.string(_lib.ares_version(_ffi.NULL)))
76PYCARES_ADDRTTL_SIZE = 256
77
78
79class AresError(Exception):
80 pass
81
82
83# callback helpers
84
85_global_set = set()
86
87@_ffi.def_extern()
88def _sock_state_cb(data, socket_fd, readable, writable):
89 sock_state_cb = _ffi.from_handle(data)
90 sock_state_cb(socket_fd, readable, writable)
91
92@_ffi.def_extern()
93def _host_cb(arg, status, timeouts, hostent):
94 callback = _ffi.from_handle(arg)
95 _global_set.discard(arg)
96
97 if status != _lib.ARES_SUCCESS:
98 result = None
99 else:
100 result = ares_host_result(hostent)
101 status = None
102
103 callback(result, status)
104
105@_ffi.def_extern()
106def _nameinfo_cb(arg, status, timeouts, node, service):
107 callback = _ffi.from_handle(arg)
108 _global_set.discard(arg)
109
110 if status != _lib.ARES_SUCCESS:
111 result = None
112 else:
113 result = ares_nameinfo_result(node, service)
114 status = None
115
116 callback(result, status)
117
118@_ffi.def_extern()
119def _query_cb(arg, status, timeouts, abuf, alen):
120 callback, query_type = _ffi.from_handle(arg)
121 _global_set.discard(arg)
122
123 if status == _lib.ARES_SUCCESS:
124 if query_type == _lib.T_ANY:
125 result = []
126 for qtype in (_lib.T_A, _lib.T_AAAA, _lib.T_CAA, _lib.T_CNAME, _lib.T_MX, _lib.T_NAPTR, _lib.T_NS, _lib.T_PTR, _lib.T_SOA, _lib.T_SRV, _lib.T_TXT):
127 r, status = parse_result(qtype, abuf, alen)
128 if status not in (None, _lib.ARES_ENODATA, _lib.ARES_EBADRESP):
129 result = None
130 break
131 if r is not None:
132 if isinstance(r, Iterable):
133 result.extend(r)
134 else:
135 result.append(r)
136 else:
137 status = None
138 else:
139 result, status = parse_result(query_type, abuf, alen)
140 else:
141 result = None
142
143 callback(result, status)
144
145@_ffi.def_extern()
146def _addrinfo_cb(arg, status, timeouts, res):
147 callback = _ffi.from_handle(arg)
148 _global_set.discard(arg)
149
150 if status != _lib.ARES_SUCCESS:
151 result = None
152 else:
153 result = ares_addrinfo_result(res)
154 status = None
155
156 callback(result, status)
157
158def parse_result(query_type, abuf, alen):
159 if query_type == _lib.T_A:
160 addrttls = _ffi.new("struct ares_addrttl[]", PYCARES_ADDRTTL_SIZE)
161 naddrttls = _ffi.new("int*", PYCARES_ADDRTTL_SIZE)
162 parse_status = _lib.ares_parse_a_reply(abuf, alen, _ffi.NULL, addrttls, naddrttls)
163 if parse_status != _lib.ARES_SUCCESS:
164 result = None
165 status = parse_status
166 else:
167 result = [ares_query_a_result(addrttls[i]) for i in range(naddrttls[0])]
168 status = None
169 elif query_type == _lib.T_AAAA:
170 addrttls = _ffi.new("struct ares_addr6ttl[]", PYCARES_ADDRTTL_SIZE)
171 naddrttls = _ffi.new("int*", PYCARES_ADDRTTL_SIZE)
172 parse_status = _lib.ares_parse_aaaa_reply(abuf, alen, _ffi.NULL, addrttls, naddrttls)
173 if parse_status != _lib.ARES_SUCCESS:
174 result = None
175 status = parse_status
176 else:
177 result = [ares_query_aaaa_result(addrttls[i]) for i in range(naddrttls[0])]
178 status = None
179 elif query_type == _lib.T_CAA:
180 caa_reply = _ffi.new("struct ares_caa_reply **")
181 parse_status = _lib.ares_parse_caa_reply(abuf, alen, caa_reply)
182 if parse_status != _lib.ARES_SUCCESS:
183 result = None
184 status = parse_status
185 else:
186 result = []
187 caa_reply_ptr = caa_reply[0]
188 while caa_reply_ptr != _ffi.NULL:
189 result.append(ares_query_caa_result(caa_reply_ptr))
190 caa_reply_ptr = caa_reply_ptr.next
191 _lib.ares_free_data(caa_reply[0])
192 status = None
193 elif query_type == _lib.T_CNAME:
194 host = _ffi.new("struct hostent **")
195 parse_status = _lib.ares_parse_a_reply(abuf, alen, host, _ffi.NULL, _ffi.NULL)
196 if parse_status != _lib.ARES_SUCCESS:
197 result = None
198 status = parse_status
199 else:
200 result = ares_query_cname_result(host[0])
201 _lib.ares_free_hostent(host[0])
202 status = None
203 elif query_type == _lib.T_MX:
204 mx_reply = _ffi.new("struct ares_mx_reply **")
205 parse_status = _lib.ares_parse_mx_reply(abuf, alen, mx_reply)
206 if parse_status != _lib.ARES_SUCCESS:
207 result = None
208 status = parse_status
209 else:
210 result = []
211 mx_reply_ptr = mx_reply[0]
212 while mx_reply_ptr != _ffi.NULL:
213 result.append(ares_query_mx_result(mx_reply_ptr))
214 mx_reply_ptr = mx_reply_ptr.next
215 _lib.ares_free_data(mx_reply[0])
216 status = None
217 elif query_type == _lib.T_NAPTR:
218 naptr_reply = _ffi.new("struct ares_naptr_reply **")
219 parse_status = _lib.ares_parse_naptr_reply(abuf, alen, naptr_reply)
220 if parse_status != _lib.ARES_SUCCESS:
221 result = None
222 status = parse_status
223 else:
224 result = []
225 naptr_reply_ptr = naptr_reply[0]
226 while naptr_reply_ptr != _ffi.NULL:
227 result.append(ares_query_naptr_result(naptr_reply_ptr))
228 naptr_reply_ptr = naptr_reply_ptr.next
229 _lib.ares_free_data(naptr_reply[0])
230 status = None
231 elif query_type == _lib.T_NS:
232 hostent = _ffi.new("struct hostent **")
233 parse_status = _lib.ares_parse_ns_reply(abuf, alen, hostent)
234 if parse_status != _lib.ARES_SUCCESS:
235 result = None
236 status = parse_status
237 else:
238 result = []
239 host = hostent[0]
240 i = 0
241 while host.h_aliases[i] != _ffi.NULL:
242 result.append(ares_query_ns_result(host.h_aliases[i]))
243 i += 1
244 _lib.ares_free_hostent(host)
245 status = None
246 elif query_type == _lib.T_PTR:
247 hostent = _ffi.new("struct hostent **")
248 parse_status = _lib.ares_parse_ptr_reply(abuf, alen, _ffi.NULL, 0, socket.AF_UNSPEC, hostent)
249 if parse_status != _lib.ARES_SUCCESS:
250 result = None
251 status = parse_status
252 else:
253 aliases = []
254 host = hostent[0]
255 i = 0
256 while host.h_aliases[i] != _ffi.NULL:
257 aliases.append(maybe_str(_ffi.string(host.h_aliases[i])))
258 i += 1
259 result = ares_query_ptr_result(host, aliases)
260 _lib.ares_free_hostent(host)
261 status = None
262 elif query_type == _lib.T_SOA:
263 soa_reply = _ffi.new("struct ares_soa_reply **")
264 parse_status = _lib.ares_parse_soa_reply(abuf, alen, soa_reply)
265 if parse_status != _lib.ARES_SUCCESS:
266 result = None
267 status = parse_status
268 else:
269 result = ares_query_soa_result(soa_reply[0])
270 _lib.ares_free_data(soa_reply[0])
271 status = None
272 elif query_type == _lib.T_SRV:
273 srv_reply = _ffi.new("struct ares_srv_reply **")
274 parse_status = _lib.ares_parse_srv_reply(abuf, alen, srv_reply)
275 if parse_status != _lib.ARES_SUCCESS:
276 result = None
277 status = parse_status
278 else:
279 result = []
280 srv_reply_ptr = srv_reply[0]
281 while srv_reply_ptr != _ffi.NULL:
282 result.append(ares_query_srv_result(srv_reply_ptr))
283 srv_reply_ptr = srv_reply_ptr.next
284 _lib.ares_free_data(srv_reply[0])
285 status = None
286 elif query_type == _lib.T_TXT:
287 txt_reply = _ffi.new("struct ares_txt_ext **")
288 parse_status = _lib.ares_parse_txt_reply_ext(abuf, alen, txt_reply)
289 if parse_status != _lib.ARES_SUCCESS:
290 result = None
291 status = parse_status
292 else:
293 result = []
294 txt_reply_ptr = txt_reply[0]
295 tmp_obj = None
296 while True:
297 if txt_reply_ptr == _ffi.NULL:
298 if tmp_obj is not None:
299 result.append(ares_query_txt_result(tmp_obj))
300 break
301 if txt_reply_ptr.record_start == 1:
302 if tmp_obj is not None:
303 result.append(ares_query_txt_result(tmp_obj))
304 tmp_obj = ares_query_txt_result_chunk(txt_reply_ptr)
305 else:
306 new_chunk = ares_query_txt_result_chunk(txt_reply_ptr)
307 tmp_obj.text += new_chunk.text
308 txt_reply_ptr = txt_reply_ptr.next
309 _lib.ares_free_data(txt_reply[0])
310 status = None
311 else:
312 raise ValueError("invalid query type specified")
313
314 return result, status
315
316
317class Channel:
318 __qtypes__ = (_lib.T_A, _lib.T_AAAA, _lib.T_ANY, _lib.T_CAA, _lib.T_CNAME, _lib.T_MX, _lib.T_NAPTR, _lib.T_NS, _lib.T_PTR, _lib.T_SOA, _lib.T_SRV, _lib.T_TXT)
319 __qclasses__ = (_lib.C_IN, _lib.C_CHAOS, _lib.C_HS, _lib.C_NONE, _lib.C_ANY)
320
321 def __init__(self,
322 flags: Optional[int] = None,
323 timeout: Optional[float] = None,
324 tries: Optional[int] = None,
325 ndots: Optional[int] = None,
326 tcp_port: Optional[int] = None,
327 udp_port: Optional[int] = None,
328 servers: Optional[Iterable[Union[str, bytes]]] = None,
329 domains: Optional[Iterable[Union[str, bytes]]] = None,
330 lookups: Union[str, bytes, None] = None,
331 sock_state_cb: Optional[Callable[[int, bool, bool], None]] = None,
332 socket_send_buffer_size: Optional[int] = None,
333 socket_receive_buffer_size: Optional[int] = None,
334 rotate: bool = False,
335 local_ip: Union[str, bytes, None] = None,
336 local_dev: Optional[str] = None,
337 resolvconf_path: Union[str, bytes, None] = None,
338 event_thread: bool = False) -> None:
339
340 channel = _ffi.new("ares_channel *")
341 options = _ffi.new("struct ares_options *")
342 optmask = 0
343
344 if flags is not None:
345 options.flags = flags
346 optmask = optmask | _lib.ARES_OPT_FLAGS
347
348 if timeout is not None:
349 options.timeout = int(timeout * 1000)
350 optmask = optmask | _lib.ARES_OPT_TIMEOUTMS
351
352 if tries is not None:
353 options.tries = tries
354 optmask = optmask | _lib.ARES_OPT_TRIES
355
356 if ndots is not None:
357 options.ndots = ndots
358 optmask = optmask | _lib.ARES_OPT_NDOTS
359
360 if tcp_port is not None:
361 options.tcp_port = tcp_port
362 optmask = optmask | _lib.ARES_OPT_TCP_PORT
363
364 if udp_port is not None:
365 options.udp_port = udp_port
366 optmask = optmask | _lib.ARES_OPT_UDP_PORT
367
368 if socket_send_buffer_size is not None:
369 options.socket_send_buffer_size = socket_send_buffer_size
370 optmask = optmask | _lib.ARES_OPT_SOCK_SNDBUF
371
372 if socket_receive_buffer_size is not None:
373 options.socket_receive_buffer_size = socket_receive_buffer_size
374 optmask = optmask | _lib.ARES_OPT_SOCK_RCVBUF
375
376 if sock_state_cb:
377 if not callable(sock_state_cb):
378 raise TypeError("sock_state_cb is not callable")
379 if event_thread:
380 raise RuntimeError("sock_state_cb and event_thread cannot be used together")
381
382 userdata = _ffi.new_handle(sock_state_cb)
383
384 # This must be kept alive while the channel is alive.
385 self._sock_state_cb_handle = userdata
386
387 options.sock_state_cb = _lib._sock_state_cb
388 options.sock_state_cb_data = userdata
389 optmask = optmask | _lib.ARES_OPT_SOCK_STATE_CB
390
391 if event_thread:
392 if not ares_threadsafety():
393 raise RuntimeError("c-ares is not built with thread safety")
394 if sock_state_cb:
395 raise RuntimeError("sock_state_cb and event_thread cannot be used together")
396 optmask = optmask | _lib.ARES_OPT_EVENT_THREAD
397 options.evsys = _lib.ARES_EVSYS_DEFAULT
398
399 if lookups:
400 options.lookups = _ffi.new('char[]', ascii_bytes(lookups))
401 optmask = optmask | _lib.ARES_OPT_LOOKUPS
402
403 if domains:
404 strs = [_ffi.new("char[]", ascii_bytes(i)) for i in domains]
405 c = _ffi.new("char *[%d]" % (len(domains) + 1))
406 for i in range(len(domains)):
407 c[i] = strs[i]
408
409 options.domains = c
410 options.ndomains = len(domains)
411 optmask = optmask | _lib.ARES_OPT_DOMAINS
412
413 if rotate:
414 optmask = optmask | _lib.ARES_OPT_ROTATE
415
416 if resolvconf_path is not None:
417 optmask = optmask | _lib.ARES_OPT_RESOLVCONF
418 options.resolvconf_path = _ffi.new('char[]', ascii_bytes(resolvconf_path))
419
420 r = _lib.ares_init_options(channel, options, optmask)
421 if r != _lib.ARES_SUCCESS:
422 raise AresError('Failed to initialize c-ares channel')
423
424 self._channel = _ffi.gc(channel, lambda x: _lib.ares_destroy(x[0]))
425
426 if servers:
427 self.servers = servers
428
429 if local_ip:
430 self.set_local_ip(local_ip)
431
432 if local_dev:
433 self.set_local_dev(local_dev)
434
435 def cancel(self) -> None:
436 _lib.ares_cancel(self._channel[0])
437
438 def reinit(self) -> None:
439 r = _lib.ares_reinit(self._channel[0])
440 if r != _lib.ARES_SUCCESS:
441 raise AresError(r, errno.strerror(r))
442
443 @property
444 def servers(self) -> list[str]:
445 servers = _ffi.new("struct ares_addr_node **")
446
447 r = _lib.ares_get_servers(self._channel[0], servers)
448 if r != _lib.ARES_SUCCESS:
449 raise AresError(r, errno.strerror(r))
450
451 server_list = []
452 server = _ffi.new("struct ares_addr_node **", servers[0])
453 while True:
454 if server == _ffi.NULL:
455 break
456
457 ip = _ffi.new("char []", _lib.INET6_ADDRSTRLEN)
458 s = server[0]
459 if _ffi.NULL != _lib.ares_inet_ntop(s.family, _ffi.addressof(s.addr), ip, _lib.INET6_ADDRSTRLEN):
460 server_list.append(maybe_str(_ffi.string(ip, _lib.INET6_ADDRSTRLEN)))
461
462 server = s.next
463
464 return server_list
465
466 @servers.setter
467 def servers(self, servers: Iterable[Union[str, bytes]]) -> None:
468 c = _ffi.new("struct ares_addr_node[%d]" % len(servers))
469 for i, server in enumerate(servers):
470 if _lib.ares_inet_pton(socket.AF_INET, ascii_bytes(server), _ffi.addressof(c[i].addr.addr4)) == 1:
471 c[i].family = socket.AF_INET
472 elif _lib.ares_inet_pton(socket.AF_INET6, ascii_bytes(server), _ffi.addressof(c[i].addr.addr6)) == 1:
473 c[i].family = socket.AF_INET6
474 else:
475 raise ValueError("invalid IP address")
476
477 if i > 0:
478 c[i - 1].next = _ffi.addressof(c[i])
479
480 r = _lib.ares_set_servers(self._channel[0], c)
481 if r != _lib.ARES_SUCCESS:
482 raise AresError(r, errno.strerror(r))
483
484 def getsock(self):
485 rfds = []
486 wfds = []
487 socks = _ffi.new("ares_socket_t [%d]" % _lib.ARES_GETSOCK_MAXNUM)
488 bitmask = _lib.ares_getsock(self._channel[0], socks, _lib.ARES_GETSOCK_MAXNUM)
489 for i in range(_lib.ARES_GETSOCK_MAXNUM):
490 if _lib.ARES_GETSOCK_READABLE(bitmask, i):
491 rfds.append(socks[i])
492 if _lib.ARES_GETSOCK_WRITABLE(bitmask, i):
493 wfds.append(socks[i])
494
495 return rfds, wfds
496
497 def process_fd(self, read_fd: int, write_fd: int) -> None:
498 _lib.ares_process_fd(self._channel[0], _ffi.cast("ares_socket_t", read_fd), _ffi.cast("ares_socket_t", write_fd))
499
500 def timeout(self, t = None):
501 maxtv = _ffi.NULL
502 tv = _ffi.new("struct timeval*")
503
504 if t is not None:
505 if t >= 0.0:
506 maxtv = _ffi.new("struct timeval*")
507 maxtv.tv_sec = int(math.floor(t))
508 maxtv.tv_usec = int(math.fmod(t, 1.0) * 1000000)
509 else:
510 raise ValueError("timeout needs to be a positive number or None")
511
512 _lib.ares_timeout(self._channel[0], maxtv, tv)
513
514 if tv == _ffi.NULL:
515 return 0.0
516
517 return (tv.tv_sec + tv.tv_usec / 1000000.0)
518
519 def gethostbyaddr(self, addr: str, callback: Callable[[Any, int], None]) -> None:
520 if not callable(callback):
521 raise TypeError("a callable is required")
522
523 addr4 = _ffi.new("struct in_addr*")
524 addr6 = _ffi.new("struct ares_in6_addr*")
525 if _lib.ares_inet_pton(socket.AF_INET, ascii_bytes(addr), (addr4)) == 1:
526 address = addr4
527 family = socket.AF_INET
528 elif _lib.ares_inet_pton(socket.AF_INET6, ascii_bytes(addr), (addr6)) == 1:
529 address = addr6
530 family = socket.AF_INET6
531 else:
532 raise ValueError("invalid IP address")
533
534 userdata = _ffi.new_handle(callback)
535 _global_set.add(userdata)
536 _lib.ares_gethostbyaddr(self._channel[0], address, _ffi.sizeof(address[0]), family, _lib._host_cb, userdata)
537
538 def gethostbyname(self, name: str, family: socket.AddressFamily, callback: Callable[[Any, int], None]) -> None:
539 if not callable(callback):
540 raise TypeError("a callable is required")
541
542 userdata = _ffi.new_handle(callback)
543 _global_set.add(userdata)
544 _lib.ares_gethostbyname(self._channel[0], parse_name(name), family, _lib._host_cb, userdata)
545
546 def getaddrinfo(
547 self,
548 host: str,
549 port: Optional[int],
550 callback: Callable[[Any, int], None],
551 family: socket.AddressFamily = 0,
552 type: int = 0,
553 proto: int = 0,
554 flags: int = 0
555 ) -> None:
556 if not callable(callback):
557 raise TypeError("a callable is required")
558
559 if port is None:
560 service = _ffi.NULL
561 elif isinstance(port, int):
562 service = str(port).encode('ascii')
563 else:
564 service = ascii_bytes(port)
565
566 userdata = _ffi.new_handle(callback)
567 _global_set.add(userdata)
568
569 hints = _ffi.new('struct ares_addrinfo_hints*')
570 hints.ai_flags = flags
571 hints.ai_family = family
572 hints.ai_socktype = type
573 hints.ai_protocol = proto
574 _lib.ares_getaddrinfo(self._channel[0], parse_name(host), service, hints, _lib._addrinfo_cb, userdata)
575
576 def query(self, name: str, query_type: str, callback: Callable[[Any, int], None], query_class: Optional[str] = None) -> None:
577 self._do_query(_lib.ares_query, name, query_type, callback, query_class=query_class)
578
579 def search(self, name, query_type, callback, query_class=None):
580 self._do_query(_lib.ares_search, name, query_type, callback, query_class=query_class)
581
582 def _do_query(self, func, name, query_type, callback, query_class=None):
583 if not callable(callback):
584 raise TypeError('a callable is required')
585
586 if query_type not in self.__qtypes__:
587 raise ValueError('invalid query type specified')
588
589 if query_class is None:
590 query_class = _lib.C_IN
591
592 if query_class not in self.__qclasses__:
593 raise ValueError('invalid query class specified')
594
595 userdata = _ffi.new_handle((callback, query_type))
596 _global_set.add(userdata)
597 func(self._channel[0], parse_name(name), query_class, query_type, _lib._query_cb, userdata)
598
599 def set_local_ip(self, ip):
600 addr4 = _ffi.new("struct in_addr*")
601 addr6 = _ffi.new("struct ares_in6_addr*")
602 if _lib.ares_inet_pton(socket.AF_INET, ascii_bytes(ip), addr4) == 1:
603 _lib.ares_set_local_ip4(self._channel[0], socket.ntohl(addr4.s_addr))
604 elif _lib.ares_inet_pton(socket.AF_INET6, ascii_bytes(ip), addr6) == 1:
605 _lib.ares_set_local_ip6(self._channel[0], addr6)
606 else:
607 raise ValueError("invalid IP address")
608
609 def getnameinfo(self, address: Union[IP4, IP6], flags: int, callback: Callable[[Any, int], None]) -> None:
610 if not callable(callback):
611 raise TypeError("a callable is required")
612
613 if len(address) == 2:
614 (ip, port) = address
615 sa4 = _ffi.new("struct sockaddr_in*")
616 if _lib.ares_inet_pton(socket.AF_INET, ascii_bytes(ip), _ffi.addressof(sa4.sin_addr)) != 1:
617 raise ValueError("Invalid IPv4 address %r" % ip)
618 sa4.sin_family = socket.AF_INET
619 sa4.sin_port = socket.htons(port)
620 sa = sa4
621 elif len(address) == 4:
622 (ip, port, flowinfo, scope_id) = address
623 sa6 = _ffi.new("struct sockaddr_in6*")
624 if _lib.ares_inet_pton(socket.AF_INET6, ascii_bytes(ip), _ffi.addressof(sa6.sin6_addr)) != 1:
625 raise ValueError("Invalid IPv6 address %r" % ip)
626 sa6.sin6_family = socket.AF_INET6
627 sa6.sin6_port = socket.htons(port)
628 sa6.sin6_flowinfo = socket.htonl(flowinfo) # I'm unsure about byteorder here.
629 sa6.sin6_scope_id = scope_id # Yes, without htonl.
630 sa = sa6
631 else:
632 raise ValueError("Invalid address argument")
633
634 userdata = _ffi.new_handle(callback)
635 _global_set.add(userdata)
636 _lib.ares_getnameinfo(self._channel[0], _ffi.cast("struct sockaddr*", sa), _ffi.sizeof(sa[0]), flags, _lib._nameinfo_cb, userdata)
637
638 def set_local_dev(self, dev):
639 _lib.ares_set_local_dev(self._channel[0], dev)
640
641
642class AresResult:
643 __slots__ = ()
644
645 def __repr__(self):
646 attrs = ['%s=%s' % (a, getattr(self, a)) for a in self.__slots__]
647 return '<%s> %s' % (self.__class__.__name__, ', '.join(attrs))
648
649
650# DNS query result types
651#
652
653class ares_query_a_result(AresResult):
654 __slots__ = ('host', 'ttl')
655 type = 'A'
656
657 def __init__(self, ares_addrttl):
658 buf = _ffi.new("char[]", _lib.INET6_ADDRSTRLEN)
659 _lib.ares_inet_ntop(socket.AF_INET, _ffi.addressof(ares_addrttl.ipaddr), buf, _lib.INET6_ADDRSTRLEN)
660 self.host = maybe_str(_ffi.string(buf, _lib.INET6_ADDRSTRLEN))
661 self.ttl = ares_addrttl.ttl
662
663
664class ares_query_aaaa_result(AresResult):
665 __slots__ = ('host', 'ttl')
666 type = 'AAAA'
667
668 def __init__(self, ares_addrttl):
669 buf = _ffi.new("char[]", _lib.INET6_ADDRSTRLEN)
670 _lib.ares_inet_ntop(socket.AF_INET6, _ffi.addressof(ares_addrttl.ip6addr), buf, _lib.INET6_ADDRSTRLEN)
671 self.host = maybe_str(_ffi.string(buf, _lib.INET6_ADDRSTRLEN))
672 self.ttl = ares_addrttl.ttl
673
674
675class ares_query_caa_result(AresResult):
676 __slots__ = ('critical', 'property', 'value', 'ttl')
677 type = 'CAA'
678
679 def __init__(self, caa):
680 self.critical = caa.critical
681 self.property = maybe_str(_ffi.string(caa.property, caa.plength))
682 self.value = maybe_str(_ffi.string(caa.value, caa.length))
683 self.ttl = -1
684
685
686class ares_query_cname_result(AresResult):
687 __slots__ = ('cname', 'ttl')
688 type = 'CNAME'
689
690 def __init__(self, host):
691 self.cname = maybe_str(_ffi.string(host.h_name))
692 self.ttl = -1
693
694
695class ares_query_mx_result(AresResult):
696 __slots__ = ('host', 'priority', 'ttl')
697 type = 'MX'
698
699 def __init__(self, mx):
700 self.host = maybe_str(_ffi.string(mx.host))
701 self.priority = mx.priority
702 self.ttl = -1
703
704
705class ares_query_naptr_result(AresResult):
706 __slots__ = ('order', 'preference', 'flags', 'service', 'regex', 'replacement', 'ttl')
707 type = 'NAPTR'
708
709 def __init__(self, naptr):
710 self.order = naptr.order
711 self.preference = naptr.preference
712 self.flags = maybe_str(_ffi.string(naptr.flags))
713 self.service = maybe_str(_ffi.string(naptr.service))
714 self.regex = maybe_str(_ffi.string(naptr.regexp))
715 self.replacement = maybe_str(_ffi.string(naptr.replacement))
716 self.ttl = -1
717
718
719class ares_query_ns_result(AresResult):
720 __slots__ = ('host', 'ttl')
721 type = 'NS'
722
723 def __init__(self, ns):
724 self.host = maybe_str(_ffi.string(ns))
725 self.ttl = -1
726
727
728class ares_query_ptr_result(AresResult):
729 __slots__ = ('name', 'ttl', 'aliases')
730 type = 'PTR'
731
732 def __init__(self, hostent, aliases):
733 self.name = maybe_str(_ffi.string(hostent.h_name))
734 self.aliases = aliases
735 self.ttl = -1
736
737
738class ares_query_soa_result(AresResult):
739 __slots__ = ('nsname', 'hostmaster', 'serial', 'refresh', 'retry', 'expires', 'minttl', 'ttl')
740 type = 'SOA'
741
742 def __init__(self, soa):
743 self.nsname = maybe_str(_ffi.string(soa.nsname))
744 self.hostmaster = maybe_str(_ffi.string(soa.hostmaster))
745 self.serial = soa.serial
746 self.refresh = soa.refresh
747 self.retry = soa.retry
748 self.expires = soa.expire
749 self.minttl = soa.minttl
750 self.ttl = -1
751
752
753class ares_query_srv_result(AresResult):
754 __slots__ = ('host', 'port', 'priority', 'weight', 'ttl')
755 type = 'SRV'
756
757 def __init__(self, srv):
758 self.host = maybe_str(_ffi.string(srv.host))
759 self.port = srv.port
760 self.priority = srv.priority
761 self.weight = srv.weight
762 self.ttl = -1
763
764
765class ares_query_txt_result(AresResult):
766 __slots__ = ('text', 'ttl')
767 type = 'TXT'
768
769 def __init__(self, txt_chunk):
770 self.text = maybe_str(txt_chunk.text)
771 self.ttl = -1
772
773
774class ares_query_txt_result_chunk(AresResult):
775 __slots__ = ('text', 'ttl')
776 type = 'TXT'
777
778 def __init__(self, txt):
779 self.text = _ffi.string(txt.txt)
780 self.ttl = -1
781
782
783# Other result types
784#
785
786class ares_host_result(AresResult):
787 __slots__ = ('name', 'aliases', 'addresses')
788
789 def __init__(self, hostent):
790 self.name = maybe_str(_ffi.string(hostent.h_name))
791 self.aliases = []
792 self.addresses = []
793 i = 0
794 while hostent.h_aliases[i] != _ffi.NULL:
795 self.aliases.append(maybe_str(_ffi.string(hostent.h_aliases[i])))
796 i += 1
797
798 i = 0
799 while hostent.h_addr_list[i] != _ffi.NULL:
800 buf = _ffi.new("char[]", _lib.INET6_ADDRSTRLEN)
801 if _ffi.NULL != _lib.ares_inet_ntop(hostent.h_addrtype, hostent.h_addr_list[i], buf, _lib.INET6_ADDRSTRLEN):
802 self.addresses.append(maybe_str(_ffi.string(buf, _lib.INET6_ADDRSTRLEN)))
803 i += 1
804
805
806class ares_nameinfo_result(AresResult):
807 __slots__ = ('node', 'service')
808
809 def __init__(self, node, service):
810 self.node = maybe_str(_ffi.string(node))
811 self.service = maybe_str(_ffi.string(service)) if service != _ffi.NULL else None
812
813
814class ares_addrinfo_node_result(AresResult):
815 __slots__ = ('ttl', 'flags', 'family', 'socktype', 'protocol', 'addr')
816
817 def __init__(self, ares_node):
818 self.ttl = ares_node.ai_ttl
819 self.flags = ares_node.ai_flags
820 self.socktype = ares_node.ai_socktype
821 self.protocol = ares_node.ai_protocol
822
823 addr = ares_node.ai_addr
824 assert addr.sa_family == ares_node.ai_family
825 ip = _ffi.new("char []", _lib.INET6_ADDRSTRLEN)
826 if addr.sa_family == socket.AF_INET:
827 self.family = socket.AF_INET
828 s = _ffi.cast("struct sockaddr_in*", addr)
829 if _ffi.NULL != _lib.ares_inet_ntop(s.sin_family, _ffi.addressof(s.sin_addr), ip, _lib.INET6_ADDRSTRLEN):
830 # (address, port) 2-tuple for AF_INET
831 self.addr = (_ffi.string(ip, _lib.INET6_ADDRSTRLEN), socket.ntohs(s.sin_port))
832 elif addr.sa_family == socket.AF_INET6:
833 self.family = socket.AF_INET6
834 s = _ffi.cast("struct sockaddr_in6*", addr)
835 if _ffi.NULL != _lib.ares_inet_ntop(s.sin6_family, _ffi.addressof(s.sin6_addr), ip, _lib.INET6_ADDRSTRLEN):
836 # (address, port, flow info, scope id) 4-tuple for AF_INET6
837 self.addr = (_ffi.string(ip, _lib.INET6_ADDRSTRLEN), socket.ntohs(s.sin6_port), s.sin6_flowinfo, s.sin6_scope_id)
838 else:
839 raise ValueError("invalid sockaddr family")
840
841
842class ares_addrinfo_cname_result(AresResult):
843 __slots__ = ('ttl', 'alias', 'name')
844
845 def __init__(self, ares_cname):
846 self.ttl = ares_cname.ttl
847 self.alias = maybe_str(_ffi.string(ares_cname.alias))
848 self.name = maybe_str(_ffi.string(ares_cname.name))
849
850
851class ares_addrinfo_result(AresResult):
852 __slots__ = ('cnames', 'nodes')
853
854 def __init__(self, ares_addrinfo):
855 self.cnames = []
856 self.nodes = []
857 cname_ptr = ares_addrinfo.cnames
858 while cname_ptr != _ffi.NULL:
859 self.cnames.append(ares_addrinfo_cname_result(cname_ptr))
860 cname_ptr = cname_ptr.next
861 node_ptr = ares_addrinfo.nodes
862 while node_ptr != _ffi.NULL:
863 self.nodes.append(ares_addrinfo_node_result(node_ptr))
864 node_ptr = node_ptr.ai_next
865 _lib.ares_freeaddrinfo(ares_addrinfo)
866
867
868def ares_threadsafety() -> bool:
869 """
870 Check if c-ares was compiled with thread safety support.
871
872 :return: True if thread-safe, False otherwise.
873 :rtype: bool
874 """
875 return bool(_lib.ares_threadsafety())
876
877__all__ = (
878 "ARES_FLAG_USEVC",
879 "ARES_FLAG_PRIMARY",
880 "ARES_FLAG_IGNTC",
881 "ARES_FLAG_NORECURSE",
882 "ARES_FLAG_STAYOPEN",
883 "ARES_FLAG_NOSEARCH",
884 "ARES_FLAG_NOALIASES",
885 "ARES_FLAG_NOCHECKRESP",
886 "ARES_FLAG_EDNS",
887 "ARES_FLAG_NO_DFLT_SVR",
888
889 # Nameinfo flag values
890 "ARES_NI_NOFQDN",
891 "ARES_NI_NUMERICHOST",
892 "ARES_NI_NAMEREQD",
893 "ARES_NI_NUMERICSERV",
894 "ARES_NI_DGRAM",
895 "ARES_NI_TCP",
896 "ARES_NI_UDP",
897 "ARES_NI_SCTP",
898 "ARES_NI_DCCP",
899 "ARES_NI_NUMERICSCOPE",
900 "ARES_NI_LOOKUPHOST",
901 "ARES_NI_LOOKUPSERVICE",
902 "ARES_NI_IDN",
903 "ARES_NI_IDN_ALLOW_UNASSIGNED",
904 "ARES_NI_IDN_USE_STD3_ASCII_RULES",
905
906 # Bad socket
907 "ARES_SOCKET_BAD",
908
909
910 # Query types
911 "QUERY_TYPE_A",
912 "QUERY_TYPE_AAAA",
913 "QUERY_TYPE_ANY",
914 "QUERY_TYPE_CAA",
915 "QUERY_TYPE_CNAME",
916 "QUERY_TYPE_MX",
917 "QUERY_TYPE_NAPTR",
918 "QUERY_TYPE_NS",
919 "QUERY_TYPE_PTR",
920 "QUERY_TYPE_SOA",
921 "QUERY_TYPE_SRV",
922 "QUERY_TYPE_TXT",
923
924 # Query classes
925 "QUERY_CLASS_IN",
926 "QUERY_CLASS_CHAOS",
927 "QUERY_CLASS_HS",
928 "QUERY_CLASS_NONE",
929 "QUERY_CLASS_ANY",
930
931
932 "ARES_VERSION",
933 "AresError",
934 "Channel",
935 "ares_threadsafety",
936 "errno",
937 "__version__"
938)