forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture_framework.c
3761 lines (2930 loc) · 117 KB
/
capture_framework.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
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#ifdef SYS_LINUX
#define _GNU_SOURCE 1
#endif
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdbool.h>
#ifdef HAVE_CAPABILITY
#include <sys/capability.h>
#include <sys/prctl.h>
#include <pwd.h>
#include <grp.h>
#endif
#ifdef SYS_LINUX
#include <linux/sched.h>
#include <sys/mount.h>
#endif
#include "capture_framework.h"
#include "kis_external_packet.h"
#include "kis_endian.h"
#include "remote_announcement.h"
#include "protobuf_c/kismet.pb-c.h"
#include "protobuf_c/datasource.pb-c.h"
uint32_t adler32_partial_csum(uint8_t *in_buf, size_t in_len,
uint32_t *s1, uint32_t *s2) {
size_t i;
uint8_t *buf = in_buf;
int CHAR_OFFSET = 0;
if (in_len < 4)
return 0;
for (i = 0; i < (in_len - 4); i += 4) {
*s2 += 4 * (*s1 + buf[i]) + 3 * buf[i + 1] + 2 * buf[i+2] + buf[i + 3] +
10 * CHAR_OFFSET;
*s1 += (buf[i + 0] + buf[i + 1] + buf[i + 2] + buf[i + 3] + 4 * CHAR_OFFSET);
}
for (; i < in_len; i++) {
*s1 += (buf[i] + CHAR_OFFSET);
*s2 += *s1;
}
return (*s1 & 0xffff) + (*s2 << 16);
}
uint32_t adler32_csum(uint8_t *in_buf, size_t in_len) {
uint32_t s1, s2;
s1 = 0;
s2 = 0;
return adler32_partial_csum(in_buf, in_len, &s1, &s2);
}
int cf_parse_interface(char **ret_interface, char *definition) {
char *colonpos;
colonpos = strstr(definition, ":");
*ret_interface = definition;
/* If there is no : separator, the entire line is the interface and there
* are no flags. */
if (colonpos == NULL) {
return strlen(definition);
}
/* Otherwise return until the separator */
return (colonpos - definition);
}
int cf_find_flag(char **ret_value, const char *flag, char *definition) {
char *colonpos, *flagpos, *comma, *equals, *quote, *equote;
colonpos = strstr(definition, ":");
/* If there is no : separator, the entire line is the interface, return NULL
* and 0 */
if (colonpos == NULL) {
*ret_value = NULL;
return 0;
}
flagpos = colonpos + 1;
while ((size_t) (flagpos - definition) < strlen(definition)) {
equals = strstr(flagpos, "=");
/* If we have a flag with no =value that's an error */
if (equals == NULL) {
*ret_value = NULL;
return -1;
}
/* Compare the flag */
if (strncasecmp(flag, flagpos, (equals - flagpos)) == 0) {
/* Find the next comma */
comma = strstr(equals, ",");
/* Find the next quote */
quote = strstr(equals, "\"");
/* If it's null we're the last flag, so use the total length after
* the equals as the value */
if (comma == NULL && quote == NULL) {
*ret_value = equals + 1;
return strlen(equals) - 1;
}
/* If we've got a quote inside the string, before the terminating comma,
* find the next quote and return that as the length */
if (quote != NULL) {
if ((comma != NULL && quote < comma) || comma == NULL) {
equote = strstr(quote + 1, "\"");
*ret_value = quote + 1;
return (equote - (quote + 1));
} else {
}
}
/* Otherwise return until the next comma */
*ret_value = equals + 1;
return (comma - (equals + 1));
}
/* Otherwise find the next comma */
comma = strstr(flagpos, ",");
/* Find the next quote */
quote = strstr(equals, "\"");
/* No comma, no more flags, nothing to find */
if (comma == NULL && quote == NULL) {
*ret_value = NULL;
return 0;
}
/* If we have a quote */
if (quote != NULL) {
/* And it contains the current flag */
if ((comma != NULL && quote < comma) || comma == NULL) {
/* Jump to the end of the quote */
equote = strstr(quote + 1, "\"");
/* Find a trailing comma */
comma = strstr(equote, ",");
/* bail if this was the last */
if (comma == NULL) {
*ret_value = NULL;
return 0;
}
}
}
/* Jump past the flagpos */
flagpos = comma + 1;
}
*ret_value = NULL;
return 0;
}
int cf_count_flag(const char *flag, char *definition) {
int n_flags = 0;
char *placeholder = NULL;
char *last_holder = definition;
int len;
do {
len = cf_find_flag(&placeholder, flag, last_holder);
if (len == 0 || placeholder == NULL)
break;
n_flags++;
last_holder = placeholder + len;
} while (placeholder != NULL);
return n_flags;
}
int cf_split_list(char *in_str, size_t in_sz, char in_split, char ***ret_splitlist,
size_t *ret_splitlist_sz) {
char *start = in_str;
char *end = in_str;
size_t num_fields = 0;
*ret_splitlist = NULL;
*ret_splitlist_sz = 0;
/* Count all the fields */
while ((size_t) (end - in_str) <= in_sz) {
if (*start == in_split)
start++;
if (*end == in_split || *end == 0 || end == in_str + in_sz) {
if (end != start) {
num_fields++;
}
start = end + 1;
end = start;
}
end++;
}
if (num_fields == 1) {
*ret_splitlist = (char **) malloc(sizeof(char *));
if (*ret_splitlist == NULL)
return -1;
(*ret_splitlist)[0] = strndup(in_str, in_sz);
*ret_splitlist_sz = 1;
return 0;
}
*ret_splitlist = (char **) malloc(sizeof(char *) * num_fields);
start = in_str;
end = in_str;
*ret_splitlist_sz = num_fields;
num_fields = 0;
while ((size_t) (end - in_str) <= in_sz && num_fields <= *ret_splitlist_sz) {
if (*start == in_split)
start++;
if (*end == in_split || *end == 0 || end == in_str + in_sz) {
if (end != start) {
(*ret_splitlist)[num_fields++] = strndup(start, end - start);
}
start = end + 1;
end = start;
}
end++;
}
return 0;
}
size_t cf_append_unique_chans(char **in_list1, size_t in_list1_sz,
char **in_list2, size_t in_list2_sz, char ***ret_list) {
size_t max_sz = in_list1_sz + in_list2_sz;
size_t i, n, offt;
int skip = 0;
if (max_sz == 0) {
*ret_list = NULL;
return 0;
}
/* Make a max-size list; we only lose a few char* sizes so we don't care */
*ret_list = (char **) malloc(sizeof(char *) * max_sz);
if (*ret_list == NULL)
return 0;
/* dupe all of the first list */
for (i = 0; i < in_list1_sz; i++) {
(*ret_list)[i] = strdup(in_list1[i]);
}
offt = in_list1_sz;
/* Dupe uniques of the second list */
for (i = 0; i < in_list2_sz; i++) {
skip = 0;
for (n = 0; n < in_list1_sz; n++) {
if (strcasecmp(in_list1[n], in_list2[i]) == 0) {
skip = 1;
break;
}
}
if (skip)
continue;
(*ret_list)[offt++] = strdup(in_list2[i]);
}
return offt;
}
kis_capture_handler_t *cf_handler_init(const char *in_type) {
kis_capture_handler_t *ch;
pthread_mutexattr_t mutexattr;
ch = (kis_capture_handler_t *) malloc(sizeof(kis_capture_handler_t));
if (ch == NULL)
return NULL;
ch->last_ping = time(0);
ch->seqno = 1;
ch->capsource_type = strdup(in_type);
ch->remote_capable = 1;
ch->remote_host = NULL;
ch->remote_port = 0;
ch->use_tcp = 0;
ch->use_ipc = 0;
ch->use_ws = 0;
#ifdef HAVE_LIBWEBSOCKETS
ch->lwscontext = NULL;
ch->lwsvhost = NULL;
ch->lwsprotocol = NULL;
ch->lwsring = NULL;
ch->lwstail = 0;
ch->lwsclientwsi = NULL;
ch->lwsestablished = 0;
ch->lwsusessl = 0;
ch->lwssslcapath = NULL;
ch->lwsuri = NULL;
ch->lwsuuid = NULL;
#endif
ch->announced_uuid = NULL;
ch->cli_sourcedef = NULL;
ch->in_fd = -1;
ch->out_fd = -1;
ch->tcp_fd = -1;
/* Disable retry by default */
ch->remote_retry = 0;
/* Disable daemon mode by default */
ch->daemonize = 0;
/* Zero the GPS */
ch->gps_fixed_lat = 0;
ch->gps_fixed_lon = 0;
ch->gps_fixed_alt = 0;
ch->gps_name = NULL;
ch->in_ringbuf = NULL;
ch->out_ringbuf = NULL;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&(ch->out_ringbuf_lock), &mutexattr);
pthread_cond_init(&(ch->out_ringbuf_flush_cond), NULL);
pthread_mutex_init(&(ch->out_ringbuf_flush_cond_mutex), NULL);
ch->shutdown = 0;
ch->spindown = 0;
pthread_mutex_init(&(ch->handler_lock), &mutexattr);
ch->listdevices_cb = NULL;
ch->probe_cb = NULL;
ch->open_cb = NULL;
ch->unknown_cb = NULL;
ch->chantranslate_cb = NULL;
ch->chanfree_cb = NULL;
ch->chancontrol_cb = NULL;
ch->spectrumconfig_cb = NULL;
ch->capture_cb = NULL;
ch->userdata = NULL;
ch->capture_running = 0;
ch->hopping_running = 0;
ch->channel = NULL;
ch->channel_hop_list = NULL;
ch->custom_channel_hop_list = NULL;
ch->channel_hop_list_sz = 0;
ch->channel_hop_shuffle = 0;
ch->channel_hop_shuffle_spacing = 1;
ch->channel_hop_failure_list = NULL;
ch->channel_hop_failure_list_sz = 0;
ch->max_channel_hop_rate = 0;
ch->verbose = 0;
return ch;
}
#ifdef HAVE_LIBWEBSOCKETS
static const struct lws_protocols kismet_lws_protocols[] = {
{"kismet-remote", ws_remotecap_broker, 0, 0},
{NULL, NULL, 0, 0}
};
#endif
void cf_set_remote_capable(kis_capture_handler_t *caph, int in_capable) {
caph->remote_capable = in_capable;
}
void cf_handler_free(kis_capture_handler_t *caph) {
size_t szi;
if (caph == NULL)
return;
pthread_mutex_lock(&(caph->handler_lock));
pthread_mutex_lock(&(caph->out_ringbuf_lock));
if (caph->in_fd >= 0)
close(caph->in_fd);
if (caph->out_fd >= 0)
close(caph->out_fd);
if (caph->remote_host)
free(caph->remote_host);
if (caph->capsource_type)
free(caph->capsource_type);
if (caph->cli_sourcedef)
free(caph->cli_sourcedef);
if (caph->tcp_fd >= 0)
close(caph->tcp_fd);
if (caph->in_ringbuf != NULL)
kis_simple_ringbuf_free(caph->in_ringbuf);
if (caph->out_ringbuf != NULL)
kis_simple_ringbuf_free(caph->out_ringbuf);
for (szi = 0; szi < caph->channel_hop_list_sz; szi++) {
if (caph->channel_hop_list[szi] != NULL)
free(caph->channel_hop_list[szi]);
if (caph->chanfree_cb != NULL) {
(*(caph->chanfree_cb))(caph->custom_channel_hop_list[szi]);
} else if (caph->custom_channel_hop_list[szi] != NULL) {
free(caph->custom_channel_hop_list[szi]);
}
}
if (caph->channel_hop_list != NULL)
free(caph->channel_hop_list);
if (caph->custom_channel_hop_list != NULL)
free(caph->custom_channel_hop_list);
if (caph->capture_running) {
pthread_cancel(caph->capturethread);
caph->capture_running = 0;
}
if (caph->hopping_running) {
pthread_cancel(caph->hopthread);
caph->hopping_running = 0;
}
pthread_mutex_destroy(&(caph->out_ringbuf_lock));
pthread_mutex_destroy(&(caph->handler_lock));
}
cf_params_interface_t *cf_params_interface_new() {
cf_params_interface_t *cpi =
(cf_params_interface_t *) malloc(sizeof(cf_params_interface_t));
memset(cpi, 0, sizeof(cf_params_interface_t));
return cpi;
}
void cf_params_interface_free(cf_params_interface_t *pi) {
if (pi->capif != NULL)
free(pi->capif);
if (pi->chanset != NULL)
free(pi->chanset);
if (pi->channels != NULL) {
size_t x = 0;
for (x = 0; x < pi->channels_len; x++) {
free(pi->channels[x]);
}
free(pi->channels);
}
free(pi);
}
cf_params_spectrum_t *cf_params_spectrum_new() {
cf_params_spectrum_t *cps =
(cf_params_spectrum_t *) malloc(sizeof(cf_params_spectrum_t));
memset(cps, 0, sizeof(cf_params_spectrum_t));
return cps;
}
void cf_params_spectrum_free(cf_params_spectrum_t *si) {
free(si);
}
void cf_handler_shutdown(kis_capture_handler_t *caph) {
if (caph == NULL)
return;
pthread_mutex_lock(&(caph->handler_lock));
caph->shutdown = 1;
/* Kill the capture thread */
if (caph->capture_running) {
pthread_cancel(caph->capturethread);
caph->capture_running = 0;
}
pthread_mutex_unlock(&(caph->handler_lock));
}
void cf_handler_spindown(kis_capture_handler_t *caph) {
if (caph == NULL)
return;
pthread_mutex_lock(&(caph->handler_lock));
caph->spindown = 1;
pthread_mutex_unlock(&(caph->handler_lock));
}
void cf_handler_assign_hop_channels(kis_capture_handler_t *caph, char **stringchans,
void **privchans, size_t chan_sz, double rate, int shuffle, int shuffle_spacing,
int offset) {
size_t szi;
/*
fprintf(stderr, "debug - assign hop channels\n");
for (szi = 0; szi < chan_sz; szi++) {
fprintf(stderr, "debug - channel %s priv %p\n", stringchans[szi], privchans[szi]);
}
*/
pthread_mutex_lock(&(caph->handler_lock));
/* Purge any existing data */
for (szi = 0; szi < caph->channel_hop_list_sz; szi++) {
if (caph->channel_hop_list[szi] != NULL)
free(caph->channel_hop_list[szi]);
if (caph->chanfree_cb != NULL) {
(*(caph->chanfree_cb))(caph->custom_channel_hop_list[szi]);
} else if (caph->custom_channel_hop_list[szi] != NULL) {
free(caph->custom_channel_hop_list[szi]);
}
}
if (caph->channel_hop_list)
free(caph->channel_hop_list);
if (caph->custom_channel_hop_list)
free(caph->custom_channel_hop_list);
caph->channel_hop_list = stringchans;
caph->custom_channel_hop_list = privchans;
caph->channel_hop_list_sz = chan_sz;
if (caph->max_channel_hop_rate != 0 && rate < caph->max_channel_hop_rate)
caph->channel_hop_rate = caph->max_channel_hop_rate;
else
caph->channel_hop_rate = rate;
caph->channel_hop_shuffle = shuffle;
caph->channel_hop_shuffle_spacing = shuffle_spacing;
caph->channel_hop_offset = offset;
if (caph->channel_hop_shuffle && caph->channel_hop_shuffle_spacing && chan_sz != 0) {
/* To find a proper randomization number, take the length of the channel
* list, divide by the preferred skipping distance.
*
* We then need to find the closest number to the skipping distance that
* is not a factor of the maximum so that we get full coverage.
*/
if (caph->channel_hop_shuffle_spacing > chan_sz)
caph->channel_hop_shuffle_spacing = 1;
while ((chan_sz % (chan_sz / caph->channel_hop_shuffle_spacing)) == 0) {
if (caph->channel_hop_shuffle_spacing >= chan_sz - 1) {
caph->channel_hop_shuffle_spacing = 1;
break;
}
caph->channel_hop_shuffle_spacing++;
}
}
pthread_mutex_unlock(&(caph->handler_lock));
/* Launch the channel hop thread (cancelling any current channel hopping) */
cf_handler_launch_hopping_thread(caph);
}
void cf_handler_set_hop_shuffle_spacing(kis_capture_handler_t *caph, int spacing) {
pthread_mutex_lock(&(caph->handler_lock));
caph->channel_hop_shuffle_spacing = spacing;
/* Set the shuffle hop; the channel hop thread will pick it up on its own if it
* needs to */
if (caph->channel_hop_shuffle && caph->channel_hop_shuffle_spacing &&
caph->channel_hop_list_sz != 0) {
while ((caph->channel_hop_list_sz % (caph->channel_hop_list_sz /
caph->channel_hop_shuffle_spacing)) == 0) {
if (caph->channel_hop_shuffle_spacing >= caph->channel_hop_list_sz - 1) {
caph->channel_hop_shuffle_spacing = 1;
break;
}
caph->channel_hop_shuffle_spacing++;
}
}
pthread_mutex_unlock(&(caph->handler_lock));
}
void cf_handler_list_devices(kis_capture_handler_t *caph) {
cf_params_list_interface_t **interfaces = NULL;
/* Callback ret */
int cbret = -1;
/* Status buffer */
char msgstr[STATUS_MAX];
unsigned int i;
msgstr[0] = 0;
if (caph->listdevices_cb == NULL) {
fprintf(stderr, "%s does not support listing available devices, sorry!\n",
caph->capsource_type);
return;
}
cbret = (*(caph->listdevices_cb))(caph, 0, msgstr, &interfaces);
if (strlen(msgstr) != 0 && caph->verbose) {
if (cbret <= 0)
fprintf(stderr, "ERROR: %s\n", msgstr);
else
fprintf(stderr, "INFO: %s\n", msgstr);
}
if (cbret <= 0) {
fprintf(stderr, "%s - No supported data sources found...\n", caph->capsource_type);
return;
}
fprintf(stderr, "%s supported data sources:\n", caph->capsource_type);
if (cbret > 0) {
for (i = 0; i < (size_t) cbret; i++) {
if (interfaces[i] != NULL) {
fprintf(stderr, " %s", interfaces[i]->interface);
if (interfaces[i]->flags != NULL) {
fprintf(stderr, ":%s", interfaces[i]->flags);
}
if (interfaces[i]->hardware != NULL) {
fprintf(stderr, " (%s)", interfaces[i]->hardware);
}
fprintf(stderr, "\n");
if (interfaces[i]->interface != NULL)
free(interfaces[i]->interface);
if (interfaces[i]->flags != NULL)
free(interfaces[i]->flags);
if (interfaces[i]->hardware != NULL)
free(interfaces[i]->hardware);
free(interfaces[i]);
}
}
free(interfaces);
}
}
int cf_handler_parse_opts(kis_capture_handler_t *caph, int argc, char *argv[]) {
int option_idx;
optind = 0;
opterr = 0;
option_idx = 0;
char parse_hname[513];
unsigned int parse_port;
int retry = 1;
int daemon = 0;
int autodetect = 0;
static struct option longopt[] = {
{ "in-fd", required_argument, 0, 1 },
{ "out-fd", required_argument, 0, 2 },
{ "connect", required_argument, 0, 3 },
{ "source", required_argument, 0, 4 },
{ "disable-retry", no_argument, 0, 5 },
{ "daemonize", no_argument, 0, 6 },
{ "list", no_argument, 0, 7 },
{ "fixed-gps", required_argument, 0, 8 },
{ "gps-name", required_argument, 0, 9 },
{ "host", required_argument, 0, 10 },
{ "autodetect", optional_argument, 0, 11 },
{ "tcp", no_argument, 0, 12 },
{ "ssl", no_argument, 0, 13},
{ "user", required_argument, 0, 14},
{ "password", required_argument, 0, 15},
{ "apikey", required_argument, 0, 16},
{ "endpoint", required_argument, 0, 17},
{ "ssl-certificate", required_argument, 0, 18},
{ "help", no_argument, 0, 'h'},
{ 0, 0, 0, 0 }
};
char *gps_arg = NULL;
int pr;
#ifdef HAVE_LIBWEBSOCKETS
char *user = NULL, *password = NULL, *token = NULL, *endp_arg = NULL;
char uri[1024];
#endif
int ret = 0;
while (1) {
int r = getopt_long(argc, argv, "h-", longopt, &option_idx);
if (r < 0)
break;
if (r == 'h') {
ret = -2;
goto cleanup;
} else if (r == 1) {
if (sscanf(optarg, "%d", &(caph->in_fd)) != 1) {
fprintf(stderr, "FATAL: Unable to parse incoming file descriptor\n");
ret = -1;
goto cleanup;
}
caph->use_ipc = 1;
} else if (r == 2) {
if (sscanf(optarg, "%d", &(caph->out_fd)) != 1) {
fprintf(stderr, "FATAL: Unable to parse outgoing file descriptor\n");
ret = -1;
goto cleanup;
}
caph->use_ipc = 1;
} else if (r == 3) {
if (sscanf(optarg, "%512[^:]:%u", parse_hname, &parse_port) != 2) {
fprintf(stderr, "FATAL: Expected host:port for --connect\n");
ret = -1;
goto cleanup;
}
caph->remote_host = strdup(parse_hname);
caph->remote_port = parse_port;
} else if (r == 4) {
caph->cli_sourcedef = strdup(optarg);
} else if (r == 5) {
fprintf(stderr, "INFO: Disabling automatic reconnection to remote servers\n");
retry = 0;
} else if (r == 6) {
fprintf(stderr, "INFO: Entering daemon mode after initial setup\n");
daemon = 1;
} else if (r == 7) {
cf_handler_list_devices(caph);
cf_handler_free(caph);
exit(1);
} else if (r == 8) {
gps_arg = strdup(optarg);
} else if (r == 9) {
caph->gps_name = strdup(optarg);
} else if (r == 10) {
if (sscanf(optarg, "%512[^:]:%u", parse_hname, &parse_port) != 2) {
fprintf(stderr, "FATAL: Expected ip:port for --host\n");
ret = -1;
goto cleanup;
}
caph->remote_host = strdup(parse_hname);
caph->remote_port = parse_port;
} else if (r == 11) {
autodetect = 1;
if (optarg != NULL)
caph->announced_uuid = strdup(optarg);
} else if (r == 12) {
caph->use_tcp = 1;
} else if (r == 13) {
#ifdef HAVE_LIBWEBSOCKETS
caph->lwsusessl = 1;
#else
fprintf(stderr, "FATAL: Cannot specify ssl when not compiled with websockets support\n");
ret = -1;
goto cleanup;
#endif
} else if (r == 14) {
#ifdef HAVE_LIBWEBSOCKETS
user = strdup(optarg);
#else
fprintf(stderr, "FATAL: Cannot user a username or password login when not "
"compiled with websockets support\n");
ret = -1;
goto cleanup;
#endif
} else if (r == 15) {
#ifdef HAVE_LIBWEBSOCKETS
password = strdup(optarg);
#else
fprintf(stderr, "FATAL: Cannot use a username or password login when not "
"compiled with websockets support\n");
ret = -1;
goto cleanup;
#endif
} else if (r == 16) {
#ifdef HAVE_LIBWEBSOCKETS
token = strdup(optarg);
#else
fprintf(stderr, "FATAL: Cannot use an API key when not compiled with "
"websockets support\n");
ret = -1;
goto cleanup;
#endif
} else if (r == 17) {
#ifdef HAVE_LIBWEBSOCKETS
endp_arg = strdup(optarg);
#else
fprintf(stderr, "FATAL: Cannot use custom endpoint when not compiled "
"with websockets support.\n");
ret = -1;
goto cleanup;
#endif
} else if (r == 18) {
#ifdef HAVE_LIBWEBSOCKETS
caph->lwssslcapath = strdup(optarg);
#else
fprintf(stderr, "FATAL: Cannot use SSL certificates when not compiled "
"with websockets support\n");
return -1;
goto cleanup;
#endif
}
}
#ifndef HAVE_LIBWEBSOCKETS
if (caph->remote_host != NULL && caph->use_tcp == 0) {
fprintf(stderr, "FATAL: Must specify --tcp when not compiled with websockets support\n");
ret = -1;
goto cleanup;
}
#endif
if (caph->remote_host != NULL && (caph->use_tcp == 0 && caph->remote_port == 3501)) {
fprintf(stderr, "WARNING: It looks like you're using a legacy TCP remote capture port, but\n"
" did not specify '--tcp'; this probably is not what you want!\n");
}
/* Spin looking for the remote announcement */
if (autodetect)
if (cf_wait_announcement(caph) < 0) {
ret = -1;
goto cleanup;
}
if (caph->remote_host == NULL) {
if (caph->cli_sourcedef != NULL)
fprintf(stderr, "WARNING: Ignoring --source option when not in remote mode.\n");
#ifdef HAVE_LIBWEBSOCKETS
if (user != NULL || password != NULL)
fprintf(stderr, "WARNING: Ignoring --user and --password options when not in "
"remote mode\n");
if (token != NULL)
fprintf(stderr, "WARNING: Ignoring --apikey when not in remote mode\n");
#endif
}
#ifdef HAVE_LIBWEBSOCKETS
if (caph->use_tcp && (user != NULL || password != NULL || token != NULL))
fprintf(stderr, "WARNING: Ignoring user, password, and apikeys in legacy TCP mode\n");
if ((user != NULL || password != NULL) && (user == NULL || password == NULL)) {
fprintf(stderr, "FATAL: Must specify both username and password\n");
ret = -1;
goto cleanup;
}
#endif
if (caph->remote_host == NULL && gps_arg != NULL) {
fprintf(stderr, "WARNING: Ignoring --fixed-gps option when not in remote mode.\n");
}
if (gps_arg != NULL) {
pr = sscanf(gps_arg, "%lf,%lf,%lf",
&(caph->gps_fixed_lat), &(caph->gps_fixed_lon),
&(caph->gps_fixed_alt));
if (pr == 2) {
caph->gps_fixed_alt = 0;
} else if (pr < 2) {
fprintf(stderr, "FATAL: --fixed-gps expects lat,lon or lat,lon,alt\n");
ret = -1;
goto cleanup;
}
free(gps_arg);
gps_arg = NULL;
}
if (caph->remote_host != NULL) {
/* Must have a --source to present to the remote host */
if (caph->cli_sourcedef == NULL) {
fprintf(stderr, "FATAL: --source option required when connecting to a remote host\n");
return -1;
}
/* Set retry only when we have a remote host */
caph->remote_retry = retry;
/* Set daemon mode only when we have a remote host */
caph->daemonize = daemon;
/* If we're not tcp, we're websockets */
if (!caph->use_tcp) {
#ifdef HAVE_LIBWEBSOCKETS
caph->use_ws = 1;
#else
fprintf(stderr, "FATAL: Not compiled with libwebsockets support, cannot use websockets remote capture.\n");
#endif
}
#ifdef HAVE_LIBWEBSOCKETS
if (caph->use_ws && user == NULL && password == NULL && token == NULL) {
fprintf(stderr, "FATAL: User and password or API key required for remote capture\n");
ret = -1;
goto cleanup;
}
if (user != NULL && token != NULL)
fprintf(stderr, "WARNING: Ignoring APIKEY and using login information\n");
if (endp_arg == NULL)
endp_arg = strdup("/datasource/remote/remotesource.ws");
else
fprintf(stderr, "INFO: Using custom endpoint path %s\n", endp_arg);
if (user != NULL && password != NULL) {
snprintf(uri, 1024, "%s?user=%s&password=%s", endp_arg, user, password);
} else if (token != NULL) {
snprintf(uri, 1024, "%s?KISMET=%s", endp_arg, token);
}