1998-04-23 22:01:15 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- IP Addresses et Cetera for IPv4
|
|
|
|
*
|
1999-03-30 03:14:43 +08:00
|
|
|
* (c) 1998--1999 Martin Mares <mj@ucw.cz>
|
1998-04-23 22:01:15 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_IPV4_H_
|
|
|
|
#define _BIRD_IPV4_H_
|
|
|
|
|
1999-03-30 03:14:43 +08:00
|
|
|
#include "lib/endian.h"
|
1998-05-04 00:43:39 +08:00
|
|
|
#include "lib/bitops.h"
|
2010-01-03 19:17:52 +08:00
|
|
|
#include "lib/unaligned.h"
|
1998-05-04 00:43:39 +08:00
|
|
|
|
1998-11-29 22:38:34 +08:00
|
|
|
#ifdef DEBUGGING
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use the structural representation when you want to make sure
|
|
|
|
* nobody unauthorized attempts to handle ip_addr as number.
|
|
|
|
*/
|
|
|
|
|
1998-04-23 22:01:15 +08:00
|
|
|
typedef struct ipv4_addr {
|
|
|
|
u32 addr;
|
|
|
|
} ip_addr;
|
|
|
|
|
|
|
|
#define _I(x) (x).addr
|
1998-05-15 15:56:13 +08:00
|
|
|
#define _MI(x) ((struct ipv4_addr) { x })
|
1998-04-23 22:01:15 +08:00
|
|
|
|
1998-05-04 00:43:39 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
typedef u32 ip_addr;
|
|
|
|
|
|
|
|
#define _I(x) (x)
|
|
|
|
#define _MI(x) (x)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-03-31 18:55:57 +08:00
|
|
|
#define MAX_PREFIX_LENGTH 32
|
1998-05-15 15:56:13 +08:00
|
|
|
#define BITS_PER_IP_ADDRESS 32
|
1998-06-17 22:26:30 +08:00
|
|
|
#define STD_ADDRESS_P_LENGTH 15
|
2000-06-07 09:18:36 +08:00
|
|
|
#define SIZE_OF_IP_HEADER 24
|
1998-05-15 15:56:13 +08:00
|
|
|
|
1998-04-28 22:39:34 +08:00
|
|
|
#define IPA_NONE (_MI(0))
|
1998-04-23 22:01:15 +08:00
|
|
|
|
|
|
|
#define ipa_equal(x,y) (_I(x) == _I(y))
|
1998-05-04 00:43:39 +08:00
|
|
|
#define ipa_nonzero(x) _I(x)
|
1998-04-23 22:01:15 +08:00
|
|
|
#define ipa_and(x,y) _MI(_I(x) & _I(y))
|
|
|
|
#define ipa_or(x,y) _MI(_I(x) | _I(y))
|
1998-06-02 05:36:58 +08:00
|
|
|
#define ipa_xor(x,y) _MI(_I(x) ^ _I(y))
|
1998-04-23 22:01:15 +08:00
|
|
|
#define ipa_not(x) _MI(~_I(x))
|
1998-05-04 00:43:39 +08:00
|
|
|
#define ipa_mkmask(x) _MI(u32_mkmask(x))
|
|
|
|
#define ipa_mklen(x) u32_masklen(_I(x))
|
|
|
|
#define ipa_hash(x) ipv4_hash(_I(x))
|
|
|
|
#define ipa_hton(x) x = _MI(htonl(_I(x)))
|
|
|
|
#define ipa_ntoh(x) x = _MI(ntohl(_I(x)))
|
|
|
|
#define ipa_classify(x) ipv4_classify(_I(x))
|
2009-09-08 19:45:02 +08:00
|
|
|
#define ipa_has_link_scope(x) ipv4_has_link_scope(_I(x))
|
2008-08-25 19:19:49 +08:00
|
|
|
#define ipa_opposite(x,len) _MI(_I(x) ^ (len == 30 ? 3 : 1))
|
1999-10-12 15:46:08 +08:00
|
|
|
#define ipa_class_mask(x) _MI(ipv4_class_mask(_I(x)))
|
1998-10-19 06:22:28 +08:00
|
|
|
#define ipa_from_u32(x) _MI(x)
|
|
|
|
#define ipa_to_u32(x) _I(x)
|
1999-04-13 04:26:06 +08:00
|
|
|
#define ipa_compare(x,y) ipv4_compare(_I(x),_I(y))
|
2009-03-31 18:55:57 +08:00
|
|
|
/* ipa_pxlen() requires that x != y */
|
|
|
|
#define ipa_pxlen(x, y) ipv4_pxlen(_I(x), _I(y))
|
|
|
|
#define ipa_getbit(x, y) (_I(x) & (0x80000000 >> (y)))
|
2010-01-03 19:17:52 +08:00
|
|
|
#define ipa_put_addr(x, y) ipv4_put_addr(x, y)
|
1998-04-23 22:01:15 +08:00
|
|
|
|
1999-05-15 02:03:09 +08:00
|
|
|
#define ip_skip_header(x, y) ipv4_skip_header(x, y)
|
|
|
|
|
1998-05-04 00:43:39 +08:00
|
|
|
int ipv4_classify(u32);
|
1998-07-10 16:38:29 +08:00
|
|
|
u32 ipv4_class_mask(u32);
|
1999-05-15 02:03:09 +08:00
|
|
|
byte *ipv4_skip_header(byte *, int *);
|
1998-04-23 22:01:15 +08:00
|
|
|
|
2010-02-21 21:34:53 +08:00
|
|
|
static inline int ipv4_has_link_scope(u32 a UNUSED)
|
2009-09-08 19:45:02 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-05-04 00:43:39 +08:00
|
|
|
static inline unsigned ipv4_hash(u32 a)
|
|
|
|
{
|
1998-12-20 21:57:49 +08:00
|
|
|
/* Returns a 16-bit value */
|
|
|
|
a ^= a >> 16;
|
|
|
|
a ^= a << 10;
|
|
|
|
return a & 0xffff;
|
1998-05-04 00:43:39 +08:00
|
|
|
}
|
1998-04-23 22:01:15 +08:00
|
|
|
|
1999-04-13 04:26:06 +08:00
|
|
|
static inline int ipv4_compare(u32 x, u32 y)
|
|
|
|
{
|
|
|
|
return (x > y) - (x < y);
|
|
|
|
}
|
|
|
|
|
2009-03-31 18:55:57 +08:00
|
|
|
static inline u32 ipv4_pxlen(u32 a, u32 b)
|
|
|
|
{
|
|
|
|
return 31 - u32_log2(a ^ b);
|
|
|
|
}
|
|
|
|
|
2010-01-03 19:17:52 +08:00
|
|
|
static inline byte * ipv4_put_addr(byte *buf, ip_addr a)
|
|
|
|
{
|
|
|
|
put_u32(buf, _I(a));
|
|
|
|
return buf+4;
|
|
|
|
}
|
2009-03-31 18:55:57 +08:00
|
|
|
|
1999-06-01 21:57:24 +08:00
|
|
|
#define IP_PREC_INTERNET_CONTROL 0xc0
|
|
|
|
|
1998-04-23 22:01:15 +08:00
|
|
|
#endif
|