Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <sthemmin@microsoft.com>,
	Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH 1/2] ip: convert monitor to switch
Date: Wed, 15 Aug 2018 14:29:41 -0700	[thread overview]
Message-ID: <20180815212942.25276-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20180815212942.25276-1-stephen@networkplumber.org>

From: Stephen Hemminger <sthemmin@microsoft.com>

The decoding of netlink message types is natural for a C
switch statement.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 ip/ipmonitor.c | 63 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/ip/ipmonitor.c b/ip/ipmonitor.c
index 8b017341da6b..5552d98ee9e5 100644
--- a/ip/ipmonitor.c
+++ b/ip/ipmonitor.c
@@ -58,7 +58,9 @@ static int accept_msg(const struct sockaddr_nl *who,
 {
 	FILE *fp = (FILE *)arg;
 
-	if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) {
+	switch (n->nlmsg_type) {
+	case RTM_NEWROUTE:
+	case RTM_DELROUTE: {
 		struct rtmsg *r = NLMSG_DATA(n);
 		int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
 
@@ -82,24 +84,28 @@ static int accept_msg(const struct sockaddr_nl *who,
 		}
 	}
 
-	if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
+	case RTM_NEWLINK:
+	case RTM_DELLINK:
 		ll_remember_index(who, n, NULL);
 		print_headers(fp, "[LINK]", ctrl);
 		print_linkinfo(who, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
+
+	case RTM_NEWADDR:
+	case RTM_DELADDR:
 		print_headers(fp, "[ADDR]", ctrl);
 		print_addrinfo(who, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) {
+
+	case RTM_NEWADDRLABEL:
+	case RTM_DELADDRLABEL:
 		print_headers(fp, "[ADDRLABEL]", ctrl);
 		print_addrlabel(who, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH ||
-	    n->nlmsg_type == RTM_GETNEIGH) {
+
+	case RTM_NEWNEIGH:
+	case RTM_DELNEIGH:
+	case RTM_GETNEIGH:
 		if (preferred_family) {
 			struct ndmsg *r = NLMSG_DATA(n);
 
@@ -110,34 +116,41 @@ static int accept_msg(const struct sockaddr_nl *who,
 		print_headers(fp, "[NEIGH]", ctrl);
 		print_neigh(who, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type == RTM_NEWPREFIX) {
+
+	case RTM_NEWPREFIX:
 		print_headers(fp, "[PREFIX]", ctrl);
 		print_prefix(who, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) {
+
+	case RTM_NEWRULE:
+	case RTM_DELRULE:
 		print_headers(fp, "[RULE]", ctrl);
 		print_rule(who, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type == RTM_NEWNETCONF) {
+
+	case NLMSG_TSTAMP:
+		print_nlmsg_timestamp(fp, n);
+		return 0;
+
+	case RTM_NEWNETCONF:
 		print_headers(fp, "[NETCONF]", ctrl);
 		print_netconf(who, ctrl, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type == NLMSG_TSTAMP) {
-		print_nlmsg_timestamp(fp, n);
-		return 0;
-	}
-	if (n->nlmsg_type == RTM_NEWNSID || n->nlmsg_type == RTM_DELNSID) {
+
+	case RTM_DELNSID:
+	case RTM_NEWNSID:
 		print_headers(fp, "[NSID]", ctrl);
 		print_nsid(who, n, arg);
 		return 0;
-	}
-	if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
-	    n->nlmsg_type != NLMSG_DONE) {
-		fprintf(fp, "Unknown message: type=0x%08x(%d) flags=0x%08x(%d)len=0x%08x(%d)\n",
+
+	case NLMSG_ERROR:
+	case NLMSG_NOOP:
+	case NLMSG_DONE:
+		break;	/* ignore */
+
+	default:
+		fprintf(stderr,
+			"Unknown message: type=0x%08x(%d) flags=0x%08x(%d) len=0x%08x(%d)\n",
 			n->nlmsg_type, n->nlmsg_type,
 			n->nlmsg_flags, n->nlmsg_flags, n->nlmsg_len,
 			n->nlmsg_len);
-- 
2.18.0

  reply	other threads:[~2018-08-16  0:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-15 21:29 [PATCH iproute2 0/2] ip monitor fixes Stephen Hemminger
2018-08-15 21:29 ` Stephen Hemminger [this message]
2018-08-15 21:29 ` [PATCH 2/2] ipmonitor: decode DELNETCONF message Stephen Hemminger

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=20180815212942.25276-2-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=netdev@vger.kernel.org \
    --cc=sthemmin@microsoft.com \
    /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