Tag Archives: namelookup

lookup name from IP , pretty useless for LAN, since not NBT query. just keep for record that do not spent more time

char* name = argv[1];
sa.sin_family = AF_INET;
sa.sin_addr.s_addr=inet_addr(name);
len = sizeof (struct sockaddr_in);

char hbuf[NI_MAXHOST];

if (int r=getnameinfo((sockaddr *)&sa, len, hbuf, sizeof(hbuf),NULL, 0, NI_NAMEREQD|NI_DGRAM)!=0)
printf("could not resolve hostname %d %s",r,gai_strerror(r));
else
printf("host=%s\n", hbuf);
//EAI_AGAIN

struct hostent *hp;
long addr;

addr = inet_addr(name);
if (hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET)) {
printf("Hostname:\t%s\n", hp->h_name);
printf("Aliases:\t");
while (hp->h_aliases[0])
printf("%s ", *hp->h_aliases++);
printf("\n");
printf("Addresses:\t");
while (hp->h_addr_list[0])
printf("%s ", inet_ntoa(*(struct in_addr *) * hp->h_addr_list++));
printf("\n");
exit(0);
}