[PATCH 1/2] coio: make hints in coio_getaddrinfo optional

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri May 17 18:21:23 MSK 2019


According to the standard by Open Group, getaddrinfo() hints
argument is optional - it can be NULL. When it is NULL, hints
is assumed to have 0 in ai_flags, ai_socktype, and ai_protocol;
AF_UNSPEC in ai_family.

See The Open Group Base Specifications.
---
 src/lib/core/coio_task.c | 12 ++++++++----
 test/unit/coio.cc        | 18 ++++++++++++++++++
 test/unit/coio.result    |  4 ++++
 3 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/lib/core/coio_task.c b/src/lib/core/coio_task.c
index 61f376203..d8095eef3 100644
--- a/src/lib/core/coio_task.c
+++ b/src/lib/core/coio_task.c
@@ -378,12 +378,16 @@ coio_getaddrinfo(const char *host, const char *port,
 	 *
 	 * Based on the workaround in https://bugs.python.org/issue17269
 	 */
+	if (hints != NULL) {
 #if defined(__APPLE__) && defined(AI_NUMERICSERV)
-	if (hints && (hints->ai_flags & AI_NUMERICSERV) &&
-	    (port == NULL || (port[0]=='0' && port[1]=='\0'))) port = "00";
+		if ((hints->ai_flags & AI_NUMERICSERV) != 0 &&
+		    (port == NULL || (port[0]=='0' && port[1]=='\0')))
+			port = "00";
 #endif
-	/* Fill hinting information for use by connect(2) or bind(2). */
-	memcpy(&task->hints, hints, sizeof(task->hints));
+		memcpy(&task->hints, hints, sizeof(task->hints));
+	} else {
+		task->hints.ai_family = AF_UNSPEC;
+	}
 	/* make no difference between empty string and NULL for host */
 	if (host != NULL && *host) {
 		task->host = strdup(host);
diff --git a/test/unit/coio.cc b/test/unit/coio.cc
index d1e744508..a2439bf46 100644
--- a/test/unit/coio.cc
+++ b/test/unit/coio.cc
@@ -68,6 +68,22 @@ test_call_f(va_list ap)
 	return res;
 }
 
+static void
+test_getaddrinfo(void)
+{
+	header();
+	plan(1);
+	const char *host = "127.0.0.1";
+	const char *port = "3333";
+	struct addrinfo *i;
+	/* NULL hints should work. It is a standard. */
+	int rc = coio_getaddrinfo(host, port, NULL, &i, 1);
+	is(rc, 0, "getaddringo");
+	freeaddrinfo(i);
+	check_plan();
+	footer();
+}
+
 static int
 main_f(va_list ap)
 {
@@ -86,6 +102,8 @@ main_f(va_list ap)
 	fiber_cancel(call_fiber);
 	fiber_join(call_fiber);
 
+	test_getaddrinfo();
+
 	ev_break(loop(), EVBREAK_ALL);
 	return 0;
 }
diff --git a/test/unit/coio.result b/test/unit/coio.result
index 7d7477979..0373530b0 100644
--- a/test/unit/coio.result
+++ b/test/unit/coio.result
@@ -6,3 +6,7 @@
 	*** test_call_f ***
 # call done with res 0
 	*** test_call_f: done ***
+	*** test_getaddrinfo ***
+1..1
+ok 1 - getaddringo
+	*** test_getaddrinfo: done ***
-- 
2.20.1 (Apple Git-117)




More information about the Tarantool-patches mailing list