-
Notifications
You must be signed in to change notification settings - Fork 15
/
nDPIsrvd.c
1933 lines (1725 loc) · 63.4 KB
/
nDPIsrvd.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#if !defined(__FreeBSD__) && !defined(__APPLE__)
#include <sys/signalfd.h>
#endif
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "config.h"
#include "nDPIsrvd.h"
#include "nio.h"
#include "utils.h"
enum sock_type
{
COLLECTOR_UN,
DISTRIBUTOR_UN,
DISTRIBUTOR_IN,
};
struct nDPIsrvd_write_buffer
{
struct nDPIsrvd_buffer buf;
size_t written;
};
struct remote_desc
{
enum sock_type sock_type;
int fd;
union
{
struct
{
struct sockaddr_un peer;
unsigned long long int json_bytes;
#if !defined(__FreeBSD__) && !defined(__APPLE__)
pid_t pid;
#endif
struct nDPIsrvd_json_buffer main_read_buffer;
} event_collector_un;
struct
{
struct sockaddr_un peer;
#if !defined(__FreeBSD__) && !defined(__APPLE__)
pid_t pid;
char * user_name;
#endif
struct nDPIsrvd_write_buffer main_write_buffer;
UT_array * additional_write_buffers;
} event_distributor_un; /* UNIX socket */
struct
{
struct sockaddr_in peer;
char peer_addr[INET_ADDRSTRLEN];
struct nDPIsrvd_write_buffer main_write_buffer;
UT_array * additional_write_buffers;
} event_distributor_in; /* TCP/IP socket */
};
};
static struct
{
struct remote_desc * desc;
nDPIsrvd_ull desc_size;
nDPIsrvd_ull desc_used;
} remotes = {NULL, 0, 0};
static int nDPIsrvd_main_thread_shutdown = 0;
static int collector_un_sockfd = -1;
static int distributor_un_sockfd = -1;
static int distributor_in_sockfd = -1;
static struct nDPIsrvd_address distributor_in_address = {
.raw.sa_family = (sa_family_t)0xFFFF,
};
static struct
{
struct cmdarg config_file;
struct cmdarg pidfile;
struct cmdarg collector_un_sockpath;
struct cmdarg distributor_un_sockpath;
struct cmdarg distributor_in_address;
struct cmdarg user;
struct cmdarg group;
struct cmdarg collector_group;
struct cmdarg distributor_group;
struct cmdarg max_remote_descriptors;
struct cmdarg max_write_buffers;
struct cmdarg bufferbloat_fallback_to_blocking;
#ifdef ENABLE_EPOLL
struct cmdarg use_poll;
#endif
} nDPIsrvd_options = {.config_file = CMDARG_STR(NULL),
.pidfile = CMDARG_STR(nDPIsrvd_PIDFILE),
.collector_un_sockpath = CMDARG_STR(COLLECTOR_UNIX_SOCKET),
.distributor_un_sockpath = CMDARG_STR(DISTRIBUTOR_UNIX_SOCKET),
.distributor_in_address = CMDARG_STR(NULL),
.user = CMDARG_STR(DEFAULT_CHUSER),
.group = CMDARG_STR(NULL),
.collector_group = CMDARG_STR(NULL),
.distributor_group = CMDARG_STR(NULL),
.max_remote_descriptors = CMDARG_ULL(nDPIsrvd_MAX_REMOTE_DESCRIPTORS),
.max_write_buffers = CMDARG_ULL(nDPIsrvd_MAX_WRITE_BUFFERS),
.bufferbloat_fallback_to_blocking = CMDARG_BOOL(1)
#ifdef ENABLE_EPOLL
,
.use_poll = CMDARG_BOOL(0)
#endif
};
struct confopt config_map[] = {CONFOPT("pidfile", &nDPIsrvd_options.pidfile),
CONFOPT("collector", &nDPIsrvd_options.collector_un_sockpath),
CONFOPT("distributor-unix", &nDPIsrvd_options.distributor_un_sockpath),
CONFOPT("distributor-in", &nDPIsrvd_options.distributor_in_address),
CONFOPT("user", &nDPIsrvd_options.user),
CONFOPT("group", &nDPIsrvd_options.group),
CONFOPT("collector-group", &nDPIsrvd_options.collector_group),
CONFOPT("distributor-group", &nDPIsrvd_options.distributor_group),
CONFOPT("max-remote-descriptors", &nDPIsrvd_options.max_remote_descriptors),
CONFOPT("max-write-buffers", &nDPIsrvd_options.max_write_buffers),
CONFOPT("blocking-io-fallback", &nDPIsrvd_options.bufferbloat_fallback_to_blocking)
#ifdef ENABLE_EPOLL
,
CONFOPT("poll", &nDPIsrvd_options.use_poll)
#endif
};
static void logger_nDPIsrvd(struct remote_desc const * const remote,
char const * const prefix,
char const * const format,
...);
static int fcntl_add_flags(int fd, int flags);
static int fcntl_del_flags(int fd, int flags);
static int add_in_event_fd(struct nio * const io, int fd);
static int add_in_event(struct nio * const io, struct remote_desc * const remote);
static int del_event(struct nio * const io, int fd);
static int set_in_event(struct nio * const io, struct remote_desc * const remote);
static void disconnect_client(struct nio * const io, struct remote_desc * const current);
static int drain_write_buffers_blocking(struct remote_desc * const remote);
static void nDPIsrvd_buffer_array_copy(void * dst, const void * src)
{
struct nDPIsrvd_write_buffer * const buf_dst = (struct nDPIsrvd_write_buffer *)dst;
struct nDPIsrvd_write_buffer const * const buf_src = (struct nDPIsrvd_write_buffer *)src;
buf_dst->buf.ptr.raw = NULL;
if (nDPIsrvd_buffer_init(&buf_dst->buf, buf_src->buf.used) != 0)
{
logger(1, "Additional write buffer init failed, size: %zu bytes", buf_src->buf.used);
return;
}
buf_dst->written = buf_src->written;
buf_dst->buf.used = buf_src->buf.used;
memcpy(buf_dst->buf.ptr.raw, buf_src->buf.ptr.raw, buf_src->buf.used);
}
static void nDPIsrvd_buffer_array_dtor(void * elt)
{
struct nDPIsrvd_write_buffer * const buf_dst = (struct nDPIsrvd_write_buffer *)elt;
nDPIsrvd_buffer_free(&buf_dst->buf);
buf_dst->written = 0;
}
static const UT_icd nDPIsrvd_buffer_array_icd = {sizeof(struct nDPIsrvd_write_buffer),
NULL,
nDPIsrvd_buffer_array_copy,
nDPIsrvd_buffer_array_dtor};
#ifndef NO_MAIN
#ifdef ENABLE_MEMORY_PROFILING
void nDPIsrvd_memprof_log_alloc(size_t alloc_size)
{
(void)alloc_size;
}
void nDPIsrvd_memprof_log_free(size_t free_size)
{
(void)free_size;
}
void nDPIsrvd_memprof_log(char const * const format, ...)
{
va_list ap;
va_start(ap, format);
vlogger(0, format, ap);
va_end(ap);
}
#endif
#endif
static struct nDPIsrvd_json_buffer * get_read_buffer(struct remote_desc * const remote)
{
switch (remote->sock_type)
{
case COLLECTOR_UN:
return &remote->event_collector_un.main_read_buffer;
case DISTRIBUTOR_UN:
case DISTRIBUTOR_IN:
return NULL;
}
return NULL;
}
static struct nDPIsrvd_write_buffer * get_write_buffer(struct remote_desc * const remote)
{
switch (remote->sock_type)
{
case COLLECTOR_UN:
return NULL;
case DISTRIBUTOR_UN:
return &remote->event_distributor_un.main_write_buffer;
case DISTRIBUTOR_IN:
return &remote->event_distributor_in.main_write_buffer;
}
return NULL;
}
static UT_array * get_additional_write_buffers(struct remote_desc * const remote)
{
switch (remote->sock_type)
{
case COLLECTOR_UN:
return NULL;
case DISTRIBUTOR_UN:
return remote->event_distributor_un.additional_write_buffers;
case DISTRIBUTOR_IN:
return remote->event_distributor_in.additional_write_buffers;
}
return NULL;
}
static int add_to_additional_write_buffers(struct remote_desc * const remote,
uint8_t * const buf,
nDPIsrvd_ull json_message_length)
{
struct nDPIsrvd_write_buffer buf_src = {};
UT_array * const additional_write_buffers = get_additional_write_buffers(remote);
if (additional_write_buffers == NULL)
{
return -1;
}
if (utarray_len(additional_write_buffers) >= GET_CMDARG_ULL(nDPIsrvd_options.max_write_buffers))
{
if (GET_CMDARG_BOOL(nDPIsrvd_options.bufferbloat_fallback_to_blocking) == 0)
{
logger_nDPIsrvd(remote,
"Buffer limit for",
"for reached, remote too slow: %u lines",
utarray_len(additional_write_buffers));
logger_nDPIsrvd(remote, "%s", "You can try to increase buffer limits with `-C'.");
return -1;
}
else
{
logger_nDPIsrvd(remote,
"Buffer limit for",
"reached, falling back to blocking I/O: %u lines",
utarray_len(additional_write_buffers));
if (drain_write_buffers_blocking(remote) != 0)
{
return -1;
}
}
}
buf_src.buf.ptr.raw = buf;
buf_src.buf.used = buf_src.buf.max = json_message_length;
utarray_push_back(additional_write_buffers, &buf_src);
return 0;
}
static void logger_nDPIsrvd(struct remote_desc const * const remote,
char const * const prefix,
char const * const format,
...)
{
char logbuf[512];
va_list ap;
va_start(ap, format);
vsnprintf(logbuf, sizeof(logbuf), format, ap);
switch (remote->sock_type)
{
case DISTRIBUTOR_UN:
#if !defined(__FreeBSD__) && !defined(__APPLE__)
logger(1,
"%s PID %d (User: %s) %s",
prefix,
remote->event_distributor_un.pid,
remote->event_distributor_un.user_name,
logbuf);
#else
logger(1, "%s %s", prefix, logbuf);
#endif
break;
case DISTRIBUTOR_IN:
logger(1,
"%s %.*s:%u %s",
prefix,
(int)sizeof(remote->event_distributor_in.peer_addr),
remote->event_distributor_in.peer_addr,
ntohs(remote->event_distributor_in.peer.sin_port),
logbuf);
break;
case COLLECTOR_UN:
#if !defined(__FreeBSD__) && !defined(__APPLE__)
logger(1, "%s PID %d %s", prefix, remote->event_collector_un.pid, logbuf);
#else
logger(1, "%s %s", prefix, logbuf);
#endif
break;
}
va_end(ap);
}
static int drain_main_buffer(struct remote_desc * const remote)
{
ssize_t bytes_written;
struct nDPIsrvd_write_buffer * const write_buffer = get_write_buffer(remote);
if (write_buffer == NULL)
{
return -1;
}
if (write_buffer->buf.used == 0)
{
return 0;
}
errno = 0;
while ((bytes_written = write(remote->fd, write_buffer->buf.ptr.raw, write_buffer->buf.used)) < 0 && errno == EINTR)
{
errno = 0;
}
if (errno == EAGAIN)
{
return 0;
}
if (bytes_written < 0 || errno != 0)
{
logger_nDPIsrvd(remote, "Distributor connection", "closed, send failed: %s", strerror(errno));
return -1;
}
if (bytes_written == 0)
{
logger_nDPIsrvd(remote, "Distributor connection", "closed");
return -1;
}
if ((size_t)bytes_written < write_buffer->buf.used)
{
#if 0
logger_nDPIsrvd(
remote, "Distributor", "wrote less than expected: %zd < %zu", bytes_written, remote->buf.used);
#endif
memmove(write_buffer->buf.ptr.raw,
write_buffer->buf.ptr.raw + bytes_written,
write_buffer->buf.used - bytes_written);
}
write_buffer->buf.used -= bytes_written;
return 0;
}
static int drain_write_buffers(struct remote_desc * const remote)
{
UT_array * const additional_write_buffers = get_additional_write_buffers(remote);
errno = 0;
if (drain_main_buffer(remote) != 0 || additional_write_buffers == NULL)
{
return -1;
}
while (utarray_len(additional_write_buffers) > 0)
{
struct nDPIsrvd_write_buffer * buf = (struct nDPIsrvd_write_buffer *)utarray_front(additional_write_buffers);
ssize_t written;
while ((written = write(remote->fd, buf->buf.ptr.raw + buf->written, buf->buf.used - buf->written)) < 0 &&
errno == EINTR)
{
// Retry if interrupted by a signal.
}
switch (written)
{
case -1:
if (errno == EAGAIN)
{
return 0;
}
return -1;
case 0:
return -1;
default:
buf->written += written;
if (buf->written == buf->buf.max)
{
utarray_erase(additional_write_buffers, 0, 1);
}
break;
}
}
return 0;
}
static int drain_write_buffers_blocking(struct remote_desc * const remote)
{
int retval = 0;
if (fcntl_del_flags(remote->fd, O_NONBLOCK) != 0)
{
logger_nDPIsrvd(remote, "Error setting distributor", "fd flags to blocking mode: %s", strerror(errno));
return -1;
}
if (drain_write_buffers(remote) != 0)
{
logger_nDPIsrvd(remote, "Could not drain buffers for", "in blocking I/O: %s", strerror(errno));
retval = -1;
}
if (fcntl_add_flags(remote->fd, O_NONBLOCK) != 0)
{
logger_nDPIsrvd(remote, "Error setting distributor", "fd flags to non-blocking mode: %s", strerror(errno));
return -1;
}
return retval;
}
static int handle_outgoing_data(struct nio * const io, struct remote_desc * const remote)
{
UT_array * const additional_write_buffers = get_additional_write_buffers(remote);
if (additional_write_buffers == NULL)
{
return -1;
}
if (drain_write_buffers(remote) != 0)
{
logger_nDPIsrvd(remote, "Could not drain buffers for", ": %s", strerror(errno));
disconnect_client(io, remote);
return -1;
}
if (utarray_len(additional_write_buffers) == 0)
{
struct nDPIsrvd_write_buffer const * const write_buffer = get_write_buffer(remote);
if (write_buffer->buf.used == 0)
{
return set_in_event(io, remote);
}
else
{
return drain_main_buffer(remote);
}
}
return 0;
}
static int fcntl_add_flags(int fd, int flags)
{
int cur_flags = fcntl(fd, F_GETFL, 0);
if (cur_flags == -1)
{
return 1;
}
return fcntl(fd, F_SETFL, cur_flags | flags);
}
static int fcntl_del_flags(int fd, int flags)
{
int cur_flags = fcntl(fd, F_GETFL, 0);
if (cur_flags == -1)
{
return -1;
}
return fcntl(fd, F_SETFL, cur_flags & ~flags);
}
static int create_listen_sockets(void)
{
collector_un_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
distributor_un_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (collector_un_sockfd < 0 || distributor_un_sockfd < 0 || set_fd_cloexec(collector_un_sockfd) < 0 ||
set_fd_cloexec(distributor_un_sockfd) < 0)
{
logger(1, "Error creating UNIX socket: %s", strerror(errno));
return 1;
}
if (IS_CMDARG_SET(nDPIsrvd_options.distributor_in_address) != 0)
{
distributor_in_sockfd = socket(distributor_in_address.raw.sa_family, SOCK_STREAM, 0);
if (distributor_in_sockfd < 0 || set_fd_cloexec(distributor_in_sockfd) < 0)
{
logger(1, "Error creating TCP/IP socket: %s", strerror(errno));
return 1;
}
int opt = 1;
if (setsockopt(distributor_in_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
{
logger(1, "Setting TCP/IP socket option SO_REUSEADDR failed: %s", strerror(errno));
}
}
{
int opt = 1;
if (setsockopt(collector_un_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0 ||
setsockopt(distributor_un_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
{
logger(1, "Setting UNIX socket option SO_REUSEADDR failed: %s", strerror(errno));
}
}
{
struct sockaddr_un collector_addr;
collector_addr.sun_family = AF_UNIX;
int written = snprintf(collector_addr.sun_path,
sizeof(collector_addr.sun_path),
"%s",
GET_CMDARG_STR(nDPIsrvd_options.collector_un_sockpath));
if (written < 0)
{
logger(1, "snprintf failed: %s", strerror(errno));
return 1;
}
else if (written == sizeof(collector_addr.sun_path))
{
logger(1, "Collector UNIX socket path too long, max: %zu characters", sizeof(collector_addr.sun_path) - 1);
return 1;
}
if (bind(collector_un_sockfd, (struct sockaddr *)&collector_addr, sizeof(collector_addr)) < 0)
{
logger(1,
"Error binding Collector UNIX socket to `%s': %s",
GET_CMDARG_STR(nDPIsrvd_options.collector_un_sockpath),
strerror(errno));
return 1;
}
}
{
struct sockaddr_un distributor_addr;
distributor_addr.sun_family = AF_UNIX;
int written = snprintf(distributor_addr.sun_path,
sizeof(distributor_addr.sun_path),
"%s",
GET_CMDARG_STR(nDPIsrvd_options.distributor_un_sockpath));
if (written < 0)
{
logger(1, "snprintf failed: %s", strerror(errno));
return 2;
}
else if (written == sizeof(distributor_addr.sun_path))
{
logger(1,
"Distributor UNIX socket path too long, max: %zu characters",
sizeof(distributor_addr.sun_path) - 1);
return 2;
}
if (bind(distributor_un_sockfd, (struct sockaddr *)&distributor_addr, sizeof(distributor_addr)) < 0)
{
logger(1,
"Error binding Distributor socket to `%s': %s",
GET_CMDARG_STR(nDPIsrvd_options.distributor_un_sockpath),
strerror(errno));
return 2;
}
}
if (IS_CMDARG_SET(nDPIsrvd_options.distributor_in_address) != 0)
{
if (bind(distributor_in_sockfd, &distributor_in_address.raw, distributor_in_address.size) < 0)
{
logger(1,
"Error binding Distributor TCP/IP socket to %s: %s",
GET_CMDARG_STR(nDPIsrvd_options.distributor_in_address),
strerror(errno));
return 3;
}
if (listen(distributor_in_sockfd, 16) < 0)
{
logger(1,
"Error listening Distributor TCP/IP socket to %s: %s",
GET_CMDARG_STR(nDPIsrvd_options.distributor_in_address),
strerror(errno));
return 3;
}
if (fcntl_add_flags(distributor_in_sockfd, O_NONBLOCK) != 0)
{
logger(1,
"Error setting Distributor TCP/IP socket %s to non-blocking mode: %s",
GET_CMDARG_STR(nDPIsrvd_options.distributor_in_address),
strerror(errno));
return 3;
}
}
if (listen(collector_un_sockfd, 16) < 0 || listen(distributor_un_sockfd, 16) < 0)
{
logger(1, "Error listening UNIX socket: %s", strerror(errno));
return 3;
}
if (fcntl_add_flags(collector_un_sockfd, O_NONBLOCK) != 0)
{
logger(1,
"Error setting Collector UNIX socket `%s' to non-blocking mode: %s",
GET_CMDARG_STR(nDPIsrvd_options.collector_un_sockpath),
strerror(errno));
return 3;
}
if (fcntl_add_flags(distributor_un_sockfd, O_NONBLOCK) != 0)
{
logger(1,
"Error setting Distributor UNIX socket `%s' to non-blocking mode: %s",
GET_CMDARG_STR(nDPIsrvd_options.distributor_un_sockpath),
strerror(errno));
return 3;
}
return 0;
}
static struct remote_desc * get_remote_descriptor(enum sock_type type, int remote_fd, size_t max_buffer_size)
{
if (remotes.desc_used == remotes.desc_size)
{
logger(1, "Max number of connections reached: %llu", remotes.desc_used);
return NULL;
}
for (size_t i = 0; i < remotes.desc_size; ++i)
{
if (remotes.desc[i].fd == -1)
{
remotes.desc_used++;
struct nDPIsrvd_write_buffer * write_buffer = NULL;
UT_array ** additional_write_buffers = NULL;
switch (type)
{
case COLLECTOR_UN:
if (nDPIsrvd_json_buffer_init(&remotes.desc[i].event_collector_un.main_read_buffer,
max_buffer_size) != 0)
{
logger(1, "Read/JSON buffer init failed, size: %zu bytes", max_buffer_size);
return NULL;
}
break;
case DISTRIBUTOR_UN:
write_buffer = &remotes.desc[i].event_distributor_un.main_write_buffer;
additional_write_buffers = &remotes.desc[i].event_distributor_un.additional_write_buffers;
break;
case DISTRIBUTOR_IN:
write_buffer = &remotes.desc[i].event_distributor_in.main_write_buffer;
additional_write_buffers = &remotes.desc[i].event_distributor_in.additional_write_buffers;
break;
}
if (additional_write_buffers != NULL && *additional_write_buffers == NULL)
{
utarray_new(*additional_write_buffers, &nDPIsrvd_buffer_array_icd);
if (*additional_write_buffers == NULL)
{
logger(1, "%s", "Could not create additional write buffers");
return NULL;
}
}
if (write_buffer != NULL && nDPIsrvd_buffer_init(&write_buffer->buf, max_buffer_size) != 0)
{
logger(1, "Write buffer init failed, size: %zu bytes", max_buffer_size);
return NULL;
}
remotes.desc[i].sock_type = type;
remotes.desc[i].fd = remote_fd;
return &remotes.desc[i];
}
}
logger(1, "%s", "BUG: Unknown error while finding the remote descriptor");
return NULL;
}
static void free_remote(struct nio * const io, struct remote_desc * remote)
{
if (remote->fd > -1)
{
errno = 0;
if (del_event(io, remote->fd) != 0)
{
logger_nDPIsrvd(remote,
"Could not delete event from queue for connection",
": %s",
(errno != 0 ? strerror(errno) : "Internal Error"));
}
errno = 0;
close(remote->fd);
switch (remote->sock_type)
{
case COLLECTOR_UN:
if (errno != 0)
{
logger_nDPIsrvd(remote, "Error closing collector connection", ": %s", strerror(errno));
}
nDPIsrvd_json_buffer_free(&remote->event_collector_un.main_read_buffer);
break;
case DISTRIBUTOR_UN:
if (errno != 0)
{
logger_nDPIsrvd(remote, "Error closing distributor connection", ": %s", strerror(errno));
}
if (remote->event_distributor_un.additional_write_buffers != NULL)
{
utarray_free(remote->event_distributor_un.additional_write_buffers);
}
nDPIsrvd_buffer_free(&remote->event_distributor_un.main_write_buffer.buf);
#if !defined(__FreeBSD__) && !defined(__APPLE__)
free(remote->event_distributor_un.user_name);
#endif
break;
case DISTRIBUTOR_IN:
if (errno != 0)
{
logger_nDPIsrvd(remote, "Error closing distributor connection", ": %s", strerror(errno));
}
if (remote->event_distributor_in.additional_write_buffers != NULL)
{
utarray_free(remote->event_distributor_in.additional_write_buffers);
}
nDPIsrvd_buffer_free(&remote->event_distributor_in.main_write_buffer.buf);
break;
}
memset(remote, 0, sizeof(*remote));
remote->fd = -1;
remotes.desc_used--;
}
}
static void free_remotes(struct nio * const io)
{
for (size_t i = 0; i < remotes.desc_size; ++i)
{
free_remote(io, &remotes.desc[i]);
}
nDPIsrvd_free(remotes.desc);
remotes.desc = NULL;
remotes.desc_used = 0;
remotes.desc_size = 0;
}
static int add_in_event_fd(struct nio * const io, int fd)
{
return nio_add_fd(io, fd, NIO_EVENT_INPUT, NULL) != NIO_SUCCESS;
}
static int add_in_event(struct nio * const io, struct remote_desc * const remote)
{
return nio_add_fd(io, remote->fd, NIO_EVENT_INPUT, remote) != NIO_SUCCESS;
}
static int set_out_event(struct nio * const io, struct remote_desc * const remote)
{
return nio_mod_fd(io, remote->fd, NIO_EVENT_OUTPUT, remote) != NIO_SUCCESS;
}
static int set_in_event(struct nio * const io, struct remote_desc * const remote)
{
return nio_mod_fd(io, remote->fd, NIO_EVENT_INPUT, remote) != NIO_SUCCESS;
}
static int del_event(struct nio * const io, int fd)
{
return nio_del_fd(io, fd) != NIO_SUCCESS;
}
static void disconnect_client(struct nio * const io, struct remote_desc * const remote)
{
free_remote(io, remote);
}
static int nDPIsrvd_parse_options(int argc, char ** argv)
{
int opt;
while ((opt = getopt(argc, argv, "f:lL:c:dp:s:S:G:m:u:g:C:Dvh")) != -1)
{
switch (opt)
{
case 'f':
set_cmdarg_string(&nDPIsrvd_options.config_file, optarg);
break;
case 'l':
enable_console_logger();
break;
case 'L':
if (enable_file_logger(optarg) != 0)
{
return 1;
}
break;
case 'c':
set_cmdarg_string(&nDPIsrvd_options.collector_un_sockpath, optarg);
break;
case 'e':
#ifdef ENABLE_EPOLL
set_cmdarg_boolean(&nDPIsrvd_options.use_poll, 1);
#else
logger_early(1, "%s", "nDPIsrvd was built w/o epoll() support, poll() is already the default");
#endif
break;
case 'd':
daemonize_enable();
break;
case 'p':
set_cmdarg_string(&nDPIsrvd_options.pidfile, optarg);
break;
case 's':
set_cmdarg_string(&nDPIsrvd_options.distributor_un_sockpath, optarg);
break;
case 'S':
set_cmdarg_string(&nDPIsrvd_options.distributor_in_address, optarg);
break;
case 'G':
{
char const * const sep = strchr(optarg, ':');
char group[256];
if (sep == NULL)
{
fprintf(stderr, "%s: Argument for `-G' is not in the format group:group\n", argv[0]);
return 1;
}
if (snprintf(group, sizeof(group), "%.*s", (int)(sep - optarg), optarg) > 0)
{
set_cmdarg_string(&nDPIsrvd_options.collector_group, group);
}
if (snprintf(group, sizeof(group), "%s", sep + 1) > 0)
{
set_cmdarg_string(&nDPIsrvd_options.distributor_group, group);
}
break;
}
case 'm':
{
nDPIsrvd_ull tmp;
if (str_value_to_ull(optarg, &tmp) != CONVERSION_OK)
{
fprintf(stderr, "%s: Argument for `-C' is not a number: %s\n", argv[0], optarg);
return 1;
}
set_cmdarg_ull(&nDPIsrvd_options.max_remote_descriptors, tmp);
break;
}
case 'u':
set_cmdarg_string(&nDPIsrvd_options.user, optarg);
break;
case 'g':
set_cmdarg_string(&nDPIsrvd_options.group, optarg);
break;
case 'C':
{
nDPIsrvd_ull tmp;
if (str_value_to_ull(optarg, &tmp) != CONVERSION_OK)
{
fprintf(stderr, "%s: Argument for `-C' is not a number: %s\n", argv[0], optarg);
return 1;
}
set_cmdarg_ull(&nDPIsrvd_options.max_write_buffers, tmp);
break;
}
case 'D':
set_cmdarg_boolean(&nDPIsrvd_options.bufferbloat_fallback_to_blocking, 0);
break;
case 'v':
fprintf(stderr, "%s", get_nDPId_version());
return 1;
case 'h':
default:
fprintf(stderr, "%s\n", get_nDPId_version());
fprintf(stderr,
"Usage: %s [-f config-file] [-l] [-L logfile]\n"
"\t[-c path-to-unix-sock] [-e] [-d] [-p pidfile]\n"
"\t[-s path-to-distributor-unix-socket] [-S distributor-host:port]\n"
"\t[-G collector-unix-socket-group:distributor-unix-socket-group]\n"
"\t[-m max-remote-descriptors] [-u user] [-g group]\n"
"\t[-C max-buffered-json-lines] [-D]\n"
"\t[-v] [-h]\n\n"
"\t-f\tLoad nDPIsrvd options from a configuration file.\n"
"\t-l\tLog all messages to stderr.\n"
"\t-L\tLog all messages to a log file.\n"
"\t-c\tPath to a listening UNIX socket (nDPIsrvd Collector).\n"
"\t \tDefault: %s\n"
"\t-e\tUse poll() instead of epoll().\n"
"\t \tDefault: epoll() on Linux, poll() otherwise\n"
"\t-d\tFork into background after initialization.\n"
"\t-p\tWrite the daemon PID to the given file path.\n"
"\t \tDefault: %s\n"
"\t-m\tMax accepted (Collector and Distributor) clients.\n"
"\t-u\tChange UID to the numeric value of user.\n"
"\t \tDefault: %s\n"
"\t-g\tChange GID to the numeric value of group.\n"
"\t-C\tMax buffered JSON lines before nDPIsrvd disconnects/blocking-IO a client.\n"
"\t-D\tDisconnect a slow client instead of falling back to blocking-IO.\n"
"\t-s\tPath to a listening UNIX socket (nDPIsrvd Distributor).\n"
"\t \tDefault: %s\n"
"\t-S\tAddress:Port of the listening TCP/IP socket (nDPIsrvd Distributor).\n"
"\t-G\tGroup owner of the UNIX collector/distributor socket.\n"
"\t \tDefault: Either the group set via `-g', otherwise the primary group of `-u'\n"
"\t-v\tversion\n"
"\t-h\tthis\n\n",
argv[0],
nDPIsrvd_options.collector_un_sockpath.string.default_value,
nDPIsrvd_options.pidfile.string.default_value,
nDPIsrvd_options.user.string.default_value,
nDPIsrvd_options.distributor_un_sockpath.string.default_value);
return 1;
}
}
set_config_defaults(&config_map[0], nDPIsrvd_ARRAY_LENGTH(config_map));
if (is_path_absolute("Pidfile", GET_CMDARG_STR(nDPIsrvd_options.pidfile)) != 0)
{
return 1;
}
if (is_path_absolute("Collector UNIX socket", GET_CMDARG_STR(nDPIsrvd_options.collector_un_sockpath)) != 0)
{
return 1;
}
if (is_path_absolute("Distributor UNIX socket", GET_CMDARG_STR(nDPIsrvd_options.distributor_un_sockpath)) != 0)
{
return 1;
}
if (IS_CMDARG_SET(nDPIsrvd_options.distributor_in_address) != 0)
{
if (nDPIsrvd_setup_address(&distributor_in_address, GET_CMDARG_STR(nDPIsrvd_options.distributor_in_address)) !=
0)
{
logger_early(1,
"%s: Could not parse address %s",
argv[0],
GET_CMDARG_STR(nDPIsrvd_options.distributor_in_address));
return 1;
}
if (distributor_in_address.raw.sa_family == AF_UNIX)
{
logger_early(1,
"%s: You've requested to setup another UNIX socket `%s', but there is already one at `%s'",
argv[0],