Skip to contentSkip to conversation

- in.h - IP4_ADDR

PATCH In review Joe Perches 2 participants 3 messages Nov 12, 2007 – Nov 13, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 - in.h - IP4_ADDR

Add inline functions to in.h that make the IP4 address tests a bit easier to read and also add some type safety.

gcc optimizes IP4_ADDR to a constant (O2 or Os)

---

 include/linux/in.h |   75 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 70 insertions(+), 5 deletions(-)
include/linux/in.h +70 -5
index 3975cbf..17d1878 100644
@@ -247,11 +247,76 @@ struct sockaddr_in {
 
 #ifdef __KERNEL__
 /* 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))
+
+static inline __be32 IP4_ADDR(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 LOOPBACK(__be32 x)
+{
+	return (x & IP4_ADDR(255,0,0,0)) == IP4_ADDR(127,0,0,0);
+}
+
+static inline bool MULTICAST(__be32 x)
+{
+	return (x & IP4_ADDR(240,0,0,0)) == IP4_ADDR(224,0,0,0);
+}
+
+static inline bool BADCLASS(__be32 x)
+{
+	return (x & IP4_ADDR(240,0,0,0)) == IP4_ADDR(240,0,0,0);
+}
+
+static inline bool ZERONET(__be32 x)
+{
+	return (x & IP4_ADDR(255,0,0,0)) == IP4_ADDR(0,0,0,0);
+}
+
+static inline bool LOCAL_MCAST(__be32 x)
+{
+	return (x & IP4_ADDR(255,255,255,0)) == IP4_ADDR(224,0,0,0);
+}
+
+/* Special-Use IPv4 Addresses (RFC3330) */
+
+static inline bool PRIVATE_10(__be32 x)
+{
+	return (x & IP4_ADDR(255,0,0,0)) == IP4_ADDR(10,0,0,0);
+}
+
+static inline bool PRIVATE_172(__be32 x)
+{
+	return (x & IP4_ADDR(255,240,0,0)) == IP4_ADDR(172,16,0,0);
+}
+
+static inline bool PRIVATE_192(__be32 x)
+{
+	return (x & IP4_ADDR(255,255,0,0)) == IP4_ADDR(192,168,0,0);
+}
+
+static inline bool TEST_192(__be32 x)
+{
+	return (x & IP4_ADDR(255,255,255,0)) == IP4_ADDR(192,0,2,0);
+}
+
+static inline bool TEST_198(__be32 x)
+{
+	return (x & IP4_ADDR(255,254,0,0)) == IP4_ADDR(198,18,0,0);
+}
+
+static inline bool ANYCAST_6TO4(__be32 x)
+{
+	return (x & IP4_ADDR(255,255,255,0)) == IP4_ADDR(192,88,99,0);
+}
+
+static inline bool LINK_169(__be32 x)
+{
+ 	return (x & IP4_ADDR(255,255,0,0)) == IP4_ADDR(169,254,0,0);
+}
 
 #endif
 
maintainer
#1 lore raw

From: Joe Perches <[email protected]>
Date: Sun, 11 Nov 2007 19:19:30 -0800

Quoted 6 lines

Add inline functions to in.h that make the IP4 address tests a bit easier to read and also add some type safety.

gcc optimizes IP4_ADDR to a constant (O2 or Os)

I have no problems with this, but I'd like to add it along with subsequent patches that use the new routines and also I'd like to defer this to net-2.6.25 so please resubmit this later.

Thanks!

#2 lore raw
Quoted 4 lines · David Miller

I have no problems with this, but I'd like to add it along with subsequent patches that use the new routines and also I'd like to defer this to net-2.6.25 so please resubmit this later.

I've since changed the functions in my tree to:

static inline bool is_ip4_foo(__be32 addr)

and added

	# define FOO(x) is_ip4_foo(x)

which I think makes more sense and allows macro
removal when all current uses are converted.

When you open net-2.6.25, I'll resubmit it.