]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/ipv4.h
Fixed bug in unused function.
[thirdparty/bird.git] / lib / ipv4.h
1 /*
2 * BIRD -- IP Addresses et Cetera for IPv4
3 *
4 * (c) 1998--1999 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #ifndef _BIRD_IPV4_H_
10 #define _BIRD_IPV4_H_
11
12 #include "lib/endian.h"
13 #include "lib/bitops.h"
14 #include "lib/unaligned.h"
15
16 #ifdef DEBUGGING
17
18 /*
19 * Use the structural representation when you want to make sure
20 * nobody unauthorized attempts to handle ip_addr as number.
21 */
22
23 typedef struct ipv4_addr {
24 u32 addr;
25 } ip_addr;
26
27 #define _I(x) (x).addr
28 #define _MI(x) ((struct ipv4_addr) { x })
29
30 #else
31
32 typedef u32 ip_addr;
33
34 #define _I(x) (x)
35 #define _MI(x) (x)
36
37 #endif
38
39 #define MAX_PREFIX_LENGTH 32
40 #define BITS_PER_IP_ADDRESS 32
41 #define STD_ADDRESS_P_LENGTH 15
42 #define SIZE_OF_IP_HEADER 24
43
44 #define IPA_NONE (_MI(0))
45
46 #define ipa_equal(x,y) (_I(x) == _I(y))
47 #define ipa_nonzero(x) _I(x)
48 #define ipa_and(x,y) _MI(_I(x) & _I(y))
49 #define ipa_or(x,y) _MI(_I(x) | _I(y))
50 #define ipa_xor(x,y) _MI(_I(x) ^ _I(y))
51 #define ipa_not(x) _MI(~_I(x))
52 #define ipa_mkmask(x) _MI(u32_mkmask(x))
53 #define ipa_mklen(x) u32_masklen(_I(x))
54 #define ipa_hash(x) ipv4_hash(_I(x))
55 #define ipa_hash32(x) ipv4_hash32(_I(x))
56 #define ipa_hton(x) x = _MI(htonl(_I(x)))
57 #define ipa_ntoh(x) x = _MI(ntohl(_I(x)))
58 #define ipa_classify(x) ipv4_classify(_I(x))
59 #define ipa_has_link_scope(x) ipv4_has_link_scope(_I(x))
60 #define ipa_opposite_m1(x) _MI(_I(x) ^ 1)
61 #define ipa_opposite_m2(x) _MI(_I(x) ^ 3)
62 #define ipa_class_mask(x) _MI(ipv4_class_mask(_I(x)))
63 #define ipa_from_u32(x) _MI(x)
64 #define ipa_to_u32(x) _I(x)
65 #define ipa_compare(x,y) ipv4_compare(_I(x),_I(y))
66 /* ipa_pxlen() requires that x != y */
67 #define ipa_pxlen(x, y) ipv4_pxlen(_I(x), _I(y))
68 #define ipa_getbit(x, y) (_I(x) & (0x80000000 >> (y)))
69 #define ipa_put_addr(x, y) ipv4_put_addr(x, y)
70
71 #define ip_skip_header(x, y) ipv4_skip_header(x, y)
72
73 int ipv4_classify(u32);
74 u32 ipv4_class_mask(u32);
75 byte *ipv4_skip_header(byte *, int *);
76
77 static inline int ipv4_has_link_scope(u32 a UNUSED)
78 {
79 return 0;
80 }
81
82 static inline unsigned ipv4_hash(u32 a)
83 {
84 /* Returns a 16-bit value */
85 a ^= a >> 16;
86 a ^= a << 10;
87 return a & 0xffff;
88 }
89
90 static inline u32 ipv4_hash32(u32 a)
91 {
92 /* Returns a 32-bit value, although low-order bits are not mixed */
93 a ^= a << 16;
94 a ^= a << 12;
95 return a;
96 }
97
98 static inline int ipv4_compare(u32 x, u32 y)
99 {
100 return (x > y) - (x < y);
101 }
102
103 static inline u32 ipv4_pxlen(u32 a, u32 b)
104 {
105 return 31 - u32_log2(a ^ b);
106 }
107
108 static inline byte * ipv4_put_addr(byte *buf, ip_addr a)
109 {
110 put_u32(buf, _I(a));
111 return buf+4;
112 }
113
114 #define IP_PREC_INTERNET_CONTROL 0xc0
115
116 #endif