diff mbox series

[V2,net-next] selftests/net: fix bugs in address and port initialization

Message ID 1514241784-115652-1-git-send-email-sowmini.varadhan@oracle.com
State Accepted, archived
Delegated to: David Miller
Headers show
Series [V2,net-next] selftests/net: fix bugs in address and port initialization | expand

Commit Message

Sowmini Varadhan Dec. 25, 2017, 10:43 p.m. UTC
Address/port initialization should work correctly regardless
of the order in which command line arguments are supplied,
E.g, cfg_port should be used to connect to the remote host
even if it is processed after -D, src/dst address initialization
should not require that [-4|-6] be specified before
the -S or -D args, receiver should be able to bind to *.<cfg_port>

Achieve this by making sure that the address/port structures
are initialized after all command line options are parsed.

Store cfg_port in host-byte order, and use htons()
to set up the sin_port/sin6_port before bind/connect,
so that the network system calls get the correct values
in network-byte order.

Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com>
---
v2: fix fragility in address initialization as well.

 tools/testing/selftests/net/msg_zerocopy.c |   21 +++++++++++++++------
 1 files changed, 15 insertions(+), 6 deletions(-)

Comments

Willem de Bruijn Dec. 26, 2017, 7:35 p.m. UTC | #1
On Mon, Dec 25, 2017 at 5:43 PM, Sowmini Varadhan
<sowmini.varadhan@oracle.com> wrote:
> Address/port initialization should work correctly regardless
> of the order in which command line arguments are supplied,
> E.g, cfg_port should be used to connect to the remote host
> even if it is processed after -D, src/dst address initialization
> should not require that [-4|-6] be specified before
> the -S or -D args, receiver should be able to bind to *.<cfg_port>
>
> Achieve this by making sure that the address/port structures
> are initialized after all command line options are parsed.
>
> Store cfg_port in host-byte order, and use htons()
> to set up the sin_port/sin6_port before bind/connect,
> so that the network system calls get the correct values
> in network-byte order.
>
> Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com>

Acked-by: Willem de Bruijn <willemb@google.com>
David Miller Jan. 2, 2018, 6:27 p.m. UTC | #2
From: Sowmini Varadhan <sowmini.varadhan@oracle.com>
Date: Mon, 25 Dec 2017 14:43:04 -0800

> Address/port initialization should work correctly regardless
> of the order in which command line arguments are supplied,
> E.g, cfg_port should be used to connect to the remote host
> even if it is processed after -D, src/dst address initialization
> should not require that [-4|-6] be specified before
> the -S or -D args, receiver should be able to bind to *.<cfg_port>
> 
> Achieve this by making sure that the address/port structures
> are initialized after all command line options are parsed.
> 
> Store cfg_port in host-byte order, and use htons()
> to set up the sin_port/sin6_port before bind/connect,
> so that the network system calls get the correct values
> in network-byte order.
> 
> Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com>

Applied, thank you.
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/msg_zerocopy.c b/tools/testing/selftests/net/msg_zerocopy.c
index 3ab6ec4..e11fe84 100644
--- a/tools/testing/selftests/net/msg_zerocopy.c
+++ b/tools/testing/selftests/net/msg_zerocopy.c
@@ -259,22 +259,28 @@  static int setup_ip6h(struct ipv6hdr *ip6h, uint16_t payload_len)
 	return sizeof(*ip6h);
 }
 
-static void setup_sockaddr(int domain, const char *str_addr, void *sockaddr)
+
+static void setup_sockaddr(int domain, const char *str_addr,
+			   struct sockaddr_storage *sockaddr)
 {
 	struct sockaddr_in6 *addr6 = (void *) sockaddr;
 	struct sockaddr_in *addr4 = (void *) sockaddr;
 
 	switch (domain) {
 	case PF_INET:
+		memset(addr4, 0, sizeof(*addr4));
 		addr4->sin_family = AF_INET;
 		addr4->sin_port = htons(cfg_port);
-		if (inet_pton(AF_INET, str_addr, &(addr4->sin_addr)) != 1)
+		if (str_addr &&
+		    inet_pton(AF_INET, str_addr, &(addr4->sin_addr)) != 1)
 			error(1, 0, "ipv4 parse error: %s", str_addr);
 		break;
 	case PF_INET6:
+		memset(addr6, 0, sizeof(*addr6));
 		addr6->sin6_family = AF_INET6;
 		addr6->sin6_port = htons(cfg_port);
-		if (inet_pton(AF_INET6, str_addr, &(addr6->sin6_addr)) != 1)
+		if (str_addr &&
+		    inet_pton(AF_INET6, str_addr, &(addr6->sin6_addr)) != 1)
 			error(1, 0, "ipv6 parse error: %s", str_addr);
 		break;
 	default:
@@ -603,6 +609,7 @@  static void parse_opts(int argc, char **argv)
 				    sizeof(struct tcphdr) -
 				    40 /* max tcp options */;
 	int c;
+	char *daddr = NULL, *saddr = NULL;
 
 	cfg_payload_len = max_payload_len;
 
@@ -627,7 +634,7 @@  static void parse_opts(int argc, char **argv)
 			cfg_cpu = strtol(optarg, NULL, 0);
 			break;
 		case 'D':
-			setup_sockaddr(cfg_family, optarg, &cfg_dst_addr);
+			daddr = optarg;
 			break;
 		case 'i':
 			cfg_ifindex = if_nametoindex(optarg);
@@ -638,7 +645,7 @@  static void parse_opts(int argc, char **argv)
 			cfg_cork_mixed = true;
 			break;
 		case 'p':
-			cfg_port = htons(strtoul(optarg, NULL, 0));
+			cfg_port = strtoul(optarg, NULL, 0);
 			break;
 		case 'r':
 			cfg_rx = true;
@@ -647,7 +654,7 @@  static void parse_opts(int argc, char **argv)
 			cfg_payload_len = strtoul(optarg, NULL, 0);
 			break;
 		case 'S':
-			setup_sockaddr(cfg_family, optarg, &cfg_src_addr);
+			saddr = optarg;
 			break;
 		case 't':
 			cfg_runtime_ms = 200 + strtoul(optarg, NULL, 10) * 1000;
@@ -660,6 +667,8 @@  static void parse_opts(int argc, char **argv)
 			break;
 		}
 	}
+	setup_sockaddr(cfg_family, daddr, &cfg_dst_addr);
+	setup_sockaddr(cfg_family, saddr, &cfg_src_addr);
 
 	if (cfg_payload_len > max_payload_len)
 		error(1, 0, "-s: payload exceeds max (%d)", max_payload_len);