-
Notifications
You must be signed in to change notification settings - Fork 36
/
packets.c
347 lines (300 loc) · 7 KB
/
packets.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Many codes in this file was borrowed from GdbConnection.cc in rr
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "packets.h"
struct packet_buf
{
uint8_t buf[PACKET_BUF_SIZE];
int end;
} in, out;
int sock_fd;
uint8_t *inbuf_get()
{
return in.buf;
}
int inbuf_end()
{
return in.end;
}
void pktbuf_insert(struct packet_buf *pkt, const uint8_t *buf, ssize_t len)
{
if (pkt->end + len >= sizeof(pkt->buf))
{
puts("Packet buffer overflow");
exit(-2);
}
memcpy(pkt->buf + pkt->end, buf, len);
pkt->end += len;
}
void pktbuf_erase_head(struct packet_buf *pkt, ssize_t end)
{
memmove(pkt->buf, pkt->buf + end, pkt->end - end);
pkt->end -= end;
}
void inbuf_erase_head(ssize_t end)
{
pktbuf_erase_head(&in, end);
}
void pktbuf_clear(struct packet_buf *pkt)
{
pkt->end = 0;
}
static bool poll_socket(int sock_fd, short events)
{
struct pollfd pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.fd = sock_fd;
pfd.events = events;
int ret = poll(&pfd, 1, -1);
if (ret < 0)
{
perror("poll() failed");
exit(-1);
}
return ret > 0;
}
static bool poll_incoming(int sock_fd)
{
return poll_socket(sock_fd, POLLIN);
}
static void poll_outgoing(int sock_fd)
{
poll_socket(sock_fd, POLLOUT);
}
void read_data_once()
{
int ret;
ssize_t nread;
uint8_t buf[4096];
poll_incoming(sock_fd);
nread = read(sock_fd, buf, sizeof(buf));
if (nread <= 0)
{
puts("Connection closed");
exit(0);
}
pktbuf_insert(&in, buf, nread);
}
void write_flush()
{
size_t write_index = 0;
while (write_index < out.end)
{
ssize_t nwritten;
poll_outgoing(sock_fd);
nwritten = write(sock_fd, out.buf + write_index, out.end - write_index);
if (nwritten < 0)
{
printf("Write error\n");
exit(-2);
}
write_index += nwritten;
}
pktbuf_clear(&out);
}
void write_data_raw(const uint8_t *data, ssize_t len)
{
pktbuf_insert(&out, data, len);
}
void write_hex(unsigned long hex)
{
char buf[32];
size_t len;
len = snprintf(buf, sizeof(buf) - 1, "%02lx", hex);
write_data_raw((uint8_t *)buf, len);
}
void write_packet_bytes(const uint8_t *data, size_t num_bytes)
{
uint8_t checksum;
size_t i;
write_data_raw((uint8_t *)"$", 1);
for (i = 0, checksum = 0; i < num_bytes; ++i)
checksum += data[i];
write_data_raw((uint8_t *)data, num_bytes);
write_data_raw((uint8_t *)"#", 1);
write_hex(checksum);
}
void write_packet(const char *data)
{
write_packet_bytes((const uint8_t *)data, strlen(data));
}
void write_binary_packet(const char *pfx, const uint8_t *data, ssize_t num_bytes)
{
uint8_t *buf;
ssize_t pfx_num_chars = strlen(pfx);
ssize_t buf_num_bytes = 0;
int i;
buf = malloc(2 * num_bytes + pfx_num_chars);
memcpy(buf, pfx, pfx_num_chars);
buf_num_bytes += pfx_num_chars;
for (i = 0; i < num_bytes; ++i)
{
uint8_t b = data[i];
switch (b)
{
case '#':
case '$':
case '}':
case '*':
buf[buf_num_bytes++] = '}';
buf[buf_num_bytes++] = b ^ 0x20;
break;
default:
buf[buf_num_bytes++] = b;
break;
}
}
write_packet_bytes(buf, buf_num_bytes);
free(buf);
}
bool skip_to_packet_start()
{
ssize_t end = -1;
for (size_t i = 0; i < in.end; ++i)
if (in.buf[i] == '$' || in.buf[i] == INTERRUPT_CHAR)
{
end = i;
break;
}
if (end < 0)
{
pktbuf_clear(&in);
return false;
}
pktbuf_erase_head(&in, end);
assert(1 <= in.end);
assert('$' == in.buf[0] || INTERRUPT_CHAR == in.buf[0]);
return true;
}
void read_packet()
{
while (!skip_to_packet_start())
read_data_once();
write_data_raw((uint8_t *)"+", 1);
write_flush();
}
static int async_io_enabled;
void (*request_interrupt)(void);
static void enable_async_notification(int fd)
{
#if defined(F_SETFL) && defined(FASYNC)
int save_fcntl_flags;
save_fcntl_flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, save_fcntl_flags | FASYNC);
#if defined(F_SETOWN)
fcntl(fd, F_SETOWN, getpid());
#endif
#endif
}
static void input_interrupt(int unused)
{
if (async_io_enabled)
{
int nread;
char buf;
nread = read(sock_fd, &buf, 1);
assert(nread == 1 && buf == INTERRUPT_CHAR);
request_interrupt();
}
}
static void block_unblock_async_io(int block)
{
sigset_t sigio_set;
sigemptyset(&sigio_set);
sigaddset(&sigio_set, SIGIO);
sigprocmask(block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
}
void enable_async_io(void)
{
if (async_io_enabled)
return;
block_unblock_async_io(0);
async_io_enabled = 1;
}
void disable_async_io(void)
{
if (!async_io_enabled)
return;
block_unblock_async_io(1);
async_io_enabled = 0;
}
void initialize_async_io(void (*intr_func)(void))
{
request_interrupt = intr_func;
async_io_enabled = 1;
disable_async_io();
signal(SIGIO, input_interrupt);
}
void remote_prepare(char *name)
{
int ret;
char *port_str;
int port;
struct sockaddr_in addr;
char *port_end;
const int one = 1;
int listen_fd;
port_str = strchr(name, ':');
if (port_str == NULL)
return;
*port_str = '\0';
port = strtoul(port_str + 1, &port_end, 10);
if (port_str[1] == '\0' || *port_end != '\0')
printf("Bad port argument: %s", name);
listen_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
if (listen_fd < 0)
{
perror("socket() failed");
exit(-1);
}
ret = setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (ret < 0)
{
perror("setsockopt() failed");
exit(-1);
}
printf("Listening on port %d\n", port);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = *name ? inet_addr(name) : INADDR_ANY;
addr.sin_port = htons(port);
if (addr.sin_addr.s_addr == INADDR_NONE)
{
printf("Bad host argument: %s", name);
exit(-1);
}
ret = bind(listen_fd, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0)
{
perror("bind() failed");
exit(-1);
}
ret = listen(listen_fd, 1);
if (ret < 0)
{
perror("listen() failed");
exit(-1);
}
sock_fd = accept(listen_fd, NULL, NULL);
if (sock_fd < 0)
{
perror("accept() failed");
exit(-1);
}
ret = setsockopt(sock_fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
ret = setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
enable_async_notification(sock_fd);
close(listen_fd);
pktbuf_clear(&in);
pktbuf_clear(&out);
}