aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2015-04-28 13:14:14 +0200
committerHarald Welte <laforge@gnumonks.org>2015-04-28 13:14:14 +0200
commit0a3bf76ac05b914daa982d27e592b9476f2d0305 (patch)
tree5c59e397e9b7791f5fa9c4b47554099f8a648854
parent6ca09c230d4b01ecf4cf7b54e6048191c2fb7d9a (diff)
add missing .c files for ares integrationlaforge/c-ares
-rw-r--r--src/ares.c62
-rw-r--r--tests/ares/ares_test.c50
2 files changed, 112 insertions, 0 deletions
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 <string.h>
+#include <ares.h>
+
+#include <osmocom/core/logging.h>
+#include <osmocom/core/utils.h>
+
+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 <stdio.h>
+#include <stdlib.h>
+
+#include <netdb.h>
+#include <ares.h>
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <osmocom/core/select.h>
+
+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);
+}