/
ping.c
220 lines (181 loc) · 6.22 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
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
/* Packet types. */
#define ECHO_REPLY 0
#define ECHO_REQUEST 8
struct echo_header_t {
uint8_t type;
uint8_t code;
uint16_t checksum;
uint16_t id;
uint16_t seqno;
};
struct module_t {
int initialized;
short ping_id;
#if CONFIG_FS_CMD_PING_PING == 1
struct fs_command_t cmd_ping;
#endif
};
static struct module_t module;
#if CONFIG_FS_CMD_PING_PING == 1
static int cmd_ping_cb(int argc,
const char *argv[],
void *out_p,
void *in_p,
void *arg_p,
void *call_arg_p)
{
int res;
int round_trip_time_ms;
struct inet_ip_addr_t address;
struct time_t round_trip_time, timeout;
const char *remote_host_p;
if (argc != 2) {
std_fprintf(out_p, OSTR("Usage: ping <remote host>\r\n"));
return (-1);
}
remote_host_p = argv[1];
if (inet_aton(remote_host_p, &address) != 0) {
std_fprintf(out_p, OSTR("Bad ip address '%s'.\r\n"), remote_host_p);
return (-1);
}
timeout.seconds = 3;
timeout.nanoseconds = 0;
res = ping_host_by_ip_address(&address, &timeout, &round_trip_time);
if (res == 0) {
round_trip_time_ms = (round_trip_time.seconds * 1000
+ round_trip_time.nanoseconds / 1000000);
std_fprintf(out_p,
OSTR("Successfully pinged '%s' in %d ms.\r\n"),
remote_host_p,
round_trip_time_ms);
} else {
std_fprintf(out_p, OSTR("Failed to ping '%s'.\r\n"), remote_host_p);
}
return (0);
}
#endif
int ping_module_init(void)
{
/* Return immediately if the module is already initialized. */
if (module.initialized == 1) {
return (0);
}
module.initialized = 1;
#if CONFIG_FS_CMD_PING_PING == 1
fs_command_init(&module.cmd_ping,
CSTR("/inet/ping/ping"),
cmd_ping_cb,
NULL);
fs_command_register(&module.cmd_ping);
#endif
return (socket_module_init());
}
int ping_host_by_ip_address(struct inet_ip_addr_t *address_p,
struct time_t *timeout_p,
struct time_t *round_trip_time_p)
{
ASSERTN(address_p != NULL, EINVAL);
ASSERTN(timeout_p != NULL, EINVAL);
ASSERTN(round_trip_time_p != NULL, EINVAL);
int res = -1;
ssize_t reply_size;
struct socket_t socket;
struct time_t start, stop, timeout;
struct echo_header_t request;
struct echo_header_t *reply_p;
uint8_t reply[28]; /* IP header and icmp packet. */
struct inet_addr_t address;
reply_size = -1;
address.ip = *address_p;
timeout = *timeout_p;
/* Initiate the ping echo packet. */
request.type = ECHO_REQUEST;
request.code = 0;
request.checksum = 0;
request.id = htons(module.ping_id++);
request.seqno = htons(1);
request.checksum = htons(inet_checksum(&request, sizeof(request)));
/* Use a raw socket to send and receive ICMP packets. */
if (socket_open_raw(&socket) != 0) {
return (-1);
}
time_get(&start);
if (socket_sendto(&socket,
&request,
sizeof(request),
0,
&address) == sizeof(request)) {
/* Packets for _all_ raw sockets with IMCP and received. */
while (1) {
if (chan_poll(&socket, &timeout) == NULL) {
res = -ETIMEDOUT;
break;
}
time_get(&stop);
reply_size = socket_recvfrom(&socket,
&reply,
sizeof(reply),
0,
&address);
/* Calculate the round trip time (RTT). */
time_diff(round_trip_time_p, &stop, &start);
/* Decrement the timeout by the elapsed time waiting for a
packet. */
if ((round_trip_time_p->seconds < 0)
|| (round_trip_time_p->nanoseconds < 0)) {
timeout.seconds = 0;
timeout.nanoseconds = -1;
} else {
time_diff(&timeout, timeout_p, round_trip_time_p);
}
if (reply_size != sizeof(reply)) {
continue;
}
reply_p = (struct echo_header_t *)&reply[20];
/* Check the reply. */
if ((reply_p->type != ECHO_REPLY)
|| (reply_p->code != request.code)
|| (reply_p->id != request.id)
|| (reply_p->seqno != request.seqno)) {
continue;
}
if (inet_checksum(reply_p, sizeof(*reply_p)) == 0) {
/* A valid reply was received. */
res = 0;
}
break;
}
}
socket_close(&socket);
return (res);
}