forked from fifteenhex/smolutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.c
More file actions
173 lines (136 loc) · 3 KB
/
Copy pathping.c
File metadata and controls
173 lines (136 loc) · 3 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
// SPDX-License-Identifier: GPL-3.0-or-later
#include "config.h"
#include "common.h"
#include "net.h"
#include "resolv.h"
#define TIMEOUT 2
#define ICMP_ECHO 8
struct icmphdr {
__u8 type;
__u8 code;
__sum16 checksum;
union {
struct {
__be16 id;
__be16 sequence;
} echo;
__be32 gateway;
struct {
__be16 __unused;
__be16 mtu;
} frag;
__u8 reserved[4];
} un;
};
#define PAYLOAD_LEN 4
static const char payload[] = "smol4life";
#define PACKET_LEN (sizeof(struct icmphdr) + sizeof(payload))
static uint16_t checksum(void *data, int len) {
uint16_t *buf = data;
uint32_t sum = 0;
while (len > 1) {
sum += *buf++;
len -= 2;
}
if (len == 1)
sum += *(uint8_t *)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
return ~sum;
}
static int send_request(int sock, struct sockaddr_in *dst)
{
uint8_t buf[PACKET_LEN] = { 0 };
struct icmphdr *hdr = (struct icmphdr *)buf;
int ret;
hdr->type = ICMP_ECHO;
hdr->un.echo.id = (uint16_t)getpid();
hdr->un.echo.sequence = 1;
memcpy(buf + sizeof(struct icmphdr), payload, sizeof(payload));
hdr->checksum = checksum(buf, PACKET_LEN);
ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr *)dst, sizeof(*dst));
if (ret != sizeof(buf)) {
error("Failed to send ICMP packet: %d:%d\n", ret, errno);
return -1;
}
return 0;
}
/* 0 - no error, no response, 1 - no error, response, >0 error */
static int wait_for_response(int sock)
{
struct sockaddr_in src;
socklen_t srclen = sizeof(src);
uint8_t resp[256];
ssize_t len;
len = recvfrom(sock, resp, sizeof(resp), 0, (struct sockaddr *)&src, &srclen);
if (len < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
printf("Timeout waiting for response\n");
return 0;
}
else
return -1;
}
return 1;
}
static int try_to_lookup_host(const char *host, struct in_addr *result)
{
struct resolv_buf addresses;
int ret;
ret = resolv_doit(host, &addresses);
if (ret)
return ret;
memcpy(result, &addresses.addr_v4[0], sizeof(*result));
return 0;
}
int main (int argc, char **argv, char **envp)
{
int __cleanup_fd sock = -1;
struct timeval t0, t1;
struct sockaddr_in dst = {
.sin_family = AF_INET,
};
const char *host;
int ret;
int i;
if (argc != 2)
return 1;
host = argv[1];
ret = inet_aton(argv[1], &dst.sin_addr);
if (ret) {
}
else {
char ip[INET_ADDRSTRLEN];
ret = try_to_lookup_host(host, &dst.sin_addr);
if (ret) {
verbose("Failed to resolv host\n");
return 1;
}
inet_ntop(AF_INET, &dst.sin_addr, ip, sizeof(ip));
printf("Resolved %s to %s\n", host, ip);
}
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0) {
verbose("Failed to create socket\n");
return 1;
}
ret = smolutils_net_setsockrxtimeout(sock, TIMEOUT);
if (ret)
return 1;
for (i = 0; i < 10; i++)
{
ret = send_request(sock, &dst);
if (ret)
return 1;
gettimeofday(&t0, NULL);
ret = wait_for_response(sock);
if (ret == 0)
continue;
if (ret < 0)
return 1;
gettimeofday(&t1, NULL);
printf("Got reply\n");
sleep(2);
}
return 0;
}