The branch, master has been updated via b89bc40 tests: Add a test for the NS record handling. via cf5b0fa rwrap: Add support to handle NS records from 826e765 cmake: Add missing HAVE_RESOLV_H define
https://git.samba.org/?p=resolv_wrapper.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit b89bc402bbc183d5ed2b54862c7229e2ab1d6629 Author: Richard Sharpe <rsha...@samba.org> Date: Fri May 20 06:36:05 2016 -0700 tests: Add a test for the NS record handling. Signed-off-by: Richard Sharpe <rsha...@samba.org> Reviewed-by: Andreas Schneider <a...@samba.org> Reviewed-by: Jakub Hrozek <jakub.hro...@posteo.se> commit cf5b0fa7b6aa76531c8b57e4f0a0c6dab36a8000 Author: Richard Sharpe <rsha...@samba.org> Date: Fri May 20 06:31:32 2016 -0700 rwrap: Add support to handle NS records Signed-off-by: Richard Sharpe <rsha...@samba.org> Reviewed-by: Andreas Schneider <a...@samba.org> Reviewed-by: Jakub Hrozek <jakub.hro...@posteo.se> ----------------------------------------------------------------------- Summary of changes: src/resolv_wrapper.c | 61 ++++++++++++++++++++++++++++++++++++++++++++- tests/fake_hosts.in | 4 +++ tests/test_real_res_query.c | 37 +++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) Changeset truncated at 500 lines: diff --git a/src/resolv_wrapper.c b/src/resolv_wrapper.c index 72005fa..48018be 100644 --- a/src/resolv_wrapper.c +++ b/src/resolv_wrapper.c @@ -231,6 +231,15 @@ static int rwrap_create_fake_aaaa_rr(const char *key, rr->type = ns_t_aaaa; return 0; } +static int rwrap_create_fake_ns_rr(const char *key, + const char *value, + struct rwrap_fake_rr *rr) +{ + memcpy(rr->rrdata.srv_rec.hostname, value, strlen(value) + 1); + memcpy(rr->key, key, strlen(key) + 1); + rr->type = ns_t_ns; + return 0; +} static int rwrap_create_fake_srv_rr(const char *key, const char *value, @@ -473,6 +482,47 @@ static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr, return resp_size; } +static ssize_t rwrap_fake_ns(struct rwrap_fake_rr *rr, + uint8_t *answer, + size_t anslen) +{ + uint8_t *a = answer; + ssize_t resp_size = 0; + size_t rdata_size; + unsigned char hostname_compressed[MAXDNAME]; + ssize_t compressed_len; + + if (rr == NULL || rr->type != ns_t_ns) { + RWRAP_LOG(RWRAP_LOG_ERROR, + "Malformed record, no or wrong value!\n"); + return -1; + } + RWRAP_LOG(RWRAP_LOG_TRACE, "Adding NS RR"); + + /* Prepare the data to write */ + compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname, + hostname_compressed, + MAXDNAME, + NULL, + NULL); + if (compressed_len < 0) { + return -1; + } + + /* Is this enough? */ + rdata_size = compressed_len; + + resp_size = rwrap_fake_rdata_common(ns_t_ns, rdata_size, + rr->key, anslen, &a); + if (resp_size < 0) { + return -1; + } + + memcpy(a, hostname_compressed, compressed_len); + + return resp_size; +} + static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr, uint8_t *answer, size_t anslen) @@ -666,7 +716,8 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion, } RWRAP_LOG(RWRAP_LOG_TRACE, - "Searching in fake hosts file %s\n", hostfile); + "Searching in fake hosts file %s for %s:%d\n", hostfile, + query, type); fp = fopen(hostfile, "r"); if (fp == NULL) { @@ -706,6 +757,10 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion, rec_type, "AAAA", key, query)) { rc = rwrap_create_fake_aaaa_rr(key, value, rr); break; + } else if (TYPE_MATCH(type, ns_t_ns, + rec_type, "NS", key, query)) { + rc = rwrap_create_fake_ns_rr(key, value, rr); + break; } else if (TYPE_MATCH(type, ns_t_srv, rec_type, "SRV", key, query)) { rc = rwrap_create_fake_srv_rr(key, value, rr); @@ -780,6 +835,7 @@ static inline bool rwrap_known_type(int type) switch (type) { case ns_t_a: case ns_t_aaaa: + case ns_t_ns: case ns_t_srv: case ns_t_soa: case ns_t_cname: @@ -839,6 +895,9 @@ static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr, case ns_t_aaaa: resp_data = rwrap_fake_aaaa(rr, answer, anslen); break; + case ns_t_ns: + resp_data = rwrap_fake_ns(rr, answer, anslen); + break; case ns_t_srv: resp_data = rwrap_fake_srv(rr, answer, anslen); break; diff --git a/tests/fake_hosts.in b/tests/fake_hosts.in index e441f0e..19f7551 100644 --- a/tests/fake_hosts.in +++ b/tests/fake_hosts.in @@ -1,3 +1,5 @@ +NS cwrap.org ns1.cwrap.org +NS cwrap.org ns2.cwrap.org A brokenrecord.com A cwrap.org 127.0.0.21 AAAA cwrap6.org 2a00:1450:4013:c01::63 @@ -8,3 +10,5 @@ CNAME rwrap.org web.cwrap.org CNAME web.cwrap.org www.cwrap.org A www.cwrap.org 127.0.0.22 A krb5.cwrap.org 127.0.0.23 +A ns1.cwrap.org 127.0.0.24 +A ns2.cwrap.org 127.0.0.25 diff --git a/tests/test_real_res_query.c b/tests/test_real_res_query.c index f563a3d..1d688c4 100644 --- a/tests/test_real_res_query.c +++ b/tests/test_real_res_query.c @@ -130,6 +130,42 @@ static void test_res_query_a_record(void **state) assert_string_equal(addr, "78.46.80.163"); } +static void test_res_query_ns_record(void **state) +{ + int rv; + struct __res_state dnsstate; + unsigned char answer[ANSIZE] = { 0 }; + char addr[INET_ADDRSTRLEN]; + ns_msg handle; + ns_rr rr; /* expanded resource record */ + + (void) state; /* unused */ + + memset(&dnsstate, 0, sizeof(struct __res_state)); + rv = res_ninit(&dnsstate); + assert_int_equal(rv, 0); + + rv = res_nquery(&dnsstate, "cwrap.org", ns_c_in, ns_t_ns, + answer, sizeof(answer)); + assert_in_range(rv, 1, 150); + + printf("dump answer:\n"); + dump_data(answer, rv); + + ns_initparse(answer, sizeof(answer), &handle); + /* The query must finish w/o an error, have two answers and the answer + * must be a parseable RR of type A and have the address that our + * fake hosts file contains + */ + assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror); + assert_int_equal(ns_msg_count(handle, ns_s_an), 2); + assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0); + assert_int_equal(ns_rr_type(rr), ns_t_ns); + assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr), + addr, sizeof(addr))); + /*assert_string_equal(addr, "3.110.115.50");*/ +} + static void test_res_query_srv_record(void **state) { int rv; @@ -191,6 +227,7 @@ int main(void) const struct CMUnitTest real_tests[] = { cmocka_unit_test(test_res_query_a_record), + cmocka_unit_test(test_res_query_ns_record), cmocka_unit_test(test_res_query_srv_record), }; -- Resolv Wrapper Repository