-
Notifications
You must be signed in to change notification settings - Fork 1
/
ping.c
172 lines (149 loc) · 4.61 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
/*
* _ping.c
*
* Created on: 11 апр. 2019 г.
* Author: a.khakimov
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/ip_icmp.h>
#define PING_PKT_DATA_SZ 64
#define _err_ok 0
#define _err_failed -1
#define _err_timeout 1
typedef struct {
struct icmp hdr;
char data[PING_PKT_DATA_SZ];
} ping_pkt_t;
typedef struct {
struct ip ip_hdr;
ping_pkt_t ping_pkt;
} ip_pkt_t;
static void prepare_icmp_pkt (ping_pkt_t *ping_pkt );
static ulong get_cur_time_ms ( );
static ushort checksum (void *b, int len );
/* _ping - Check the availability of the communication partner with a ping request.
* result:
* * _err_ok(0)
* * _err_failed(-1)
* * _err_timeout(1)
*/
int ping(const char* ip, const ulong timeout, ulong* reply_time)
{
if (ip == NULL || timeout == 0) {
return _err_failed;
}
ping_pkt_t ping_pkt;
prepare_icmp_pkt(&ping_pkt);
const short reply_id = ping_pkt.hdr.icmp_hun.ih_idseq.icd_id;
struct sockaddr_in to_addr;
to_addr.sin_family = AF_INET;
if (!inet_aton(ip, (struct in_addr*)&to_addr.sin_addr.s_addr)) {
printf("inet_aton\n");
return _err_failed;
}
if (!strcmp(ip, "255.255.255.255") || to_addr.sin_addr.s_addr == 0xFFFFFFFF) {
return _err_failed;
}
const int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0) {
printf("socket() %s\n", strerror(errno));
return _err_failed;
}
const ulong start_time_ms = get_cur_time_ms();
const socklen_t socklen = sizeof(struct sockaddr_in);
if (sendto(sock, &ping_pkt, sizeof(ping_pkt_t), 0, (struct sockaddr*)&to_addr, socklen) <= 0) {
close(sock);
return _err_failed;
}
int result = _err_failed;
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
for (;;) {
fd_set rfd;
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
int n = select(sock + 1, &rfd, 0, 0, &tv);
if (n == 0) {
result = _err_timeout;
break;
}
if (n < 0) {
break;
}
const ulong elapsed_time = (get_cur_time_ms() - start_time_ms);
if (elapsed_time > timeout) {
result = _err_timeout;
break;
} else {
const int new_timeout = timeout - elapsed_time;
tv.tv_sec = new_timeout / 1000;
tv.tv_usec = (new_timeout % 1000) * 1000;
}
if (FD_ISSET(sock, &rfd)) {
ip_pkt_t ip_pkt;
struct sockaddr_in from_addr;
socklen_t socklen = sizeof(struct sockaddr_in);
if (recvfrom(sock, &ip_pkt, sizeof(ip_pkt_t), 0, (struct sockaddr*)&from_addr, &socklen) <= 0) {
break;
}
if (to_addr.sin_addr.s_addr == from_addr.sin_addr.s_addr
&& reply_id == ip_pkt.ping_pkt.hdr.icmp_hun.ih_idseq.icd_id) {
if (reply_time != NULL) {
*reply_time = elapsed_time;
}
result = _err_ok;
break;
}
}
}
close(sock);
return result;
}
static void prepare_icmp_pkt(ping_pkt_t *ping_pkt)
{
memset(ping_pkt, 0, sizeof(ping_pkt_t));
int i = 0;
for (; i < sizeof(ping_pkt->data) - 1; ++i) {
ping_pkt->data[i] = i + 'a';
}
ping_pkt->data[i] = 0;
srand(time(NULL));
const short random_id = rand();
ping_pkt->hdr.icmp_type = ICMP_ECHO;
ping_pkt->hdr.icmp_hun.ih_idseq.icd_id = random_id;
ping_pkt->hdr.icmp_hun.ih_idseq.icd_seq = 0;
ping_pkt->hdr.icmp_cksum = checksum(ping_pkt, sizeof(ping_pkt_t));
}
static ulong get_cur_time_ms()
{
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
ulong time_ms = time.tv_sec * 1000 + (time.tv_nsec / 1000000);
return time_ms;
}
static ushort checksum(void *b, int len)
{
ushort *buf = b;
uint sum=0;
ushort 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;
}