-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ping.cpp
195 lines (160 loc) · 4.28 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
#include "Ping.h"
#define BYTE 8
#define DEFDATALEN (64-ICMP_MINLEN)
#define MAXIPLEN 60
#define MAXICMPLEN 76
void decToBinary(int n, char *binary_number) {
unsigned int i;
int mask = 1 << (sizeof(int) * BYTE) - 1;
for(i=0; i<(sizeof(int) * BYTE); i++) {
binary_number[i] = n & mask ? '1':'0';
n = n << 1;
}
binary_number[i] = '\0';
}
uint16_t in_cksum(uint16_t *addr, unsigned len)
{
uint16_t answer = 0;
/*
* Algorithm is simple, using a 32 bit accumulator (sum), add
* sequential 16 bit words to it, and at the end, fold back all the
* carry bits from the t 16 bits into the lower 16 bits.
*/
uint32_t sum = 0;
while (len > 1) {
sum += *addr++;
len -= 2;
}
if (len == 1) {
*(unsigned char *)&answer = *(unsigned char *)addr ;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
Ping::Ping() {
}
Ping::Ping(std::string ip) {
this->ip = ip;
this->initSendToSockAddr();
}
void Ping::initSendToSockAddr() {
this->sendTo.sin_family = SOCK_DOMAIN;
this->sendTo.sin_addr.s_addr = inet_addr(this->ip.c_str());
}
Ping::Ping(const Ping& orig) {
}
void Ping::startPinging() {
int ssize;
unsigned char buff[MAX_ICMP_DATA_SIZE];
int cc = DEFDATALEN + ICMP_MINLEN;
cout << "CC PREDICTED SIZE: " << cc << endl;
struct icmp *icp;
icp = (struct icmp *)buff;
icp->icmp_type = ICMP_ECHO;
icp->icmp_code = 0;
icp->icmp_cksum = 0;
icp->icmp_seq = 12345;
icp->icmp_id = getpid();
cout << "BEFORE CHECKSUM ICMP SIZE: " << sizeof(*icp) << endl;
icp->icmp_cksum = in_cksum((unsigned short *)icp, cc);
cout << "AFTER CHECKSUM ICMP SIZE: " << sizeof(icp) << endl;
ssize = sendto(this->sockFd, (unsigned char*)buff, cc, 0, (struct sockaddr*)&this->sendTo, (socklen_t)sizeof(struct sockaddr_in));
/*int ssize, cc, retval, fromlen, ret, packlen, hlen, end_t;
cc = DEFDATALEN + ICMP_MINLEN;
u_char *packet, outpack[MAX_ICMP_DATA];
struct ip *ip;
fd_set rfds;
struct timeval tv;
struct timeval start, end;
* /
//ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
// const struct sockaddr *dest_addr, socklen_t addrlen);
struct sockaddr_in from;
packlen = DEFDATALEN + MAXIPLEN + MAXICMPLEN;
/*if ( (packet = (u_char *)malloc((u_int)packlen)) == NULL)
{
cerr << "malloc error\n";
//return -1;
}
*/
//ssize = sendto(this->sockFd, (char *)outpack, cc, 0, (struct sockaddr*)&this->sendTo, (socklen_t)sizeof(struct sockaddr_in));
/*if (ssize < 0 || ssize != cc)
{
if (ssize < 0)
perror("sendto error");
cout << "wrote " << "hoestname " << cc << " chars, ret= " << ssize << endl;
}
*/
/*
FD_ZERO(&rfds);
FD_SET(this->sockFd, &rfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
bool cont = true;
*/
/*
while(cont)
{
retval = select(s+1, &rfds, NULL, NULL, &tv);
if (retval == -1)
{
perror("select()");
//return -1;
}
else if (retval)
{
fromlen = sizeof(sockaddr_in);
if ( (ret = recvfrom(s, (char *)packet, packlen, 0,(struct sockaddr *)&from, (socklen_t*)&fromlen)) < 0)
{
perror("recvfrom error");
//return -1;
}
// Check the IP header
ip = (struct ip *)((char*)packet);
hlen = sizeof( struct ip );
if (ret < (hlen + ICMP_MINLEN))
{
cerr << "packet too short (" << ret << " bytes) from " << this->ip << endl;;
//return -1;
}
// Now the ICMP part
icp = (struct icmp *)(packet + hlen);
if (icp->icmp_type == ICMP_ECHOREPLY)
{
if (icp->icmp_seq != 12345)
{
cout << "received sequence # " << icp->icmp_seq << endl;
continue;
}
if (icp->icmp_id != getpid())
{
cout << "received id " << icp->icmp_id << endl;
continue;
}
cont = false;
}
else
{
cout << "Recv: not an echo reply" << endl;
continue;
}
gettimeofday(&end, NULL);
end_t = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
if(end_t < 1)
end_t = 1;
cout << "Elapsed time = " << end_t << " usec" << endl;
return end_t;
}
else
{
cout << "No data within one seconds.\n";
//return 0;
}
}
*/
}
Ping::~Ping() {
}