48
48
// 24 hours
49
49
#define CACHE_EXPIRY 5
50
50
51
+ #ifndef DEBUG
52
+ #define DEBUG 0
53
+ #endif
54
+
51
55
DCPLUGIN_MAIN (__FILE__ );
52
56
53
57
int publish_query (plugin_data_t * data );
@@ -84,13 +88,15 @@ int dcplugin_init(DCPlugin * const dcplugin, int argc, char *argv[]) {
84
88
85
89
data -> cache = NULL ;
86
90
data -> offline = 0 ;
87
- data -> debug = 0 ;
91
+ data -> debug = DEBUG ;
92
+ if (data -> debug ) act ("BUILD TIME DEBUG ON" );
88
93
89
94
for (i = 0 ; i < argc ; i ++ ) {
90
95
func ("%u arg: %s" , i , argv [i ]);
91
96
92
97
if ( strncmp (argv [i ], "debug" , 5 ) == 0 ) {
93
98
data -> debug = 1 ;
99
+ act ("RUN TIME DEBUG ON" );
94
100
// TODO: check error
95
101
}
96
102
@@ -153,14 +159,14 @@ int dcplugin_init(DCPlugin * const dcplugin, int argc, char *argv[]) {
153
159
data -> redis_stor = connect_redis (REDIS_HOST , REDIS_PORT , db_storage );
154
160
if (!data -> redis_stor ) return 1 ;
155
161
156
- data -> cache = connect_redis (REDIS_HOST , REDIS_PORT , db_runtime );
157
- if (!data -> cache ) return 1 ;
162
+ // data->cache = connect_redis(REDIS_HOST, REDIS_PORT, db_runtime);
163
+ // if(!data->cache) return 1;
158
164
159
165
// // save the cache connection to runtime db as logger
160
166
// log_redis = data->cache;
161
167
162
168
dcplugin_set_user_data (dcplugin , data );
163
-
169
+ notice ( "Dowse plugin initialisation succesfull" );
164
170
return 0 ;
165
171
}
166
172
@@ -327,7 +333,7 @@ DCPluginSyncFilterResult dcplugin_sync_pre_filter(DCPlugin *dcplugin, DCPluginDN
327
333
328
334
// resolve locally leased hostnames with a O(1) operation on redis
329
335
data -> reply = cmd_redis (data -> redis_stor ,
330
- "GET dns-lease -%s" ,
336
+ "GET dns-reverse -%s" ,
331
337
reverse_str );
332
338
if (data -> reply )
333
339
if (data -> reply -> len ) { // it exists, return that
@@ -418,11 +424,9 @@ DCPluginSyncFilterResult dcplugin_sync_pre_filter(DCPlugin *dcplugin, DCPluginDN
418
424
// retrieve mac_address of client and writes it into data->mac
419
425
if ( ip4_derive_mac (data ) != 0 ) {
420
426
421
- // redirect to dowse if it can't resolve the macaddress
422
- data -> reply = cmd_redis (data -> redis , "GET dns-lease-dowse.it" );
423
427
warn ("can't resolv mac address of IP: %s" , data -> ip4 );
424
428
func ("redirect on captive portal due to ip2mac() internal error" );
425
- snprintf (rr_to_redirect , 1024 , "%s 0 IN A %s" , data -> query , data -> reply -> str );
429
+ snprintf (rr_to_redirect , 1024 , "%s 0 IN A %s" , data -> query , data -> ownip4 );
426
430
freeReplyObject (data -> reply );
427
431
428
432
// return a wire packet immediately
@@ -441,27 +445,27 @@ DCPluginSyncFilterResult dcplugin_sync_pre_filter(DCPlugin *dcplugin, DCPluginDN
441
445
442
446
// check if party_mode is on then no need to control authorization to browse
443
447
data -> reply = cmd_redis (data -> redis_stor ,"GET party_mode" );
444
- if (data -> reply -> str )
445
- if ( strncmp (data -> reply -> str ,"yes" ,3 ) == 0 )
446
- party_mode = 1 ;
447
- freeReplyObject (data -> reply );
448
+ if (data -> reply ) {
449
+ if (data -> reply -> str )
450
+ if ( strncmp (data -> reply -> str ,"yes" ,3 ) == 0 )
451
+ party_mode = 1 ;
452
+ freeReplyObject (data -> reply );
453
+ }
448
454
449
455
if (!party_mode ) {
450
-
451
456
// check if the mac address is authorized
452
457
data -> reply = cmd_redis (data -> redis_stor , "HGET thing_%s enable_to_browse" , data -> mac );
453
- if (data -> reply -> str )
454
- if ( strncmp (data -> reply -> str , "yes" , 3 ) == 0 )
455
- enable_to_browse = 1 ;
456
- freeReplyObject (data -> reply );
458
+ if (data -> reply ) {
459
+ if (data -> reply -> str )
460
+ if ( strncmp (data -> reply -> str , "yes" , 3 ) == 0 )
461
+ enable_to_browse = 1 ;
462
+ freeReplyObject (data -> reply );
463
+ }
457
464
458
465
if (!enable_to_browse ) {
459
-
460
466
// redirect to dowse if it is not authorized
461
- data -> reply = cmd_redis (data -> redis , "GET dns-lease-dowse.it" );
462
467
func ("redirect on captive portal for ip %s mac %s" , data -> ip4 , data -> mac );
463
- snprintf (rr_to_redirect , 1024 , "%s 0 IN A %s" , data -> query , data -> reply -> str );
464
- freeReplyObject (data -> reply );
468
+ snprintf (rr_to_redirect , 1024 , "%s 0 IN A %s" , data -> query , data -> ownip4 );
465
469
466
470
// return a wire packet immediately
467
471
outbuf = answer_to_question (packet_id , question_rr , rr_to_redirect , & answer_size );
@@ -482,55 +486,58 @@ DCPluginSyncFilterResult dcplugin_sync_pre_filter(DCPlugin *dcplugin, DCPluginDN
482
486
483
487
// DIRECT ENDPOINT
484
488
// resolve locally leased hostnames with a O(1) operation on redis
485
- data -> reply = cmd_redis (data -> redis_stor , "GET dns-lease-%s" , data -> query );
486
- if (data -> reply -> len ) { // it exists, return that
487
- size_t answer_size = 0 ;
488
- uint8_t * outbuf = NULL ;
489
- char tmprr [1024 ];
489
+ data -> reply = cmd_redis (data -> redis , "GET dns-lease-%s" , data -> query );
490
+ if (data -> reply ) {
491
+ if (data -> reply -> len ) { // it exists, return that
492
+ size_t answer_size = 0 ;
493
+ uint8_t * outbuf = NULL ;
494
+ char tmprr [1024 ];
490
495
491
- if (data -> debug )
492
- func ("local lease found: %s" , data -> reply -> str );
496
+ if (data -> debug )
497
+ func ("local lease found: %s" , data -> reply -> str );
493
498
494
- snprintf (tmprr , 1024 , "%s 0 IN A %s" , data -> query , data -> reply -> str );
495
- freeReplyObject (data -> reply );
499
+ snprintf (tmprr , 1024 , "%s 0 IN A %s" , data -> query , data -> reply -> str );
500
+ freeReplyObject (data -> reply );
496
501
497
- outbuf = answer_to_question (packet_id , question_rr ,
498
- tmprr , & answer_size );
502
+ outbuf = answer_to_question (packet_id , question_rr ,
503
+ tmprr , & answer_size );
499
504
500
- if (!outbuf ) {
501
- ldns_pkt_free (packet );
502
- return DCP_SYNC_FILTER_RESULT_KILL ;
503
- }
505
+ if (!outbuf ) {
506
+ ldns_pkt_free (packet );
507
+ return DCP_SYNC_FILTER_RESULT_KILL ;
508
+ }
504
509
505
- dcplugin_set_wire_data (dcp_packet , outbuf , answer_size );
510
+ dcplugin_set_wire_data (dcp_packet , outbuf , answer_size );
506
511
507
- if (outbuf ) LDNS_FREE (outbuf );
508
- ldns_pkt_free (packet );
509
- return DCP_SYNC_FILTER_RESULT_DIRECT ;
512
+ if (outbuf ) LDNS_FREE (outbuf );
513
+ ldns_pkt_free (packet );
514
+ return DCP_SYNC_FILTER_RESULT_DIRECT ;
515
+ }
516
+ ////////////////
517
+ freeReplyObject (data -> reply );
510
518
}
511
- ////////////////
512
- freeReplyObject (data -> reply );
513
519
514
520
if (data -> cache ) {
515
521
// check if the answer is cached (the key is the domain string)
516
522
data -> reply = cmd_redis (data -> cache , "GET dns-cache-%s" , data -> query );
517
- if (data -> reply -> len ) { // it exists in cache, return that
523
+ if (data -> reply )
524
+ if (data -> reply -> len ) { // it exists in cache, return that
518
525
519
- if (data -> debug )
520
- func ("found in cache wire packet of %u bytes" , data -> reply -> len );
526
+ if (data -> debug )
527
+ func ("found in cache wire packet of %u bytes" , data -> reply -> len );
521
528
522
529
523
- // a bit dangerous, but veeery fast: working directly on the wire packet
524
- // copy message ID (first 16 bits)
525
- data -> reply -> str [0 ] = wire [0 ];
526
- data -> reply -> str [1 ] = wire [1 ];
530
+ // a bit dangerous, but veeery fast: working directly on the wire packet
531
+ // copy message ID (first 16 bits)
532
+ data -> reply -> str [0 ] = wire [0 ];
533
+ data -> reply -> str [1 ] = wire [1 ];
527
534
528
- dcplugin_set_wire_data (dcp_packet , data -> reply -> str , data -> reply -> len );
529
- freeReplyObject (data -> reply );
535
+ dcplugin_set_wire_data (dcp_packet , data -> reply -> str , data -> reply -> len );
536
+ freeReplyObject (data -> reply );
530
537
531
- ldns_pkt_free (packet );
532
- return DCP_SYNC_FILTER_RESULT_DIRECT ;
533
- }
538
+ ldns_pkt_free (packet );
539
+ return DCP_SYNC_FILTER_RESULT_DIRECT ;
540
+ }
534
541
}
535
542
536
543
// if(from_sa->ss_family == AF_PACKET) { // if contains mac address
@@ -729,30 +736,34 @@ int publish_query(plugin_data_t *data) {
729
736
// domain hit count
730
737
extracted = extract_domain (data );
731
738
data -> reply = cmd_redis (data -> redis , "INCR dns-query-%s" , extracted );
732
- val = data -> reply -> integer ;
733
- freeReplyObject (data -> reply );
739
+ if (data -> reply ) {
740
+ val = data -> reply -> integer ;
741
+ freeReplyObject (data -> reply );
742
+ }
734
743
735
744
data -> reply = cmd_redis (data -> redis , "EXPIRE dns-query-%s %u" , extracted , DNS_HIT_EXPIRE ); // DNS_HIT_EXPIRE
736
- freeReplyObject (data -> reply );
745
+ if ( data -> reply ) freeReplyObject (data -> reply );
737
746
738
747
// timestamp
739
748
time (& epoch_t );
740
749
741
750
// retrieve thing's name from redis
742
751
data -> reply = cmd_redis (data -> redis_stor , "HGET thing_%s name" , data -> mac );
743
- if (data -> reply -> str ) { // we have the name
744
- // compose the path of the detected query
745
- snprintf (outnew , MAX_OUTPUT ,
746
- "DNS,%s,%d,%lu,%s,%s" ,
747
- data -> reply -> str , val ,
748
- epoch_t , extracted , data -> tld );
749
- } else {
750
- snprintf (outnew , MAX_OUTPUT ,
751
- "DNS,%s,%d,%lu,%s,%s" ,
752
- data -> from , val ,
753
- epoch_t , extracted , data -> tld );
752
+ if (data -> reply ) {
753
+ if (data -> reply -> str ) { // we have the name
754
+ // compose the path of the detected query
755
+ snprintf (outnew , MAX_OUTPUT ,
756
+ "DNS,%s,%d,%lu,%s,%s" ,
757
+ data -> reply -> str , val ,
758
+ epoch_t , extracted , data -> tld );
759
+ } else {
760
+ snprintf (outnew , MAX_OUTPUT ,
761
+ "DNS,%s,%d,%lu,%s,%s" ,
762
+ data -> from , val ,
763
+ epoch_t , extracted , data -> tld );
764
+ }
765
+ freeReplyObject (data -> reply );
754
766
}
755
- freeReplyObject (data -> reply );
756
767
757
768
// add domainlist group if found
758
769
if (data -> listpath ) {
@@ -765,7 +776,7 @@ int publish_query(plugin_data_t *data) {
765
776
}
766
777
767
778
data -> reply = cmd_redis (data -> redis , "PUBLISH dns-query-channel %s" , outnew );
768
- freeReplyObject (data -> reply );
779
+ if ( data -> reply ) freeReplyObject (data -> reply );
769
780
770
781
771
782
0 commit comments