@@ -102,7 +102,6 @@ static int
102
102
testframe_init (struct module_env * env , struct cachedb_env * cachedb_env )
103
103
{
104
104
struct testframe_moddata * d ;
105
- (void )env ;
106
105
verbose (VERB_ALGO , "testframe_init" );
107
106
d = (struct testframe_moddata * )calloc (1 ,
108
107
sizeof (struct testframe_moddata ));
@@ -111,6 +110,15 @@ testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
111
110
log_err ("out of memory" );
112
111
return 0 ;
113
112
}
113
+ /* Register an EDNS option (65534) to bypass the worker cache lookup
114
+ * for testing */
115
+ if (!edns_register_option (LDNS_EDNS_UNBOUND_CACHEDB_TESTFRAME_TEST ,
116
+ 1 /* bypass cache */ ,
117
+ 0 /* no aggregation */ , env )) {
118
+ log_err ("testframe_init, could not register test opcode" );
119
+ free (d );
120
+ return 0 ;
121
+ }
114
122
lock_basic_init (& d -> lock );
115
123
lock_protect (& d -> lock , d , sizeof (* d ));
116
124
return 1 ;
@@ -218,6 +226,8 @@ static int
218
226
cachedb_apply_cfg (struct cachedb_env * cachedb_env , struct config_file * cfg )
219
227
{
220
228
const char * backend_str = cfg -> cachedb_backend ;
229
+ if (!backend_str || * backend_str == 0 )
230
+ return 1 ;
221
231
cachedb_env -> backend = cachedb_find_backend (backend_str );
222
232
if (!cachedb_env -> backend ) {
223
233
log_err ("cachedb: cannot find backend name '%s'" , backend_str );
@@ -228,7 +238,7 @@ cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
228
238
return 1 ;
229
239
}
230
240
231
- int
241
+ int
232
242
cachedb_init (struct module_env * env , int id )
233
243
{
234
244
struct cachedb_env * cachedb_env = (struct cachedb_env * )calloc (1 ,
@@ -255,31 +265,28 @@ cachedb_init(struct module_env* env, int id)
255
265
return 0 ;
256
266
}
257
267
cachedb_env -> enabled = 1 ;
258
- if (env -> cfg -> serve_expired_reply_ttl )
268
+ if (env -> cfg -> serve_expired && env -> cfg -> serve_expired_reply_ttl )
259
269
log_warn (
260
270
"cachedb: serve-expired-reply-ttl is set but not working for data "
261
- "originating from the external cache; 0 TLL is used for those." );
262
- if (env -> cfg -> serve_expired_client_timeout )
271
+ "originating from the external cache; 0 TTL is used for those." );
272
+ if (env -> cfg -> serve_expired && env -> cfg -> serve_expired_client_timeout )
263
273
log_warn (
264
274
"cachedb: serve-expired-client-timeout is set but not working for "
265
275
"data originating from the external cache; expired data are used "
266
276
"in the reply without first trying to refresh the data." );
267
277
return 1 ;
268
278
}
269
279
270
- void
280
+ void
271
281
cachedb_deinit (struct module_env * env , int id )
272
282
{
273
283
struct cachedb_env * cachedb_env ;
274
284
if (!env || !env -> modinfo [id ])
275
285
return ;
276
286
cachedb_env = (struct cachedb_env * )env -> modinfo [id ];
277
- /* free contents */
278
- /* TODO */
279
287
if (cachedb_env -> enabled ) {
280
288
(* cachedb_env -> backend -> deinit )(env , cachedb_env );
281
289
}
282
-
283
290
free (cachedb_env );
284
291
env -> modinfo [id ] = NULL ;
285
292
}
@@ -406,6 +413,14 @@ prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
406
413
if (qstate -> return_msg -> rep -> ttl == 0 &&
407
414
!qstate -> env -> cfg -> serve_expired )
408
415
return 0 ;
416
+
417
+ /* The EDE is added to the out-list so it is encoded in the cached message */
418
+ if (qstate -> env -> cfg -> ede && qstate -> return_msg -> rep -> reason_bogus != LDNS_EDE_NONE ) {
419
+ edns_opt_list_append_ede (& edns .opt_list_out , qstate -> env -> scratch ,
420
+ qstate -> return_msg -> rep -> reason_bogus ,
421
+ qstate -> return_msg -> rep -> reason_bogus_str );
422
+ }
423
+
409
424
if (verbosity >= VERB_ALGO )
410
425
log_dns_msg ("cachedb encoding" , & qstate -> return_msg -> qinfo ,
411
426
qstate -> return_msg -> rep );
@@ -502,6 +517,7 @@ parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
502
517
{
503
518
struct msg_parse * prs ;
504
519
struct edns_data edns ;
520
+ struct edns_option * ede ;
505
521
uint64_t timestamp , expiry ;
506
522
time_t adjust ;
507
523
size_t lim = sldns_buffer_limit (buf );
@@ -539,6 +555,24 @@ parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
539
555
if (!qstate -> return_msg )
540
556
return 0 ;
541
557
558
+ /* We find the EDE in the in-list after parsing */
559
+ if (qstate -> env -> cfg -> ede &&
560
+ (ede = edns_opt_list_find (edns .opt_list_in , LDNS_EDNS_EDE ))) {
561
+ if (ede -> opt_len >= 2 ) {
562
+ qstate -> return_msg -> rep -> reason_bogus =
563
+ sldns_read_uint16 (ede -> opt_data );
564
+ }
565
+ /* allocate space and store the error string and it's size */
566
+ if (ede -> opt_len > 2 ) {
567
+ size_t ede_len = ede -> opt_len - 2 ;
568
+ qstate -> return_msg -> rep -> reason_bogus_str = regional_alloc (
569
+ qstate -> region , sizeof (char ) * (ede_len + 1 ));
570
+ memcpy (qstate -> return_msg -> rep -> reason_bogus_str ,
571
+ ede -> opt_data + 2 , ede_len );
572
+ qstate -> return_msg -> rep -> reason_bogus_str [ede_len ] = 0 ;
573
+ }
574
+ }
575
+
542
576
qstate -> return_rcode = LDNS_RCODE_NOERROR ;
543
577
544
578
/* see how much of the TTL expired, and remove it */
@@ -630,11 +664,15 @@ cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
630
664
* See if unbound's internal cache can answer the query
631
665
*/
632
666
static int
633
- cachedb_intcache_lookup (struct module_qstate * qstate )
667
+ cachedb_intcache_lookup (struct module_qstate * qstate , struct cachedb_env * cde )
634
668
{
635
669
uint8_t * dpname = NULL ;
636
670
size_t dpnamelen = 0 ;
637
671
struct dns_msg * msg ;
672
+ /* for testframe bypass this lookup */
673
+ if (cde -> backend == & testframe_backend ) {
674
+ return 0 ;
675
+ }
638
676
if (iter_stub_fwd_no_cache (qstate , & qstate -> qinfo ,
639
677
& dpname , & dpnamelen ))
640
678
return 0 ; /* no cache for these queries */
@@ -693,6 +731,7 @@ cachedb_handle_query(struct module_qstate* qstate,
693
731
struct cachedb_qstate * ATTR_UNUSED (iq ),
694
732
struct cachedb_env * ie , int id )
695
733
{
734
+ qstate -> is_cachedb_answer = 0 ;
696
735
/* check if we are enabled, and skip if so */
697
736
if (!ie -> enabled ) {
698
737
/* pass request to next module */
@@ -709,7 +748,7 @@ cachedb_handle_query(struct module_qstate* qstate,
709
748
710
749
/* lookup inside unbound's internal cache.
711
750
* This does not look for expired entries. */
712
- if (cachedb_intcache_lookup (qstate )) {
751
+ if (cachedb_intcache_lookup (qstate , ie )) {
713
752
if (verbosity >= VERB_ALGO ) {
714
753
if (qstate -> return_msg -> rep )
715
754
log_dns_msg ("cachedb internal cache lookup" ,
@@ -746,6 +785,7 @@ cachedb_handle_query(struct module_qstate* qstate,
746
785
qstate -> ext_state [id ] = module_wait_module ;
747
786
return ;
748
787
}
788
+ qstate -> is_cachedb_answer = 1 ;
749
789
/* we are done with the query */
750
790
qstate -> ext_state [id ] = module_finished ;
751
791
return ;
@@ -768,12 +808,18 @@ static void
768
808
cachedb_handle_response (struct module_qstate * qstate ,
769
809
struct cachedb_qstate * ATTR_UNUSED (iq ), struct cachedb_env * ie , int id )
770
810
{
811
+ qstate -> is_cachedb_answer = 0 ;
771
812
/* check if we are not enabled or instructed to not cache, and skip */
772
813
if (!ie -> enabled || qstate -> no_cache_store ) {
773
814
/* we are done with the query */
774
815
qstate -> ext_state [id ] = module_finished ;
775
816
return ;
776
817
}
818
+ if (qstate -> env -> cfg -> cachedb_no_store ) {
819
+ /* do not store the item in the external cache */
820
+ qstate -> ext_state [id ] = module_finished ;
821
+ return ;
822
+ }
777
823
778
824
/* store the item into the backend cache */
779
825
cachedb_extcache_store (qstate , ie );
0 commit comments