//-----------------------------------------------------------------------------
// Copyright © 2003 - 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/net
// 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	map_seq_int_hostent
//
// purpose	Add entries from a hostent structure, from gethostbyname() and
//		other functions, to the specified mapping in sequential order
//		with an increasing integer as key and the address as data.
//		This preserves the original sequential order.
//
// arguments	1 (MAP) mapping to insert addresses into
//		2 (struct hostent *) pointer to hostent structure
//
// returns	(int) == -1 : error
//		(int) >=  0 : number of addresses inserted
//-----------------------------------------------------------------------------
int
map_seq_int_hostent (
    MAP			arg_map
    ,
    struct hostent *	arg_hostent
    )
__PROTO_END__
{
    char * *		list_p	;
    long		key	;
    int			length	;
    int			count	;

    //------------------
    // Verify arguments.
    //------------------
    if ( ! arg_hostent ) return -1;
    if ( ! arg_map ) return -1;
    if ( map_key_type( arg_map ) != MAP_DATA_UL ) return -1;

    //---------------------------
    // Get highest current entry.
    //---------------------------
    key = map_last( arg_map ) >= 0 ? map_ul_key( arg_map ) : 0;

    //-----------------------------------------------
    // Populate mapping from each host entry address.
    //-----------------------------------------------
    list_p = arg_hostent->h_addr_list;
    length = arg_hostent->h_length;
    count = 0;

    while ( * list_p ) {
	++ key;
	if ( map_ul_insert( arg_map, key ) > 0 ) {
	    map_uca_store( arg_map, (unsigned char *) * list_p, length );
	}
	++ count;
	++ list_p;
    }

    return count;
}

