Statistics
| Branch: | Revision:

iof-bird-daemon / lib / ipv6.h @ 061ab802

History | View | Annotate | Download (3.3 KB)

1

    
2
/*
3
 *        BIRD -- IP Addresses et Cetera for IPv6
4
 *
5
 *        (c) 1999--2000 Martin Mares <mj@ucw.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#ifndef _BIRD_IPV6_H_
11
#define _BIRD_IPV6_H_
12

    
13
#include <sys/types.h>
14
#include <netinet/in.h>
15
#include "lib/string.h"
16
#include "lib/bitops.h"
17

    
18
typedef struct ipv6_addr {
19
  u32 addr[4];
20
} ip_addr;
21

    
22
#define _MI(a,b,c,d) ((struct ipv6_addr) {{ a, b, c, d }})
23
#define _I0(a) ((a).addr[0])
24
#define _I1(a) ((a).addr[1])
25
#define _I2(a) ((a).addr[2])
26
#define _I3(a) ((a).addr[3])
27

    
28
#define MAX_PREFIX_LENGTH 128
29
#define BITS_PER_IP_ADDRESS 128
30
#define STD_ADDRESS_P_LENGTH 39
31
#define SIZE_OF_IP_HEADER 40
32

    
33
#define IPA_NONE _MI(0,0,0,0)
34

    
35
#define ipa_equal(x,y)  ({ ip_addr _a=(x), _b=(y); \
36
                           _I0(_a) == _I0(_b) && \
37
                           _I1(_a) == _I1(_b) && \
38
                           _I2(_a) == _I2(_b) && \
39
                           _I3(_a) == _I3(_b); })
40
#define ipa_nonzero(x) ({ ip_addr _a=(x); (_I0(_a) || _I1(_a) || _I2(_a) || _I3(_a)); })
41
#define ipa_and(x,y) ({ ip_addr _a=(x), _b=(y); \
42
                     _MI(_I0(_a) & _I0(_b), \
43
                         _I1(_a) & _I1(_b), \
44
                         _I2(_a) & _I2(_b), \
45
                         _I3(_a) & _I3(_b)); })
46
#define ipa_or(x,y)  ({ ip_addr _a=(x), _b=(y); \
47
                     _MI(_I0(_a) | _I0(_b), \
48
                         _I1(_a) | _I1(_b), \
49
                         _I2(_a) | _I2(_b), \
50
                         _I3(_a) | _I3(_b)); })
51
#define ipa_xor(x,y) ({ ip_addr _a=(x), _b=(y); \
52
                     _MI(_I0(_a) ^ _I0(_b), \
53
                         _I1(_a) ^ _I1(_b), \
54
                         _I2(_a) ^ _I2(_b), \
55
                         _I3(_a) ^ _I3(_b)); })
56
#define ipa_not(x) ({ ip_addr _a=(x); _MI(~_I0(_a),~_I1(_a),~_I2(_a),~_I3(_a)); })
57
#define ipa_mkmask(x) ipv6_mkmask(x)
58
#define ipa_mklen(x) ipv6_mklen(&(x))
59
#define ipa_hash(x) ipv6_hash(&(x))
60
#define ipa_hton(x) ipv6_hton(&(x))
61
#define ipa_ntoh(x) ipv6_ntoh(&(x))
62
#define ipa_classify(x) ipv6_classify(&(x))
63
/* ipa_opposite and ipa_class_mask don't make sense with IPv6 */
64
/* ipa_from_u32 and ipa_to_u32 replaced by ipa_build */
65
#define ipa_build(a,b,c,d) _MI(a,b,c,d)
66
#define ipa_compare(x,y) ipv6_compare(x,y)
67
/* ipa_pxlen() requires that x != y */
68
#define ipa_pxlen(x, y) ipv6_pxlen(x, y)
69
#define ipa_getbit(x, y) ipv6_getbit(x, y)
70
#define ipa_absolutize(x,y) ipv6_absolutize(x,y)
71

    
72
/* In IPv6, SOCK_RAW does not return packet header */
73
#define ip_skip_header(x, y) x
74

    
75
ip_addr ipv6_mkmask(unsigned);
76
unsigned ipv6_mklen(ip_addr *);
77
int ipv6_classify(ip_addr *);
78
void ipv6_hton(ip_addr *);
79
void ipv6_ntoh(ip_addr *);
80
int ipv6_compare(ip_addr, ip_addr);
81
int ipv4_pton_u32(char *, u32 *);
82
void ipv6_absolutize(ip_addr *, ip_addr *);
83

    
84
/*
85
 *  This hash function looks well, but once IPv6 enters
86
 *  mainstream use, we need to check that it has good
87
 *  distribution properties on real routing tables.
88
 */
89

    
90
static inline unsigned ipv6_hash(ip_addr *a)
91
{
92
  /* Returns a 16-bit hash key */
93
  u32 x = _I0(*a) ^ _I1(*a) ^ _I2(*a) ^ _I3(*a);
94
  return (x ^ (x >> 16) ^ (x >> 8)) & 0xffff;
95
}
96

    
97
static inline u32 ipv6_getbit(ip_addr a, u32 y)
98
{
99
  return a.addr[y / 32] & (0x80000000 >> (y % 32));
100
}
101

    
102
static inline u32 ipv6_pxlen(ip_addr a, ip_addr b)
103
{
104
  int i = 0;
105
  i+= (a.addr[i] == b.addr[i]);
106
  i+= (a.addr[i] == b.addr[i]);
107
  i+= (a.addr[i] == b.addr[i]);
108
  i+= (a.addr[i] == b.addr[i]);
109
  return 32 * i + 31 - u32_log2(a.addr[i] ^ b.addr[i]);
110
}
111

    
112
/*
113
 *  RFC 1883 defines packet precendece, but RFC 2460 replaces it
114
 *  by generic Traffic Class ID with no defined semantics. Better
115
 *  not use it yet.
116
 */
117
#define IP_PREC_INTERNET_CONTROL -1
118

    
119
#endif