forked from cntools/cnping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.c
267 lines (229 loc) · 5.32 KB
/
ping.c
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//Copyright 2017 <>< C. Lohr, under the MIT/x11 License
//Rewritten from Sean Walton and Macmillan Publishers.
//Most of it was rewritten but the header was never updated.
//Now I finished the job.
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "ping.h"
#include "os_generic.h"
#include "error_handling.h"
#ifdef WIN32
#include <winsock2.h>
#define SOL_IP 0
#define F_SETFL 4
#define ICMP_ECHO 8
#define IP_TTL 2
#define O_NONBLOCK 04000
#pragma comment(lib, "Ws2_32.lib")
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdint.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#ifdef __APPLE__
#ifndef SOL_IP
#define SOL_IP IPPROTO_IP
#endif
#endif
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#endif
#if defined WIN32 || defined __APPLE__
struct icmphdr
{
uint8_t type;
uint8_t code;
uint16_t checksum;
union
{
struct
{
uint16_t id;
uint16_t sequence;
} echo;
uint32_t gateway;
struct
{
uint16_t __unused;
uint16_t mtu;
} frag;
} un;
};
#endif
float pingperiodseconds;
int precise_ping;
#define PACKETSIZE 65536
struct packet
{
struct icmphdr hdr;
unsigned char msg[PACKETSIZE-sizeof(struct icmphdr)];
};
int sd;
int pid=-1;
int ping_failed_to_send;
struct sockaddr_in psaddr;
uint16_t checksum( const unsigned char * start, uint16_t len )
{
uint16_t i;
const uint16_t * wptr = (uint16_t*) start;
uint32_t csum = 0;
for (i=1;i<len;i+=2)
csum += (uint32_t)(*(wptr++));
if( len & 1 ) //See if there's an odd number of bytes?
csum += *(uint8_t*)wptr;
if (csum>>16)
csum = (csum & 0xFFFF)+(csum >> 16);
//csum = (csum>>8) | ((csum&0xff)<<8);
return ~csum;
}
void listener()
{
#ifndef WIN32
const int val=255;
int sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
{
ERRM("Error: could not set TTL option - did you forget to run as root or sticky bit cnping?\n");
exit( -1 );
}
#endif
struct sockaddr_in addr;
unsigned char buf[66000];
#ifdef WIN32
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
#endif
for (;;)
{
socklen_t addrlenval=sizeof(addr);
int bytes;
#ifdef WIN32
WSAPOLLFD fda[1];
fda[0].fd = sd;
fda[0].events = POLLIN;
WSAPoll(fda, 1, 10);
#endif
keep_retry_quick:
bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &addrlenval );
if( bytes == -1 ) continue;
if( buf[20] != 0 ) continue; //Make sure ping response.
if( buf[9] != 1 ) continue; //ICMP
if( addr.sin_addr.s_addr != psaddr.sin_addr.s_addr ) continue;
if ( bytes > 0 )
display(buf + 28, bytes - 28 );
else
{
ERRM("Error: recvfrom failed");
}
goto keep_retry_quick;
}
ERRM( "Fault on listen.\n" );
exit( 0 );
}
void ping(struct sockaddr_in *addr )
{
int cnt=1;
#ifdef WIN32
{
//Setup windows socket for nonblocking io.
unsigned long iMode = 1;
ioctlsocket(sd, FIONBIO, &iMode);
}
#else
if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
ERRM("Warning: Request nonblocking I/O failed.");
#endif
double stime = OGGetAbsoluteTime();
struct packet pckt;
do
{
int rsize = load_ping_packet( pckt.msg, sizeof( pckt.msg ) );
memset( &pckt.hdr, 0, sizeof( pckt.hdr ) ); //This needs to be here, but I don't know why, since I think the struct is fully populated.
pckt.hdr.code = 0;
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = pid;
pckt.hdr.un.echo.sequence = cnt++;
pckt.hdr.checksum = checksum((const unsigned char *)&pckt, sizeof( pckt.hdr ) + rsize );
int sr = sendto(sd, (char*)&pckt, sizeof( pckt.hdr ) + rsize , 0, (struct sockaddr*)addr, sizeof(*addr));
if( sr <= 0 )
{
ping_failed_to_send = 1;
}
else
{
ping_failed_to_send = 0;
}
if( precise_ping )
{
double ctime;
do
{
ctime = OGGetAbsoluteTime();
if( pingperiodseconds >= 1000 ) stime = ctime;
} while( ctime < stime + pingperiodseconds );
stime += pingperiodseconds;
}
else
{
if( pingperiodseconds > 0 )
{
uint32_t dlw = 1000000.0*pingperiodseconds;
OGUSleep( dlw );
}
}
} while( pingperiodseconds >= 0 );
//close( sd ); //Hacky, we don't close here because SD doesn't come from here, rather from ping_setup. We may want to run this multiple times.
}
void ping_setup()
{
pid = getpid();
#ifdef WIN32
WSADATA wsaData;
int r = WSAStartup(MAKEWORD(2,2), &wsaData);
if( r )
{
ERRM( "Fault in WSAStartup\n" );
exit( -2 );
}
#endif
#ifdef WIN32
sd = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, WSA_FLAG_OVERLAPPED);
{
int lttl = 0xff;
if (setsockopt(sd, IPPROTO_IP, IP_TTL, (const char*)<tl, sizeof(lttl)) == SOCKET_ERROR)
{
printf( "Warning: No IP_TTL.\n" );
}
}
#else
const int val=255;
sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
{
ERRM("Error: Failed to set TTL option. Are you root? Or can do sock_raw sockets?\n");
exit( -1 );
}
#endif
if ( sd < 0 )
{
ERRM("Error: Could not create raw socket\n");
exit(0);
}
}
void do_pinger( const char * strhost )
{
struct hostent *hname;
hname = gethostbyname(strhost);
memset(&psaddr, 0, sizeof(psaddr));
psaddr.sin_family = hname->h_addrtype;
psaddr.sin_port = 0;
psaddr.sin_addr.s_addr = *(long*)hname->h_addr;
ping(&psaddr );
}
char errbuffer[1024];