All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: David Miller <davem@davemloft.net>
Cc: netdev <netdev@vger.kernel.org>,
	"Pekka Savola (ipv6)" <pekkas@netcore.fi>,
	Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>,
	Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>,
	James Morris <jmorris@namei.org>,
	Patrick McHardy <kaber@coreworks.de>
Subject: [PATCH net-2.6.25 1/4] include - Convert IP4 address class macros to inline functions
Date: Wed, 14 Nov 2007 07:53:23 -0800	[thread overview]
Message-ID: <1195055603.5163.179.camel@localhost> (raw)

Change LOOPBACK MULTICAST LOCAL_MCAST BADCLASS and ZERONET
macros to inline functions is_ip4_[type](__be32 addr)
Adds some type safety and maybe some readability

No change in compiled image size

Signed-off-by: Joe Perches <joe@perches.com>

---

 include/linux/in.h |   45 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/include/linux/in.h b/include/linux/in.h
index 3975cbf..ac6eff1 100644
--- a/include/linux/in.h
+++ b/include/linux/in.h
@@ -246,12 +246,47 @@ struct sockaddr_in {
 #include <asm/byteorder.h> 
 
 #ifdef __KERNEL__
+
+static inline __be32 ip4_addr_octets(unsigned char a, unsigned char b, unsigned char c, unsigned char d)
+{
+	return htonl((((__u32)(a & 0xff)) << 24) |
+		     (((__u32)(b & 0xff)) << 16) |
+		     (((__u32)(c & 0xff)) << 8) |
+		     (((__u32)(d & 0xff)) << 0));
+}
+
+static inline bool is_ip4_loopback(__be32 addr)
+{
+	return (addr & ip4_addr_octets(255,0,0,0)) == ip4_addr_octets(127,0,0,0);
+}
+
+static inline bool is_ip4_multicast(__be32 addr)
+{
+	return (addr & ip4_addr_octets(240,0,0,0)) == ip4_addr_octets(224,0,0,0);
+}
+
+static inline bool is_ip4_local_multicast(__be32 addr)
+{
+	return (addr & ip4_addr_octets(255,255,255,0)) == ip4_addr_octets(224,0,0,0);
+}
+
+static inline bool is_ip4_badclass(__be32 addr)
+{
+	return (addr & ip4_addr_octets(240,0,0,0)) == ip4_addr_octets(240,0,0,0);
+}
+
+static inline bool is_ip4_zeronet(__be32 addr)
+{
+	return (addr & ip4_addr_octets(255,0,0,0)) == ip4_addr_octets(0,0,0,0);
+}
+
 /* Some random defines to make it easier in the kernel.. */
-#define LOOPBACK(x)	(((x) & htonl(0xff000000)) == htonl(0x7f000000))
-#define MULTICAST(x)	(((x) & htonl(0xf0000000)) == htonl(0xe0000000))
-#define BADCLASS(x)	(((x) & htonl(0xf0000000)) == htonl(0xf0000000))
-#define ZERONET(x)	(((x) & htonl(0xff000000)) == htonl(0x00000000))
-#define LOCAL_MCAST(x)	(((x) & htonl(0xFFFFFF00)) == htonl(0xE0000000))
+
+#define LOOPBACK(x)	is_ip4_loopback(x)
+#define MULTICAST(x)	is_ip4_multicast(x)
+#define LOCAL_MCAST(x)	is_ip4_local_multicast(x)
+#define BADCLASS(x)	is_ip4_badclass(x)
+#define ZERONET(x)	is_ip4_zeronet(x)
 
 #endif
 



             reply	other threads:[~2007-11-14 15:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-14 15:53 Joe Perches [this message]
2007-11-14 16:53 ` [PATCH net-2.6.25 1/4] include - Convert IP4 address class macros to inline functions David Stevens
2007-11-15  1:54   ` Joe Perches
2007-11-15  2:09     ` Joe Perches
2007-11-20  5:46   ` David Miller
2007-11-20  5:59     ` Joe Perches
2007-11-20  6:18       ` David Miller
  -- strict thread matches above, loose matches on Subject: below --
2007-11-20  6:41 Joe Perches

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=1195055603.5163.179.camel@localhost \
    --to=joe@perches.com \
    --cc=davem@davemloft.net \
    --cc=jmorris@namei.org \
    --cc=kaber@coreworks.de \
    --cc=kuznet@ms2.inr.ac.ru \
    --cc=netdev@vger.kernel.org \
    --cc=pekkas@netcore.fi \
    --cc=yoshfuji@linux-ipv6.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.