saghul / pycares

Python interface for c-ares
https://pypi.org/project/pycares/
MIT License
162 stars 74 forks source link

CNAME answers to A request #161

Open maelp opened 3 years ago

maelp commented 3 years ago

I think that it is possible for a server to answer an A request with a CNAME response, but it seems that pycares, although in both cases using the c-ares ares_parse_a_reply method, forces the result of an A query to be a list of IPs

https://github.com/saghul/pycares/blob/4e6e36f839255ebef05e0682b98cbee1533805ce/src/pycares/__init__.py#L168

maelp commented 3 years ago

This seems to work

    if query_type == _lib.T_A:
        host = _ffi.new("struct hostent **")
        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, host, addrttls, naddrttls)
        if parse_status != _lib.ARES_SUCCESS:
            result = None
            status = parse_status
        else:
            if host[0].h_aliases[0] != _ffi.NULL:
                result = ares_query_cname_result(host[0])
                _lib.ares_free_hostent(host[0])
            else:
                result = [ares_query_a_result(addrttls[i]) for i in range(naddrttls[0])]
            status = None
saghul commented 3 years ago

Nice! Is there a publicly available record we can use to test? If so, mind making a PR?

maelp commented 3 years ago

Well I guess we'd have to update this https://github.com/saghul/pycares/blob/master/tests/tests.py#L186 but I'm not exactly sure how we are supposed to build the response for each case?

saghul commented 3 years ago

Right. Up until this point I've used publicly available records instead of synthetic ones.

maelp commented 3 years ago

I'm not sure how we should go about generating the synthetic one, perhaps creating a fake server ?

saghul commented 3 years ago

Not sure, I suppose it might be tricky to test on Windows, that's why I relied on existing DNS records, though this is not ideal.