-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.c
More file actions
278 lines (228 loc) · 6.16 KB
/
ping.c
File metadata and controls
278 lines (228 loc) · 6.16 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/ip_icmp.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
// Define the Packet Constants
// ping packet size
#define PING_PKT_S 64
// Automatic port number
#define PORT_NO 0
// Automatic port number
#define PING_SLEEP_RATE 1000000
// Gives the timeout delay for receiving packets
// in seconds
#define RECV_TIMEOUT 1
// Define the Ping Loop
int pingloop=1;
// Flag to determine whether -rtt was set
int ttl_flag = 0;
// User value for ttl 64
// 64 will be the default
int ttl_param = 64;
// ping packet structure
struct ping_pkt
{
struct icmphdr hdr;
char msg[PING_PKT_S-sizeof(struct icmphdr)];
};
// Calculating the Check Sum
unsigned short checksum(void *b, int len)
{ unsigned short *buf = b;
unsigned int sum=0;
unsigned short result;
for ( sum = 0; len > 1; len -= 2 )
sum += *buf++;
if ( len == 1 )
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
// Interrupt handler
void intHandler(int dummy)
{
pingloop=0;
}
// Performs a DNS lookup
char *dns_lookup(char *addr_host, struct sockaddr_in *addr_con)
{
printf("\nResolving DNS..\n");
struct hostent *host_entity;
char *ip=(char*)malloc(NI_MAXHOST*sizeof(char));
int i;
if ((host_entity = gethostbyname(addr_host)) == NULL)
{
// No ip found for hostname
return NULL;
}
//filling up address structure
strcpy(ip, inet_ntoa(*(struct in_addr *)
host_entity->h_addr));
(*addr_con).sin_family = host_entity->h_addrtype;
(*addr_con).sin_port = htons (PORT_NO);
(*addr_con).sin_addr.s_addr = *(long*)host_entity->h_addr;
return ip;
}
// make a ping request
void send_ping(int ping_sockfd, struct sockaddr_in *ping_addr,
char *ping_ip, char *rev_host)
{
int ttl_val=ttl_param, msg_count=0, i, addr_len, flag=1,
msg_received_count=0;
struct ping_pkt pckt;
struct sockaddr_in r_addr;
struct timespec time_start, time_end, tfs, tfe;
long double rtt_msec=0, total_msec=0;
struct timeval tv_out;
tv_out.tv_sec = RECV_TIMEOUT;
tv_out.tv_usec = 0;
clock_gettime(CLOCK_MONOTONIC, &tfs);
// set socket options at ip to TTL and value to 64,
// change to what you want by setting ttl_val
if (setsockopt(ping_sockfd, SOL_IP, IP_TTL,
&ttl_val, sizeof(ttl_val)) != 0)
{
printf("\nSetting socket options to TTL failed!\n");
return;
}
if (ttl_flag)
{
printf("\nSocket set to TTL %d\n", ttl_val);
}
// setting timeout of recv setting
setsockopt(ping_sockfd, SOL_SOCKET, SO_RCVTIMEO,
(const char*)&tv_out, sizeof tv_out);
// send icmp packet in an infinite loop
while(pingloop)
{
// flag is whether packet was sent or not
flag=1;
//filling packet
bzero(&pckt, sizeof(pckt));
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = getpid();
for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
pckt.msg[i] = i+'0';
pckt.msg[i] = 0;
pckt.hdr.un.echo.sequence = msg_count++;
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
usleep(PING_SLEEP_RATE);
//send packet
clock_gettime(CLOCK_MONOTONIC, &time_start);
if ( sendto(ping_sockfd, &pckt, sizeof(pckt), 0,
(struct sockaddr*) ping_addr,
sizeof(*ping_addr)) <= 0)
{
printf("\nPacket Sending Failed!\n");
flag=0;
}
//receive packet
addr_len=sizeof(r_addr);
if ( recvfrom(ping_sockfd, &pckt, sizeof(pckt), 0,
(struct sockaddr*)&r_addr, &addr_len) <= 0
&& msg_count>1)
{
printf("\nPacket receive failed!\n");
}
else
{
clock_gettime(CLOCK_MONOTONIC, &time_end);
double timeElapsed = ((double)(time_end.tv_nsec -
time_start.tv_nsec))/1000000.0;
rtt_msec = (time_end.tv_sec-
time_start.tv_sec) * 1000.0
+ timeElapsed;
// if packet was not sent, don't receive
if(flag)
{
if(!(pckt.hdr.type ==69 && pckt.hdr.code==0))
{
printf("Error..Packet received with ICMP type %d code %d\n",
pckt.hdr.type, pckt.hdr.code);
}
else if (ttl_flag == 0)
{
printf("%d bytes (h: %s) (%s) msg_seq=%d rtt = %Lf ms.\n",
PING_PKT_S, rev_host,
ping_ip, msg_count,
rtt_msec);
msg_received_count++;
}
else
{
printf("%d bytes (h: %s) (%s) msg_seq=%d ttl=%d rtt = %Lf ms.\n",
PING_PKT_S, rev_host,
ping_ip, msg_count,
ttl_val, rtt_msec);
msg_received_count++;
}
}
}
}
clock_gettime(CLOCK_MONOTONIC, &tfe);
double timeElapsed = ((double)(tfe.tv_nsec -
tfs.tv_nsec))/1000000.0;
total_msec = (tfe.tv_sec-tfs.tv_sec)*1000.0+
timeElapsed;
printf("\n===%s ping statistics===\n", ping_ip);
printf("\n%d packets sent, %d packets received, %f percent packet loss. Total time: %Lf ms.\n\n",
msg_count, msg_received_count,
((msg_count - msg_received_count)/msg_count) * 100.0,
total_msec);
}
int main(int argc, char *argv[])
{
int sockfd;
char *ip_addr;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
char net_buf[NI_MAXHOST];
int address = 1;
if((argc < 2) && (argc != 4))
{
printf("\nFormat %s <-ttl 'x'> <address>\n-ttl flag is optional", argv[0]);
return 0;
}
if ((argc == 4) && (strcmp(argv[1], "-ttl") != 0))
{
printf("\nFormat %s <-ttl 'x'> <address>\n-ttl flag is optional", argv[0]);
return 0;
}
if (argc == 4)
{
ttl_flag = 1;
ttl_param = atoi(argv[2]);
address = 3;
}
ip_addr = dns_lookup(argv[address], &addr_con);
if(ip_addr==NULL)
{
printf("\nDNS lookup failed! Could not resolve hostname!\n");
return 0;
}
printf("\nTrying to connect to '%s' IP: %s\n",
argv[address], ip_addr);
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(sockfd<0)
{
printf("\nSocket file descriptor not received!!\n");
return 0;
}
else
printf("\nSocket file descriptor %d received\n", sockfd);
signal(SIGINT, intHandler);//catching interrupt
//send pings continuously
send_ping(sockfd, &addr_con, ip_addr, argv[address]);
return 0;
}