-
Notifications
You must be signed in to change notification settings - Fork 4
/
event_loop.c
420 lines (390 loc) · 9.74 KB
/
event_loop.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Part of `snmp-query-engine`.
*
* Copyright 2012-2013, Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include "sqe.h"
struct program_stats PS;
static JudyL socks = NULL;
#ifdef WITH_KQUEUE
int kq = -1;
#endif
#ifdef WITH_EPOLL
int ep = -1;
#endif
struct socket_info *
new_socket_info(int fd)
{
struct socket_info *si, **slot;
si = malloc(sizeof(*si));
if (!si)
croak(1, "new_socket_info: malloc(socket_info)");
bzero(si, sizeof(*si));
si->PS.active_client_connections = -42;
si->PS.total_client_connections = -42;
si->PS.active_timers_sec = -42;
si->PS.active_timers_usec = -42;
si->PS.total_timers_sec = -42;
si->PS.total_timers_usec = -42;
si->PS.bad_snmp_responses = -42;
si->PS.active_oid_infos = -42;
si->PS.total_oid_infos = -42;
si->PS.destination_throttles = -42;
si->PS.oids_ignored = -42;
si->PS.destination_ignores = -42;
si->PS.udp_receive_buffer_size = -42;
si->PS.udp_send_buffer_size = -42;
si->PS.udp_send_buffer_overflow = -42;
si->PS.packets_on_the_wire = -42;
si->PS.max_packets_on_the_wire = -42;
si->PS.global_throttles = -42;
si->PS.program_version = -42;
si->PS.octets_received = -42;
si->PS.octets_sent = -42;
gettimeofday(&si->created, NULL);
si->fd = fd;
TAILQ_INIT(&si->send_bufs);
JLI(slot, socks, fd);
if (slot == PJERR)
croak(2, "new_socket_info: JLI failed");
if (*slot)
croak(3, "new_socket_info: assertion failed, fd %d is already there", fd);
*slot = si;
return si;
}
static struct iovec io_buf[IOV_MAX];
static void
flush_buffers(struct socket_info *si)
{
struct send_buf *sb;
int i, n, tot;
if (!si->n_send_bufs) {
fprintf(stderr, "flush_buffers: fd %d: unexpectedly nothing to flush\n", si->fd);
on_write(si, NULL);
return;
}
/* XXX handle case where there is only one specially */
i = 0;
tot = 0;
TAILQ_FOREACH(sb, &si->send_bufs, send_list) {
io_buf[i].iov_base = sb->buf + sb->offset;
io_buf[i].iov_len = sb->size - sb->offset;
tot += sb->size - sb->offset;
i++;
if (i >= IOV_MAX)
break;
}
if ( (n = writev(si->fd, io_buf, i)) < 0) {
switch (errno) {
case EPIPE:
fprintf(stderr, "flush_buffers: EPIPE during writev\n");
if (si->eof_handler) si->eof_handler(si);
return;
case ECONNRESET:
fprintf(stderr, "flush_buffers: ECONNRESET during writev\n");
if (si->eof_handler) si->eof_handler(si);
return;
}
croak(1, "flush_buffers: writev");
}
while (n > 0) {
sb = TAILQ_FIRST(&si->send_bufs);
if (!sb)
croakx(2, "flush_buffers: send_bufs queue unexpectedly empty");
if (n >= sb->size - sb->offset) {
TAILQ_REMOVE(&si->send_bufs, sb, send_list);
n -= sb->size - sb->offset;
free(sb->buf);
free(sb);
si->n_send_bufs--;
} else {
sb->offset += n;
n = 0;
}
}
if (TAILQ_EMPTY(&si->send_bufs)) {
on_write(si, NULL);
}
//if (write(fd, buf, size) < 0)
// croak(1, "tcp_send: write");
}
void
tcp_send(struct socket_info *si, void *buf, int size)
{
struct send_buf *sb;
int buf_size;
sb = TAILQ_LAST(&si->send_bufs, send_buf_head);
if (sb && sb->buf_size - sb->size >= size) {
memcpy(sb->buf + sb->size, buf, size);
sb->size += size;
return;
}
/* XXX in reality, try to send something right away */
sb = malloc(sizeof(*sb));
if (!sb)
croak(1, "tcp_send: malloc(send_buf)");
bzero(sb, sizeof(*sb));
buf_size = size > 4096 ? size : 4096;
sb->buf = malloc(buf_size);
if (!sb->buf)
croak(1, "tcp_send: malloc(sb->buf)");
sb->size = size;
sb->buf_size = buf_size;
sb->offset = 0;
memcpy(sb->buf, buf, size);
if (TAILQ_EMPTY(&si->send_bufs)) {
on_write(si, flush_buffers);
}
TAILQ_INSERT_TAIL(&si->send_bufs, sb, send_list);
si->n_send_bufs++;
}
void
delete_socket_info(struct socket_info *si)
{
int rc;
struct send_buf *n1, *n2;
n1 = TAILQ_FIRST(&si->send_bufs);
while (n1 != NULL) {
n2 = TAILQ_NEXT(n1, send_list);
free(n1->buf);
free(n1);
n1 = n2;
}
TAILQ_INIT(&si->send_bufs);
si->n_send_bufs = 0;
JLD(rc, socks, si->fd);
close(si->fd);
free(si);
}
#ifdef WITH_EPOLL
static void
set_handlers(struct socket_info *si,
void (*read_handler)(struct socket_info *si),
void (*write_handler)(struct socket_info *si))
{
struct epoll_event set_ev;
int op;
int was_monitored = 0;
if (ep < 0) {
if ( (ep = epoll_create(10)) < 0)
croak(1, "set_handlers: epoll_create");
}
if (si->read_handler || si->write_handler) {
was_monitored = 1;
}
si->read_handler = read_handler;
si->write_handler = write_handler;
set_ev.events = 0;
if (si->read_handler)
set_ev.events |= EPOLLIN;
if (si->write_handler)
set_ev.events |= EPOLLOUT;
if (was_monitored) {
if (set_ev.events)
op = EPOLL_CTL_MOD;
else
op = EPOLL_CTL_DEL;
} else {
if (set_ev.events)
op = EPOLL_CTL_ADD;
else
return;
}
set_ev.data.fd = si->fd;
if (epoll_ctl(ep, op, si->fd, &set_ev) < 0)
croak(1, "set_handlers: epoll_ctl");
}
#endif
void
binary_dump(FILE *f, void *buf, int len)
{
unsigned char *s = buf;
int i;
for (i = 0; i < len; i++) {
fprintf(f, "%02x ", (unsigned)s[i]);
if (i % 16 == 15 && i < len-1) {
int j;
fprintf(f, " ");
for (j = i - 16; j <= i; j++) {
fprintf(f, "%c", isprint(s[j]) ? s[j] : '.');
}
fprintf(f, "\n");
}
}
fprintf(f, "\n");
}
void
on_eof(struct socket_info *si, void (*eof_handler)(struct socket_info *si))
{
si->eof_handler = eof_handler;
}
void
on_read(struct socket_info *si, void (*read_handler)(struct socket_info *si))
{
#ifdef WITH_KQUEUE
si->read_handler = read_handler;
if (kq < 0) {
if ( (kq = kqueue()) < 0)
croak(1, "on_read: kqueue");
}
{
struct kevent set_ke, get_ke;
int nev;
unsigned flags = EV_ADD | EV_RECEIPT;
flags |= read_handler ? EV_ENABLE : EV_DISABLE;
EV_SET(&set_ke, si->fd, EVFILT_READ, flags, 0, 0, 0);
nev = kevent(kq, &set_ke, 1, &get_ke, 1, NULL);
if (nev < 0)
croak(1, "on_read: kevent");
if (nev != 1)
croakx(1, "on_read: unexpected nev %d", nev);
if ((get_ke.flags & EV_ERROR) == 0)
croakx(1, "on_read: unexpectedly EV_ERROR is not set");
if (get_ke.data != 0) {
errno = get_ke.data;
croak(1, "on_read: kevent (error in data)");
}
}
#endif
#ifdef WITH_EPOLL
set_handlers(si, read_handler, si->write_handler);
#endif
}
void
on_write(struct socket_info *si, void (*write_handler)(struct socket_info *si))
{
si->write_handler = write_handler;
#ifdef WITH_KQUEUE
if (kq < 0) {
if ( (kq = kqueue()) < 0)
croak(1, "on_write: kqueue");
}
{
struct kevent set_ke, get_ke;
int nev;
unsigned flags = EV_ADD | EV_RECEIPT;
flags |= write_handler ? EV_ENABLE : EV_DISABLE;
EV_SET(&set_ke, si->fd, EVFILT_WRITE, flags, 0, 0, 0);
nev = kevent(kq, &set_ke, 1, &get_ke, 1, NULL);
if (nev < 0)
croak(1, "on_write: kevent");
if (nev != 1)
croakx(1, "on_write: unexpected nev %d", nev);
if ((get_ke.flags & EV_ERROR) == 0)
croakx(1, "on_write: unexpectedly EV_ERROR is not set");
if (get_ke.data != 0) {
errno = get_ke.data;
croak(1, "on_write: kevent (error in data)");
}
}
#endif
#ifdef WITH_EPOLL
set_handlers(si, si->read_handler, write_handler);
#endif
}
#ifdef WITH_KQUEUE
void
event_loop(void)
{
struct kevent ke[10];
int nev, i, ms;
struct timespec to;
while (1) {
ms = ms_to_next_timer();
to.tv_sec = ms / 1000;
to.tv_nsec = (ms % 1000)*1000000;
nev = kevent(kq, NULL, 0, ke, 10, &to);
if (nev < 0)
croak(1, "event_loop: kevent");
for (i = 0; i < nev; i++) {
struct socket_info *si, **slot;
if (ke[i].filter == EVFILT_READ) {
JLG(slot, socks, ke[i].ident);
if (slot && *slot) {
si = *slot;
if (ke[i].flags & EV_EOF) {
if (si->eof_handler) {
si->eof_handler(si);
} else {
fprintf(stderr, "event_loop: EVFILT_READ: ident %u - socket does not have an eof handler\n", (unsigned)ke[i].ident);
}
} else {
if (si->read_handler) {
si->read_handler(si);
} else {
fprintf(stderr, "event_loop: EVFILT_READ: ident %u - socket does not have a read handler\n", (unsigned)ke[i].ident);
}
}
}
} else if (ke[i].filter == EVFILT_WRITE) {
JLG(slot, socks, ke[i].ident);
if (slot && *slot) {
si = *slot;
if (ke[i].flags & EV_EOF) {
if (si->eof_handler) {
si->eof_handler(si);
} else {
fprintf(stderr, "event_loop: EVFILT_WRITE: ident %u - socket does not have an eof handler\n", (unsigned)ke[i].ident);
}
} else {
if (si->write_handler) {
si->write_handler(si);
} else {
fprintf(stderr, "event_loop: EVFILT_WRITE: ident %u - socket does not have a write handler\n", (unsigned)ke[i].ident);
}
}
}
} else {
fprintf(stderr, "event_loop: unexpected filter value %d, ident %u\n", ke[i].filter, (unsigned)ke[i].ident);
}
}
trigger_timers();
}
}
#endif
#ifdef WITH_EPOLL
void
event_loop(void)
{
struct epoll_event ev[10];
int nev, i, ms;
while (1) {
ms = ms_to_next_timer();
nev = epoll_wait(ep, ev, 10, ms);
if (nev < 0)
croak(1, "event_loop: epoll_wait");
for (i = 0; i < nev; i++) {
struct socket_info *si, **slot;
if ((ev[i].events & EPOLLIN)) {
JLG(slot, socks, ev[i].data.fd);
if (slot && *slot) {
si = *slot;
if (si->read_handler) {
si->read_handler(si);
} else {
fprintf(stderr, "event_loop: EPOLLIN: fd %u - socket does not have a read handler\n", (unsigned)ev[i].data.fd);
}
}
}
if ((ev[i].events & EPOLLOUT)) {
JLG(slot, socks, ev[i].data.fd);
if (slot && *slot) {
si = *slot;
if (si->write_handler) {
si->write_handler(si);
} else {
fprintf(stderr, "event_loop: EPOLLOUT: fd %u - socket does not have a write handler\n", (unsigned)ev[i].data.fd);
}
}
}
if (!(ev[i].events & (EPOLLIN|EPOLLOUT))) {
fprintf(stderr, "event_loop: unexpected event 0x%x, fd %u\n", ev[i].events, (unsigned)ev[i].data.fd);
}
}
trigger_timers();
}
}
#endif