-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathping.c
More file actions
89 lines (68 loc) · 1.79 KB
/
ping.c
File metadata and controls
89 lines (68 loc) · 1.79 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "slip.h"
#include "ip.h"
#include "icmp.h"
#include "dns.h"
static uint8_t is_ip_address(char *str) {
unsigned int a, b, c, d;
return (sscanf(str, "%u.%u.%u.%u", &a, &b, &c, &d) == 4);
}
static uint8_t response_received = 0;
static uint16_t response_ms;
static uint16_t expected_seq = 0;
static void ping_rx(struct ip_hdr *iph, struct icmp_hdr *icmph) {
uint16_t seq = ntohs(icmph->seq);
uint16_t ttl = iph->ttl;
uint16_t size = ip_data_len(iph);
if (seq != expected_seq) {
return;
}
response_received = 1;
printf("%u bytes from %u.%u.%u.%u: icmp_seq=%u ttl=%u\n",
size,
iph->saddr[0], iph->saddr[1], iph->saddr[2], iph->saddr[3],
seq, ttl);
}
int main(int argc, char *argv[]) {
uint8_t ping_addr[4];
uint8_t dns_server[4] = {8, 8, 8, 8};
uint16_t seq = 0;
uint8_t i = 10;
unsigned int a, b, c, d;
char *host;
const uint16_t timeout_ms = 2000;
if (argc != 2 || !argv[1]) {
puts("Usage: ping host");
return 1;
}
host = argv[1];
ip_init();
icmp_listen(ping_rx);
if (is_ip_address(host)) {
sscanf(host, "%u.%u.%u.%u", &a, &b, &c, &d);
ping_addr[0] = a;
ping_addr[1] = b;
ping_addr[2] = c;
ping_addr[3] = d;
printf("PING %u.%u.%u.%u...\n\n", ping_addr[0], ping_addr[1], ping_addr[2], ping_addr[3]);
} else {
dns_init(dns_server);
if (!dns_resolve(host, ping_addr)) {
printf("Error: Failed to resolve %s\n", host);
return 0;
}
printf("PING %s (%u.%u.%u.%u)...\n\n", host, ping_addr[0], ping_addr[1], ping_addr[2], ping_addr[3]);
}
while (i--) {
response_received = 0;
expected_seq = seq;
icmp_tx_request(ping_addr, seq++);
while (!response_received) {
slip_rx();
}
sleep(2);
}
return 0;
}