$Revision: 1.37 $
IP handling (ip). ip.c - conversions between ascii and binary forms
of IP addresses, prefixes and ranges.
various operations on binary forms.
Status: NOT REVUED, TESTED, COMPLETE
Design and implementation by: Marek Bukowy
Included Files
Preprocessor definitions
#define IP_IMPL
ascii IP address to binary.
In IP_EXPN mode IP will be "expanded"
(missing octets will be set to 0, MSB's will be set).
In IP_PLAIN mode the routine will complain if it sees less octets.
why not use the standard inet_blabla routine ?
it's because if some octets are missing, we make the address zero-padded
(unlike the inet_blabla, which puts zeros in the middle). We also want
to control the expansion with a flag.
#define _IPV6_LENGTH 128
#define CPYLEN 264
this is a shorthand notation to pull out the first word of the address.
it is defined for the scope od the following functions
#define ad( which )
const ip_addr_t IP_ADDR_UNSPEC
pthread_mutex_t Lock
er_ret_t IP_addr_a2v4 ( const char* avalue, ip_addr_t* ipaddr, unsigned int* address )
unsigned IP_addr_b2_space ( ip_addr_t* addrptr )
converts the IP binary address (binaddr) to a string (ascaddr)
of at most strmax characters. Independent of the result
(success or failure) it messes up the string.
er_ret_t IP_addr_b2a ( ip_addr_t* binaddr, char* ascaddr, unsigned strmax )
void IP_addr_b2v4 ( ip_addr_t* addrptr, unsigned* address )
unsigned IP_addr_b2v4_addr ( ip_addr_t* addrptr )
ip_v6word_t IP_addr_b2v6_hi ( ip_addr_t* addrptr )
ip_v6word_t IP_addr_b2v6_lo ( ip_addr_t* addrptr )
return the bitnum bit of the address,
COUNTING FROM THE TOP !!!!! ,
starting with 0 for the *most significant bit*.
int IP_addr_bit_get ( ip_addr_t* binaddr, unsigned bitnum )
set the bitnum bit of the address to bitval,
COUNTING FROM THE TOP !!!!! ,
starting with 0 for the *most significant bit*.
void IP_addr_bit_set ( ip_addr_t* binaddr, unsigned bitnum, unsigned bitval )
compares two IP addresses up to the bit # len,
returns 0 if equal, 1 if ptra greater, -1 if ptrb greater.
It is the responsility of the caller to ensure that both addresses
are from the same IP space.
This is pretty slow; it is used in the searches of the radix tree,
so it might be good to optimise this.
int IP_addr_cmp ( ip_addr_t* ptra, ip_addr_t* ptrb, unsigned len )
er_ret_t IP_addr_f2b_v4 ( ip_addr_t* addrptr, const char* adrstr )
er_ret_t IP_addr_f2b_v6 ( ip_addr_t* addrptr, const char* msbstr, const char* lsbstr )
checks if an IP address is contained within the prefix
returns 1 if it is, 0 otherwise
It is the responsility of the caller to ensure that both address
and prefix are from the same IP space.
int IP_addr_in_pref ( ip_addr_t* ptra, ip_prefix_t* prefix )
checks if an IP address is contained within the range
returns 1 if it is, 0 otherwise
It is the responsility of the caller to ensure that both address
and range are from the same IP space.
works only for IPv4
int IP_addr_in_rang ( ip_addr_t* ptra, ip_range_t* rangptr )
convert the socket's idea of address into a binary range struct.
space select the address type (and consequently struct type)
er_ret_t IP_addr_s2b ( ip_addr_t* addrptr, void* addr_in, int addr_len )
ascii IP address to binary.
In IP_EXPN mode IP will be "expanded"
(missing octets will be set to 0, MSB's will be set).
In IP_PLAIN mode the routine will complain if it sees less octets.
why not use the standard inet_blabla routine ?
it's because if some octets are missing, we make the address zero-padded
(unlike the inet_blabla, which puts zeros in the middle). We also want
to control the expansion with a flag.
er_ret_t IP_addr_t2b ( ip_addr_t* ipptr, const char* addr, ip_exp_t expf )
er_ret_t IP_addr_v4_mk ( ip_addr_t* addrptr, unsigned addrval )
er_ret_t IP_addr_v6_mk ( ip_addr_t* addrptr, ip_v6word_t high, ip_v6word_t low )
sets a range equal to a prefix
er_ret_t IP_pref_2_rang ( ip_range_t* rangptr, ip_prefix_t* prefptr )
a2v4 == functions to convert the ascii representation into binary,
* and then set the unsigned values at the pointers provided.
*
er_ret_t IP_pref_a2v4 ( const char* avalue, ip_prefix_t* pref, unsigned* prefix, unsigned* prefix_length )
er_ret_t IP_pref_a2v6 ( const char* avalue, ip_prefix_t* pref, ip_v6word_t* high, ip_v6word_t* low, unsigned* prefix_length )
unsigned IP_pref_b2_len ( ip_prefix_t* prefix )
unsigned IP_pref_b2_space ( ip_prefix_t* prefix )
convert a binary prefix back into ascii string at most strmax chars long
er_ret_t IP_pref_b2a ( ip_prefix_t* prefptr, char* ascaddr, unsigned strmax )
void IP_pref_b2v4 ( ip_prefix_t* prefptr, unsigned int* prefix, unsigned int* prefix_length )
unsigned IP_pref_b2v4_addr ( ip_prefix_t* prefix )
void IP_pref_b2v6 ( ip_prefix_t* prefptr, ip_v6word_t* high, ip_v6word_t* low, unsigned int* prefix_length )
this fixes a prefix by setting insignificant bits to 0
void IP_pref_bit_fix ( ip_prefix_t* prefix )
er_ret_t IP_pref_f2b_v4 ( ip_prefix_t* prefptr, const char* prefixstr, const char* lengthstr )
er_ret_t IP_pref_f2b_v6 ( ip_prefix_t* prefptr, const char* msbstr, const char* lsbstr, const char* lengthstr )
converts a "IP/length" string into a binary prefix
er_ret_t IP_pref_t2b ( ip_prefix_t* prefptr, const char* prefstr, ip_exp_t expf )
er_ret_t IP_pref_v4_mk ( ip_prefix_t* prefix, unsigned prefval, unsigned preflen )
er_ret_t IP_rang_a2v4 ( const char* rangstr, ip_range_t* myrang, unsigned int* begin_in, unsigned int* end_in )
unsigned IP_rang_b2_space ( ip_range_t* myrang )
convert a binary range back into ascii string at most strmax chars long
er_ret_t IP_rang_b2a ( ip_range_t* rangptr, char* ascaddr, unsigned strmax )
void IP_rang_b2v4 ( ip_range_t* myrang, unsigned* begin, unsigned* end )
This is to parse a classfull address into a range.
Takes the address by pointer from addrptr and puts the result
at rangptr.
Throws error if the address does not fall into any of the
classfull categories
er_ret_t IP_rang_classful ( ip_range_t* rangptr, ip_addr_t* addrptr )
Decomposes a binary range into prefixes and appends them to the list.
Allocates prefix structures and list elements, they must be freed
after use.
returns a bitmask of prefix lengths used.
unsigned IP_rang_decomp ( ip_range_t* rangptr, GList** preflist )
Similar name, slightly different code, totally different functionality.
finds the smallest canonical block encompassing the whole given range,
then MODIFIES the range pointed to by the argument
so that it's equal to this block.
void IP_rang_encomp ( ip_range_t* rangptr )
er_ret_t IP_rang_f2b_v4 ( ip_range_t* rangptr, const char* beginstr, const char* endstr )
calculate the span of a range == size - 1
ip_rangesize_t IP_rang_span ( ip_range_t* rangptr )
convert a range string into a binary range struct.
er_ret_t IP_rang_t2b ( ip_range_t* rangptr, const char* rangstr, ip_exp_t expf )
er_ret_t IP_rang_v4_mk ( ip_range_t* rangptr, unsigned addrbegin, unsigned addrend )
er_ret_t IP_revd_a2v4 ( const char* avalue, ip_prefix_t* pref, unsigned int* prefix, unsigned int* prefix_length )
converts an inaddr/ip6int string into a binary prefix.
RFC2317 support for IPv4:
For expf==IP_EXPN (e2b macro) the unparsable part will be silently accepted
(with the result being the prefix of the succesfully parsed bits).
For expf==IP_PLAIN the unparsable part will make the function return an error.
For IPv6 the expf doesn't matter, the address must be parsable in whole.
er_ret_t IP_revd_t2b ( ip_prefix_t* prefptr, const char* domstr, ip_exp_t expf )
return the max. length of bits per space
Yes, it *could* be a macro - but as a function it can detect
more programmer's errors. And will get inlined anyway.
unsigned IP_sizebits ( ip_space_t spc_id )
Trying to be smart :-) and convert a query search term into prefix(es),
regardless of whether specified as IP address, prefix or range.
justcheck - if just checking the syntax (justcheck == 1),
then the prefixes are freed before the function returns,
otherwise it is the responsibility of the caller to free the list.
er_ret_t IP_smart_conv ( char* key, int justcheck, int encomp, GList** preflist, ip_exp_t expf, ip_keytype_t* keytype )
er_ret_t IP_smart_range ( char* key, ip_range_t* rangptr, ip_exp_t expf, ip_keytype_t* keytype )
static char* asctime_r ( const struct tm* __tm, char* __buf )
static char* ctime_r ( const time_t* __time, char* __buf )
static int getlogin_r ( char* __name, int __len )
static er_ret_t ip_rang_validate ( ip_range_t* rangptr )
static int readdir_r ( DIR* __dp, struct dirent* __ent, struct dirent** __res )
static int sigwait ( const sigset_t* __setp, int* __signo )
static int ttyname_r ( int __fildes, char* __buf, size_t __size )