본문 바로가기

[+] Information/[-] Network

[2009/06/21] 예제소스(gethostbyname.c, gethostbyname_win.c, gethostbyaddr.c, gethostbyaddr_win.c)

/* gethostbyname.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>

void error_handling(char *message);

int main(int argc, char **argv)
{
 int i;
 struct hostent *host;
 
 if(argc!=2){
  printf("Usage : %s <addr>\n", argv[0]);
  exit(1);
 }
 
 host=gethostbyname(argv[1]);
 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(char *message)
{
 fputs(message, stderr);
 fputc('\n', stderr);
 exit(1);
}



/* gethostbyname_win.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define BUFSIZE 30

void ErrorHandling(char *message);

int main(int argc, char **argv)
{
 WSADATA  wsaData;
 
 int i;
 struct hostent *host; //HOSTENT* host
 
 if(argc!=2){
  printf("Usage : %s <IP>\n", argv[0]);
  exit(1);
 }
 
 if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  ErrorHandling("WSAStartup() error!");
 
 host=gethostbyname(argv[1]);
 if(!host)
  ErrorHandling("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( *(IN_ADDR*)host->h_addr_list[i] ));
 }
 
 WSACleanup();
 return 0;
}

void ErrorHandling(char *message)
{
 fputs(message, stderr);
 fputc('\n', stderr);
 exit(1);
}



/* gethostbyaddr.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>

void error_handling(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(char *message)
{
 fputs(message, stderr);
 fputc('\n', stderr);
 exit(1);
}




/* gethostbyaddr_win.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define BUFSIZE 30

void ErrorHandling(char *message);

int main(int argc, char **argv)
{
 WSADATA  wsaData;

 int i;
 struct hostent *host;  // HOSTENT* host
 SOCKADDR_IN addr;
 
 if(argc!=2){
  printf("Usage : %s <IP>\n", argv[0]);
  exit(1);
 }
 
 if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  ErrorHandling("WSAStartup() error!");
 
 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)
  ErrorHandling("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( *(IN_ADDR*)host->h_addr_list[i] ));
 }
 
 WSACleanup();
 return 0;
}

void ErrorHandling(char *message)
{
 fputs(message, stderr);
 fputc('\n', stderr);
 exit(1);
}