-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.c
209 lines (174 loc) · 4.47 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
/*************************************************************************
> File Name: ping.c
> Author: xxx
> Mail: XXX.com
> Created Time: 2017年07月14日 星期五 15时49分02秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netdb.h>
#define ICMP_SIZE (sizeof(struct icmp))
#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0
#define BUF_SIZE 1024
#define NUM 6
#define UCHAR unsigned char
#define USHORT unsigned short
#define UINT unsigned int
struct icmp
{
UCHAR type;
UCHAR code;
USHORT checksum;
USHORT id;
USHORT sequence;
struct timeval timestamp;
};
struct ip
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
UCHAR hlen:4;
UCHAR version:4;
#endif
#if __BYTE_ORDER == __BIG__ENDIAN
UCHAR versin:4;
UCHAR hlen:4;
#endif
UCHAR tos;
USHORT len;
USHORT id;
USHORT offsset;
UCHAR ttl;
UCHAR protocol;
USHORT checksum;
struct in_addr ipsrc;
struct in_addr ipdst;
};
char buf[BUF_SIZE] = {0};
USHORT checkSum(USHORT *, int);
float timediff(struct timeval *, struct timeval *);
void pack(struct icmp *, int);
int unpack(char *,int , char *);
int main(int argc, char *argv[])
{
struct hostent *host;
struct icmp sendicmp;
struct sockaddr_in from;
struct sockaddr_in to;
int fromlen = 0;
int sockfd;
int nsend = 0;
int nreceived = 0;
int i,n;
in_addr_t inaddr;
memset(&from, 0, sizeof(struct sockaddr_in));
memset(&to, 0, sizeof(struct sockaddr_in));
if(argc < 2)
{
printf("use : %s hostname/IP address\n",argv[0]);
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
{
printf("socket() error\n");
exit(1);
}
to.sin_family = AF_INET;
if((inaddr = inet_addr(argv[1])) ==
INADDR_NONE)
{
if((host = gethostbyname(argv[1])) == NULL)
{
printf("gethostbyname() erroe\n");
exit(1);
}
to.sin_addr = *(struct in_addr *) host->h_addr_list[0];
}
else
to.sin_addr.s_addr = inaddr;
printf("ping %s (%s) : %d bytes of data.\n",argv[1],inet_ntoa(to.sin_addr),(int)ICMP_SIZE);
for(int i = 0; i < NUM; i++)
{
nsend++;
memset(&sendicmp, 0 , ICMP_SIZE);
pack(&sendicmp, nsend);
if(sendto(sockfd, &sendicmp, ICMP_SIZE,0,(struct sockaddr *)&to,sizeof(to))==-1)
{
printf("send() error!\n");
continue;
}
if((n = recvfrom(sockfd, buf,BUF_SIZE,0,(struct sockaddr *)&from,&fromlen)) < 0)
{
printf("recvfrom() error!\n");
continue;
}
nreceived ++;
if(unpack(buf, n, inet_ntoa(from.sin_addr))==-1)
{
printf("unpack() error \n");
}
sleep(1);
}
printf("%s pint statistics \n",argv[1]);
printf("%d packets transmitted, %d received, %%%d packets loss\n",nsend,nreceived,(nsend-nreceived)/nsend*100);
return 0;
}
USHORT checkSum(USHORT *addr, int len)
{
UINT sum = 0;
while(len > 1)
{
sum += *addr++;
len -= 2;
}
if(len == 1)sum += *(UCHAR *)addr;
sum = (sum >> 16)+(sum & 0xffff);
sum += (sum >> 16);
return (USHORT) ~sum;
}
float timediff(struct timeval *begin, struct timeval *end)
{
int n;
n = (end->tv_sec - begin->tv_sec)*1000000+(end->tv_usec - begin->tv_usec);
return (float)(n/1000);
}
void pack(struct icmp *icmp, int sequence)
{
icmp->type = ICMP_ECHO;
icmp->code = 0;
icmp->checksum = 0;
icmp->id = getpid();
icmp->sequence = sequence;
gettimeofday(&icmp->timestamp,0);
icmp->checksum = checkSum((USHORT *)icmp, ICMP_SIZE);
}
int unpack(char *buf, int len, char *addr)
{
int ipheadlen;
struct ip *ip;
struct icmp *icmp;
float rtt;
struct timeval end;
ip = (struct ip *)buf;
ipheadlen = ip->hlen << 2;
icmp = (struct icmp *)(buf + ipheadlen);
len -= ipheadlen;
if(len < 8)
{
printf("ICMP packets\'s length is less than 8\n");
return -1;
}
if(icmp->type != ICMP_ECHOREPLY || icmp->id != getpid())
{
printf("ERROE!\n");
return -1;
}
gettimeofday(&end,0);
rtt = timediff(&icmp->timestamp, &end);
printf("%d bytes from %s : icmp_seq = %u ttl = %d rttl = %f ms\n",len, addr,icmp->sequence, ip->ttl, rtt);
return 0;
}