mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Joe Perches <joe@perches.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrew Morton <akpm@linux-foundation.org>,
	andriy.shevchenko@linux.intel.com,
	LKML <linux-kernel@vger.kernel.org>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH] vsprintf: automatic parameters for %pIS via 'a'
Date: Fri,  5 Feb 2016 14:37:20 +0100	[thread overview]
Message-ID: <1454679440-18557-1-git-send-email-Jason@zx2c4.com> (raw)
In-Reply-To: <87y4b0m35z.fsf@rasmusvillemoes.dk>

This patch adds a variable 'a' which indicates that the 'p',
'f', and 's' options should active depending on whether
or not those parameters are actually valid inside the
passed sockaddr.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 Documentation/printk-formats.txt |  7 +++++--
 lib/test_printf.c                | 23 +++++++++++++++++++++++
 lib/vsprintf.c                   | 19 +++++++++++++++++--
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/Documentation/printk-formats.txt b/Documentation/printk-formats.txt
index 5d1128b..5c45db4 100644
--- a/Documentation/printk-formats.txt
+++ b/Documentation/printk-formats.txt
@@ -193,7 +193,7 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope):
 	%piS	001.002.003.004	or 00010002000300040005000600070008
 	%pISc	1.2.3.4		or 1:2:3:4:5:6:7:8
 	%pISpc	1.2.3.4:12345	or [1:2:3:4:5:6:7:8]:12345
-	%p[Ii]S[pfschnbl]
+	%p[Ii]S[pfsachnbl]
 
 	For printing an IP address without the need to distinguish whether it's
 	of type AF_INET or AF_INET6, a pointer to a valid 'struct sockaddr',
@@ -201,7 +201,10 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope):
 
 	The additional 'p', 'f', and 's' specifiers are used to specify port
 	(IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ':' prefix,
-	flowinfo a '/' and scope a '%', each followed by the actual value.
+	flowinfo a '/' and scope a '%', each followed by the actual value. If 'a'
+	is given, 'p', 'f', and 's', when also specified, are only printed
+	depending on whether or not the socketaddr has a non-zero port,
+	flowinfo, and scope.
 
 	In case of an IPv6 address the compressed IPv6 address as described by
 	http://tools.ietf.org/html/rfc5952 is being used if the additional
diff --git a/lib/test_printf.c b/lib/test_printf.c
index 4f6ae60..c3f975e 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -16,6 +16,7 @@
 #include <linux/dcache.h>
 #include <linux/socket.h>
 #include <linux/in.h>
+#include <linux/in6.h>
 
 #define BUF_SIZE 256
 #define PAD_SIZE 16
@@ -301,11 +302,33 @@ ip4(void)
 	test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
 	sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
 	test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
+	test("1.2.3.4:12345", "%pISpa", &sa);
+	test("1.2.3.4", "%pISa", &sa);
+	sa.sin_port = 0;
+	test("1.2.3.4", "%pISpa", &sa);
 }
 
 static void __init
 ip6(void)
 {
+	struct sockaddr_in6 sa = { 0 };
+
+	sa.sin6_family = AF_INET6;
+	sa.sin6_port = cpu_to_be16(12345);
+	sa.sin6_addr.in6_u.u6_addr16[0] = cpu_to_be16(0xabcd);
+	sa.sin6_addr.in6_u.u6_addr16[7] = cpu_to_be16(0x1234);
+	sa.sin6_flowinfo = cpu_to_be32(20);
+	sa.sin6_scope_id = 98;
+
+	test("[abcd::1234]:12345", "%pISpca", &sa);
+	test("[abcd::1234]:12345/20", "%pISpfca", &sa);
+	test("[abcd::1234]:12345/20%98", "%pISpfsca", &sa);
+	sa.sin6_flowinfo = 0;
+	test("[abcd::1234]:12345%98", "%pISpsfca", &sa);
+	sa.sin6_port = 0;
+	test("[abcd::1234]%98", "%pISpfsca", &sa);
+	sa.sin6_scope_id = 0;
+	test("abcd::1234", "%pISpfsca", &sa);
 }
 
 static void __init
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 48ff9c3..80d8ce5 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1145,7 +1145,7 @@ static noinline_for_stack
 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
 			 struct printf_spec spec, const char *fmt)
 {
-	bool have_p = false, have_s = false, have_f = false, have_c = false;
+	bool have_p = false, have_s = false, have_f = false, have_c = false, have_a = false;
 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
 		      sizeof(":12345") + sizeof("/123456789") +
 		      sizeof("%1234567890")];
@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
 		case 'c':
 			have_c = true;
 			break;
+		case 'a':
+			have_a = true;
+			break;
 		}
 	}
 
+	if (have_a) {
+		have_p = have_p && sa->sin6_port;
+		have_s = have_s && sa->sin6_scope_id;
+		have_f = have_f && (sa->sin6_flowinfo & IPV6_FLOWINFO_MASK);
+	}
+
 	if (have_p || have_s || have_f) {
 		*p = '[';
 		off = 1;
@@ -1207,7 +1216,7 @@ static noinline_for_stack
 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
 			 struct printf_spec spec, const char *fmt)
 {
-	bool have_p = false;
+	bool have_p = false, have_a = false;
 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
 	char *pend = ip4_addr + sizeof(ip4_addr);
 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
@@ -1225,9 +1234,15 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
 		case 'b':
 			fmt4[2] = *fmt;
 			break;
+		case 'a':
+			have_a = true;
+			break;
 		}
 	}
 
+	if (have_a)
+		have_p = have_p && sa->sin_port;
+
 	p = ip4_string(ip4_addr, addr, fmt4);
 	if (have_p) {
 		*p++ = ':';
-- 
2.7.0

  parent reply	other threads:[~2016-02-05 13:36 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-03 10:41 [PATCH] vsprintf: do not append unset Scope ID to IPv6 Jason A. Donenfeld
2016-02-03 12:13 ` [PATCH] vsprintf: flowinfo in IPv6 is optional too Jason A. Donenfeld
2016-02-03 17:56   ` IRe: " Joe Perches
2016-02-03 21:09     ` Jason A. Donenfeld
2016-02-03 21:13       ` Andy Shevchenko
2016-02-03 21:13     ` IRe: " Daniel Borkmann
2016-02-03 21:15       ` Jason A. Donenfeld
2016-02-03 21:07 ` [PATCH] vsprintf: do not append unset Scope ID to IPv6 Daniel Borkmann
2016-02-03 21:14   ` Jason A. Donenfeld
2016-02-03 21:47     ` Joe Perches
2016-02-03 22:09       ` Daniel Borkmann
2016-02-03 22:42       ` Jason A. Donenfeld
2016-02-03 22:53       ` [PATCH] vsprintf: automatic parameters for %pIS via 'a' Jason A. Donenfeld
2016-02-03 23:05         ` Joe Perches
2016-02-03 23:25           ` Jason A. Donenfeld
2016-02-03 23:29           ` [PATCH v2] " Jason A. Donenfeld
2016-02-03 23:37             ` Joe Perches
2016-02-04  0:59               ` [PATCH v3] " Jason A. Donenfeld
2016-02-05  0:06             ` [PATCH v2] " Rasmus Villemoes
2016-02-05 13:06               ` Jason A. Donenfeld
2016-02-05 13:37               ` Jason A. Donenfeld [this message]
2016-02-05 13:39               ` [PATCH v4] " Jason A. Donenfeld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1454679440-18557-1-git-send-email-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=daniel@iogearbox.net \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®