From 0a3bf76ac05b914daa982d27e592b9476f2d0305 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 28 Apr 2015 13:14:14 +0200 Subject: add missing .c files for ares integration --- src/ares.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/ares/ares_test.c | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/ares.c create mode 100644 tests/ares/ares_test.c diff --git a/src/ares.c b/src/ares.c new file mode 100644 index 00000000..9530f830 --- /dev/null +++ b/src/ares.c @@ -0,0 +1,62 @@ +#include +#include + +#include +#include + +ares_channel osmo_ares_channel; + +struct value_string ares_status_strs[25] = { + /* Server error codes */ + { ARES_SUCCESS, "Success" }, + { ARES_ENODATA, "Server: No relevant answer" }, + { ARES_ESERVFAIL, "Server: Server failure" }, + { ARES_ENOTFOUND, "Server: Not found" }, + { ARES_ENOTIMP, "Server: Not implemented" }, + { ARES_EREFUSED, "Server: Refused" }, + /* Locally generated error codes */ + { ARES_EBADQUERY, "Local: Bad Query" }, + { ARES_EBADNAME, "Local: Bad Name" }, + { ARES_EBADFAMILY, "Local: Bad Family" }, + { ARES_EBADRESP, "Local: Bad Response" }, + { ARES_ECONNREFUSED, "Local: Connection refused" }, + { ARES_ETIMEOUT, "Local: Timeout" }, + { ARES_EOF, "Local: End of file" }, + { ARES_EFILE, "Local: EFILE?" }, + { ARES_ENOMEM, "Local: Out of memory" }, + { ARES_EDESTRUCTION, "Local: Destruction?" }, + { ARES_EBADSTR, "Local: Bad String?" }, + { ARES_EBADFLAGS, "genameinfo: Bad flags" }, + { ARES_ENONAME, "getaddrinfo: No name" }, + { ARES_EBADHINTS, "getaddrinfo: Bad Hints" }, + { ARES_ENOTINITIALIZED, "Library not initialized" }, + { ARES_ELOADIPHLPAPI, "?" }, + { ARES_EADDRGETNETWORKPARAMS, "?" }, + { ARES_ECANCELLED, "Cancelled" }, + { 0, NULL } +}; + +int osmo_ares_init() +{ + struct ares_options options; + unsigned int optmask = 0; + int rc; + + memset(&options, 0, sizeof(options)); + + rc = ares_library_init(ARES_LIB_INIT_ALL); + if (rc != ARES_SUCCESS) { + LOGP(DLGLOBAL, LOGL_ERROR, "ares_library_init(): %s\n", + ares_strerror(rc)); + return -1; + } + + rc = ares_init_options(&osmo_ares_channel, &options, optmask); + if (rc != ARES_SUCCESS) { + LOGP(DLGLOBAL, LOGL_ERROR, "ares_init_options(): %s\n", + ares_strerror(rc)); + return -1; + } + + return 0; +} diff --git a/tests/ares/ares_test.c b/tests/ares/ares_test.c new file mode 100644 index 00000000..c6ad5ba8 --- /dev/null +++ b/tests/ares/ares_test.c @@ -0,0 +1,50 @@ + +#include +#include + +#include +#include + +#include +#include +#include + +#include + +extern ares_channel osmo_ares_channel; + + +static void ares_cb(void *arg, int status, int timeouts, struct hostent *hostent) +{ + struct in_addr *ia; + int i; + + printf("Callback called: status=%d, timeouts=%d\n", status, timeouts); + + if (status != ARES_SUCCESS) + return; + + if (hostent->h_length != sizeof(struct in_addr)) + return; + + for (i = 0, ia = (struct in_addr *) hostent->h_addr_list[i]; ia; + i++, ia = (struct in_addr *) hostent->h_addr_list[i]) { + printf("%s -> %s\n", hostent->h_name, inet_ntoa(*ia)); + } + + exit(0); +} + +int main(int argc, char **argv) +{ + osmo_ares_init(); + + ares_gethostbyname(osmo_ares_channel, "localhost", AF_INET, + ares_cb, NULL); + + while (1) { + osmo_select_main(0); + } + + exit(1); +} -- cgit v1.2.3