-
Notifications
You must be signed in to change notification settings - Fork 9
/
Ping.cpp
230 lines (188 loc) · 7.62 KB
/
Ping.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "Ping.h"
bool Ping::ping(const char * hostname, unsigned int numICMPPackets) {
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 0);
int retWSA;
bool ret = false;
retWSA = WSAStartup(wVersionRequested, &wsaData);
if (!retWSA) {
if (wsaData.wVersion == wVersionRequested) {
ret = sendICMPPacket(hostname, numICMPPackets);
}
WSACleanup();
}
return ret;
}
bool Ping::ping(Network * network, unsigned int numICMPPackets) {
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 0);
int retWSA;
bool ret = false;
retWSA = WSAStartup(wVersionRequested, &wsaData);
if (!retWSA) {
if (wsaData.wVersion == wVersionRequested) {
ret = sendICMPPacket(network, numICMPPackets);
}
WSACleanup();
}
return ret;
}
bool Ping::sendICMPPacket(Network * network, unsigned int numICMPPackets) {
int rawSocket;
LPHOSTENT lpHost;
ECHOREPLY echoReply;
struct sockaddr_in saDest;
struct sockaddr_in saSrc;
bool noData = false;
char * currentIPAddress, * IPDestinyAddr, * computerName;
int nAddrLen = sizeof(struct sockaddr_in);
int nRet;
rawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (rawSocket != SOCKET_ERROR) {
network->clearActiveComputers();
for (unsigned long i = 0; i < network->getNumNetComputers(); i++) {
currentIPAddress = FunctionsIP::sumIP(network->getStartIP(), i);
lpHost = gethostbyname(currentIPAddress);
if (lpHost != NULL) {
saDest.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
saDest.sin_family = AF_INET;
for (unsigned int j = 0; j < numICMPPackets; j++) {
sendEchoRequest(rawSocket, &saDest, (i));
}
}
free(currentIPAddress);
}
Sleep(TIME_TO_SLEEP);
for(;(!noData);) {
if (waitForEchoReply(rawSocket, 1, (network->getNumNetComputers() - 1))) {
nRet = recvfrom(rawSocket, (LPSTR) &echoReply, sizeof(ECHOREPLY), 0, (LPSOCKADDR) &saSrc, &nAddrLen);
if (nRet > 0) {
IPDestinyAddr = FunctionsIP::castingNumberToStringIP(echoReply.ipHdr.iaSrc.s_addr, true);
if ((strcmp(IPDestinyAddr, network->getGateway()) != 0) && (strcmp(IPDestinyAddr, network->getIPmachine()) != 0) &&
(FunctionsIP::castingStringIPToNumber(IPDestinyAddr) >= FunctionsIP::castingStringIPToNumber(network->getStartIP())) &&
(FunctionsIP::castingStringIPToNumber(IPDestinyAddr) <= FunctionsIP::castingStringIPToNumber(network->getEndIP()))) {
if (network->getNumNetActiveComputers() > 0)
network->setComputerList((Computer **) realloc(network->getComputerList(), sizeof(Computer *) * (network->getNumNetActiveComputers() + 1)));
*(network->getComputerList() + network->getNumNetActiveComputers()) = (Computer *) malloc(sizeof(Computer));
*(network->getComputerList() + network->getNumNetActiveComputers()) = new Computer(IPDestinyAddr, false);
network->setNumNetActiveComputers(network->getNumNetActiveComputers() + 1);
}
free(IPDestinyAddr);
}
}
else noData = true;
}
closesocket(rawSocket);
/* Set ComputerName && ResourceShares -> ActiveComputers in Network */
for (unsigned long i = 0; i < network->getNumNetActiveComputers(); i++) {
computerName = FunctionsWindowsNT::getComputerName((*(network->getComputerList() + i))->getIPAddress());
if (computerName != NULL) (*(network->getComputerList() + i))->setComputerName(computerName);
(*(network->getComputerList() + i))->scanResourceShares();
free(computerName);
}
}
return (network->getNumNetActiveComputers() > 0);
}
bool Ping::sendICMPPacket(const char * hostname, unsigned int numICMPPackets) {
int rawSocket;
LPHOSTENT lpHost;
DWORD dwTimeSent, dwElapsed;
u_char cTTL;
struct sockaddr_in saDest;
struct sockaddr_in saSrc;
bool retPing = false;
rawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (rawSocket != SOCKET_ERROR) {
lpHost = gethostbyname(hostname);
if (lpHost != NULL) {
saDest.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
saDest.sin_family = AF_INET;
saSrc.sin_addr.s_addr = htonl(INADDR_ANY);
saSrc.sin_family = AF_INET;
for (unsigned int i = 0; i < numICMPPackets; i++) {
if (sendEchoRequest(rawSocket, &saDest, (i + 1))) {
if (waitForEchoReply(rawSocket, 5, 1)) {
dwTimeSent = recvEchoReply(rawSocket, &saSrc, &cTTL); // Receive reply
if (dwTimeSent != 0) {
dwElapsed = GetTickCount() - dwTimeSent; // Calculate elapsed time
retPing = true;
}
}
}
}
}
closesocket(rawSocket);
}
return retPing;
}
bool Ping::sendEchoRequest(int fdSocket, LPSOCKADDR_IN lpstToAddr, unsigned int IDPacket) {
ECHOREQUEST echoReq;
int nRet;
// Fill in echo request
echoReq.icmpHdr.Type = ICMP_ECHOREQ;
echoReq.icmpHdr.Code = 0;
echoReq.icmpHdr.Checksum = 0;
echoReq.icmpHdr.ID = IDPacket;
echoReq.icmpHdr.Seq = IDPacket;
// Fill in some data to send
for (nRet = 0; nRet < REQ_DATASIZE; nRet++)
echoReq.cData[nRet] = ' ' + nRet;
// Save tick count when sent
echoReq.dwTime = GetTickCount();
// Put data in packet and compute checksum
echoReq.icmpHdr.Checksum = in_cksum((u_short *) &echoReq, sizeof(ECHOREQUEST));
// Send the echo request
nRet = sendto(fdSocket, (LPSTR)&echoReq, sizeof(ECHOREQUEST), 0, (LPSOCKADDR) lpstToAddr, sizeof(SOCKADDR_IN));
if (nRet != SOCKET_ERROR) return true;
return false;
}
DWORD Ping::recvEchoReply(int fdSocket, LPSOCKADDR_IN lpsaFrom, u_char * pTTL) {
ECHOREPLY echoReply;
int nRet;
int nAddrLen = sizeof(struct sockaddr_in);
// Receive the echo reply
nRet = recvfrom(fdSocket, (LPSTR) &echoReply, sizeof(ECHOREPLY), 0, (LPSOCKADDR)lpsaFrom, &nAddrLen);
// Check return value
if (nRet > 0) {
// return time sent and IP TTL
*pTTL = echoReply.ipHdr.TTL;
return(echoReply.echoRequest.dwTime);
}
return 0;
}
u_short Ping::in_cksum(u_short *addr, int len) {
register int nleft = len;
register u_short *w = addr;
register u_short answer;
register int sum = 0;
/* Our algorithm is simple, using a 32 bit accumulator (sum),
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits. */
while( nleft > 1 ) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1 ) {
u_short u = 0;
*(u_char *)(&u) = *(u_char *)w ;
sum += u;
}
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
}
bool Ping::waitForEchoReply(int fdSocket, unsigned int seconds, unsigned int maxIDPacket) {
struct timeval Timeout;
fd_set readfds;
int val = 0;
FD_ZERO(&readfds);
FD_SET(fdSocket, &readfds);
Timeout.tv_sec = seconds;
Timeout.tv_usec = 0;
val = select(maxIDPacket, &readfds, NULL, NULL, &Timeout);
if ((val > 0) && (FD_ISSET(fdSocket, &readfds))) return true;
return false;
}