Skip to contentSkip to conversation

Create separate .c file for include/tst_net.h

PATCH New Martin Doucha 2 participants 4 messages Mar 20, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/3 Create separate .c file for include/tst_net.h

---

Changes since v1:
- new patch

Changes since v2:
- Also separate print_svar*()
- Added tst_ prefix to functions separated into tst_net.c

Changes since v3:
- Split off tst_private.h from tst_net.h
- Make some helper functions in tst_net.c static

Changes since v4:
- Licence and #include cleanup

 include/tst_net.h                    | 139 +--------------------------
 include/tst_private.h                |  31 ++++++
 lib/tst_net.c                        | 135 ++++++++++++++++++++++++++
 testcases/lib/tst_net_iface_prefix.c |  10 +-
 testcases/lib/tst_net_ip_prefix.c    |  11 ++-
 testcases/lib/tst_net_vars.c         |  65 +++++++------
 6 files changed, 213 insertions(+), 178 deletions(-)
 create mode 100644 include/tst_private.h
 create mode 100644 lib/tst_net.c
include/tst_net.h +2 -137
index e1edc7200..e2100f641 100644
@@ -4,141 +4,6 @@
  */
 
 #include <arpa/inet.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
-#define MAX_IPV4_PREFIX 32
-#define MAX_IPV6_PREFIX 128
-
-#define tst_res_comment(...) { \
-	fprintf(stderr, "# "); \
-	tst_res(__VA_ARGS__); } \
-
-
-#define tst_brk_comment(...) { \
-	fprintf(stderr, "# "); \
-	tst_brk(TCONF, __VA_ARGS__); } \
-
-static inline void print_svar(const char *name, const char *val)
-{
-	if (name && val)
-		printf("export %s=\"%s\"\n", name, val);
-}
-
-static inline void print_svar_change(const char *name, const char *val)
-{
-	if (name && val)
-		printf("export %s=\"${%s:-%s}\"\n", name, name, val);
-}
-
-/*
- * Function bit_count is from ipcalc project, ipcalc.c.
- */
-static inline int bit_count(uint32_t i)
-{
-	int c = 0;
-	unsigned int seen_one = 0;
-
-	while (i > 0) {
-		if (i & 1) {
-			seen_one = 1;
-			c++;
-		} else {
-			if (seen_one)
-				return -1;
-		}
-		i >>= 1;
-	}
-
-	return c;
-}
-
-/*
- * Function mask2prefix is from ipcalc project, ipcalc.c.
- */
-static inline int mask2prefix(struct in_addr mask)
-{
-	return bit_count(ntohl(mask.s_addr));
-}
-
-/*
- * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
- */
-static inline int ipv4_mask_to_int(const char *prefix)
-{
-	int ret;
-	struct in_addr in;
-
-	ret = inet_pton(AF_INET, prefix, &in);
-	if (ret == 0)
-		return -1;
-
-	return mask2prefix(in);
-}
-
-/*
- * Function safe_atoi is from ipcalc project, ipcalc.c.
- */
-static inline int safe_atoi(const char *s, int *ret_i)
-{
-	char *x = NULL;
-	long l;
-
-	errno = 0;
-	l = strtol(s, &x, 0);
-
-	if (!x || x == s || *x || errno)
-		return errno > 0 ? -errno : -EINVAL;
-
-	if ((long)(int)l != l)
-		return -ERANGE;
-
-	*ret_i = (int)l;
-
-	return 0;
-}
-
-/*
- * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
- */
-static inline int get_prefix(const char *ip_str, int is_ipv6)
-{
-	char *prefix_str = NULL;
-	int prefix = -1, r;
-
-	prefix_str = strchr(ip_str, '/');
-	if (!prefix_str)
-		return -1;
-
-	*(prefix_str++) = '\0';
-
-	if (!is_ipv6 && strchr(prefix_str, '.'))
-		prefix = ipv4_mask_to_int(prefix_str);
-	else {
-		r = safe_atoi(prefix_str, &prefix);
-		if (r != 0)
-			tst_brk_comment("conversion error: '%s' is not integer",
-					prefix_str);
-	}
-
-	if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
-		(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
-		tst_brk_comment("bad %s prefix: %s", is_ipv6 ?  "IPv6" : "IPv4",
-				prefix_str);
-
-	return prefix;
-}
-
-static inline void get_in_addr(const char *ip_str, struct in_addr *ip)
-{
-	if (inet_pton(AF_INET, ip_str, ip) <= 0)
-		tst_brk_comment("bad IPv4 address: '%s'", ip_str);
-}
-
-static inline void get_in6_addr(const char *ip_str, struct in6_addr *ip6)
-{
-	if (inet_pton(AF_INET6, ip_str, ip6) <= 0)
-		tst_brk_comment("bad IPv6 address: '%s'", ip_str);
-}
+void tst_get_in_addr(const char *ip_str, struct in_addr *ip);
+void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6);
include/tst_private.h new file +31 -0
new file mode 100644
index 000000000..00cd17fce
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2017-2019 Petr Vorel <[email protected]>
+ *
+ * Internal helper functions for the shell library. Do not use directly
+ * in test programs.
+ */
+
+#ifndef TST_PRIVATE_H_
+#define TST_PRIVATE_H_
+
+#include <stdio.h>
+
+#define MAX_IPV4_PREFIX 32
+#define MAX_IPV6_PREFIX 128
+
+#define tst_res_comment(...) { \
+	fprintf(stderr, "# "); \
+	tst_res(__VA_ARGS__); } \
+
+
+#define tst_brk_comment(...) { \
+	fprintf(stderr, "# "); \
+	tst_brk(TCONF, __VA_ARGS__); } \
+
+void tst_print_svar(const char *name, const char *val);
+void tst_print_svar_change(const char *name, const char *val);
+
+int tst_get_prefix(const char *ip_str, int is_ipv6);
+
+#endif
lib/tst_net.c new file +135 -0

135 lines not on this page · raw message

testcases/lib/tst_net_iface_prefix.c +6 -4
index a40a8edc2..4b024872d 100644
@@ -13,6 +13,7 @@
 #include "tst_test.h"
 
 #include "tst_net.h"
+#include "tst_private.h"
 
 static char *iface;
 static int prefix;
@@ -124,7 +125,7 @@ int main(int argc, char *argv[])
 
 	prefix_str = strchr(ip_str, '/');
 	if (prefix_str) {
-		prefix = get_prefix(ip_str, is_ipv6);
+		prefix = tst_get_prefix(ip_str, is_ipv6);
 		tst_res_comment(TINFO,
 			"IP address '%s' contains prefix %d, using it and don't search for iface.\n",
 			ip_str, prefix);
@@ -136,11 +137,12 @@ int main(int argc, char *argv[])
 
 	/* checks for validity of IP string */
 	if (is_ipv6)
-		get_in6_addr(ip_str, &ip6);
+		tst_get_in6_addr(ip_str, &ip6);
 	else
-		get_in_addr(ip_str, &ip);
+		tst_get_in_addr(ip_str, &ip);
 
-	print_svar_change(is_rhost ? "RHOST_IFACES" : "LHOST_IFACES", iface);
+	tst_print_svar_change(is_rhost ? "RHOST_IFACES" : "LHOST_IFACES",
+		iface);
 	if (is_ipv6)
 		print_ivar(is_rhost ? "IPV6_RPREFIX" : "IPV6_LPREFIX", prefix);
 	else
testcases/lib/tst_net_ip_prefix.c +6 -5
index 2ac06e724..59c70ba7d 100644
@@ -10,6 +10,7 @@
 #include "tst_test.h"
 
 #include "tst_net.h"
+#include "tst_private.h"
 
 #define DEFAULT_IPV4_PREFIX 24
 #define DEFAULT_IPV6_PREFIX 64
@@ -67,22 +68,22 @@ int main(int argc, char *argv[])
 	prefix_str = strchr(ip_str, '/');
 
 	if (prefix_str)
-		prefix = get_prefix(ip_str, is_ipv6);
+		prefix = tst_get_prefix(ip_str, is_ipv6);
 	else
 		prefix = is_ipv6 ? DEFAULT_IPV6_PREFIX : DEFAULT_IPV4_PREFIX;
 
 	/* checks for validity of IP string */
 	if (is_ipv6)
-		get_in6_addr(ip_str, &ip6);
+		tst_get_in6_addr(ip_str, &ip6);
 	else
-		get_in_addr(ip_str, &ip);
+		tst_get_in_addr(ip_str, &ip);
 
 	if (is_ipv6) {
 		print_ivar(is_rhost ? "IPV6_RPREFIX" : "IPV6_LPREFIX", prefix);
-		print_svar(is_rhost ? "IPV6_RHOST" : "IPV6_LHOST", ip_str);
+		tst_print_svar(is_rhost ? "IPV6_RHOST" : "IPV6_LHOST", ip_str);
 	} else {
 		print_ivar(is_rhost ? "IPV4_RPREFIX" : "IPV4_LPREFIX", prefix);
-		print_svar(is_rhost ? "IPV4_RHOST" : "IPV4_LHOST", ip_str);
+		tst_print_svar(is_rhost ? "IPV4_RHOST" : "IPV4_LHOST", ip_str);
 	}
 
 	exit(EXIT_SUCCESS);
testcases/lib/tst_net_vars.c +33 -32

139 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

PATCH v5 2/3 Add socket address initialization functions to tst_net library

---

Changes since v1:
- new patch

Changes since v2:
- Added tst_ prefix to the new functions

Changes since v3:
- Added #define protection against multiple #includes to tst_net.h

 include/tst_net.h | 21 ++++++++++++++
 lib/tst_net.c     | 71 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
include/tst_net.h +21 -0
index e2100f641..855f4fc13 100644
@@ -3,7 +3,28 @@
  * Copyright (c) 2017-2019 Petr Vorel <[email protected]>
  */
 
+#ifndef TST_NET_H_
+#define TST_NET_H_
+
 #include <arpa/inet.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
 
 void tst_get_in_addr(const char *ip_str, struct in_addr *ip);
 void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6);
+
+/*
+ * Find valid connection address for a given bound socket
+ */
+socklen_t tst_get_connect_address(int sock, struct sockaddr_storage *addr);
+
+/*
+ * Initialize AF_INET/AF_INET6 socket address structure with address and port
+ */
+void tst_init_sockaddr_inet(struct sockaddr_in *sa, const char *ip_str, uint16_t port);
+void tst_init_sockaddr_inet_bin(struct sockaddr_in *sa, uint32_t ip_val, uint16_t port);
+void tst_init_sockaddr_inet6(struct sockaddr_in6 *sa, const char *ip_str, uint16_t port);
+void tst_init_sockaddr_inet6_bin(struct sockaddr_in6 *sa, const struct in6_addr *ip_val, uint16_t port);
+
+#endif
lib/tst_net.c +71 -0

80 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

PATCH v5 3/3 Add connection tests for bind()

In reply to Martin Doucha

Add two new test programs to verify that bind() will open sockets for incoming connections. Both programs follow the same test scenario:
- Create and bind() a socket
- Wait for connection from peer thread
- Send request to peer thread
- Receive and verify response from peer thread

bind04 tests stream-oriented sockets (SOCK_STREAM and SOCK_SEQPACKET). bind05 tests datagram-oriented sockets (SOCK_DGRAM).

Both programs test the following socket types:
- AF_UNIX (pathname and abstract addresses)
- AF_INET (loopback)
- AF_INET6 (loopback)

---

Changes since v1:
- added UDPLITE and SCTP test cases
- common constants and test_case data structure moved to libbind.h

Changes since v2:
- Code style fixes
- Makefile fix

Changes since v4:
- Use SAFE_ACCEPT()

 runtest/syscalls                          |   2 +
 testcases/kernel/syscalls/bind/.gitignore |   2 +
 testcases/kernel/syscalls/bind/Makefile   |   2 +
 testcases/kernel/syscalls/bind/bind04.c   | 169 ++++++++++++++++++++
 testcases/kernel/syscalls/bind/bind05.c   | 186 ++++++++++++++++++++++
 testcases/kernel/syscalls/bind/libbind.h  |  29 ++++
 6 files changed, 390 insertions(+)
 create mode 100644 testcases/kernel/syscalls/bind/bind04.c
 create mode 100644 testcases/kernel/syscalls/bind/bind05.c
 create mode 100644 testcases/kernel/syscalls/bind/libbind.h
runtest/syscalls +2 -0
index 6f2dcd82a..354ec824d 100644
@@ -31,6 +31,8 @@ alarm07 alarm07
 bind01 bind01
 bind02 bind02
 bind03 bind03
+bind04 bind04
+bind05 bind05
 
 bpf_map01 bpf_map01
 bpf_prog01 bpf_prog01
testcases/kernel/syscalls/bind/.gitignore +2 -0
index 4ebea9ee7..e18ceea56 100644
@@ -1,3 +1,5 @@
 /bind01
 /bind02
 /bind03
+/bind04
+/bind05
testcases/kernel/syscalls/bind/Makefile +2 -0
index 044619fb8..fffa146ad 100644
@@ -5,4 +5,6 @@ top_srcdir		?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
 
+bind04 bind05:	CFLAGS		+= -pthread
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/bind/bind04.c new file +169 -0

169 lines not on this page · raw message

testcases/kernel/syscalls/bind/bind05.c new file +186 -0

186 lines not on this page · raw message

testcases/kernel/syscalls/bind/libbind.h new file +29 -0
new file mode 100644
index 000000000..e19758f1b
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *   Copyright (c) 2019 Martin Doucha <[email protected]>
+ */
+
+/*
+ * Common settings and data types for bind() connection tests
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+
+#define MAIN_SOCKET_FILE "test.sock"
+#define ABSTRACT_SOCKET_PATH "\0test.sock"
+#define PEER_SOCKET_FILE "peer.sock"
+#define IPV4_ADDRESS "127.0.0.1"
+#define IPV6_ADDRESS "::1"
+#define BUFFER_SIZE 128
+
+struct test_case {
+	int type, protocol;
+	struct sockaddr *address;
+	socklen_t addrlen;
+	const char *description;
+};
Signature · 1 line
2.25.1
#3 lore raw

PATCH v5 3/3 Add connection tests for bind()

Hi Martin,

merged whole patchset.
Thanks!

Kind regards,
Petr