//-----------------------------------------------------------------------------
// 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_delete
//
// purpose	Delete an IPv4 address entry from an IPv4 lookup table.
//
// arguments	1 (ipv4_table_p) IPv4 table to delete from
//		2 (ipv4_t) IPv4 (network base) address to delete
//		3 (int) prefix length of network (32 for single IP)
//
// returns	(int) -1 : error
//		(int)  0 : deleted
//		(int)  1 : not found
//-----------------------------------------------------------------------------
int
ipv4_table_delete (
    ipv4_table_p	arg_table
    ,
    ipv4_t		arg_addr
    ,
    int			arg_prefix
    )
__PROTO_END__
{
    ipv4_table_node_p	this_ptr	;
    ipv4_table_node_p	prev_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 ) break;						// level 32 is special
	next_ptr = this_ptr->next[ ( arg_addr & mask ) ? 1 : 0 ];	// next pointer
	if ( ! next_ptr ) return 1;					// 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
    }

    //-------------------------------
    // Clear the data as appropriate.
    //-------------------------------
    if ( mask == 1 ) {
	this_ptr->next[ ( arg_addr & mask ) ? 1 : 0 ] = NULL;
    } else {
	this_ptr->data = NULL;
    }

    //-----------------------------------------
    // Prune back the tree as much as possible.
    //-----------------------------------------
    for (;;) {
	if ( this_ptr->data ) break;			// do not lose any other data
	if ( this_ptr->next[0] ) break;			// do not lose another branch
	if ( this_ptr->next[1] ) break;			// do not lose another branch

	if ( ! ( prev_ptr = this_ptr->prev ) ) break;	// do not prune the apex

	if ( prev_ptr->next[0] == this_ptr ) {
	    prev_ptr->next[0] = NULL;			// unlink from the parent
	} else if ( prev_ptr->next[1] == this_ptr ) {
	    prev_ptr->next[1] = NULL;			// unlink from the parent
	} else {
	    return -3;
	}

	this_ptr->next[0]	= NULL;
	this_ptr->next[1]	= NULL;
	this_ptr->prev		= NULL;
	this_ptr->data		= NULL;

	free( this_ptr );
    }

    return 0;
}

