/* 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);
}
'[+] Information > [-] Network' 카테고리의 다른 글
[2009/06/22] 옵션 예제소스(sock_type.c, set_buf.c, get_buf.c, get_buf_win.c) (0) | 2009.06.22 |
---|---|
[2009/06/22] 소켓의 옵션 조작하기 (0) | 2009.06.22 |
[2009/06/21] 도메인 이름과 IP주소의 변환. (0) | 2009.06.21 |
[2009/06/21] 도메인 이름과 DNS (0) | 2009.06.21 |