netdb: compat getaddrinfo_a()
authorMarko Kreen <markokr@gmail.com>
Tue, 12 Oct 2010 10:50:15 +0000 (13:50 +0300)
committerMarko Kreen <markokr@gmail.com>
Tue, 12 Oct 2010 10:58:52 +0000 (13:58 +0300)
m4/usual.m4
test/Makefile
test/force_compat.sed
test/test_common.c
test/test_common.h
test/test_netdb.c [new file with mode: 0644]
usual/netdb.c [new file with mode: 0644]
usual/netdb.h [new file with mode: 0644]

index 6232d5c09562ebb484a209bcb130f6135ce3ca3a..627a7c2bec87fd4fb6854311c00d03aaad05d482 100644 (file)
@@ -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
index 869b066aeee98cfc17b054896c4f8106754ebcbf..22662c78505c21041bbd9823fef6e9cf1e235618 100644 (file)
@@ -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
 
index 61bac0fc0eaacf47bbbc12cf961787b8ddf19285..f49f6bb0003acaba1241bb419460534a48cdb740 100644 (file)
@@ -5,3 +5,4 @@
 /BASENAME/s,^,//,
 /DIRNAME/s,^,//,
 /REGCOMP/s,^,//,
+/GETADDRINFO_A/s,^,//,
index 4d48b21feaa7c35974967c05a404e97e2e71024f..c0be1bd24d4db8428b6a7053e1cabbe884c06817 100644 (file)
@@ -16,6 +16,7 @@ struct testgroup_t groups[] = {
        { "strpool/", strpool_tests },
        { "pgutil/", pgutil_tests },
        { "regex/", regex_tests },
+       { "netdb/", netdb_tests },
        END_OF_GROUPS
 };
 
index 12144db2cb387042615c23b74f52d875830b651f..8eae52221a64d5f2531b91f884ee188f46092b12 100644 (file)
@@ -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 (file)
index 0000000..d8a6226
--- /dev/null
@@ -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 (file)
index 0000000..1612681
--- /dev/null
@@ -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 (file)
index 0000000..da22c5f
--- /dev/null
@@ -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_ */
+