[rtems commit] libnetworking: Add minimal getnameinfo()

Sebastian Huber sebh at rtems.org
Tue Jun 28 13:19:24 UTC 2016


Module:    rtems
Branch:    master
Commit:    195d412d397a50383a0a2703022613d97cca2736
Changeset: http://git.rtems.org/rtems/commit/?id=195d412d397a50383a0a2703022613d97cca2736

Author:    Christian Mauderer <Christian.Mauderer at embedded-brains.de>
Date:      Mon May  2 14:49:33 2016 +0200

libnetworking: Add minimal getnameinfo()

This implementation just falls back to giving a string representation of
the IP.  It supports IPv4 only.

Add test for getnameinfo().

---

 cpukit/libnetworking/Makefile.am                  |   2 +-
 cpukit/libnetworking/libc/getnameinfo.c           |  61 ++++++++
 testsuites/libtests/Makefile.am                   |   1 +
 testsuites/libtests/configure.ac                  |   1 +
 testsuites/libtests/networking01/Makefile.am      |  22 +++
 testsuites/libtests/networking01/init.c           | 161 ++++++++++++++++++++++
 testsuites/libtests/networking01/networking01.doc |  26 ++++
 testsuites/libtests/networking01/networking01.scn |   9 ++
 8 files changed, 282 insertions(+), 1 deletion(-)

diff --git a/cpukit/libnetworking/Makefile.am b/cpukit/libnetworking/Makefile.am
index c8c5f50..57ceee9 100644
--- a/cpukit/libnetworking/Makefile.am
+++ b/cpukit/libnetworking/Makefile.am
@@ -214,7 +214,7 @@ include_HEADERS += ifaddrs.h
 libc_a_SOURCES = libc/base64.c \
     libc/gethostbydns.c libc/gethostbyht.c libc/gethostbynis.c \
     libc/gethostnamadr.c libc/getnetbydns.c libc/getnetbyht.c \
-    libc/getnetbynis.c libc/getnetnamadr.c libc/getproto.c \
+    libc/getnetbynis.c libc/getnetnamadr.c libc/getnameinfo.c libc/getproto.c \
     libc/getprotoent.c libc/getprotoname.c libc/getservbyname.c \
     libc/getservbyport.c libc/getservent.c libc/herror.c libc/inet_addr.c \
     libc/inet_lnaof.c libc/inet_makeaddr.c libc/inet_netof.c \
diff --git a/cpukit/libnetworking/libc/getnameinfo.c b/cpukit/libnetworking/libc/getnameinfo.c
new file mode 100644
index 0000000..415f150
--- /dev/null
+++ b/cpukit/libnetworking/libc/getnameinfo.c
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Dornierstr. 4
+ *  82178 Puchheim
+ *  Germany
+ *  <rtems at embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+
+int
+getnameinfo(const struct sockaddr *sa, socklen_t salen, char *node,
+    size_t nodelen, char *service, size_t servicelen, int flags)
+{
+	int af;
+	const struct sockaddr_in *sa_in = (const struct sockaddr_in *)sa;
+
+	(void) salen;
+
+	af = sa->sa_family;
+	if (af != AF_INET) {
+		return EAI_FAMILY;
+	}
+
+	if ((flags & NI_NAMEREQD) != 0) {
+		return EAI_NONAME;
+	}
+
+	/* FIXME: This return just the address value. Try resolving instead. */
+	if (node != NULL && nodelen > 0) {
+		const void *addr = &sa_in->sin_addr;
+
+		if(inet_ntop(af, addr, node, nodelen) == NULL) {
+			return EAI_FAIL;
+		}
+	}
+
+	if (service != NULL && servicelen > 0) {
+		in_port_t port = sa_in->sin_port;
+		int rv;
+
+		rv = snprintf(service, servicelen, "%u", port);
+		if (rv <= 0) {
+			return EAI_FAIL;
+		} else if ((unsigned)rv >= servicelen) {
+			return EAI_OVERFLOW;
+		}
+	}
+
+	return 0;
+}
diff --git a/testsuites/libtests/Makefile.am b/testsuites/libtests/Makefile.am
index d775c77..4eae14e 100644
--- a/testsuites/libtests/Makefile.am
+++ b/testsuites/libtests/Makefile.am
@@ -41,6 +41,7 @@ if HAS_POSIX
 _SUBDIRS += mghttpd01
 endif
 _SUBDIRS += ftp01
+_SUBDIRS += networking01
 _SUBDIRS += syscall01
 endif
 
diff --git a/testsuites/libtests/configure.ac b/testsuites/libtests/configure.ac
index 5ac2dfd..a136749 100644
--- a/testsuites/libtests/configure.ac
+++ b/testsuites/libtests/configure.ac
@@ -79,6 +79,7 @@ AM_CONDITIONAL(DLTESTS,[test x"$TEST_LIBDL" = x"yes"])
 
 # Explicitly list all Makefiles here
 AC_CONFIG_FILES([Makefile
+networking01/Makefile
 libfdt01/Makefile
 defaultconfig01/Makefile
 pwdgrp02/Makefile
diff --git a/testsuites/libtests/networking01/Makefile.am b/testsuites/libtests/networking01/Makefile.am
new file mode 100644
index 0000000..0f1fb70
--- /dev/null
+++ b/testsuites/libtests/networking01/Makefile.am
@@ -0,0 +1,22 @@
+
+rtems_tests_PROGRAMS = networking01
+networking01_SOURCES = init.c
+
+dist_rtems_tests_DATA = networking01.scn
+dist_rtems_tests_DATA += networking01.doc
+
+include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP at .cfg
+include $(top_srcdir)/../automake/compile.am
+include $(top_srcdir)/../automake/leaf.am
+
+AM_CPPFLAGS += -I$(top_srcdir)/../support/include
+AM_CPPFLAGS += -I$(top_srcdir)/termios04
+
+LINK_OBJS = $(networking01_OBJECTS)
+LINK_LIBS = $(networking01_LDLIBS)
+
+networking01$(EXEEXT): $(networking01_OBJECTS) $(networking01_DEPENDENCIES)
+	@rm -f networking01$(EXEEXT)
+	$(make-exe)
+
+include $(top_srcdir)/../automake/local.am
diff --git a/testsuites/libtests/networking01/init.c b/testsuites/libtests/networking01/init.c
new file mode 100644
index 0000000..800d785
--- /dev/null
+++ b/testsuites/libtests/networking01/init.c
@@ -0,0 +1,161 @@
+/*
+ * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Dornierstr. 4
+ *  82178 Puchheim
+ *  Germany
+ *  <rtems at embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/socket.h>
+#include <netdb.h>
+#include <string.h>
+#include <netinet/in.h>
+
+#include <rtems.h>
+#include <tmacros.h>
+
+const char rtems_test_name[] = "NETWORKING 1";
+
+/* forward declarations to avoid warnings */
+static rtems_task Init(rtems_task_argument argument);
+
+static void fill_sa(struct sockaddr *sa, sa_family_t family)
+{
+  memset(sa, 0, sizeof(struct sockaddr));
+  sa->sa_len = sizeof(struct sockaddr);
+  sa->sa_family = family;
+}
+
+static void fill_sa_in(struct sockaddr_in *sa_in,
+  in_addr_t addr, in_port_t port)
+{
+  fill_sa((struct sockaddr *)sa_in, AF_INET);
+  sa_in->sin_port = port;
+  sa_in->sin_addr.s_addr = addr;
+}
+
+static void test_getnameinfo(
+  const struct sockaddr *sa,
+  int flags,
+  bool ask_node,
+  bool ask_service,
+  int expected_returnvalue,
+  const char *expected_node,
+  const char *expected_service
+)
+{
+  char node[] = "255.255.255.255";
+  char service[] = "65535";
+  socklen_t salen = sa->sa_len;
+  int rv;
+
+  char *node_p = node;
+  char *service_p = service;
+  size_t node_l = sizeof(node);
+  size_t service_l = sizeof(service);
+
+  if(ask_node == false) {
+    node_p = NULL;
+    node_l = 0;
+  }
+
+  if(ask_service == false) {
+    service_p = NULL;
+    service_l = 0;
+  }
+
+  rv = getnameinfo(sa, salen, node_p, node_l, service_p, service_l, flags);
+  rtems_test_assert(rv == expected_returnvalue);
+
+  if(expected_node != NULL) {
+    rtems_test_assert(strcmp(expected_node, node) == 0);
+  }
+
+  if(expected_service != NULL) {
+    rtems_test_assert(strcmp(expected_service, service) == 0);
+  }
+}
+
+static void test(void)
+{
+  struct sockaddr sa;
+  struct sockaddr_in sa_in;
+  struct sockaddr *sa_in_p = (struct sockaddr *) &sa_in;
+
+  const in_addr_t ip1_num = 0x7F000001u;
+  const char ip1_string[] = "127.0.0.1";
+
+  const in_addr_t ip2_num = 0xC0A86464u;
+  const char ip2_string[] = "192.168.100.100";
+
+  const in_port_t port1_num = 7u;
+  const char port1_string[] = "7";
+
+  const in_port_t port2_num = 65534u;
+  const char port2_string[] = "65534";
+
+
+  printk("Try AF_INET6\n");
+  fill_sa(&sa, AF_INET6);
+  test_getnameinfo(&sa, 0, true, true, EAI_FAMILY, NULL, NULL);
+
+  printk("force node name\n");
+  fill_sa_in(&sa_in, ip1_num, port1_num);
+  test_getnameinfo(sa_in_p, NI_NAMEREQD, true, true, EAI_NONAME, NULL, NULL);
+
+  printk("force service name\n");
+  fill_sa_in(&sa_in, ip1_num, port1_num);
+  test_getnameinfo(sa_in_p, NI_NAMEREQD, true, true, EAI_NONAME, NULL, NULL);
+
+  printk("get node only\n");
+  fill_sa_in(&sa_in, ip1_num, port1_num);
+  test_getnameinfo(sa_in_p, 0, true, false, 0, ip1_string, NULL);
+
+  printk("get service only\n");
+  fill_sa_in(&sa_in, ip1_num, port1_num);
+  test_getnameinfo(sa_in_p, 0, false, true, 0, NULL, port1_string);
+
+  printk("get node and service\n");
+  fill_sa_in(&sa_in, ip1_num, port1_num);
+  test_getnameinfo(sa_in_p, 0, true, true, 0, ip1_string, port1_string);
+
+  printk("get node and service with maximum number of characters for IP\n");
+  fill_sa_in(&sa_in, ip2_num, port2_num);
+  test_getnameinfo(sa_in_p, 0, true, true, 0, ip2_string, port2_string);
+}
+
+static rtems_task Init(rtems_task_argument argument)
+{
+  TEST_BEGIN();
+  test();
+  TEST_END();
+
+  rtems_test_exit(0);
+}
+
+#define CONFIGURE_INIT
+
+#define CONFIGURE_MICROSECONDS_PER_TICK 10000
+
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+
+#define CONFIGURE_MAXIMUM_DRIVERS 2
+
+#define CONFIGURE_MAXIMUM_TASKS (1)
+
+#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
+
+#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
+
+#include <rtems/confdefs.h>
diff --git a/testsuites/libtests/networking01/networking01.doc b/testsuites/libtests/networking01/networking01.doc
new file mode 100644
index 0000000..949a32c
--- /dev/null
+++ b/testsuites/libtests/networking01/networking01.doc
@@ -0,0 +1,26 @@
+#
+# Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
+#
+#  embedded brains GmbH
+#  Dornierstr. 4
+#  82178 Puchheim
+#  Germany
+#  <rtems at embedded-brains.de>
+#
+# The license and distribution terms for this file may be
+# found in the file LICENSE in this distribution or at
+# http://www.rtems.org/license/LICENSE.
+
+This file describes the directives and concepts tested by this test set.
+
+test set name: networking
+
+directives:
+
++ getnameinfo()
+
+concepts:
+
++ Try to get some valid and invalid name infos.
+
+NOTE: This test works without a network connection.
diff --git a/testsuites/libtests/networking01/networking01.scn b/testsuites/libtests/networking01/networking01.scn
new file mode 100644
index 0000000..75e8457
--- /dev/null
+++ b/testsuites/libtests/networking01/networking01.scn
@@ -0,0 +1,9 @@
+*** BEGIN OF TEST LIBNETWORKING 1 ***
+Try AF_INET6
+force node name
+force service name
+get node only
+get service only
+get node and service
+get node and service with maximum number of characters for IP
+*** END OF TEST LIBNETWORKING 1 ***



More information about the vc mailing list