#include <stdio.h>

#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <malloc.h>

extern int h_errno;

int
ipFindHostByName(char *host_name,struct sockaddr_in *host)
{
	struct hostent *hp;
	char *errmsg;
	memset(host,0,sizeof(struct sockaddr_in));
	hp=gethostbyname(host_name);
   	if(hp)
	{
		memcpy(&(host->sin_addr),hp->h_addr,hp->h_length);
		return 1;
    }
	switch(h_errno)
	{
		case HOST_NOT_FOUND:
/*		case NO_ADDRESS:
		case NO_DATA:*/
			errmsg="host not found";
			break;
		case NO_RECOVERY:
			errmsg="DNS error";
			break;
		case TRY_AGAIN:
			/* TODO */
			errmsg="try again later";
			break;
	}
	printf("ipFindHost('%s'): %s.\n",host_name,errmsg);
	return 0;
}

int ipGetLocalhost(struct sockaddr_in *host)
{
	char buf[256];
	if(gethostname(buf, sizeof(buf))==0)
		return ipFindHostByName(buf,host);
	else
		return 0;
}

int main()
{
	struct sockaddr_in addr;
	// get_my_addr(&addr);
	ipFindHostByName("wonderbox.lightside.ru",&addr);
	printf("host is '%s'\n",inet_ntoa(addr.sin_addr));
	return 1;
}
