From 26774309fee97ec29f038fe1f624d59f85aedcb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vavru=C5=A1a?= <marek.vavrusa@nic.cz> Date: Thu, 17 Sep 2015 13:50:36 +0200 Subject: [PATCH] lib: removed synchronous api code the reason is that it's not actively used since we moved to binary testing, and it depends on libknot internal api that has changed also removed several unused libknot internal headers --- daemon/main.c | 1 - daemon/worker.c | 1 - lib/resolve.c | 123 ------------------------------------------ lib/resolve.h | 37 +------------ modules/hints/hints.c | 1 - tests/test_resolve.c | 62 --------------------- tests/unit.mk | 3 +- 7 files changed, 3 insertions(+), 225 deletions(-) delete mode 100644 tests/test_resolve.c diff --git a/daemon/main.c b/daemon/main.c index 49735c62f..d0e2ba426 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -18,7 +18,6 @@ #include <string.h> #include <getopt.h> #include <uv.h> -#include <libknot/internal/sockaddr.h> #include "contrib/ucw/mempool.h" #include "contrib/ccan/asprintf/asprintf.h" diff --git a/daemon/worker.c b/daemon/worker.c index 5f1e01ee3..abf5e4ab8 100644 --- a/daemon/worker.c +++ b/daemon/worker.c @@ -17,7 +17,6 @@ #include <uv.h> #include <lua.h> #include <libknot/packet/pkt.h> -#include <libknot/internal/net.h> #include <contrib/ucw/lib.h> #include <contrib/ucw/mempool.h> #if defined(__GLIBC__) && defined(_GNU_SOURCE) diff --git a/lib/resolve.c b/lib/resolve.c index a9ca2c087..4d5b69a41 100644 --- a/lib/resolve.c +++ b/lib/resolve.c @@ -19,7 +19,6 @@ #include <fcntl.h> #include <libknot/rrtype/rdname.h> #include <libknot/descriptor.h> -#include <libknot/internal/net.h> #include <ucw/mempool.h> #include "lib/resolve.h" #include "lib/layer.h" @@ -136,70 +135,6 @@ static int ns_resolve_addr(struct kr_query *qry, struct kr_request *param) return kr_ok(); } -static int connected(struct sockaddr *addr, int proto, struct timeval *timeout) -{ - unsigned flags = (proto == SOCK_STREAM) ? O_NONBLOCK : 0; - int fd = net_connected_socket(proto, (struct sockaddr_storage *)addr, NULL, flags); - if (fd < 0) { - return kr_error(ECONNREFUSED); - } - - /* Workaround for timeout, as we have no control over - * connect() time limit in blocking mode. */ - if (proto == SOCK_STREAM) { - fd_set set; - FD_ZERO(&set); - FD_SET(fd, &set); - int ret = select(fd + 1, NULL, &set, NULL, timeout); - if (ret == 0) { - close(fd); - return kr_error(ETIMEDOUT); - } - if (ret < 0) { - close(fd); - return kr_error(ECONNREFUSED); - } - fcntl(fd, F_SETFL, 0); - } - - return fd; -} - -static int sendrecv(struct sockaddr *addr, int proto, const knot_pkt_t *query, knot_pkt_t *resp) -{ - struct timeval timeout = { KR_CONN_RTT_MAX / 1000, 0 }; - auto_close int fd = connected(addr, proto, &timeout); - resp->size = 0; - if (fd < 0) { - return fd; - } - - /* Send packet */ - int ret = 0; - if (proto == SOCK_STREAM) { - ret = tcp_send_msg(fd, query->wire, query->size, &timeout); - } else { - ret = udp_send_msg(fd, query->wire, query->size, NULL); - } - if (ret != query->size) { - return kr_error(EIO); - } - - /* Receive it */ - if (proto == SOCK_STREAM) { - ret = tcp_recv_msg(fd, resp->wire, resp->max_size, &timeout); - } else { - ret = udp_recv_msg(fd, resp->wire, resp->max_size, &timeout); - } - if (ret <= 0) { - return kr_error(ETIMEDOUT); - } - - /* Parse and return */ - resp->size = ret; - return knot_pkt_parse(resp, 0); -} - static int edns_put(knot_pkt_t *pkt) { if (!pkt->opt_rr) { @@ -272,64 +207,6 @@ static int query_finalize(struct kr_request *request, knot_pkt_t *pkt) return ret; } -int kr_resolve(struct kr_context* ctx, knot_pkt_t *answer, - const knot_dname_t *qname, uint16_t qclass, uint16_t qtype) -{ - if (ctx == NULL || answer == NULL || qname == NULL) { - return kr_error(EINVAL); - } - - /* Create memory pool */ - mm_ctx_t pool = { - .ctx = mp_new (KNOT_WIRE_MAX_PKTSIZE), - .alloc = (mm_alloc_t) mp_alloc - }; - knot_pkt_t *query = knot_pkt_new(NULL, KR_EDNS_PAYLOAD, &pool); - knot_pkt_t *resp = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, &pool); - if (!query || !resp) { - mp_delete(pool.ctx); - return kr_error(ENOMEM); - } - - /* Initialize context. */ - struct kr_request request; - request.pool = pool; - kr_resolve_begin(&request, ctx, answer); -#ifdef WITH_DEBUG - struct kr_rplan *rplan = &request.rplan; /* for DEBUG_MSG */ -#endif - /* Resolve query, iteratively */ - int proto = 0; - struct sockaddr *addr = NULL; - unsigned iter_count = 0; - int state = kr_resolve_query(&request, qname, qclass, qtype); - while (state == KNOT_STATE_PRODUCE) { - /* Hardlimit on iterative queries */ - if (++iter_count > KR_ITER_LIMIT) { - DEBUG_MSG("iteration limit %d reached\n", KR_ITER_LIMIT); - state = KNOT_STATE_FAIL; - break; - } - /* Produce next query or finish */ - state = kr_resolve_produce(&request, &addr, &proto, query); - while (state == KNOT_STATE_CONSUME) { - /* Get answer from nameserver and consume it */ - int ret = sendrecv(addr, proto, query, resp); - if (ret != 0) { - DEBUG_MSG("sendrecv: %s\n", kr_strerror(ret)); - } - state = kr_resolve_consume(&request, resp); - knot_pkt_clear(resp); - } - knot_pkt_clear(query); - } - - /* Cleanup */ - kr_resolve_finish(&request, state); - mp_delete(pool.ctx); - return state == KNOT_STATE_DONE ? 0 : kr_error(EIO); -} - int kr_resolve_begin(struct kr_request *request, struct kr_context *ctx, knot_pkt_t *answer) { /* Initialize request */ diff --git a/lib/resolve.h b/lib/resolve.h index 67aca7461..a6afb2ba6 100644 --- a/lib/resolve.h +++ b/lib/resolve.h @@ -28,26 +28,8 @@ /** * @file resolve.h - * @brief The API provides a high-level API for simple name resolution, - * and an API providing a "consumer-producer"-like interface to enable - * you write custom I/O or special iterative resolution driver. - * - * # Example usage of the high-level API: - * - * @code{.c} - * - * struct kr_context ctx = { - * .cache = ..., // open cache instance (or NULL) - * .layers = &modules, - * }; - * - * // Resolve "IN A cz." - * knot_pkt_t *answer = knot_pkt_new(NULL, 65535, ctx.pool); - * int ret = kr_resolve(&ctx, answer, (uint8_t*)"\x02cz", 1, 1); - * printf("rcode: %d, ancount: %u\n", - * knot_wire_get_rcode(answer->wire), - * knot_wire_get_ancount(answer->wire)); - * @endcode + * @brief The API provides an API providing a "consumer-producer"-like interface to enable + * user to plug it into existing event loop or I/O code. * * # Example usage of the iterative API: * @@ -131,21 +113,6 @@ struct kr_request { mm_ctx_t pool; }; -/** - * Resolve an input query and produce a packet with an answer. - * - * @note The function doesn't change the packet question or message ID. - * - * @param ctx resolution context - * @param answer answer packet to be written - * @param qname resolved query name - * @param qclass resolved query class - * @param qtype resolved query type - * @return 0 or an error code - */ -int kr_resolve(struct kr_context* ctx, knot_pkt_t *answer, - const knot_dname_t *qname, uint16_t qclass, uint16_t qtype); - /** * Begin name resolution. * diff --git a/modules/hints/hints.c b/modules/hints/hints.c index c99d17016..509ebfd79 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -23,7 +23,6 @@ #include <libknot/packet/pkt.h> #include <libknot/descriptor.h> -#include <libknot/internal/lists.h> #include <libknot/rrtype/aaaa.h> #include <ccan/json/json.h> #include <ucw/mempool.h> diff --git a/tests/test_resolve.c b/tests/test_resolve.c deleted file mode 100644 index eafff00d8..000000000 --- a/tests/test_resolve.c +++ /dev/null @@ -1,62 +0,0 @@ -/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <libknot/descriptor.h> -#include "tests/test.h" -#include <cmocka.h> -#include "lib/resolve.h" - -static void test_resolve_nullparams(void **state) -{ - int ret = KNOT_EOK; - - /* NULL parameter check */ - void *non_null = (void *)0xDEADBEEF; - ret = kr_resolve(NULL, non_null, non_null, KNOT_CLASS_NONE, KNOT_RRTYPE_ANY); - assert_int_equal(ret, KNOT_EINVAL); - ret = kr_resolve(non_null, NULL, non_null, KNOT_CLASS_NONE, KNOT_RRTYPE_ANY); - assert_int_equal(ret, KNOT_EINVAL); - ret = kr_resolve(non_null, non_null, NULL, KNOT_CLASS_NONE, KNOT_RRTYPE_ANY); - assert_int_equal(ret, KNOT_EINVAL); -} - -static void test_resolve_dummy(void **state) -{ - /* Create resolution context */ - module_array_t module_arr = { NULL, 0, 0 }; - struct kr_context ctx = { - .pool = NULL, - .modules = &module_arr - }; - /* Resolve a query (no iterator) */ - knot_pkt_t *answer = knot_pkt_new(NULL, 4096, NULL); - int ret = kr_resolve(&ctx, answer, (const uint8_t *)"", KNOT_CLASS_IN, KNOT_RRTYPE_NS); - assert_int_not_equal(ret, kr_ok()); - assert_int_equal(knot_wire_get_rcode(answer->wire), KNOT_RCODE_SERVFAIL); - knot_pkt_free(&answer); -} - -int main(void) -{ - const UnitTest tests[] = { - /* Parameter sanity checks */ - unit_test(test_resolve_nullparams), - /* Simple resolution test cases */ - unit_test(test_resolve_dummy) - }; - - return run_tests(tests); -} diff --git a/tests/unit.mk b/tests/unit.mk index 3b69799d6..d944d5967 100644 --- a/tests/unit.mk +++ b/tests/unit.mk @@ -12,8 +12,7 @@ tests_BIN := \ test_module \ test_cache \ test_zonecut \ - test_rplan \ - test_resolve + test_rplan mock_cmodule_SOURCES := tests/mock_cmodule.c $(eval $(call make_lib,mock_cmodule,tests)) -- GitLab