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