import functools import sys IS_PY2 = sys.version_info[0] < 3 if IS_PY2: b2s = lambda s: s s2b = lambda s: s _ffi_string = _ffi.string else: def b2s(b): return b.decode('utf-8') def s2b(s): return s.encode('utf-8') _ffi_string = lambda r, maxlen=-1: b2s(_ffi.string(r, maxlen)) exported_pycares_symbols = [ # Flag values 'ARES_FLAG_USEVC', 'ARES_FLAG_PRIMARY', 'ARES_FLAG_IGNTC', 'ARES_FLAG_NORECURSE', 'ARES_FLAG_STAYOPEN', 'ARES_FLAG_NOSEARCH', 'ARES_FLAG_NOALIASES', 'ARES_FLAG_NOCHECKRESP', # Nameinfo flag values 'ARES_NI_NOFQDN',
def _query_cb(arg, status, timeouts, abuf, alen): result = None callback, query_type = _ffi.from_handle(arg) if status == _lib.ARES_SUCCESS: if query_type == _lib.T_A: addrttls = _ffi.new("struct ares_addrttl[]", PYCARES_ADDRTTL_SIZE) naddrttls = _ffi.new("int*", PYCARES_ADDRTTL_SIZE) parse_status = _lib.ares_parse_a_reply(abuf, alen, _ffi.NULL, addrttls, naddrttls) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = [ ares_query_simple_result(addrttls[i]) for i in range(naddrttls[0]) ] status = None elif query_type == _lib.T_AAAA: addrttls = _ffi.new("struct ares_addr6ttl[]", PYCARES_ADDRTTL_SIZE) naddrttls = _ffi.new("int*", PYCARES_ADDRTTL_SIZE) parse_status = _lib.ares_parse_aaaa_reply(abuf, alen, _ffi.NULL, addrttls, naddrttls) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = [ ares_query_simple_result(addrttls[i]) for i in range(naddrttls[0]) ] status = None elif query_type == _lib.T_CNAME: host = _ffi.new("struct hostent **") parse_status = _lib.ares_parse_a_reply(abuf, alen, host, _ffi.NULL, _ffi.NULL) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = ares_query_cname_result(host[0]) _lib.ares_free_hostent(host[0]) status = None elif query_type == _lib.T_MX: mx_reply = _ffi.new("struct ares_mx_reply **") parse_status = _lib.ares_parse_mx_reply(abuf, alen, mx_reply) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = [] mx_reply_ptr = _ffi.new("struct ares_mx_reply **") mx_reply_ptr[0] = mx_reply[0] while True: if mx_reply_ptr[0] == _ffi.NULL: break result.append(ares_query_mx_result(mx_reply[0])) mx_reply_ptr[0] = mx_reply_ptr[0].next _lib.ares_free_data(mx_reply) status = None elif query_type == _lib.T_NAPTR: naptr_reply = _ffi.new("struct ares_naptr_reply **") parse_status = _lib.ares_parse_naptr_reply(abuf, alen, naptr_reply) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = [] naptr_reply_ptr = _ffi.new("struct ares_naptr_reply **") naptr_reply_ptr[0] = naptr_reply[0] while True: if naptr_reply_ptr[0] == _ffi.NULL: break result.append(ares_query_naptr_result(naptr_reply[0])) naptr_reply_ptr[0] = naptr_reply_ptr[0].next _lib.ares_free_data(naptr_reply) status = None elif query_type == _lib.T_NS: hostent = _ffi.new("struct hostent **") parse_status = _lib.ares_parse_ns_reply(abuf, alen, hostent) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = [] host = hostent[0] for i in range(100): if host.h_aliases[i] == _ffi.NULL: break result.append(ares_query_ns_result(host.h_aliases[i])) _lib.ares_free_hostent(host) status = None elif query_type == _lib.T_PTR: hostent = _ffi.new("struct hostent **") hostttl = _ffi.new("int*", PYCARES_ADDRTTL_SIZE) parse_status = _lib.ares_parse_ptr_reply(abuf, alen, _ffi.NULL, 0, socket.AF_UNSPEC, hostent, hostttl) if parse_status != ARES_SUCCESS: result = None status = parse_status else: aliases = [] i = 0 terminator = _ffi.cast('char*', _ffi.NULL) while hostent[0].h_aliases[i] != terminator: aliases.append(_ffi.string(hostent[0].h_aliases[i])) i += 1 result = ares_query_ptr_result(hostent[0], hostttl[0], aliases) _lib.ares_free_hostent(hostent[0]) status = None elif query_type == _lib.T_SOA: soa_reply = _ffi.new("struct ares_soa_reply **") parse_status = _lib.ares_parse_soa_reply(abuf, alen, soa_reply) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = ares_query_soa_result(soa_reply[0]) _lib.ares_free_data(soa_reply[0]) status = None elif query_type == _lib.T_SRV: srv_reply = _ffi.new("struct ares_srv_reply **") parse_status = _lib.ares_parse_srv_reply(abuf, alen, srv_reply) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = [] srv_reply_ptr = _ffi.new("struct ares_srv_reply **") srv_reply_ptr[0] = srv_reply[0] while True: if srv_reply_ptr[0] == _ffi.NULL: break result.append(ares_query_srv_result(srv_reply_ptr[0])) srv_reply_ptr[0] = srv_reply_ptr[0].next _lib.ares_free_data(srv_reply[0]) status = None elif query_type == _lib.T_TXT: txt_reply = _ffi.new("struct ares_txt_ext **") parse_status = _lib.ares_parse_txt_reply_ext( abuf, alen, txt_reply) if parse_status != ARES_SUCCESS: result = None status = parse_status else: result = [] txt_reply_ptr = _ffi.new("struct ares_txt_ext **") txt_reply_ptr[0] = txt_reply[0] tmp_obj = None while True: if txt_reply_ptr[0] == _ffi.NULL: if tmp_obj is not None: result.append(tmp_obj) break if txt_reply_ptr[0].record_start == 1: if tmp_obj is not None: result.append(tmp_obj) tmp_obj = ares_query_txt_result(txt_reply_ptr[0]) else: new_chunk = ares_query_txt_result(txt_reply_ptr[0]) tmp_obj.text += new_chunk.text txt_reply_ptr[0] = txt_reply_ptr[0].next _lib.ares_free_data(txt_reply[0]) status = None else: raise ValueError("invalid query type specified") callback(result, status) _global_set.discard(arg)
def strerror(code): return _ffi.string(_lib.ares_strerror(code))