$Revision: 1.7 $
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
- #include <include/iproutines.h>
- #include </space/dbdev/marek/include/glib.h>
- #include </space/dbdev/marek/lib/glib/include/glibconfig.h>
- #include <limits.h>
- #include <float.h>
- #include <stdarg.h>
- #include <include/erroutines.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <time.h>
- #include <stdarg.h>
- #include <strings.h>
- #include <include/bitmask.h>
- #include <include/er_aspects.h>
- #include <include/stubs.h>
- #include "include/er_facilities.h"
- #include <include/erroutines.h>
- #include <include/memwrap.h>
- #include <include/erroutines.h>
- #include <include/numconv.h>
Preprocessor definitions
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 )
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 )
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 )
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 )
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 )
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 )
sets a range equal to a prefix
er_ret_t IP_pref_2_rang ( ip_range_t* rangptr, ip_prefix_t* prefptr )
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 )
this fixes a prefix by setting insignificant bits to 0
void IP_pref_bit_fix ( ip_prefix_t* prefix )
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 )
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 )
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.
returns a bitmask of prefix length used.
unsigned IP_rang_encomp ( ip_range_t* rangptr )
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, char* rangstr, 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.
int 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 )
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 )