-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsock_events.h
556 lines (450 loc) · 13.1 KB
/
sock_events.h
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#ifndef SOCK_EVENTS_H
#define SOCK_EVENTS_H
#include <netinet/tcp.h>
#include <pcap/pcap.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <time.h>
typedef enum SockEventType {
SOCK_EV_SOCKET,
SOCK_EV_FORKED_SOCKET,
SOCK_EV_GHOST_SOCKET,
SOCK_EV_BIND,
SOCK_EV_CONNECT,
SOCK_EV_SHUTDOWN,
SOCK_EV_LISTEN,
SOCK_EV_ACCEPT,
SOCK_EV_ACCEPT4,
SOCK_EV_GETSOCKOPT,
SOCK_EV_SETSOCKOPT,
SOCK_EV_SEND,
SOCK_EV_RECV,
SOCK_EV_SENDTO,
SOCK_EV_RECVFROM,
SOCK_EV_SENDMSG,
SOCK_EV_RECVMSG,
#if !defined(__ANDROID__) || __ANDROID_API__ >= 21
SOCK_EV_SENDMMSG,
SOCK_EV_RECVMMSG,
#endif
SOCK_EV_GETSOCKNAME,
SOCK_EV_GETPEERNAME,
SOCK_EV_SOCKATMARK,
SOCK_EV_ISFDTYPE,
// unistd.h
SOCK_EV_WRITE,
SOCK_EV_READ,
SOCK_EV_CLOSE,
SOCK_EV_DUP,
SOCK_EV_DUP2,
SOCK_EV_DUP3,
// sys/uio.h
SOCK_EV_WRITEV,
SOCK_EV_READV,
// sys/ioctl.h
SOCK_EV_IOCTL,
// sendfile.h
SOCK_EV_SENDFILE,
// poll.h
SOCK_EV_POLL,
SOCK_EV_PPOLL,
// sys/select.h
SOCK_EV_SELECT,
SOCK_EV_PSELECT,
// fcntl.h
SOCK_EV_FCNTL,
// epoll.h
SOCK_EV_EPOLL_CTL,
SOCK_EV_EPOLL_WAIT,
SOCK_EV_EPOLL_PWAIT,
// stdio.h
SOCK_EV_FDOPEN,
// others
SOCK_EV_TCP_INFO
} SockEventType;
typedef struct {
SockEventType type;
unsigned long timestamp_usec;
int return_value;
bool success;
int err;
long id;
pid_t thread_id;
} SockEvent;
typedef struct {
int domain;
int type;
int protocol;
bool sock_cloexec;
bool sock_nonblock;
bool filled;
} SockInfo;
typedef struct {
SockEvent super;
SockInfo sock_info;
} SockEvSocket;
typedef struct {
SockEvent super;
SockInfo sock_info;
} SockEvForkedSocket;
/* A ghost socket represents a socket for which we never saw its creation.
* Maybe it was passed from another process using sendmsg, maybe it was created
* using a direct syscall. To verify if we don't forget any source of new
* socket. */
typedef struct {
SockEvent super;
SockInfo sock_info;
} SockEvGhostSocket;
typedef struct {
struct sockaddr_storage sockaddr_sto;
socklen_t len;
} Addr;
typedef struct {
SockEvent super;
Addr addr;
} SockEvBind;
typedef struct {
SockEvent super;
Addr addr;
} SockEvConnect;
typedef struct {
SockEvent super;
bool shut_rd;
bool shut_wr;
} SockEvShutdown;
typedef struct {
SockEvent super;
int backlog;
} SockEvListen;
typedef struct {
SockEvent super;
SockInfo sock_info;
Addr addr;
} SockEvAccept;
typedef struct {
SockEvent super;
SockInfo sock_info;
Addr addr;
int flags;
} SockEvAccept4;
typedef struct {
int level;
int optname;
void *optval;
socklen_t optlen;
bool getsockopt;
int fd;
} Sockopt;
typedef struct {
SockEvent super;
Sockopt sockopt;
} SockEvGetsockopt;
typedef struct {
SockEvent super;
Sockopt sockopt;
} SockEvSetsockopt;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
} SockEvSend;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
} SockEvRecv;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
Addr addr;
} SockEvSendto;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
Addr addr;
} SockEvRecvfrom;
typedef struct {
int iovec_count;
size_t *iovec_sizes;
} Iovec;
typedef struct {
Iovec iovec;
struct sockaddr_storage addr;
int flags;
struct msghdr *msghdr;
} Msghdr;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
Msghdr msghdr;
} SockEvSendmsg;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
Msghdr msghdr;
} SockEvRecvmsg;
typedef struct {
time_t seconds;
long nanoseconds;
} Timeout;
#if !defined(__ANDROID__) || __ANDROID_API__ >= 21
typedef struct {
Msghdr msghdr;
unsigned int bytes_transmitted;
} Mmsghdr;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
int mmsghdr_count;
Mmsghdr *mmsghdr_vec;
} SockEvSendmmsg;
typedef struct {
SockEvent super;
size_t bytes;
int flags;
Timeout timeout;
int mmsghdr_count;
Mmsghdr *mmsghdr_vec;
} SockEvRecvmmsg;
#endif
typedef struct {
SockEvent super;
Addr addr;
} SockEvGetsockname;
typedef struct {
SockEvent super;
Addr addr;
} SockEvGetpeername;
typedef struct { SockEvent super; } SockEvSockatmark;
typedef struct {
SockEvent super;
int fdtype;
} SockEvIsfdtype;
typedef struct {
SockEvent super;
size_t bytes;
} SockEvWrite;
typedef struct {
SockEvent super;
size_t bytes;
} SockEvRead;
typedef struct { SockEvent super; } SockEvClose;
typedef struct {
SockEvent super;
SockInfo sock_info;
} SockEvDup;
typedef struct {
SockEvent super;
SockInfo sock_info;
int newfd;
} SockEvDup2;
typedef struct {
SockEvent super;
SockInfo sock_info;
int newfd;
bool o_cloexec;
} SockEvDup3;
typedef struct {
SockEvent super;
size_t bytes;
Iovec iovec;
} SockEvWritev;
typedef struct {
SockEvent super;
size_t bytes;
Iovec iovec;
} SockEvReadv;
typedef struct {
SockEvent super;
#ifdef __ANDROID__
int request;
#else
unsigned long int request;
#endif
} SockEvIoctl;
typedef struct {
SockEvent super;
size_t bytes;
} SockEvSendfile;
typedef struct {
bool pollin;
bool pollpri;
bool pollout;
bool pollrdhup;
bool pollerr;
bool pollhup;
bool pollnval;
} PollEvents;
typedef struct {
SockEvent super;
Timeout timeout;
PollEvents requested_events;
PollEvents returned_events;
} SockEvPoll;
typedef struct {
SockEvent super;
Timeout timeout;
PollEvents requested_events;
PollEvents returned_events;
} SockEvPpoll;
typedef struct {
bool read;
bool write;
bool except;
} SelectEvents;
typedef struct {
SockEvent super;
Timeout timeout;
SelectEvents requested_events;
SelectEvents returned_events;
} SockEvSelect;
typedef struct {
SockEvent super;
Timeout timeout;
SelectEvents requested_events;
SelectEvents returned_events;
} SockEvPselect;
typedef struct {
SockEvent super;
SockInfo sock_info;
int cmd;
int arg;
} SockEvFcntl;
typedef struct {
SockEvent super;
int op;
uint32_t requested_events;
} SockEvEpollCtl;
typedef struct {
SockEvent super;
int timeout;
uint32_t returned_events;
} SockEvEpollWait;
typedef struct {
SockEvent super;
int timeout;
uint32_t returned_events;
} SockEvEpollPwait;
typedef struct {
SockEvent super;
char *mode;
} SockEvFdopen;
typedef struct {
SockEvent super;
struct tcp_info info;
} SockEvTcpInfo;
typedef struct SockEventNode SockEventNode;
struct SockEventNode {
SockEvent *data;
SockEventNode *next;
};
typedef struct {
// To be freed
SockEventNode *head; // Head for list of events.
SockEventNode *tail; // Tail for list of events.
// Others
int id;
int fd;
SockInfo sock_info;
long events_count;
unsigned long bytes_sent; // Total bytes sent.
unsigned long bytes_received; // Total bytes received.
long last_info_dump_micros; // Time of last info dump in microseconds.
long last_info_dump_bytes; // Total bytes (sent+recv) at last dump.
bool bound;
struct sockaddr_storage bound_addr;
int rtt;
bool *capture_switch;
} Socket;
const char *string_from_sock_event_type(SockEventType type);
void free_socket(Socket *con);
// Packet capture
void sock_start_capture(int fd, const struct sockaddr *connect_addr);
// Events hooks
void sock_ev_socket(int fd, int domain, int type, int protocol);
void sock_ev_bind(int fd, int ret, int err, const struct sockaddr *addr,
socklen_t len);
void sock_ev_connect(int fd, int ret, int err, const struct sockaddr *addr,
socklen_t len);
void sock_ev_shutdown(int fd, int ret, int err, int how);
void sock_ev_listen(int fd, int ret, int err, int backlog);
void sock_ev_accept(int fd, int ret, int err, struct sockaddr *addr,
socklen_t *addr_len);
void sock_ev_accept4(int fd, int ret, int err, struct sockaddr *addr,
socklen_t *addr_len, int flags);
void sock_ev_getsockopt(int fd, int ret, int err, int level, int optname,
const void *optval, socklen_t *optlen);
void sock_ev_setsockopt(int fd, int ret, int err, int level, int optname,
const void *optval, socklen_t optlen);
void sock_ev_send(int fd, int ret, int err, const void *buf, size_t bytes,
int flags);
void sock_ev_recv(int fd, int ret, int err, void *buf, size_t bytes, int flags);
void sock_ev_sendto(int fd, int ret, int err, const void *buf, size_t bytes,
int flags, const struct sockaddr *addr, socklen_t len);
void sock_ev_recvfrom(int fd, int ret, int err, void *buf, size_t bytes,
int flags, const struct sockaddr *addr, socklen_t *len);
void sock_ev_sendmsg(int fd, int ret, int err, const struct msghdr *msg,
int flags);
void sock_ev_recvmsg(int fd, int ret, int err, const struct msghdr *msg,
int flags);
#if !defined(__ANDROID__) || __ANDROID_API__ >= 21
void sock_ev_sendmmsg(int fd, int ret, int err, const struct mmsghdr *vmessages,
unsigned int vlen, int flags);
void sock_ev_recvmmsg(int fd, int ret, int err, const struct mmsghdr *vmessages,
unsigned int vlen, int flags, const struct timespec *tmo);
#endif
void sock_ev_getsockname(int fd, int ret, int err, struct sockaddr *addr,
socklen_t *addrlen);
void sock_ev_getpeername(int fd, int ret, int err, struct sockaddr *addr,
socklen_t *addrlen);
void sock_ev_sockatmark(int fd, int ret, int err);
void sock_ev_isfdtype(int fd, int ret, int err, int fdtype);
void sock_ev_write(int fd, int ret, int err, const void *buf, size_t bytes);
void sock_ev_read(int fd, int ret, int err, void *buf, size_t bytes);
void sock_ev_close(int fd, int ret, int err);
void sock_ev_dup(int fd, int ret, int err);
void sock_ev_dup2(int fd, int ret, int err, int newfd);
void sock_ev_dup3(int fd, int ret, int err, int newfd, int flags);
void sock_ev_writev(int fd, int ret, int err, const struct iovec *iovec,
int iovec_count);
void sock_ev_readv(int fd, int ret, int err, const struct iovec *iovec,
int iovec_count);
#ifdef __ANDROID__
void sock_ev_ioctl(int fd, int ret, int err, int request);
#else
void sock_ev_ioctl(int fd, int ret, int err, unsigned long int request);
#endif
void sock_ev_sendfile(int fd, int ret, int err, int in_fd, off_t *offset,
size_t bytes);
void sock_ev_poll(int fd, int ret, int err, short requested_events,
short returned_event, int timeout);
void sock_ev_ppoll(int fd, int ret, int err, short requested_events,
short returned_event, const struct timespec *timeout);
void sock_ev_select(int fd, int ret, int err, bool req_read, bool req_write,
bool req_except, bool ret_read, bool ret_write,
bool ret_except, struct timeval *timeout);
void sock_ev_pselect(int fd, int ret, int err, bool req_read, bool req_write,
bool req_except, bool ret_read, bool ret_write,
bool ret_except, const struct timespec *timeout);
void sock_ev_fcntl(int fd, int ret, int err, int cmd, ...);
void sock_ev_epoll_ctl(int fd, int ret, int err, int op,
uint32_t requested_events);
void sock_ev_epoll_wait(int fd, int ret, int err, int timeout,
uint32_t returned_events);
void sock_ev_epoll_pwait(int fd, int ret, int err, int timeout,
uint32_t returned_events);
void sock_ev_fdopen(int fd, FILE *ret, int err, const char *mode);
void sock_ev_tcp_info(int fd, int ret, int err, struct tcp_info *info);
void dump_all_sock_events(void);
void sock_ev_free(void); // Free state.
// Free state and restore to default state (called after fork()).
void sock_ev_reset(void);
#endif