//-----------------------------------------------------------------------------
// 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_hostent_addr_str
//
// purpose	Add entries from a hostent structure (from gethostbyname() and
//		other functions) to the specified mapping with the address as
//		the mapping key, and optional caller supplied string data.
//
// arguments	1 (MAP) mapping to insert addresses into
//		2 (struct hostent *) pointer to hostent structure
//		3 (const char *) string to store in mapping as data
//
// returns	(int) == -1 : error
//		(int) >=  0 : number of addresses inserted
//
// note		If an address is in struct hostent more than one, then each
//		instance will be entered in the mapping as a duplicate.
//-----------------------------------------------------------------------------
int
map_hostent_addr_str (
    MAP			arg_map
    ,
    struct hostent *	arg_hostent
    ,
    const char *	arg_data_str
    )
__PROTO_END__
{
    char * *		list_p	;
    int			length	;
    int			count	;

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

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

    while ( * list_p ) {
	if ( map_uca_insert_dup( arg_map, (unsigned char *) ( * list_p ), length ) > 0 ) {
	    if ( arg_data_str ) map_str_store( arg_map, arg_data_str );
	}
	++ count;
	++ list_p;
    }

    return count;
}

