-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowip.cpp
61 lines (56 loc) · 1.38 KB
/
showip.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
56
57
58
59
60
61
#include<WinSock2.h>
#include<iphlpapi.h>
#include<WS2tcpip.h>
#include<iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main(int argc, char** argv)
{
WSAData wsaData;
int status;
if (argc != 2)
{
cerr << "[Error]: Invalid Usage!\n showip host";
return 1;
}
if ((status = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)
{
cerr << "[Error]: WSAStartup failed!\n";
exit(1);
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
cerr << "[Error]: Version 2.2 can't be initialised!\n";
exit(1);
}
struct addrinfo hints, * res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
//char* ai_error;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
cerr << "[Error]: " << gai_strerror(status) << endl;
WSACleanup();
}
char buf[40];
void* addr;
cout << "IP address for " << argv[1] << "\n";
for (struct addrinfo* i = res; i != nullptr;i = i->ai_next)
{
if (i->ai_family == AF_INET) {
cout << "\tIPv4 ";
struct sockaddr_in *ain = (sockaddr_in*)i->ai_addr;
addr = (void*)(&ain->sin_addr);
}
else {
cout << "\tIPv6 ";
struct sockaddr_in6* ain = (sockaddr_in6*)i->ai_addr;
addr = (void*)(&ain->sin6_addr);
}
inet_ntop(i->ai_family, addr, buf, sizeof buf);
cout << buf<< endl;
}
freeaddrinfo(res);
WSACleanup();
return 0;
}