File modules/ip/ip.c

  $Revision: 1.11 $

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
Design and implementation by: Marek Bukowy

Included Files


Preprocessor definitions

#define IP_IMPL

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 include/iproutines.h
Visible in:  modules/ac/access_control.c
   modules/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, int strmax )
Prototyped in: include/iproutines.h
Calls: snprintf()
Called by: AC_rxwalkhook_print()modules/ac/access_control.c
  IP_pref_b2a()modules/ip/ip.c
  IP_rang_b2a()modules/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, int bitnum )
Prototyped in: include/iproutines.h
Called by: IP_addr_cmp()modules/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, int bitnum, int bitval )
Prototyped in: include/iproutines.h

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.
int IP_addr_cmp ( ip_addr_t* ptra, ip_addr_t* ptrb, int len )
Prototyped in: include/iproutines.h
Calls: IP_addr_bit_get()modules/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: include/iproutines.h
Calls: fprintf()

Global Function IP_addr_t2b()

   ascii IP address to binary. 
   In IP_EXPN mode IP will be treated as not-expanded.
   (missing octets will be set to 0, MSB will be set).
   In IP_PLAIN mode the routine will complain if it sees less octets.
er_ret_t IP_addr_t2b ( ip_addr_t* ipptr, char* addr, ip_exp_t expf )
Prototyped in: include/iproutines.h
Calls: index(), memset(), strncpy(), ut_dec_2_uns()
Called by: IP_pref_t2b()modules/ip/ip.c
  IP_rang_t2b()modules/ip/ip.c
  IP_smart_conv()modules/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 )

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, int strmax )
Prototyped in: include/iproutines.h
Calls: IP_addr_b2a()modules/ip/ip.c
  snprintf(), strlen()
Called by: AC_rxwalkhook_print_acl()modules/ac/access_control.c
  ip_print_prefix()modules/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: include/iproutines.h
Called by: IP_pref_t2b()modules/ip/ip.c

Global Function IP_pref_t2b()

 converts a "IP/length" string into a binary prefix 
er_ret_t IP_pref_t2b ( ip_prefix_t* prefptr, char* prefstr, ip_exp_t expf )
Prototyped in: include/iproutines.h
Calls: IP_addr_t2b()modules/ip/ip.c
  IP_pref_bit_fix()modules/ip/ip.c
  index(), strncpy(), ut_dec_2_uns()
Called by: IP_smart_conv()modules/ip/ip.c

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, int strmax )
Prototyped in: include/iproutines.h
Calls: IP_addr_b2a()modules/ip/ip.c
  strcat(), strlen()

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: 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 )
Calls: IP_rang_decomp()modules/ip/ip.c
  fprintf(), g_list_append(), wr_real_calloc(), wr_real_malloc()
Called by: IP_rang_decomp()modules/ip/ip.c
  IP_smart_conv()modules/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.
returns a bitmask of prefix length used.
unsigned IP_rang_encomp ( ip_range_t* rangptr )
Called by: IP_smart_conv()modules/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: include/iproutines.h

Global Function IP_rang_t2b()

 convert a range string into a binary range struct. 
er_ret_t IP_rang_t2b ( ip_range_t* rangptr, char* rangstr, ip_exp_t expf )
Prototyped in: include/iproutines.h
Calls: IP_addr_t2b()modules/ip/ip.c
  index(), strncpy(), wr_real_calloc(), wr_real_free()
Called by: IP_smart_conv()modules/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.
int IP_sizebits ( ip_space_t spc_id )
Prototyped in: include/iproutines.h
Called by: AC_check_acl()modules/ac/access_control.c
  AC_commit()modules/ac/access_control.c
  AC_fetch_acc()modules/ac/access_control.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 )
Prototyped in: include/iproutines.h
Calls: IP_addr_t2b()modules/ip/ip.c
  IP_pref_t2b()modules/ip/ip.c
  IP_rang_decomp()modules/ip/ip.c
  IP_rang_encomp()modules/ip/ip.c
  IP_rang_t2b()modules/ip/ip.c
  g_list_append(), wr_real_free(), wr_real_malloc()

Global Function ip_print_prefix()

  This is a hook function for use with g_list_foreach, to print a list
  of prefixes 
void ip_print_prefix ( void* dataptr, void* junk )
Calls: IP_pref_b2a()modules/ip/ip.c
  printf()