summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarko Kreen2010-10-12 10:50:15 +0000
committerMarko Kreen2010-10-12 10:58:52 +0000
commitbe2eeeb4157fb731d88870484bd77a9379470006 (patch)
tree07c5c055eb2867a15f26c793e84c4402e8b0ccd0
parentd8945ce07a8a2145903b2114cddcc219dc38e3b8 (diff)
netdb: compat getaddrinfo_a()
-rw-r--r--m4/usual.m42
-rw-r--r--test/Makefile4
-rw-r--r--test/force_compat.sed1
-rw-r--r--test/test_common.c1
-rw-r--r--test/test_common.h1
-rw-r--r--test/test_netdb.c28
-rw-r--r--usual/netdb.c56
-rw-r--r--usual/netdb.h73
8 files changed, 164 insertions, 2 deletions
diff --git a/m4/usual.m4 b/m4/usual.m4
index 6232d5c..627a7c2 100644
--- a/m4/usual.m4
+++ b/m4/usual.m4
@@ -143,6 +143,8 @@ AC_CHECK_FUNCS(inet_ntop poll getline memrchr regcomp)
AC_CHECK_FUNCS(err errx warn warnx getprogname setprogname)
AC_CHECK_FUNCS(posix_memalign memalign valloc)
AC_CHECK_FUNCS(fls flsl flsll ffs ffsl ffsll)
+AC_SEARCH_LIBS(getaddrinfo_a, anl)
+AC_CHECK_FUNCS(getaddrinfo_a)
### Functions provided only on win32
AC_CHECK_FUNCS(localtime_r recvmsg sendmsg usleep)
### Functions used by libusual itself
diff --git a/test/Makefile b/test/Makefile
index 869b066..22662c7 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -12,9 +12,9 @@ override DEFS = -DUSUAL_TEST_CONFIG
OBJS = test_string.o test_crypto.o test_aatree.o test_heap.o \
test_common.o test_list.o tinytest.o test_cbtree.o \
test_utf8.o test_strpool.o test_pgutil.o test_regex.o \
- test_cxalloc.o test_bits.o test_base.o
+ test_cxalloc.o test_bits.o test_base.o test_netdb.o
-test-all: test
+test-all: regtest.compat
include ../Makefile
diff --git a/test/force_compat.sed b/test/force_compat.sed
index 61bac0f..f49f6bb 100644
--- a/test/force_compat.sed
+++ b/test/force_compat.sed
@@ -5,3 +5,4 @@
/BASENAME/s,^,//,
/DIRNAME/s,^,//,
/REGCOMP/s,^,//,
+/GETADDRINFO_A/s,^,//,
diff --git a/test/test_common.c b/test/test_common.c
index 4d48b21..c0be1bd 100644
--- a/test/test_common.c
+++ b/test/test_common.c
@@ -16,6 +16,7 @@ struct testgroup_t groups[] = {
{ "strpool/", strpool_tests },
{ "pgutil/", pgutil_tests },
{ "regex/", regex_tests },
+ { "netdb/", netdb_tests },
END_OF_GROUPS
};
diff --git a/test/test_common.h b/test/test_common.h
index 12144db..8eae522 100644
--- a/test/test_common.h
+++ b/test/test_common.h
@@ -21,4 +21,5 @@ extern struct testcase_t regex_tests[];
extern struct testcase_t cxalloc_tests[];
extern struct testcase_t bits_tests[];
extern struct testcase_t base_tests[];
+extern struct testcase_t netdb_tests[];
diff --git a/test/test_netdb.c b/test/test_netdb.c
new file mode 100644
index 0000000..d8a6226
--- /dev/null
+++ b/test/test_netdb.c
@@ -0,0 +1,28 @@
+
+#include <usual/netdb.h>
+
+#include <usual/string.h>
+
+#include "test_common.h"
+
+
+static void test_gai(void *p)
+{
+ int res;
+ struct sigevent sev;
+
+ memset(&sev, 0, sizeof(sev));
+
+ sev.sigev_notify = SIGEV_THREAD;
+
+ res = getaddrinfo_a(GAI_NOWAIT, NULL, 0, &sev);
+
+ int_check(res, 0);
+end:;
+}
+
+struct testcase_t netdb_tests[] = {
+ { "getaddrinfo_a", test_gai },
+ END_OF_TESTCASES
+};
+
diff --git a/usual/netdb.c b/usual/netdb.c
new file mode 100644
index 0000000..1612681
--- /dev/null
+++ b/usual/netdb.c
@@ -0,0 +1,56 @@
+/*
+ * libusual - Utility library for C
+ *
+ * Copyright (c) 2010 Marko Kreen, Skype Technologies
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <usual/netdb.h>
+
+#ifndef HAVE_GETADDRINFO_A
+
+int getaddrinfo_a(int mode, struct gaicb *list[], int nitems, struct sigevent *sevp)
+{
+ struct gaicb *g;
+ int i, res;
+
+ if (nitems <= 0)
+ return 0;
+ if (mode != GAI_WAIT || mode != GAI_NOWAIT)
+ goto einval;
+
+ for (i = 0; i < nitems; i++) {
+ g = list[i];
+ res = getaddrinfo(g->ar_name, g->ar_service, g->ar_request, &g->ar_result);
+ g->_state = res;
+ }
+
+ if (!sevp || sevp->sigev_notify == SIGEV_NONE)
+ return 0;
+
+ if (sevp->sigev_notify == SIGEV_SIGNAL) {
+ raise(sevp->sigev_signo);
+ } else if (sevp->sigev_notify == SIGEV_THREAD) {
+ sigval_t sv;
+ sevp->sigev_notify_function(sv);
+ } else
+ goto einval;
+ return 0;
+einval:
+ errno = EINVAL;
+ return EAI_SYSTEM;
+}
+
+#endif
+
diff --git a/usual/netdb.h b/usual/netdb.h
new file mode 100644
index 0000000..da22c5f
--- /dev/null
+++ b/usual/netdb.h
@@ -0,0 +1,73 @@
+/*
+ * libusual - Utility library for C
+ *
+ * Copyright (c) 2010 Marko Kreen, Skype Technologies
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/** @file
+ *
+ * DNS lookup.
+ */
+
+#ifndef _USUAL_NETDB_H_
+#define _USUAL_NETDB_H_
+
+#include <usual/signal.h>
+
+#include <netdb.h>
+
+#ifndef HAVE_GETADDRINFO_A
+
+/** Async execution */
+#ifndef GAI_WAIT
+#define GAI_WAIT 0
+#endif
+
+/** Synchronous execution */
+#ifndef GAI_NOWAIT
+#define GAI_NOWAIT 1
+#endif
+
+/* avoid name conflicts */
+#define gaicb usual_gaicb
+#define getaddrinfo_a(a,b,c,d) usual_getaddrinfo_a(a,b,c,d)
+
+/**
+ * Request data for getaddrinfo_a().
+ *
+ * Fields correspond to getaddrinfo() parameters.
+ */
+struct gaicb {
+ /** node name */
+ const char *ar_name;
+ /** service name */
+ const char *ar_service;
+ /** hints */
+ const struct addrinfo *ar_request;
+ /** result */
+ struct addrinfo *ar_result;
+ /* internal state */
+ int _state;
+};
+
+/**
+ * Compat: Async DNS lookup.
+ */
+int getaddrinfo_a(int mode, struct gaicb *list[], int nitems, struct sigevent *sevp);
+
+#endif /* HAVE_GETADDRINFO_A */
+
+#endif /* _USUAL_NETDB_H_ */
+