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"
|
|
|
|
|
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
|
|
|
|
|
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
|
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))
|
1998-05-27 05:36:48 +08:00
|
|
|
#define ipa_opposite(x) _MI(_I(x) ^ 1)
|
1998-07-10 16:38:29 +08:00
|
|
|
#define ipa_class_mask(x) 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))
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1998-04-23 22:01:15 +08:00
|
|
|
#endif
|