/*
 * Narcotic Domain Name Resolver (nao tinha um nomezinho melhor nao?)
 *  
 * compilar no linux   --> gcc -o dns dns.c -DLINUX
 * compilar no windows --> cl /DWIN32 dns.c /link ws2_32.lib
 */
#include <stdio.h>
#include <stdlib.h>

#ifdef WIN32
  #include <winsock2.h>
#endif

#ifdef LINUX
  #include <netdb.h>            /* gethostbyname, gethostbyaddr, etc */
  #include <netinet/in.h>       /* definicao do struct in_addr       */
#endif
  

#define TAMBUF  1024

void usage(char *programname)
{
	printf("Narcotic Domain Name Resolver ---\n");
	printf("Copyright (c) - 2003 by Narcotic <narcotic@motdlabs.org>\n");
	printf("Uso: %s <arquivo entrada> <arquivo saida>\n\n",programname);
}

void debuging(struct hostent *h)
{
	int i = 0;
      	
      	printf("Hostname: %s\n", h->h_name);
      	
      	while (h->h_aliases[i])
        	printf("Alias: %s\n", h->h_aliases[i++]);
        	
      	switch (h->h_addrtype) 
      	{
        	case AF_INET:
        	case AF_INET6:
        	
          	for (i = 0; h->h_addr_list[i]; ++i)
            		printf("Address: %u.%u.%u.%u\n",
                    		(0xff & h->h_addr_list[i][0]),
                    		(0xff & h->h_addr_list[i][1]),
                    		(0xff & h->h_addr_list[i][2]),
                    		(0xff & h->h_addr_list[i][3]));
	}
}

int resolve(char *s,FILE *fp)
{
	struct hostent *hp;
	unsigned long	a;
			
	a = inet_addr(s);
	
	if(a != INADDR_NONE)
	{
		if((hp = gethostbyaddr((char *)&a, sizeof(a), AF_INET)) == NULL)
		{
			fprintf(stderr,"Impossivel resolver %s\n",s);
			return -2;
		}
		
		fprintf(fp,"%s\n",hp->h_name);
		debuging(hp);
		
		return 0;
	}
	
	if((hp = gethostbyname(s)) != NULL)
	{	
		debuging(hp);
		fprintf(fp,"%u.%u.%u.%u\n",
                    		(0xff & hp->h_addr[0]),
                    		(0xff & hp->h_addr[1]),
                    		(0xff & hp->h_addr[2]),
                    		(0xff & hp->h_addr[3]));
		
		return 0;
	}

	return -1;
}

int main(int argc, char **argv)
{
	char buf[TAMBUF];
	char *infile, *outfile;
	FILE *infp, *outfp;
	int cnt;

#ifdef WIN32

	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
   
	wVersionRequested = MAKEWORD( 2, 0 );
	err = WSAStartup( wVersionRequested, &wsaData );

  	if ( err != 0 )
      		return -1;

#endif   /* #ifdef WIN32 */

	
	
	if(argc < 2)
	{
		usage(argv[0]);
		return -1;
	}
	
	infile  = argv[1];
	outfile = argv[2];
	
	infp = fopen(infile,"r");
	
	if(!infp)
	{
		fprintf(stderr,"Erro ao abrir o arquivo %s. Programa encerrado!\n",infile);
		return -2;
	}
	
	outfp = fopen(outfile,"w");
	
	while(fgets(buf,TAMBUF,infp) != NULL)
	{		
		/* remover \r\n */
		cnt = strlen(buf);				
		
		if(buf[cnt - 1] == '\n')
			cnt--;			
		if(buf[cnt - 1] == '\r')
			cnt--;
					
		if(cnt == 0)
		   continue;
		   
		buf[cnt] = '\0';
		
			
		resolve(buf,outfp);
	}
	
	fclose(infp);
	fclose(outfp);
	
	printf("Programa terminado!!!\n");
	return 0;
}

