Cross reference for modules/ip/ip.c of index.

# File modules/ip/ip.c

      $Revision: 1.25 $

      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

- `#include <include/iproutines.h>`

- `#include <string.h>`

- `#include <stdio.h>`

- `#include <erroutines.h>`

- `#include <ctype.h>`

- `#include <include/memwrap.h>`

- `#include <include/numconv.h>`

- `#include <stubs.h>`

- `#include <sys/socket.h>`

- `#include <netinet/in.h>`

- `#include <include/inet6def.h>`

## 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 | include/iproutines.h
Visible in: |  | modules/aa/aa.c
|  | modules/df/defs.c
|  | modules/ip/ip.c

## Global Function IP_addr_a2v4()

`er_ret_t IP_addr_a2v4 ( char* avalue, ip_addr_t* ipaddr, unsigned int* address )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_b2v4() | modules/ip/ip.c
| IP_addr_t2b() | modules/ip/ip.c

## Global Function IP_addr_b2_space()

`unsigned IP_addr_b2_space ( ip_addr_t* addrptr )`

Prototyped in: | include/iproutines.h
Called by: | IP_pref_b2_space() | 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: | inet_ntop() | modules/ip/inet_ntop.c
| 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_b2v4()

`void IP_addr_b2v4 ( ip_addr_t* addrptr, unsigned* address )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_b2v4_addr() | modules/ip/ip.c
Called by: | IP_addr_a2v4() | modules/ip/ip.c

## Global Function IP_addr_b2v4_addr()

`unsigned IP_addr_b2v4_addr ( ip_addr_t* addrptr )`

Prototyped in: | include/iproutines.h
Calls: | fprintf()
Called by: | IP_addr_b2v4() | modules/ip/ip.c
| IP_pref_b2v4() | modules/ip/ip.c
| IP_pref_b2v4_addr() | modules/ip/ip.c
| IP_rang_b2v4() | modules/ip/ip.c
| aa_compose_query() | modules/aa/aa.c

## Global Function IP_addr_b2v6_hi()

`ip_v6word_t IP_addr_b2v6_hi ( ip_addr_t* addrptr )`

Prototyped in: | include/iproutines.h
Calls: | fprintf()
Called by: | IP_pref_b2v6() | modules/ip/ip.c

## Global Function IP_addr_b2v6_lo()

`ip_v6word_t IP_addr_b2v6_lo ( ip_addr_t* addrptr )`

Prototyped in: | include/iproutines.h
Calls: | fprintf()
Called by: | IP_pref_b2v6() | 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
Called by: | IP_pref_bit_fix() | modules/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, int len )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_bit_get() | modules/ip/ip.c
Called by: | IP_addr_in_pref() | modules/ip/ip.c
| IP_addr_in_rang() | modules/ip/ip.c

## Global Function IP_addr_f2b_v4()

`er_ret_t IP_addr_f2b_v4 ( ip_addr_t* addrptr, char* adrstr )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_v4_mk() | modules/ip/ip.c
| ut_dec_2_uns()
Called by: | IP_pref_f2b_v4() | modules/ip/ip.c
| IP_rang_f2b_v4() | modules/ip/ip.c

## Global Function IP_addr_f2b_v6()

`er_ret_t IP_addr_f2b_v6 ( ip_addr_t* addrptr, char* msbstr, char* lsbstr )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_v6_mk() | modules/ip/ip.c
| sscanf()
Called by: | IP_pref_f2b_v6() | modules/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: | include/iproutines.h
Calls: | IP_addr_cmp() | modules/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: | include/iproutines.h
Calls: | IP_addr_cmp() | modules/ip/ip.c
| IP_sizebits() | 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 "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, char* addr, ip_exp_t expf )`

Prototyped in: | include/iproutines.h
Calls: | inet_pton() | modules/ip/inet_pton.c
| index(), memset(), strncpy(), ut_dec_2_uns()
Called by: | IP_addr_a2v4() | modules/ip/ip.c
| IP_pref_t2b() | modules/ip/ip.c
| IP_rang_t2b() | modules/ip/ip.c
| IP_revd_t2b() | modules/ip/ip.c
| IP_smart_conv() | modules/ip/ip.c

## Global Function IP_addr_v4_mk()

`er_ret_t IP_addr_v4_mk ( ip_addr_t* addrptr, unsigned addrval )`

Prototyped in: | include/iproutines.h
Called by: | IP_addr_f2b_v4() | modules/ip/ip.c
| IP_pref_v4_mk() | modules/ip/ip.c
| IP_rang_v4_mk() | modules/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: | include/iproutines.h
Called by: | IP_addr_f2b_v6() | 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 )`

Prototyped in: | include/iproutines.h
Called by: | IP_smart_range() | modules/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 ( char* avalue, ip_prefix_t* pref, unsigned* prefix, unsigned* prefix_length )`

Prototyped in: | include/iproutines.h
Calls: | IP_pref_b2v4() | modules/ip/ip.c
| IP_pref_t2b() | modules/ip/ip.c

## Global Function IP_pref_a2v6()

`er_ret_t IP_pref_a2v6 ( char* avalue, ip_prefix_t* pref, ip_v6word_t* high, ip_v6word_t* low, unsigned* prefix_length )`

Prototyped in: | include/iproutines.h
Calls: | IP_pref_b2v6() | modules/ip/ip.c
| IP_pref_t2b() | modules/ip/ip.c

## Global Function IP_pref_b2_len()

`unsigned IP_pref_b2_len ( ip_prefix_t* prefix )`

Prototyped in: | include/iproutines.h
Called by: | IP_pref_b2v4() | modules/ip/ip.c
| IP_pref_b2v6() | modules/ip/ip.c

## Global Function IP_pref_b2_space()

`unsigned IP_pref_b2_space ( ip_prefix_t* prefix )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_b2_space() | modules/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, 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

## Global Function IP_pref_b2v4()

`void IP_pref_b2v4 ( ip_prefix_t* prefptr, unsigned int* prefix, unsigned int* prefix_length )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_b2v4_addr() | modules/ip/ip.c
| IP_pref_b2_len() | modules/ip/ip.c
Called by: | IP_pref_a2v4() | modules/ip/ip.c
| IP_revd_a2v4() | modules/ip/ip.c

## Global Function IP_pref_b2v4_addr()

`unsigned IP_pref_b2v4_addr ( ip_prefix_t* prefix )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_b2v4_addr() | modules/ip/ip.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: | include/iproutines.h
Calls: | IP_addr_b2v6_hi() | modules/ip/ip.c
| IP_addr_b2v6_lo() | modules/ip/ip.c
| IP_pref_b2_len() | modules/ip/ip.c
Called by: | IP_pref_a2v6() | 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
Calls: | IP_addr_bit_set() | modules/ip/ip.c
| IP_sizebits() | modules/ip/ip.c
Called by: | IP_pref_f2b_v4() | modules/ip/ip.c
| IP_pref_f2b_v6() | modules/ip/ip.c
| IP_pref_t2b() | modules/ip/ip.c
| IP_pref_v4_mk() | modules/ip/ip.c

## Global Function IP_pref_f2b_v4()

`er_ret_t IP_pref_f2b_v4 ( ip_prefix_t* prefptr, char* prefixstr, char* lengthstr )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_f2b_v4() | modules/ip/ip.c
| IP_pref_bit_fix() | modules/ip/ip.c
| IP_sizebits() | modules/ip/ip.c
| ut_dec_2_uns()

## Global Function IP_pref_f2b_v6()

`er_ret_t IP_pref_f2b_v6 ( ip_prefix_t* prefptr, char* msbstr, char* lsbstr, char* lengthstr )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_f2b_v6() | modules/ip/ip.c
| IP_pref_bit_fix() | modules/ip/ip.c
| IP_sizebits() | modules/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, 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
| IP_sizebits() | modules/ip/ip.c
| index(), strncpy(), ut_dec_2_uns()
Called by: | IP_pref_a2v4() | modules/ip/ip.c
| IP_pref_a2v6() | modules/ip/ip.c
| IP_smart_conv() | modules/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: | include/iproutines.h
Calls: | IP_addr_v4_mk() | modules/ip/ip.c
| IP_pref_bit_fix() | modules/ip/ip.c
| fprintf()

## Global Function IP_rang_a2v4()

`er_ret_t IP_rang_a2v4 ( char* rangstr, ip_range_t* myrang, unsigned int* begin_in, unsigned int* end_in )`

Prototyped in: | include/iproutines.h
Calls: | IP_rang_b2v4() | modules/ip/ip.c
| IP_rang_t2b() | modules/ip/ip.c

## Global Function IP_rang_b2_space()

`unsigned IP_rang_b2_space ( ip_range_t* myrang )`

Prototyped in: | 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, int strmax )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_b2a() | modules/ip/ip.c
| strcat(), strlen()

## Global Function IP_rang_b2v4()

`void IP_rang_b2v4 ( ip_range_t* myrang, unsigned* begin, unsigned* end )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_b2v4_addr() | modules/ip/ip.c
Called by: | IP_rang_a2v4() | modules/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: | 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: | include/iproutines.h
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.

`void IP_rang_encomp ( ip_range_t* rangptr )`

Prototyped in: | include/iproutines.h
Calls: | fprintf()
Called by: | IP_smart_conv() | modules/ip/ip.c

## Global Function IP_rang_f2b_v4()

`er_ret_t IP_rang_f2b_v4 ( ip_range_t* rangptr, char* beginstr, char* endstr )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_f2b_v4() | 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
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, char* rangstr, ip_exp_t expf )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_t2b() | modules/ip/ip.c
| ip_rang_validate() | modules/ip/ip.c
| index(), strncpy(), wr_real_calloc(), wr_real_free()
Called by: | IP_rang_a2v4() | modules/ip/ip.c
| IP_smart_conv() | modules/ip/ip.c
| IP_smart_range() | modules/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: | include/iproutines.h
Calls: | IP_addr_v4_mk() | modules/ip/ip.c

## Global Function IP_revd_a2v4()

`er_ret_t IP_revd_a2v4 ( char* avalue, ip_prefix_t* pref, unsigned int* prefix, unsigned int* prefix_length )`

Prototyped in: | include/iproutines.h
Calls: | IP_pref_b2v4() | modules/ip/ip.c
| IP_revd_t2b() | modules/ip/ip.c

## Global Function IP_revd_t2b()

     converts an inaddr/ip6int string into a binary prefix.
      no distinction is made with respect to "expand" argument.

`er_ret_t IP_revd_t2b ( ip_prefix_t* prefptr, char* domstr, ip_exp_t expf )`

Prototyped in: | include/iproutines.h
Calls: | IP_addr_t2b() | modules/ip/ip.c
| fprintf(), strcat(), strncpy(), strrchr(), strstr(), tolower()
Called by: | IP_revd_a2v4() | 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
| IP_addr_in_rang() | modules/ip/ip.c
| IP_pref_bit_fix() | modules/ip/ip.c
| IP_pref_f2b_v4() | modules/ip/ip.c
| IP_pref_f2b_v6() | modules/ip/ip.c
| IP_pref_t2b() | modules/ip/ip.c
| IP_smart_conv() | modules/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: | 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
| IP_sizebits() | modules/ip/ip.c
| g_list_append(), wr_real_free(), wr_real_malloc()
Called by: | AC_asc_acl_command_set() | modules/ac/access_control.c
| AC_asc_ban_set() | modules/ac/access_control.c
| IP_smart_range() | modules/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: | include/iproutines.h
Calls: | IP_pref_2_rang() | modules/ip/ip.c
| IP_rang_t2b() | modules/ip/ip.c
| IP_smart_conv() | modules/ip/ip.c
| fprintf(), g_list_first(), g_list_length(), wr_real_clear_list()

## Local Function ip_rang_validate()

`static er_ret_t ip_rang_validate ( ip_range_t* rangptr )`

Called by: | IP_rang_t2b() | modules/ip/ip.c