File ip/ip.c

  $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 )


Global Variable IP_ADDR_UNSPEC

const ip_addr_t IP_ADDR_UNSPEC
Included from /home/shane/release/RIP/include/iproutines.h
Visible in:  aa/aa.c
   ac/ac_persistence.c
   ac/access_control.c
   au/AU_util.c
   ca/ca_configFns.c
   ca/ca_initFn.c
   ca/ca_sanityCheck.c
   ca/ca_sourceLoader.c
   ca/ca_values.c
   co/constants.c
   df/defs.c
   er/er.c
   er/er.yacc.tab.c
   er/er_arrays.c
   er/er_macro.c
   er/er_paths.c
   er/er_print.c
   ip/ip.c

Global Variable Lock

pthread_mutex_t Lock
Included from /home/shane/release/RIP/include/ca_defs.h
Visible in:  aa/aa.c
   ac/ac_persistence.c
   ac/access_control.c
   au/AU_util.c
   ca/ca_configFns.c
   ca/ca_initFn.c
   ca/ca_sanityCheck.c
   ca/ca_sourceLoader.c
   ca/ca_values.c
   co/constants.c
   df/defs.c
   er/er.c
   er/er_arrays.c
   er/er_macro.c
   er/er_paths.c
   er/er_print.c
   ip/ip.c
Used in: ca_get_adminIntElement()ca/ca_configFns.c
  ca_get_adminStrElement()ca/ca_configFns.c
  ca_get_boolean()ca/ca_configFns.c
  ca_get_dirlist()ca/ca_configFns.c
  ca_get_int()ca/ca_configFns.c
  ca_get_string()ca/ca_configFns.c
  ca_set_boolean()ca/ca_configFns.c
  ca_srchandle2Intelement()ca/ca_configFns.c
  ca_srchandle2Strelement()ca/ca_configFns.c

Global Function IP_addr_a2v4()

er_ret_t IP_addr_a2v4 ( const char* avalue, ip_addr_t* ipaddr, unsigned int* address )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2v4()ip/ip.c
  IP_addr_t2b()ip/ip.c

Global Function IP_addr_b2_space()

unsigned IP_addr_b2_space ( ip_addr_t* addrptr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Called by: IP_pref_b2_space()ip/ip.c

Global Function IP_addr_b2a()

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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: inet_ntop()ip/inet_ntop.c
  snprintf()
Called by: IP_pref_b2a()ip/ip.c
  IP_rang_b2a()ip/ip.c
  ac_rxwalkhook_print()ac/access_control.c

Global Function IP_addr_b2v4()

void IP_addr_b2v4 ( ip_addr_t* addrptr, unsigned* address )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2v4_addr()ip/ip.c
Called by: IP_addr_a2v4()ip/ip.c

Global Function IP_addr_b2v4_addr()

unsigned IP_addr_b2v4_addr ( ip_addr_t* addrptr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()
Called by: IP_addr_b2v4()ip/ip.c
  IP_pref_b2v4()ip/ip.c
  IP_pref_b2v4_addr()ip/ip.c
  IP_rang_b2v4()ip/ip.c
  aa_compose_query()aa/aa.c

Global Function IP_addr_b2v6_hi()

ip_v6word_t IP_addr_b2v6_hi ( ip_addr_t* addrptr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()
Called by: IP_pref_b2v6()ip/ip.c

Global Function IP_addr_b2v6_lo()

ip_v6word_t IP_addr_b2v6_lo ( ip_addr_t* addrptr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()
Called by: IP_pref_b2v6()ip/ip.c

Global Function IP_addr_bit_get()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Called by: IP_addr_cmp()ip/ip.c

Global Function IP_addr_bit_set()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Called by: IP_pref_bit_fix()ip/ip.c

Global Function IP_addr_cmp()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_bit_get()ip/ip.c
Called by: IP_addr_in_pref()ip/ip.c
  IP_addr_in_rang()ip/ip.c

Global Function IP_addr_f2b_v4()

er_ret_t IP_addr_f2b_v4 ( ip_addr_t* addrptr, const char* adrstr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_v4_mk()ip/ip.c
  ut_dec_2_uns()
Called by: IP_pref_f2b_v4()ip/ip.c
  IP_rang_f2b_v4()ip/ip.c

Global Function IP_addr_f2b_v6()

er_ret_t IP_addr_f2b_v6 ( ip_addr_t* addrptr, const char* msbstr, const char* lsbstr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_v6_mk()ip/ip.c
  ___errno(), strtoull()
Called by: IP_pref_f2b_v6()ip/ip.c

Global Function IP_addr_in_pref()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_cmp()ip/ip.c

Global Function IP_addr_in_rang()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_cmp()ip/ip.c
  IP_sizebits()ip/ip.c

Global Function IP_addr_s2b()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()

Global Function IP_addr_t2b()

   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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: inet_pton()ip/inet_pton.c
  index(), memset(), strncpy(), ut_dec_2_uns()
Called by: AC_asc_set_nodeny()ac/access_control.c
  IP_addr_a2v4()ip/ip.c
  IP_pref_t2b()ip/ip.c
  IP_rang_t2b()ip/ip.c
  IP_revd_t2b()ip/ip.c
  IP_smart_conv()ip/ip.c

Global Function IP_addr_v4_mk()

er_ret_t IP_addr_v4_mk ( ip_addr_t* addrptr, unsigned addrval )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Called by: IP_addr_f2b_v4()ip/ip.c
  IP_pref_v4_mk()ip/ip.c
  IP_rang_v4_mk()ip/ip.c

Global Function IP_addr_v6_mk()

er_ret_t IP_addr_v6_mk ( ip_addr_t* addrptr, ip_v6word_t high, ip_v6word_t low )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Called by: IP_addr_f2b_v6()ip/ip.c

Global Function IP_pref_2_rang()

 sets a range equal to a prefix 
er_ret_t IP_pref_2_rang ( ip_range_t* rangptr, ip_prefix_t* prefptr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Called by: IP_smart_range()ip/ip.c

Global Function IP_pref_a2v4()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_pref_b2v4()ip/ip.c
  IP_pref_t2b()ip/ip.c

Global Function IP_pref_a2v6()

er_ret_t IP_pref_a2v6 ( const char* avalue, ip_prefix_t* pref, ip_v6word_t* high, ip_v6word_t* low, unsigned* prefix_length )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_pref_b2v6()ip/ip.c
  IP_pref_t2b()ip/ip.c

Global Function IP_pref_b2_len()

unsigned IP_pref_b2_len ( ip_prefix_t* prefix )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Called by: AC_persistence_walk_l()ac/ac_persistence.c
  IP_pref_b2v4()ip/ip.c
  IP_pref_b2v6()ip/ip.c

Global Function IP_pref_b2_space()

unsigned IP_pref_b2_space ( ip_prefix_t* prefix )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2_space()ip/ip.c

Global Function IP_pref_b2a()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2a()ip/ip.c
  snprintf(), strlen()
Called by: AC_ban_set()ac/access_control.c
  AC_prune()ac/access_control.c
  ac_rxwalkhook_print_acl()ac/access_control.c

Global Function IP_pref_b2v4()

void IP_pref_b2v4 ( ip_prefix_t* prefptr, unsigned int* prefix, unsigned int* prefix_length )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2v4_addr()ip/ip.c
  IP_pref_b2_len()ip/ip.c
Called by: IP_pref_a2v4()ip/ip.c
  IP_revd_a2v4()ip/ip.c

Global Function IP_pref_b2v4_addr()

unsigned IP_pref_b2v4_addr ( ip_prefix_t* prefix )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2v4_addr()ip/ip.c
Called by: AC_persistence_walk_l()ac/ac_persistence.c

Global Function IP_pref_b2v6()

void IP_pref_b2v6 ( ip_prefix_t* prefptr, ip_v6word_t* high, ip_v6word_t* low, unsigned int* prefix_length )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2v6_hi()ip/ip.c
  IP_addr_b2v6_lo()ip/ip.c
  IP_pref_b2_len()ip/ip.c
Called by: IP_pref_a2v6()ip/ip.c

Global Function IP_pref_bit_fix()

 this fixes a prefix by setting insignificant bits to 0 
void IP_pref_bit_fix ( ip_prefix_t* prefix )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_bit_set()ip/ip.c
  IP_sizebits()ip/ip.c
Called by: IP_pref_f2b_v4()ip/ip.c
  IP_pref_f2b_v6()ip/ip.c
  IP_pref_t2b()ip/ip.c
  IP_pref_v4_mk()ip/ip.c

Global Function IP_pref_f2b_v4()

er_ret_t IP_pref_f2b_v4 ( ip_prefix_t* prefptr, const char* prefixstr, const char* lengthstr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_f2b_v4()ip/ip.c
  IP_pref_bit_fix()ip/ip.c
  IP_sizebits()ip/ip.c
  ut_dec_2_uns()
Called by: AC_persistence_load()ac/ac_persistence.c

Global Function IP_pref_f2b_v6()

er_ret_t IP_pref_f2b_v6 ( ip_prefix_t* prefptr, const char* msbstr, const char* lsbstr, const char* lengthstr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_f2b_v6()ip/ip.c
  IP_pref_bit_fix()ip/ip.c
  IP_sizebits()ip/ip.c
  ut_dec_2_uns()

Global Function IP_pref_t2b()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_t2b()ip/ip.c
  IP_pref_bit_fix()ip/ip.c
  IP_sizebits()ip/ip.c
  index(), strncpy(), ut_dec_2_uns()
Called by: AC_prune()ac/access_control.c
  IP_pref_a2v4()ip/ip.c
  IP_pref_a2v6()ip/ip.c
  IP_smart_conv()ip/ip.c

Global Function IP_pref_v4_mk()

er_ret_t IP_pref_v4_mk ( ip_prefix_t* prefix, unsigned prefval, unsigned preflen )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_v4_mk()ip/ip.c
  IP_pref_bit_fix()ip/ip.c
  fprintf()

Global Function IP_rang_a2v4()

er_ret_t IP_rang_a2v4 ( const char* rangstr, ip_range_t* myrang, unsigned int* begin_in, unsigned int* end_in )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_rang_b2v4()ip/ip.c
  IP_rang_t2b()ip/ip.c

Global Function IP_rang_b2_space()

unsigned IP_rang_b2_space ( ip_range_t* myrang )
Prototyped in: /home/shane/release/RIP/include/iproutines.h

Global Function IP_rang_b2a()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2a()ip/ip.c
  strcat(), strlen()

Global Function IP_rang_b2v4()

void IP_rang_b2v4 ( ip_range_t* myrang, unsigned* begin, unsigned* end )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_b2v4_addr()ip/ip.c
Called by: IP_rang_a2v4()ip/ip.c

Global Function IP_rang_classful()

   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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()

Global Function IP_rang_decomp()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_rang_decomp()ip/ip.c
  UT_calloc_real(), UT_malloc_real(), fprintf(), g_list_append()
Called by: IP_rang_decomp()ip/ip.c
  IP_smart_conv()ip/ip.c

Global Function IP_rang_encomp()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()
Called by: IP_smart_conv()ip/ip.c

Global Function IP_rang_f2b_v4()

er_ret_t IP_rang_f2b_v4 ( ip_range_t* rangptr, const char* beginstr, const char* endstr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_f2b_v4()ip/ip.c

Global Function IP_rang_span()

 calculate the span of a range == size - 1 
ip_rangesize_t IP_rang_span ( ip_range_t* rangptr )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()

Global Function IP_rang_t2b()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_t2b()ip/ip.c
  ip_rang_validate()ip/ip.c
  UT_calloc_real(), UT_free_real(), index(), strncpy()
Called by: IP_rang_a2v4()ip/ip.c
  IP_smart_conv()ip/ip.c
  IP_smart_range()ip/ip.c

Global Function IP_rang_v4_mk()

er_ret_t IP_rang_v4_mk ( ip_range_t* rangptr, unsigned addrbegin, unsigned addrend )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_v4_mk()ip/ip.c

Global Function IP_revd_a2v4()

er_ret_t IP_revd_a2v4 ( const char* avalue, ip_prefix_t* pref, unsigned int* prefix, unsigned int* prefix_length )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_pref_b2v4()ip/ip.c
  IP_revd_t2b()ip/ip.c

Global Function IP_revd_t2b()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_t2b()ip/ip.c
  fprintf(), g_strdown(), g_strfreev(), g_strreverse(), g_strsplit(), strcat(), strncpy(), strrchr(), strstr()
Called by: IP_revd_a2v4()ip/ip.c

Global Function IP_sizebits()

 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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: fprintf()
Called by: AC_asc_set_nodeny()ac/access_control.c
  AC_check_acl()ac/access_control.c
  AC_commit()ac/access_control.c
  AC_fetch_acc()ac/access_control.c
  IP_addr_in_rang()ip/ip.c
  IP_pref_bit_fix()ip/ip.c
  IP_pref_f2b_v4()ip/ip.c
  IP_pref_f2b_v6()ip/ip.c
  IP_pref_t2b()ip/ip.c
  IP_smart_conv()ip/ip.c

Global Function IP_smart_conv()

  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 )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_addr_t2b()ip/ip.c
  IP_pref_t2b()ip/ip.c
  IP_rang_decomp()ip/ip.c
  IP_rang_encomp()ip/ip.c
  IP_rang_t2b()ip/ip.c
  IP_sizebits()ip/ip.c
  UT_free_real(), UT_malloc_real(), g_list_append()
Called by: AC_asc_acl_command_set()ac/access_control.c
  AC_asc_ban_set()ac/access_control.c
  IP_smart_range()ip/ip.c

Global Function IP_smart_range()

er_ret_t IP_smart_range ( char* key, ip_range_t* rangptr, ip_exp_t expf, ip_keytype_t* keytype )
Prototyped in: /home/shane/release/RIP/include/iproutines.h
Calls: IP_pref_2_rang()ip/ip.c
  IP_rang_t2b()ip/ip.c
  IP_smart_conv()ip/ip.c
  fprintf(), g_list_first(), g_list_length(), wr_real_clear_list()

Local Function asctime_r()

static char* asctime_r ( const struct tm* __tm, char* __buf )
Included from: /usr/include/time.h
Calls: __posix_asctime_r()

Local Function ctime_r()

static char* ctime_r ( const time_t* __time, char* __buf )
Included from: /usr/include/time.h
Calls: __posix_ctime_r()

Local Function getlogin_r()

static int getlogin_r ( char* __name, int __len )
Included from: /usr/include/unistd.h
Calls: __posix_getlogin_r()

Local Function ip_rang_validate()

static er_ret_t ip_rang_validate ( ip_range_t* rangptr )
Called by: IP_rang_t2b()ip/ip.c

Local Function readdir_r()

static int readdir_r ( DIR* __dp, struct dirent* __ent, struct dirent** __res )
Included from: /usr/include/dirent.h
Calls: __posix_readdir_r()

Local Function sigwait()

static int sigwait ( const sigset_t* __setp, int* __signo )
Included from: /usr/include/signal.h
Calls: __posix_sigwait()

Local Function ttyname_r()

static int ttyname_r ( int __fildes, char* __buf, size_t __size )
Included from: /usr/include/unistd.h
Calls: __posix_ttyname_r()