dns

Constantinos A. Kotsokalis C.Kotsokalis at ece.ntua.gr
Sun Mar 7 04:01:59 EET 1999


On Sun, 7 Mar 1999 00:12:53 -0000, Linux wrote:
>Geia xara,
>sygnwmh gia to asxeto ths erwthshs. grafw ena programmataki se C. Thelw na
>tou dinw domain name (px www.linux.gr) kai na pairnw thn IP address (px
>123.123.123.123). Emple3a me oles tis entoles tou typou:
>htons,inet_*,gethost*... alla den katafera na vgalw akrh
>Ypo8etw pws kapoios apo sas 8a exei vre8ei mprosta sto idio provlhma.
>Sygnwmh pou zalizw th lista me erwthseis ths C.
>Giannhs
>
>

Lucky man, to exw etoimo :-) Kanei hostname -> IP address & kai to
antistrofo. Here it comes.

------------------------------------------------------------------
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>

main(int argc, char *argv[]){
	unsigned long int addr;
	struct hostent *hp;
	struct in_addr ina;

	if (argc < 2){
		fprintf(stderr, "Usage: %s <host>\n", argv[0]);
		exit(0);
	}

	if ((addr = inet_addr(argv[1])) == INADDR_NONE){ /* hostname given */
		if ((hp = gethostbyname(argv[1])) == NULL){  
			herror("gethostbyname");   /* invalid hostname */
			exit(h_errno);
		}
	}
	else { /* IP address given */
		if ((hp = gethostbyaddr((char *)&addr, 4, AF_INET)) == NULL){
			herror("gethostbyaddr");  /* invalid IP address */
			exit(h_errno);
		}
	}

	printf("Hostname: %s\n", hp->h_name);
	while (*(hp->h_aliases)){  /* Go through aliases */
		printf("Alias: %s\n", *(hp->h_aliases));
		(hp->h_aliases)++;
	}
	printf("Address type: %d\n", hp->h_addrtype);
	printf("Address length: %d\n", hp->h_length);
	while (*(hp->h_addr_list)){  /* Go through address list */
		bcopy(*(hp->h_addr_list), (char *)&ina, hp->h_length);
		printf("IP address: %s\n", inet_ntoa(ina));
		(hp->h_addr_list)++;
	}
}
------------------------------------------------------------------

   Hope this helps,
       Costas

-- 
Constantinos A. Kotsokalis || C.Kotsokalis at ece.ntua.gr
National Technical University of Athens - GREECE
Electrical and Computer Engineering Department
"111111111 * 111111111 = 12345678987654321"
--
====================================================================
Gia boithia (h na diagrafhte) e-mail sto majordomo at hellug.gr
Ta archives tis listas einai sto http://lists.hellug.gr
prin steilete kapoia erothsh psakte mipos exei hdh apanththei.
Gia opoiodipote problima stilte e-mail ston owner-linux-greek-users
====================================================================



More information about the Linux-greek-users mailing list