netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2-next] ss: always prefer family as part of host condition to default family
@ 2021-01-31  8:17 Thayne McCombs
  2021-02-02  3:04 ` David Ahern
  0 siblings, 1 reply; 4+ messages in thread
From: Thayne McCombs @ 2021-01-31  8:17 UTC (permalink / raw)
  To: netdev; +Cc: Thayne McCombs

ss accepts an address family both with the -f option and as part of a
host condition. However, if the family in the host condition is
different than the the last -f option, then which family is actually
used depends on the order that different families are checked.

This changes parse_hostcond to check all family prefixes before parsing
the rest of the address, so that the host condition's family always has
a higher priority than the "preferred" family.

Signed-off-by: Thayne McCombs <astrothayne@gmail.com>
---
 misc/ss.c | 50 ++++++++++++++++++++++++--------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index 0593627b..2a5e056a 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -2119,24 +2119,39 @@ void *parse_hostcond(char *addr, bool is_port)
 	int fam = preferred_family;
 	struct filter *f = &current_filter;
 
-	if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
+    if (strncmp(addr, "unix:", 5) == 0) {
+        fam = AF_UNIX;
+        addr += 5;
+    } else if (strncmp(addr, "link:", 5) == 0) {
+        fam = AF_PACKET;
+        addr += 5;
+    } else if (strncmp(addr, "netlink:", 8) == 0) {
+        fam = AF_NETLINK;
+        addr += 8;
+    } else if (strncmp(addr, "vsock:", 6) == 0) {
+        fam = AF_VSOCK;
+        addr += 6;
+    } else if (strncmp(addr, "inet:", 5) == 0) {
+        fam = AF_INET;
+        addr += 5;
+    } else if (strncmp(addr, "inet6:", 6) == 0) {
+        fam = AF_INET6;
+        addr += 6;
+    }
+
+	if (fam == AF_UNIX) {
 		char *p;
 
 		a.addr.family = AF_UNIX;
-		if (strncmp(addr, "unix:", 5) == 0)
-			addr += 5;
 		p = strdup(addr);
 		a.addr.bitlen = 8*strlen(p);
 		memcpy(a.addr.data, &p, sizeof(p));
-		fam = AF_UNIX;
 		goto out;
 	}
 
-	if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
+	if (fam == AF_PACKET) {
 		a.addr.family = AF_PACKET;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "link:", 5) == 0)
-			addr += 5;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2155,15 +2170,12 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 			a.addr.data[0] = ntohs(tmp);
 		}
-		fam = AF_PACKET;
 		goto out;
 	}
 
-	if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
+	if (fam == AF_NETLINK) {
 		a.addr.family = AF_NETLINK;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "netlink:", 8) == 0)
-			addr += 8;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2181,16 +2193,13 @@ void *parse_hostcond(char *addr, bool is_port)
 			if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
 				return NULL;
 		}
-		fam = AF_NETLINK;
 		goto out;
 	}
 
-	if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) {
+	if (fam == AF_VSOCK) {
 		__u32 cid = ~(__u32)0;
 
 		a.addr.family = AF_VSOCK;
-		if (strncmp(addr, "vsock:", 6) == 0)
-			addr += 6;
 
 		if (is_port)
 			port = addr;
@@ -2212,20 +2221,9 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 		}
 		vsock_set_inet_prefix(&a.addr, cid);
-		fam = AF_VSOCK;
 		goto out;
 	}
 
-	if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
-		fam = AF_INET;
-		if (!strncmp(addr, "inet:", 5))
-			addr += 5;
-	} else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
-		fam = AF_INET6;
-		if (!strncmp(addr, "inet6:", 6))
-			addr += 6;
-	}
-
 	/* URL-like literal [] */
 	if (addr[0] == '[') {
 		addr++;
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH iproute2-next] ss: always prefer family as part of host condition to default family
  2021-01-31  8:17 [PATCH iproute2-next] ss: always prefer family as part of host condition to default family Thayne McCombs
@ 2021-02-02  3:04 ` David Ahern
  2021-02-02  3:32   ` [PATCH ip-route2-next v2] " Thayne McCombs
  0 siblings, 1 reply; 4+ messages in thread
From: David Ahern @ 2021-02-02  3:04 UTC (permalink / raw)
  To: Thayne McCombs, netdev

On 1/31/21 1:17 AM, Thayne McCombs wrote:
> diff --git a/misc/ss.c b/misc/ss.c
> index 0593627b..2a5e056a 100644
> --- a/misc/ss.c
> +++ b/misc/ss.c
> @@ -2119,24 +2119,39 @@ void *parse_hostcond(char *addr, bool is_port)
>  	int fam = preferred_family;
>  	struct filter *f = &current_filter;
>  
> -	if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
> +    if (strncmp(addr, "unix:", 5) == 0) {
> +        fam = AF_UNIX;
> +        addr += 5;
> +    } else if (strncmp(addr, "link:", 5) == 0) {
> +        fam = AF_PACKET;
> +        addr += 5;
> +    } else if (strncmp(addr, "netlink:", 8) == 0) {
> +        fam = AF_NETLINK;
> +        addr += 8;
> +    } else if (strncmp(addr, "vsock:", 6) == 0) {
> +        fam = AF_VSOCK;
> +        addr += 6;
> +    } else if (strncmp(addr, "inet:", 5) == 0) {
> +        fam = AF_INET;
> +        addr += 5;
> +    } else if (strncmp(addr, "inet6:", 6) == 0) {
> +        fam = AF_INET6;
> +        addr += 6;
> +    }
> +
> +	if (fam == AF_UNIX) {
>  		char *p;
>  

Looks fine to me, but you need to fix the coding style -- tabs, not spaces.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH ip-route2-next v2] ss: always prefer family as part of host condition to default family
  2021-02-02  3:04 ` David Ahern
@ 2021-02-02  3:32   ` Thayne McCombs
  2021-02-05  4:57     ` David Ahern
  0 siblings, 1 reply; 4+ messages in thread
From: Thayne McCombs @ 2021-02-02  3:32 UTC (permalink / raw)
  To: dsahern; +Cc: netdev, Thayne McCombs

I've fixed the indentation to use tabs instead of spaces. 

-- >8 --
ss accepts an address family both with the -f option and as part of a
host condition. However, if the family in the host condition is
different than the the last -f option, then which family is actually
used depends on the order that different families are checked.

This changes parse_hostcond to check all family prefixes before parsing
the rest of the address, so that the host condition's family always has
a higher priority than the "preferred" family.

Signed-off-by: Thayne McCombs <astrothayne@gmail.com>
---
 misc/ss.c | 50 ++++++++++++++++++++++++--------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index 0593627b..aefa1c2f 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -2119,24 +2119,39 @@ void *parse_hostcond(char *addr, bool is_port)
 	int fam = preferred_family;
 	struct filter *f = &current_filter;
 
-	if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
+	if (strncmp(addr, "unix:", 5) == 0) {
+		fam = AF_UNIX;
+		addr += 5;
+	} else if (strncmp(addr, "link:", 5) == 0) {
+		fam = AF_PACKET;
+		addr += 5;
+	} else if (strncmp(addr, "netlink:", 8) == 0) {
+		fam = AF_NETLINK;
+		addr += 8;
+	} else if (strncmp(addr, "vsock:", 6) == 0) {
+		fam = AF_VSOCK;
+		addr += 6;
+	} else if (strncmp(addr, "inet:", 5) == 0) {
+		fam = AF_INET;
+		addr += 5;
+	} else if (strncmp(addr, "inet6:", 6) == 0) {
+		fam = AF_INET6;
+		addr += 6;
+	}
+
+	if (fam == AF_UNIX) {
 		char *p;
 
 		a.addr.family = AF_UNIX;
-		if (strncmp(addr, "unix:", 5) == 0)
-			addr += 5;
 		p = strdup(addr);
 		a.addr.bitlen = 8*strlen(p);
 		memcpy(a.addr.data, &p, sizeof(p));
-		fam = AF_UNIX;
 		goto out;
 	}
 
-	if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
+	if (fam == AF_PACKET) {
 		a.addr.family = AF_PACKET;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "link:", 5) == 0)
-			addr += 5;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2155,15 +2170,12 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 			a.addr.data[0] = ntohs(tmp);
 		}
-		fam = AF_PACKET;
 		goto out;
 	}
 
-	if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
+	if (fam == AF_NETLINK) {
 		a.addr.family = AF_NETLINK;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "netlink:", 8) == 0)
-			addr += 8;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2181,16 +2193,13 @@ void *parse_hostcond(char *addr, bool is_port)
 			if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
 				return NULL;
 		}
-		fam = AF_NETLINK;
 		goto out;
 	}
 
-	if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) {
+	if (fam == AF_VSOCK) {
 		__u32 cid = ~(__u32)0;
 
 		a.addr.family = AF_VSOCK;
-		if (strncmp(addr, "vsock:", 6) == 0)
-			addr += 6;
 
 		if (is_port)
 			port = addr;
@@ -2212,20 +2221,9 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 		}
 		vsock_set_inet_prefix(&a.addr, cid);
-		fam = AF_VSOCK;
 		goto out;
 	}
 
-	if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
-		fam = AF_INET;
-		if (!strncmp(addr, "inet:", 5))
-			addr += 5;
-	} else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
-		fam = AF_INET6;
-		if (!strncmp(addr, "inet6:", 6))
-			addr += 6;
-	}
-
 	/* URL-like literal [] */
 	if (addr[0] == '[') {
 		addr++;
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH ip-route2-next v2] ss: always prefer family as part of host condition to default family
  2021-02-02  3:32   ` [PATCH ip-route2-next v2] " Thayne McCombs
@ 2021-02-05  4:57     ` David Ahern
  0 siblings, 0 replies; 4+ messages in thread
From: David Ahern @ 2021-02-05  4:57 UTC (permalink / raw)
  To: Thayne McCombs; +Cc: netdev

On 2/1/21 8:32 PM, Thayne McCombs wrote:
> I've fixed the indentation to use tabs instead of spaces. 
> 
> -- >8 --
> ss accepts an address family both with the -f option and as part of a
> host condition. However, if the family in the host condition is
> different than the the last -f option, then which family is actually
> used depends on the order that different families are checked.
> 
> This changes parse_hostcond to check all family prefixes before parsing
> the rest of the address, so that the host condition's family always has
> a higher priority than the "preferred" family.
> 
> Signed-off-by: Thayne McCombs <astrothayne@gmail.com>
> ---
>  misc/ss.c | 50 ++++++++++++++++++++++++--------------------------
>  1 file changed, 24 insertions(+), 26 deletions(-)
> 
>

applied to iproute2-next. Thanks



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-02-05  4:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-31  8:17 [PATCH iproute2-next] ss: always prefer family as part of host condition to default family Thayne McCombs
2021-02-02  3:04 ` David Ahern
2021-02-02  3:32   ` [PATCH ip-route2-next v2] " Thayne McCombs
2021-02-05  4:57     ` David Ahern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).