//-----------------------------------------------------------------------------
// Copyright © 2005 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/string
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include "net_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	ipv4_table_insert
//
// purpose	Insert an IPv4 address entry into an IPv4 lookup table.
//
// arguments	1 (ipv4_table_p) IPv4 table to insert into
//		2 (ipv4_t) IPv4 (network base) address to insert
//		3 (int) prefix length of network (32 for single IP)
//		4 (void *) arbitrary pointer to associate with this address
//
// note		Only the number of bits specified in the given prefix length
//		will be used from the given IPv4 address.  So any address in
//		the subnet will effectively specify the subnet.
//
// returns	(int) == -1 : error
//		(int) ==  0 : inserted
//		(int) ==  1 : already exists (unchanged)
//-----------------------------------------------------------------------------
int
ipv4_table_insert (
    ipv4_table_p	arg_table
    ,
    ipv4_t		arg_addr
    ,
    int			arg_prefix
    ,
    void *		arg_data_p
    )
__PROTO_END__
{
    ipv4_table_node_p	this_ptr	;
    ipv4_t		mask		;


    //-----------------------------------
    // Validate the subnet prefix length.
    //-----------------------------------
    if ( arg_prefix < 0 || arg_prefix > 32 ) return -1;

    //---------------------------------------
    // Find the node for this address/prefix.
    //---------------------------------------
    mask = 0x80000000;					// start at first address bit
    this_ptr = (ipv4_table_node_p) arg_table;		// start at top of table
    while ( arg_prefix ) {
	void *next_ptr;
	if ( mask == 1 ) goto ipv4_table_insert_level_32;		// level 32 is special
	next_ptr = this_ptr->next[ ( arg_addr & mask ) ? 1 : 0 ];	// next pointer
	if ( ! next_ptr ) break;					// no more nodes
	this_ptr = (ipv4_table_node_p) next_ptr;				// make it this node
	mask >>= 1;							// next address bit
	-- arg_prefix;							// count down prefix
    }

    //---------------------------
    // Add more levels as needed.
    //---------------------------
    while ( arg_prefix ) {
	ipv4_table_node_p next_ptr;
	if ( mask == 1 ) goto ipv4_table_insert_level_32;		// level 32 is special
	next_ptr = ipv4_table_node_alloc();				// get new node
	if ( ! next_ptr ) return -2;					// quit if failure
	this_ptr->next[ ( arg_addr & mask ) ? 1 : 0 ] = next_ptr;	// link down
	next_ptr->prev = this_ptr;					// link up
	this_ptr = next_ptr;						// make it this node
	mask >>= 1;							// next address bit
	-- arg_prefix;							// count down prefix
    }

    //--------------------------------------------------------
    // Put data pointer in levels 0-31 in the normal location.
    //--------------------------------------------------------
    this_ptr->data = arg_data_p;
    return 0;

 ipv4_table_insert_level_32:
    //--------------------------------------------------------------
    // Put data pointer in level 32 where the next pointer would go.
    //--------------------------------------------------------------
    this_ptr->next[ ( arg_addr & 1 ) ? 1 : 0 ] = (ipv4_table_node_p) arg_data_p;
    return 0;
}

