U E D R , A S I H C RSS

Linux Programming/Query Domainname

sample code

~cpp
/**
request domain name thru ip address from DNS server

eternalbleu

2006 03 23

**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

void error_handling(const char* message);

int main(int argc, char ** argv)
{
  struct hostent* host;
  struct sockaddr_in addr;
  int i;
  
  if (argc != 2) {
    printf("USAGE : %s <ip>\n", argv[0]);
    exit(1);
  }

  memset(&addr, 0, sizeof(addr));
  addr.sin_addr.s_addr=inet_addr(argv[1]);

  host = gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);
  if(!host)
    error_handling("gethost... error");

  printf("Officially name : %s \n\n", host->h_name);

  puts("Aliases ------");
  for(i=0; host->h_aliases[i]; i++) {
    puts(host->h_aliases[i]);
  }

  printf("Address Type:$s\n", host->h_addrtype == AF_INET? "AF_INET":"AF_INET6");

  puts("IP Address ------");
  for(i=0; host->h_addr_list[i]; i++) {
    puts(inet_ntoa( *(struct in_addr*)host->h_addr_list[i]));
  }

  return 0;
}

void error_handling(const char* message)
{
  fputs(message, stderr);
  fputc('\n', stderr);
  exit(1);
}
표준 BSD socket 을 이용한 소스. 리눅스에서 컴파일해야함. 대충 바꾸면 윈도우에서도 할 수 잇음.
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0805 sec