Browse Source

[ipv4] Redefine IP address constants to avoid unnecessary byte swapping

Redefine various IPv4 address constants and testing macros to avoid
unnecessary byte swapping at runtime, and slightly rename the macros
to prevent code from accidentally using the old definitions.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 8 years ago
parent
commit
6efcabd415
3 changed files with 44 additions and 37 deletions
  1. 15
    9
      src/include/ipxe/in.h
  2. 8
    8
      src/net/ipv4.c
  3. 21
    20
      src/tests/ipv4_test.c

+ 15
- 9
src/include/ipxe/in.h View File

4
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
4
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5
 
5
 
6
 #include <stdint.h>
6
 #include <stdint.h>
7
+#include <byteswap.h>
7
 #include <ipxe/socket.h>
8
 #include <ipxe/socket.h>
8
 
9
 
9
 /* Protocol numbers */
10
 /* Protocol numbers */
15
 
16
 
16
 /* IP address constants */
17
 /* IP address constants */
17
 
18
 
18
-#define INADDR_NONE 0xffffffff
19
+#define INADDR_NONE		htonl ( 0xffffffff )
19
 
20
 
20
-#define INADDR_BROADCAST 0xffffffff
21
+#define INADDR_BROADCAST	htonl ( 0xffffffff )
21
 
22
 
22
-#define	IN_CLASSA(addr)		( ( (addr) & 0x80000000 ) == 0x00000000 )
23
-#define	IN_CLASSA_NET		0xff000000
24
-#define	IN_CLASSB(addr)		( ( (addr) & 0xc0000000 ) == 0x80000000 )
25
-#define	IN_CLASSB_NET		0xffff0000
26
-#define	IN_CLASSC(addr)		( ( (addr) & 0xe0000000 ) == 0xc0000000 )
27
-#define	IN_CLASSC_NET		0xffffff00
28
-#define IN_MULTICAST(addr)	( ( (addr) & 0xf0000000 ) == 0xe0000000 )
23
+#define	INADDR_NET_CLASSA	htonl ( 0xff000000 )
24
+#define	INADDR_NET_CLASSB	htonl ( 0xffff0000 )
25
+#define	INADDR_NET_CLASSC	htonl ( 0xffffff00 )
26
+
27
+#define	IN_IS_CLASSA( addr ) \
28
+	( ( (addr) & htonl ( 0x80000000 ) ) == htonl ( 0x00000000 ) )
29
+#define	IN_IS_CLASSB( addr ) \
30
+	( ( (addr) & htonl ( 0xc0000000 ) ) == htonl ( 0x80000000 ) )
31
+#define	IN_IS_CLASSC( addr ) \
32
+	( ( (addr) & htonl ( 0xe0000000 ) ) == htonl ( 0xc0000000 ) )
33
+#define IN_IS_MULTICAST( addr ) \
34
+	( ( (addr) & htonl ( 0xf0000000 ) ) == htonl ( 0xe0000000 ) )
29
 
35
 
30
 /**
36
 /**
31
  * IP address structure
37
  * IP address structure

+ 8
- 8
src/net/ipv4.c View File

314
 	if ( sin_src )
314
 	if ( sin_src )
315
 		iphdr->src = sin_src->sin_addr;
315
 		iphdr->src = sin_src->sin_addr;
316
 	if ( ( next_hop.s_addr != INADDR_BROADCAST ) &&
316
 	if ( ( next_hop.s_addr != INADDR_BROADCAST ) &&
317
-	     ( ! IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) &&
317
+	     ( ! IN_IS_MULTICAST ( next_hop.s_addr ) ) &&
318
 	     ( ( miniroute = ipv4_route ( &next_hop ) ) != NULL ) ) {
318
 	     ( ( miniroute = ipv4_route ( &next_hop ) ) != NULL ) ) {
319
 		iphdr->src = miniroute->address;
319
 		iphdr->src = miniroute->address;
320
 		netmask = miniroute->netmask;
320
 		netmask = miniroute->netmask;
353
 		/* Broadcast address */
353
 		/* Broadcast address */
354
 		ipv4_stats.out_bcast_pkts++;
354
 		ipv4_stats.out_bcast_pkts++;
355
 		ll_dest = netdev->ll_broadcast;
355
 		ll_dest = netdev->ll_broadcast;
356
-	} else if ( IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) {
356
+	} else if ( IN_IS_MULTICAST ( next_hop.s_addr ) ) {
357
 		/* Multicast address */
357
 		/* Multicast address */
358
 		ipv4_stats.out_mcast_pkts++;
358
 		ipv4_stats.out_mcast_pkts++;
359
 		if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
359
 		if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
816
 		fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
816
 		fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
817
 		/* Calculate default netmask, if necessary */
817
 		/* Calculate default netmask, if necessary */
818
 		if ( ! netmask.s_addr ) {
818
 		if ( ! netmask.s_addr ) {
819
-			if ( IN_CLASSA ( ntohl ( address.s_addr ) ) ) {
820
-				netmask.s_addr = htonl ( IN_CLASSA_NET );
821
-			} else if ( IN_CLASSB ( ntohl ( address.s_addr ) ) ) {
822
-				netmask.s_addr = htonl ( IN_CLASSB_NET );
823
-			} else if ( IN_CLASSC ( ntohl ( address.s_addr ) ) ) {
824
-				netmask.s_addr = htonl ( IN_CLASSC_NET );
819
+			if ( IN_IS_CLASSA ( address.s_addr ) ) {
820
+				netmask.s_addr = INADDR_NET_CLASSA;
821
+			} else if ( IN_IS_CLASSB ( address.s_addr ) ) {
822
+				netmask.s_addr = INADDR_NET_CLASSB;
823
+			} else if ( IN_IS_CLASSC ( address.s_addr ) ) {
824
+				netmask.s_addr = INADDR_NET_CLASSC;
825
 			}
825
 			}
826
 		}
826
 		}
827
 		/* Get default gateway, if present */
827
 		/* Get default gateway, if present */

+ 21
- 20
src/tests/ipv4_test.c View File

39
 #include <ipxe/test.h>
39
 #include <ipxe/test.h>
40
 
40
 
41
 /** Define inline IPv4 address */
41
 /** Define inline IPv4 address */
42
-#define IPV4(a,b,c,d) ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
42
+#define IPV4(a,b,c,d) \
43
+	htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
43
 
44
 
44
 /**
45
 /**
45
  * Report an inet_ntoa() test result
46
  * Report an inet_ntoa() test result
51
  */
52
  */
52
 static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
53
 static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
53
 			    unsigned int line ) {
54
 			    unsigned int line ) {
54
-	struct in_addr in = { .s_addr = htonl ( addr ) };
55
+	struct in_addr in = { .s_addr = addr };
55
 	char *actual;
56
 	char *actual;
56
 
57
 
57
 	/* Format address */
58
 	/* Format address */
81
 	/* Parse address */
82
 	/* Parse address */
82
 	okx ( inet_aton ( text, &actual ) != 0, file, line );
83
 	okx ( inet_aton ( text, &actual ) != 0, file, line );
83
 	DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
84
 	DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
84
-	okx ( ntohl ( actual.s_addr ) == addr, file, line );
85
+	okx ( actual.s_addr == addr, file, line );
85
 };
86
 };
86
 #define inet_aton_ok( text, addr ) \
87
 #define inet_aton_ok( text, addr ) \
87
 	inet_aton_okx ( text, addr, __FILE__, __LINE__ )
88
 	inet_aton_okx ( text, addr, __FILE__, __LINE__ )
110
 static void ipv4_test_exec ( void ) {
111
 static void ipv4_test_exec ( void ) {
111
 
112
 
112
 	/* Address testing macros */
113
 	/* Address testing macros */
113
-	ok (   IN_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
114
-	ok ( ! IN_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
115
-	ok ( ! IN_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
116
-	ok ( ! IN_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
117
-	ok (   IN_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
118
-	ok ( ! IN_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
119
-	ok ( ! IN_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
120
-	ok ( ! IN_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
121
-	ok (   IN_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
122
-	ok ( ! IN_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
123
-	ok ( ! IN_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
124
-	ok ( ! IN_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
125
-	ok ( ! IN_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
126
-	ok ( ! IN_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
127
-	ok (   IN_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
128
-	ok (   IN_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
129
-	ok (   IN_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
114
+	ok (   IN_IS_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
115
+	ok ( ! IN_IS_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
116
+	ok ( ! IN_IS_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
117
+	ok ( ! IN_IS_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
118
+	ok (   IN_IS_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
119
+	ok ( ! IN_IS_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
120
+	ok ( ! IN_IS_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
121
+	ok ( ! IN_IS_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
122
+	ok (   IN_IS_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
123
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
124
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
125
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
126
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
127
+	ok ( ! IN_IS_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
128
+	ok (   IN_IS_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
129
+	ok (   IN_IS_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
130
+	ok (   IN_IS_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
130
 
131
 
131
 	/* inet_ntoa() tests */
132
 	/* inet_ntoa() tests */
132
 	inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
133
 	inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );

Loading…
Cancel
Save