-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_search.cpp
55 lines (49 loc) · 1.14 KB
/
dns_search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <winsock.h>
#define USING_CIN true
using namespace std;
int main(int argc, char *argv[]) {
WSADATA wsadata;
int n_result = WSAStartup(0x101, (LPWSADATA) &wsadata);
struct in_addr sAddr;
LPHOSTENT hp;
char ip_addr[16];
if (n_result != 0) {
cout << "WSA Initialization failed: " << n_result;
WSACleanup;
return 0;
}
if (USING_CIN) {
// using cin
cout << "Input an IP address: ";
cin >> ip_addr;
} else {
// using argv
if (argc > 1) {
strcpy(ip_addr, argv[1]);
} else {
strcpy(ip_addr, "140.130.41.11");
}
}
sAddr.s_addr = inet_addr(ip_addr);
try {
hp = gethostbyaddr((LPSTR) &sAddr, sizeof(sAddr), AF_INET);
} catch (exception &e) {
cout << e.what() << endl;
}
if (hp == NULL) {
cout << "WSA Error: " << WSAGetLastError() << endl;
} else {
cout << ip_addr << "'s domain name: " << hp->h_name << endl;
if (hp->h_aliases[0] != nullptr) {
for(int i=0; hp->h_aliases[i]; i++) {
cout << "nickname: " << hp->h_aliases[i] << endl;
}
} else {
cout << "nickname: (NULL)" << endl;
}
cout << "ip addr: " << inet_ntoa(*(LPIN_ADDR) (hp->h_addr));
}
return 0;
}