forked from facebookarchive/fb-adb
/
core.c
347 lines (298 loc) · 9.43 KB
/
core.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
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <errno.h>
#include <assert.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include "core.h"
#include "ringbuf.h"
#include "channel.h"
__attribute__((noreturn,format(printf,1,2)))
static void
die_proto_error(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
die(ECOMM,
"protocol error: %s",
xavprintf(fmt, args));
}
static bool
detect_msg(struct ringbuf* rb, struct msg* mhdr)
{
memset(mhdr, 0, sizeof (*mhdr));
size_t avail = ringbuf_size(rb);
if (avail < sizeof (*mhdr))
return false;
ringbuf_copy_out(rb, mhdr, sizeof (*mhdr));
if (avail < mhdr->size) {
if (mhdr->size - avail > ringbuf_room(rb))
die_proto_error("impossibly large message: "
"type:%u sz:%lu room:%lu",
mhdr->type,
(unsigned long)(mhdr->size - avail),
(unsigned long)ringbuf_room(rb));
return false;
}
return true; /* Can now read msg */
}
static void
fb_adb_sh_process_msg_channel_data(struct fb_adb_sh* sh,
struct msg_channel_data* m)
{
unsigned nrch = sh->nrch;
struct channel* cmdch = sh->ch[FROM_PEER];
if (m->channel <= NR_SPECIAL_CH || m->channel > nrch)
die_proto_error("data: invalid channel %d", m->channel);
struct channel* c = sh->ch[m->channel];
if (c->dir == CHANNEL_FROM_FD)
die_proto_error("wrong channel direction ch=%u", m->channel);
size_t payloadsz = m->msg.size - sizeof (*m);
if (c->fdh == NULL) {
/* Channel already closed. Just drop the write. */
ringbuf_note_removed(cmdch->rb, payloadsz);
return;
}
/* If we received more data than will fit in the receive
* buffer, peer didn't respect window requirements. */
if (ringbuf_room(c->rb) < payloadsz)
die_proto_error("window desync");
struct iovec iov[2];
ringbuf_readable_iov(cmdch->rb, iov, payloadsz);
channel_write(c, iov, 2);
ringbuf_note_removed(cmdch->rb, payloadsz);
}
static void
fb_adb_sh_process_msg_channel_window(struct fb_adb_sh* sh,
struct msg_channel_window* m)
{
unsigned nrch = sh->nrch;
if (m->channel <= NR_SPECIAL_CH || m->channel > nrch)
die_proto_error("window: invalid channel %d", m->channel);
struct channel* c = sh->ch[m->channel];
if (c->dir == CHANNEL_TO_FD)
die_proto_error("wrong channel direction");
if (c->fdh == NULL)
return; /* Channel already closed */
if (SATADD(&c->window, c->window, m->window_delta)) {
die_proto_error("window overflow!?");
}
}
static void
fb_adb_sh_process_msg_channel_close(struct fb_adb_sh* sh,
struct msg_channel_close* m)
{
unsigned nrch = sh->nrch;
if (m->channel <= NR_SPECIAL_CH || m->channel > nrch)
return; /* Ignore invalid close */
struct channel* c = sh->ch[m->channel];
c->sent_eof = true; /* Peer already knows we're closed. */
channel_close(c);
}
void
read_cmdmsg(struct fb_adb_sh* sh, struct msg mhdr, void* mbuf, size_t msz)
{
if (mhdr.size != msz)
die_proto_error("wrong msg size type:%u expected:%u received:%u",
mhdr.type,
(unsigned) msz,
mhdr.size);
struct channel* cmdch = sh->ch[FROM_PEER];
ringbuf_copy_out(cmdch->rb, mbuf, msz);
ringbuf_note_removed(cmdch->rb, msz);
}
void
fb_adb_sh_process_msg(struct fb_adb_sh* sh, struct msg mhdr)
{
struct channel* cmdch = sh->ch[FROM_PEER];
if (mhdr.type == MSG_CHANNEL_DATA) {
struct msg_channel_data m;
if (mhdr.size < sizeof (m))
die_proto_error("wrong msg size %u", mhdr.size);
ringbuf_copy_out(cmdch->rb, &m, sizeof (m));
ringbuf_note_removed(cmdch->rb, sizeof (m));
dbgmsg(&m.msg, "recv");
fb_adb_sh_process_msg_channel_data(sh, &m);
} else if (mhdr.type == MSG_CHANNEL_WINDOW) {
struct msg_channel_window m;
read_cmdmsg(sh, mhdr, &m, sizeof (m));
dbgmsg(&m.msg, "recv");
fb_adb_sh_process_msg_channel_window(sh, &m);
} else if (mhdr.type == MSG_CHANNEL_CLOSE) {
struct msg_channel_close m;
read_cmdmsg(sh, mhdr, &m, sizeof (m));
dbgmsg(&m.msg, "recv");
fb_adb_sh_process_msg_channel_close(sh, &m);
} else {
ringbuf_note_removed(cmdch->rb, mhdr.size);
die(ECOMM, "unrecognized command %d (sz=%hu)",
mhdr.type, mhdr.size);
}
}
static size_t
fb_adb_maxoutmsg(struct fb_adb_sh* sh)
{
return XMIN(sh->max_outgoing_msg,
ringbuf_room(sh->ch[TO_PEER]->rb));
}
static void
xmit_acks(struct channel* c, unsigned chno, struct fb_adb_sh* sh)
{
size_t maxoutmsg = fb_adb_maxoutmsg(sh);
struct msg_channel_window m;
if (c->bytes_written > 0 && maxoutmsg >= sizeof (m)) {
memset(&m, 0, sizeof (m));
m.msg.type = MSG_CHANNEL_WINDOW;
m.msg.size = sizeof (m);
m.channel = chno;
m.window_delta = c->bytes_written;
dbgmsg(&m.msg, "send");
channel_write(sh->ch[TO_PEER], &(struct iovec){&m, sizeof (m)}, 1);
c->bytes_written = 0;
}
}
static void
xmit_data(struct channel* c,
unsigned chno,
struct fb_adb_sh* sh)
{
if (c->dir != CHANNEL_FROM_FD)
return;
size_t maxoutmsg = fb_adb_maxoutmsg(sh);
size_t avail = ringbuf_size(c->rb);
struct msg_channel_data m;
if (maxoutmsg > sizeof (m) && avail > 0) {
size_t payloadsz = XMIN(avail, maxoutmsg - sizeof (m));
struct iovec iov[3] = {{ &m, sizeof (m) }};
ringbuf_readable_iov(c->rb, &iov[1], payloadsz);
memset(&m, 0, sizeof (m));
m.msg.type = MSG_CHANNEL_DATA;
m.channel = chno;
m.msg.size = iovec_sum(iov, ARRAYSIZE(iov));
assert(chno != 0);
dbgmsg(&m.msg, "send");
channel_write(sh->ch[TO_PEER], iov, ARRAYSIZE(iov));
ringbuf_note_removed(c->rb, payloadsz);
}
}
static void
xmit_eof(struct channel* c,
unsigned chno,
struct fb_adb_sh* sh)
{
struct msg_channel_close m;
if (c->fdh == NULL &&
c->sent_eof == false &&
ringbuf_size(c->rb) == 0 &&
fb_adb_maxoutmsg(sh) >= sizeof (m))
{
memset(&m, 0, sizeof (m));
m.msg.type = MSG_CHANNEL_CLOSE;
m.msg.size = sizeof (m);
m.channel = chno;
dbgmsg(&m.msg, "send");
channel_write(sh->ch[TO_PEER], &(struct iovec){&m, sizeof (m)}, 1);
c->sent_eof = true;
}
}
static void
do_pending_close(struct channel* c)
{
if (c->dir == CHANNEL_TO_FD &&
c->fdh != NULL &&
ringbuf_size(c->rb) == 0 &&
c->pending_close)
{
channel_close(c);
}
}
void
io_loop_init(struct fb_adb_sh* sh)
{
struct channel** ch = sh->ch;
unsigned nrch = sh->nrch;
unsigned chno;
for (chno = 0; chno < nrch; ++chno)
if (ch[chno]->fdh != NULL)
fd_set_blocking_mode(ch[chno]->fdh->fd, non_blocking);
}
void
io_loop_do_io(struct fb_adb_sh* sh)
{
SCOPED_RESLIST(rl);
dbgch("before io_loop_do_io", sh->ch, sh->nrch);
struct channel** ch = sh->ch;
unsigned nrch = sh->nrch;
struct pollfd polls[nrch];
short work = 0;
for (unsigned chno = 0; chno < nrch; ++chno) {
polls[chno] = channel_request_poll(ch[chno]);
work |= polls[chno].events;
}
if (work != 0) {
if (ppoll(polls, nrch, NULL, sh->poll_mask) < 0
&& errno != EINTR)
{
die_errno("poll");
}
}
for (unsigned chno = 0; chno < nrch; ++chno)
if (polls[chno].revents != 0)
channel_poll(ch[chno]);
}
void
io_loop_pump(struct fb_adb_sh* sh)
{
SCOPED_RESLIST(rl);
struct channel** ch = sh->ch;
unsigned chno;
unsigned nrch = sh->nrch;
assert(nrch >= NR_SPECIAL_CH);
struct msg mhdr;
while (detect_msg(ch[FROM_PEER]->rb, &mhdr))
sh->process_msg(sh, mhdr);
for (chno = 0; chno < nrch; ++chno)
xmit_acks(ch[chno], chno, sh);
for (chno = 0; chno < nrch; ++chno) {
if (chno > NR_SPECIAL_CH)
xmit_data(ch[chno], chno, sh);
do_pending_close(ch[chno]);
xmit_eof(ch[chno], chno, sh);
}
}
void
queue_message_synch(struct fb_adb_sh* sh, struct msg* m)
{
PUMP_WHILE(sh, fb_adb_maxoutmsg(sh) < m->size);
dbgmsg(m, "send[synch]");
channel_write(sh->ch[TO_PEER], &(struct iovec){m, m->size}, 1);
}
struct msg*
read_msg(int fd, reader rdr)
{
struct msg mhdr;
size_t nr_read = rdr(fd, &mhdr, sizeof (mhdr));
if (nr_read < sizeof (mhdr))
die_proto_error("peer disconnected");
if (mhdr.size < sizeof (mhdr))
die_proto_error("impossible message");
dbg("read msg header type:%u size:%u", mhdr.type, mhdr.size);
struct msg* m = xalloc(mhdr.size);
memcpy(m, &mhdr, sizeof (mhdr));
char* rest = (char*) m + sizeof (mhdr);
size_t restsz = mhdr.size - sizeof (mhdr);
nr_read = rdr(fd, rest, restsz);
if (nr_read < restsz)
die_proto_error("truncated message");
return m;
}