1 /*
2  * testcode/fake_event.c - fake event handling that replays existing scenario.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  * Event service that replays a scenario.
39  * This implements the same exported symbols as the files:
40  * util/netevent.c
41  * services/listen_dnsport.c
42  * services/outside_network.c
43  * But these do not actually access the network or events, instead
44  * the scenario is played.
45  */
46 
47 #include "config.h"
48 #include "testcode/fake_event.h"
49 #include "util/netevent.h"
50 #include "util/net_help.h"
51 #include "util/data/msgparse.h"
52 #include "util/data/msgreply.h"
53 #include "util/data/msgencode.h"
54 #include "util/data/dname.h"
55 #include "util/edns.h"
56 #include "util/config_file.h"
57 #include "services/listen_dnsport.h"
58 #include "services/outside_network.h"
59 #include "services/cache/infra.h"
60 #include "testcode/replay.h"
61 #include "testcode/testpkts.h"
62 #include "util/log.h"
63 #include "util/fptr_wlist.h"
64 #include "sldns/sbuffer.h"
65 #include "sldns/wire2str.h"
66 #include "sldns/str2wire.h"
67 #include "daemon/remote.h"
68 #include <signal.h>
69 struct worker;
70 struct daemon_remote;
71 
72 /** unique code to check that fake_commpoint is that structure */
73 #define FAKE_COMMPOINT_TYPECODE 97347923
74 /** fake commpoint, stores information */
75 struct fake_commpoint {
76 	/** typecode */
77 	int typecode;
78 	/** if this is a udp outgoing type of commpoint */
79 	int type_udp_out;
80 	/** if this is a tcp outgoing type of commpoint */
81 	int type_tcp_out;
82 	/** if this is a http outgoing type of commpoint. */
83 	int type_http_out;
84 
85 	/** the callback, stored for usage */
86 	comm_point_callback_type* cb;
87 	/** the callback userarg, stored for usage */
88 	void* cb_arg;
89 	/** runtime ptr */
90 	struct replay_runtime* runtime;
91 	/** the pending entry for this commpoint (if any) */
92 	struct fake_pending* pending;
93 };
94 
95 /** Global variable: the scenario. Saved here for when event_init is done. */
96 static struct replay_scenario* saved_scenario = NULL;
97 
98 /** add timers and the values do not overflow or become negative */
99 static void
timeval_add(struct timeval * d,const struct timeval * add)100 timeval_add(struct timeval* d, const struct timeval* add)
101 {
102 #ifndef S_SPLINT_S
103 	d->tv_sec += add->tv_sec;
104 	d->tv_usec += add->tv_usec;
105 	if(d->tv_usec >= 1000000) {
106 		d->tv_usec -= 1000000;
107 		d->tv_sec++;
108 	}
109 #endif
110 }
111 
112 void
fake_temp_file(const char * adj,const char * id,char * buf,size_t len)113 fake_temp_file(const char* adj, const char* id, char* buf, size_t len)
114 {
115 #ifdef USE_WINSOCK
116 	snprintf(buf, len, "testbound_%u%s%s.tmp",
117 		(unsigned)getpid(), adj, id);
118 #else
119 	snprintf(buf, len, "/tmp/testbound_%u%s%s.tmp",
120 		(unsigned)getpid(), adj, id);
121 #endif
122 }
123 
124 void
fake_event_init(struct replay_scenario * scen)125 fake_event_init(struct replay_scenario* scen)
126 {
127 	saved_scenario = scen;
128 }
129 
130 void
fake_event_cleanup(void)131 fake_event_cleanup(void)
132 {
133 	replay_scenario_delete(saved_scenario);
134 	saved_scenario = NULL;
135 }
136 
137 /** helper function that logs a sldns_pkt packet to logfile */
138 static void
log_pkt(const char * desc,uint8_t * pkt,size_t len)139 log_pkt(const char* desc, uint8_t* pkt, size_t len)
140 {
141 	char* str = sldns_wire2str_pkt(pkt, len);
142 	if(!str)
143 		fatal_exit("%s: (failed out of memory wire2str_pkt)", desc);
144 	else {
145 		log_info("%s%s", desc, str);
146 		free(str);
147 	}
148 }
149 
150 /**
151  * Returns a string describing the event type.
152  */
153 static const char*
repevt_string(enum replay_event_type t)154 repevt_string(enum replay_event_type t)
155 {
156 	switch(t) {
157 	case repevt_nothing:	 return "NOTHING";
158 	case repevt_front_query: return "QUERY";
159 	case repevt_front_reply: return "CHECK_ANSWER";
160 	case repevt_timeout:	 return "TIMEOUT";
161 	case repevt_time_passes: return "TIME_PASSES";
162 	case repevt_back_reply:  return "REPLY";
163 	case repevt_back_query:  return "CHECK_OUT_QUERY";
164 	case repevt_autotrust_check: return "CHECK_AUTOTRUST";
165 	case repevt_tempfile_check: return "CHECK_TEMPFILE";
166 	case repevt_error:	 return "ERROR";
167 	case repevt_assign:	 return "ASSIGN";
168 	case repevt_traffic:	 return "TRAFFIC";
169 	case repevt_infra_rtt:	 return "INFRA_RTT";
170 	default:		 return "UNKNOWN";
171 	}
172 }
173 
174 /** delete a fake pending */
175 static void
delete_fake_pending(struct fake_pending * pend)176 delete_fake_pending(struct fake_pending* pend)
177 {
178 	if(!pend)
179 		return;
180 	free(pend->zone);
181 	sldns_buffer_free(pend->buffer);
182 	free(pend->pkt);
183 	free(pend);
184 }
185 
186 /** delete a replay answer */
187 static void
delete_replay_answer(struct replay_answer * a)188 delete_replay_answer(struct replay_answer* a)
189 {
190 	if(!a)
191 		return;
192 	if(a->repinfo.c) {
193 		sldns_buffer_free(a->repinfo.c->buffer);
194 		free(a->repinfo.c);
195 	}
196 	free(a->pkt);
197 	free(a);
198 }
199 
200 /**
201  * return: true if pending query matches the now event.
202  */
203 static int
pending_matches_current(struct replay_runtime * runtime,struct entry ** entry,struct fake_pending ** pend)204 pending_matches_current(struct replay_runtime* runtime,
205 	struct entry** entry, struct fake_pending **pend)
206 {
207 	struct fake_pending* p;
208 	struct entry* e;
209 	if(!runtime->now || runtime->now->evt_type != repevt_back_query
210 		|| !runtime->pending_list)
211 		return 0;
212 	/* see if any of the pending queries matches */
213 	for(p = runtime->pending_list; p; p = p->next) {
214 		if(runtime->now->addrlen != 0 &&
215 			sockaddr_cmp(&p->addr, p->addrlen, &runtime->now->addr,
216 			runtime->now->addrlen) != 0)
217 			continue;
218 		if((e=find_match(runtime->now->match, p->pkt, p->pkt_len,
219 			p->transport))) {
220 			*entry = e;
221 			*pend = p;
222 			return 1;
223 		}
224 	}
225 	return 0;
226 }
227 
228 /**
229  * Find the range that matches this pending message.
230  * @param runtime: runtime with current moment, and range list.
231  * @param entry: returns the pointer to entry that matches.
232  * @param pend: the pending that the entry must match.
233  * @return: true if a match is found.
234  */
235 static int
pending_find_match(struct replay_runtime * runtime,struct entry ** entry,struct fake_pending * pend)236 pending_find_match(struct replay_runtime* runtime, struct entry** entry,
237 	struct fake_pending* pend)
238 {
239 	int timenow = runtime->now->time_step;
240 	struct replay_range* p = runtime->scenario->range_list;
241 	while(p) {
242 		if(p->start_step <= timenow && timenow <= p->end_step &&
243 		  (p->addrlen == 0 || sockaddr_cmp(&p->addr, p->addrlen,
244 		  	&pend->addr, pend->addrlen) == 0) &&
245 		  (*entry = find_match(p->match, pend->pkt, pend->pkt_len,
246 		 	 pend->transport))) {
247 			log_info("matched query time %d in range [%d, %d] "
248 				"with entry line %d", timenow,
249 				p->start_step, p->end_step, (*entry)->lineno);
250 			if(p->addrlen != 0)
251 				log_addr(0, "matched ip", &p->addr, p->addrlen);
252 			log_pkt("matched pkt: ",
253 				(*entry)->reply_list->reply_pkt,
254 				(*entry)->reply_list->reply_len);
255 			return 1;
256 		}
257 		p = p->next_range;
258 	}
259 	return 0;
260 }
261 
262 /**
263  * See if outgoing pending query matches an entry.
264  * @param runtime: runtime.
265  * @param entry: if true, the entry that matches is returned.
266  * @param pend: if true, the outgoing message that matches is returned.
267  * @return: true if pending query matches the now event.
268  */
269 static int
pending_matches_range(struct replay_runtime * runtime,struct entry ** entry,struct fake_pending ** pend)270 pending_matches_range(struct replay_runtime* runtime,
271 	struct entry** entry, struct fake_pending** pend)
272 {
273 	struct fake_pending* p = runtime->pending_list;
274 	/* slow, O(N*N), but it works as advertised with weird matching */
275 	while(p) {
276 		if(p->tcp_pkt_counter != 0) {
277 			/* continue tcp transfer */
278 			*pend = p;
279 			return 1;
280 		}
281 		if(pending_find_match(runtime, entry, p)) {
282 			*pend = p;
283 			return 1;
284 		}
285 		p = p->next;
286 	}
287 	return 0;
288 }
289 
290 /**
291  * Remove the item from the pending list.
292  */
293 static void
pending_list_delete(struct replay_runtime * runtime,struct fake_pending * pend)294 pending_list_delete(struct replay_runtime* runtime, struct fake_pending* pend)
295 {
296 	struct fake_pending** prev = &runtime->pending_list;
297 	struct fake_pending* p = runtime->pending_list;
298 
299 	while(p) {
300 		if(p == pend) {
301 			*prev = p->next;
302 			delete_fake_pending(pend);
303 			return;
304 		}
305 
306 		prev = &p->next;
307 		p = p->next;
308 	}
309 }
310 
311 /** number of replies in entry */
312 static int
count_reply_packets(struct entry * entry)313 count_reply_packets(struct entry* entry)
314 {
315 	int count = 0;
316 	struct reply_packet* reppkt = entry->reply_list;
317 	while(reppkt) {
318 		count++;
319 		reppkt = reppkt->next;
320 	}
321 	return count;
322 }
323 
324 /**
325  * Fill buffer with reply from the entry.
326  */
327 static void
fill_buffer_with_reply(sldns_buffer * buffer,struct entry * entry,uint8_t * q,size_t qlen,int tcp_pkt_counter)328 fill_buffer_with_reply(sldns_buffer* buffer, struct entry* entry, uint8_t* q,
329 	size_t qlen, int tcp_pkt_counter)
330 {
331 	struct reply_packet* reppkt;
332 	uint8_t* c;
333 	size_t clen;
334 	log_assert(entry && entry->reply_list);
335 	sldns_buffer_clear(buffer);
336 	reppkt = entry->reply_list;
337 	if(tcp_pkt_counter > 0) {
338 		int i = tcp_pkt_counter;
339 		while(reppkt && i--)
340 			reppkt = reppkt->next;
341 		if(!reppkt) fatal_exit("extra packet read from TCP stream but none is available");
342 		log_pkt("extra_packet ", reppkt->reply_pkt, reppkt->reply_len);
343 	}
344 	if(reppkt->reply_from_hex) {
345 		c = sldns_buffer_begin(reppkt->reply_from_hex);
346 		clen = sldns_buffer_limit(reppkt->reply_from_hex);
347 		if(!c) fatal_exit("out of memory");
348 	} else {
349 		c = reppkt->reply_pkt;
350 		clen = reppkt->reply_len;
351 	}
352 	if(c) {
353 		if(q) adjust_packet(entry, &c, &clen, q, qlen);
354 		sldns_buffer_write(buffer, c, clen);
355 		if(q) free(c);
356 	}
357 	sldns_buffer_flip(buffer);
358 }
359 
360 /**
361  * Perform range entry on pending message.
362  * @param runtime: runtime buffer size preference.
363  * @param entry: entry that codes for the reply to do.
364  * @param pend: pending query that is answered, callback called.
365  */
366 static void
answer_callback_from_entry(struct replay_runtime * runtime,struct entry * entry,struct fake_pending * pend)367 answer_callback_from_entry(struct replay_runtime* runtime,
368         struct entry* entry, struct fake_pending* pend)
369 {
370 	struct comm_point c;
371 	struct comm_reply repinfo;
372 	void* cb_arg = pend->cb_arg;
373 	comm_point_callback_type* cb = pend->callback;
374 
375 	memset(&c, 0, sizeof(c));
376 	c.fd = -1;
377 	c.buffer = sldns_buffer_new(runtime->bufsize);
378 	c.type = comm_udp;
379 	if(pend->transport == transport_tcp) {
380 		c.type = comm_tcp;
381 		c.tcp_timeout_msec = 30000;
382 		c.tcp_keepalive = runtime->tcp_seen_keepalive;
383 	}
384 	fill_buffer_with_reply(c.buffer, entry, pend->pkt, pend->pkt_len,
385 		pend->tcp_pkt_counter);
386 	repinfo.c = &c;
387 	repinfo.addrlen = pend->addrlen;
388 	memcpy(&repinfo.addr, &pend->addr, pend->addrlen);
389 	if(!pend->serviced) {
390 		if(entry && entry->reply_list->next &&
391 			pend->tcp_pkt_counter < count_reply_packets(entry)) {
392 			/* go to next packet next time */
393 			pend->tcp_pkt_counter++;
394 		} else {
395 			pending_list_delete(runtime, pend);
396 		}
397 	}
398 	if((*cb)(&c, cb_arg, NETEVENT_NOERROR, &repinfo)) {
399 		fatal_exit("testbound: unexpected: callback returned 1");
400 	}
401 	sldns_buffer_free(c.buffer);
402 }
403 
404 /** Check the now moment answer check event */
405 static void
answer_check_it(struct replay_runtime * runtime)406 answer_check_it(struct replay_runtime* runtime)
407 {
408 	struct replay_answer* ans = runtime->answer_list,
409 		*prev = NULL;
410 	log_assert(runtime && runtime->now &&
411 		runtime->now->evt_type == repevt_front_reply);
412 	while(ans) {
413 		enum transport_type tr = transport_tcp;
414 		if(ans->repinfo.c->type == comm_udp)
415 			tr = transport_udp;
416 		if((runtime->now->addrlen == 0 || sockaddr_cmp(
417 			&runtime->now->addr, runtime->now->addrlen,
418 			&ans->repinfo.addr, ans->repinfo.addrlen) == 0) &&
419 			find_match(runtime->now->match, ans->pkt,
420 				ans->pkt_len, tr)) {
421 			log_info("testbound matched event entry from line %d",
422 				runtime->now->match->lineno);
423 			log_info("testbound: do STEP %d %s",
424 				runtime->now->time_step,
425 				repevt_string(runtime->now->evt_type));
426 			if(prev)
427 				prev->next = ans->next;
428 			else 	runtime->answer_list = ans->next;
429 			if(!ans->next)
430 				runtime->answer_last = prev;
431 			if(ans->repinfo.c->tcp_keepalive)
432 				runtime->tcp_seen_keepalive = 1;
433 			delete_replay_answer(ans);
434 			return;
435 		} else {
436 			prev = ans;
437 			ans = ans->next;
438 		}
439 	}
440 	log_info("testbound: do STEP %d %s", runtime->now->time_step,
441 		repevt_string(runtime->now->evt_type));
442 	fatal_exit("testbound: not matched");
443 }
444 
445 /**
446  * Create commpoint (as return address) for a fake incoming query.
447  */
448 static void
fake_front_query(struct replay_runtime * runtime,struct replay_moment * todo)449 fake_front_query(struct replay_runtime* runtime, struct replay_moment *todo)
450 {
451 	struct comm_reply repinfo;
452 	memset(&repinfo, 0, sizeof(repinfo));
453 	repinfo.c = (struct comm_point*)calloc(1, sizeof(struct comm_point));
454 	if(!repinfo.c)
455 		fatal_exit("out of memory in fake_front_query");
456 	repinfo.addrlen = (socklen_t)sizeof(struct sockaddr_in);
457 	if(todo->addrlen != 0) {
458 		repinfo.addrlen = todo->addrlen;
459 		memcpy(&repinfo.addr, &todo->addr, todo->addrlen);
460 	}
461 	repinfo.c->fd = -1;
462 	repinfo.c->ev = (struct internal_event*)runtime;
463 	repinfo.c->buffer = sldns_buffer_new(runtime->bufsize);
464 	if(todo->match->match_transport == transport_tcp) {
465 		repinfo.c->type = comm_tcp;
466 		repinfo.c->tcp_timeout_msec = 30000;
467 		repinfo.c->tcp_keepalive = runtime->tcp_seen_keepalive;
468 	} else
469 		repinfo.c->type = comm_udp;
470 	fill_buffer_with_reply(repinfo.c->buffer, todo->match, NULL, 0, 0);
471 	log_info("testbound: incoming QUERY");
472 	log_pkt("query pkt", todo->match->reply_list->reply_pkt,
473 		todo->match->reply_list->reply_len);
474 	/* call the callback for incoming queries */
475 	if((*runtime->callback_query)(repinfo.c, runtime->cb_arg,
476 		NETEVENT_NOERROR, &repinfo)) {
477 		/* send immediate reply */
478 		comm_point_send_reply(&repinfo);
479 	}
480 	/* clear it again, in case copy not done properly */
481 	memset(&repinfo, 0, sizeof(repinfo));
482 }
483 
484 /**
485  * Perform callback for fake pending message.
486  */
487 static void
fake_pending_callback(struct replay_runtime * runtime,struct replay_moment * todo,int error)488 fake_pending_callback(struct replay_runtime* runtime,
489 	struct replay_moment* todo, int error)
490 {
491 	struct fake_pending* p = runtime->pending_list;
492 	struct comm_reply repinfo;
493 	struct comm_point c;
494 	void* cb_arg;
495 	comm_point_callback_type* cb;
496 
497 	memset(&c, 0, sizeof(c));
498 	if(!p) fatal_exit("No pending queries.");
499 	cb_arg = p->cb_arg;
500 	cb = p->callback;
501 	c.buffer = sldns_buffer_new(runtime->bufsize);
502 	c.type = comm_udp;
503 	if(p->transport == transport_tcp) {
504 		c.type = comm_tcp;
505 		c.tcp_timeout_msec = 30000;
506 		c.tcp_keepalive = runtime->tcp_seen_keepalive;
507 	}
508 	if(todo->evt_type == repevt_back_reply && todo->match) {
509 		fill_buffer_with_reply(c.buffer, todo->match, p->pkt,
510 			p->pkt_len, p->tcp_pkt_counter);
511 	}
512 	repinfo.c = &c;
513 	repinfo.addrlen = p->addrlen;
514 	memcpy(&repinfo.addr, &p->addr, p->addrlen);
515 	if(!p->serviced) {
516 		if(todo->match && todo->match->reply_list->next && !error &&
517 			p->tcp_pkt_counter < count_reply_packets(todo->match)) {
518 			/* go to next packet next time */
519 			p->tcp_pkt_counter++;
520 		} else {
521 			pending_list_delete(runtime, p);
522 		}
523 	}
524 	if((*cb)(&c, cb_arg, error, &repinfo)) {
525 		fatal_exit("unexpected: pending callback returned 1");
526 	}
527 	/* delete the pending item. */
528 	sldns_buffer_free(c.buffer);
529 }
530 
531 /** pass time */
532 static void
moment_assign(struct replay_runtime * runtime,struct replay_moment * mom)533 moment_assign(struct replay_runtime* runtime, struct replay_moment* mom)
534 {
535 	char* value = macro_process(runtime->vars, runtime, mom->string);
536 	if(!value)
537 		fatal_exit("could not process macro step %d", mom->time_step);
538 	log_info("assign %s = %s", mom->variable, value);
539 	if(!macro_assign(runtime->vars, mom->variable, value))
540 		fatal_exit("out of memory storing macro");
541 	free(value);
542 	if(verbosity >= VERB_ALGO)
543 		macro_print_debug(runtime->vars);
544 }
545 
546 /** pass time */
547 static void
time_passes(struct replay_runtime * runtime,struct replay_moment * mom)548 time_passes(struct replay_runtime* runtime, struct replay_moment* mom)
549 {
550 	struct fake_timer *t;
551 	struct timeval tv = mom->elapse;
552 	if(mom->string) {
553 		char* xp = macro_process(runtime->vars, runtime, mom->string);
554 		double sec;
555 		if(!xp) fatal_exit("could not macro expand %s", mom->string);
556 		verbose(VERB_ALGO, "EVAL %s", mom->string);
557 		sec = atof(xp);
558 		free(xp);
559 #ifndef S_SPLINT_S
560 		tv.tv_sec = sec;
561 		tv.tv_usec = (int)((sec - (double)tv.tv_sec) *1000000. + 0.5);
562 #endif
563 	}
564 	timeval_add(&runtime->now_tv, &tv);
565 	runtime->now_secs = (time_t)runtime->now_tv.tv_sec;
566 #ifndef S_SPLINT_S
567 	log_info("elapsed %d.%6.6d  now %d.%6.6d",
568 		(int)tv.tv_sec, (int)tv.tv_usec,
569 		(int)runtime->now_tv.tv_sec, (int)runtime->now_tv.tv_usec);
570 #endif
571 	/* see if any timers have fired; and run them */
572 	while( (t=replay_get_oldest_timer(runtime)) ) {
573 		t->enabled = 0;
574 		log_info("fake_timer callback");
575 		fptr_ok(fptr_whitelist_comm_timer(t->cb));
576 		(*t->cb)(t->cb_arg);
577 	}
578 }
579 
580 /** check autotrust file contents */
581 static void
autotrust_check(struct replay_runtime * runtime,struct replay_moment * mom)582 autotrust_check(struct replay_runtime* runtime, struct replay_moment* mom)
583 {
584 	char name[1024], line[1024];
585 	FILE *in;
586 	int lineno = 0, oke=1;
587 	char* expanded;
588 	struct config_strlist* p;
589 	line[sizeof(line)-1] = 0;
590 	log_assert(mom->autotrust_id);
591 	fake_temp_file("_auto_", mom->autotrust_id, name, sizeof(name));
592 	in = fopen(name, "r");
593 	if(!in) fatal_exit("could not open %s: %s", name, strerror(errno));
594 	for(p=mom->file_content; p; p=p->next) {
595 		lineno++;
596 		if(!fgets(line, (int)sizeof(line)-1, in)) {
597 			log_err("autotrust check failed, could not read line");
598 			log_err("file %s, line %d", name, lineno);
599 			log_err("should be: %s", p->str);
600 			fatal_exit("autotrust_check failed");
601 		}
602 		strip_end_white(line);
603 		expanded = macro_process(runtime->vars, runtime, p->str);
604 		if(!expanded)
605 			fatal_exit("could not expand macro line %d", lineno);
606 		if(verbosity >= 7 && strcmp(p->str, expanded) != 0)
607 			log_info("expanded '%s' to '%s'", p->str, expanded);
608 		if(strcmp(expanded, line) != 0) {
609 			log_err("mismatch in file %s, line %d", name, lineno);
610 			log_err("file has : %s", line);
611 			log_err("should be: %s", expanded);
612 			free(expanded);
613 			oke = 0;
614 			continue;
615 		}
616 		free(expanded);
617 		fprintf(stderr, "%s:%2d ok : %s\n", name, lineno, line);
618 	}
619 	if(fgets(line, (int)sizeof(line)-1, in)) {
620 		log_err("autotrust check failed, extra lines in %s after %d",
621 			name, lineno);
622 		do {
623 			fprintf(stderr, "file has: %s", line);
624 		} while(fgets(line, (int)sizeof(line)-1, in));
625 		oke = 0;
626 	}
627 	fclose(in);
628 	if(!oke)
629 		fatal_exit("autotrust_check STEP %d failed", mom->time_step);
630 	log_info("autotrust %s is OK", mom->autotrust_id);
631 }
632 
633 /** check tempfile file contents */
634 static void
tempfile_check(struct replay_runtime * runtime,struct replay_moment * mom)635 tempfile_check(struct replay_runtime* runtime, struct replay_moment* mom)
636 {
637 	char name[1024], line[1024];
638 	FILE *in;
639 	int lineno = 0, oke=1;
640 	char* expanded;
641 	struct config_strlist* p;
642 	line[sizeof(line)-1] = 0;
643 	log_assert(mom->autotrust_id);
644 	fake_temp_file("_temp_", mom->autotrust_id, name, sizeof(name));
645 	in = fopen(name, "r");
646 	if(!in) fatal_exit("could not open %s: %s", name, strerror(errno));
647 	for(p=mom->file_content; p; p=p->next) {
648 		lineno++;
649 		if(!fgets(line, (int)sizeof(line)-1, in)) {
650 			log_err("tempfile check failed, could not read line");
651 			log_err("file %s, line %d", name, lineno);
652 			log_err("should be: %s", p->str);
653 			fatal_exit("tempfile_check failed");
654 		}
655 		strip_end_white(line);
656 		expanded = macro_process(runtime->vars, runtime, p->str);
657 		if(!expanded)
658 			fatal_exit("could not expand macro line %d", lineno);
659 		if(verbosity >= 7 && strcmp(p->str, expanded) != 0)
660 			log_info("expanded '%s' to '%s'", p->str, expanded);
661 		if(strcmp(expanded, line) != 0) {
662 			log_err("mismatch in file %s, line %d", name, lineno);
663 			log_err("file has : %s", line);
664 			log_err("should be: %s", expanded);
665 			free(expanded);
666 			oke = 0;
667 			continue;
668 		}
669 		free(expanded);
670 		fprintf(stderr, "%s:%2d ok : %s\n", name, lineno, line);
671 	}
672 	if(fgets(line, (int)sizeof(line)-1, in)) {
673 		log_err("tempfile check failed, extra lines in %s after %d",
674 			name, lineno);
675 		do {
676 			fprintf(stderr, "file has: %s", line);
677 		} while(fgets(line, (int)sizeof(line)-1, in));
678 		oke = 0;
679 	}
680 	fclose(in);
681 	if(!oke)
682 		fatal_exit("tempfile_check STEP %d failed", mom->time_step);
683 	log_info("tempfile %s is OK", mom->autotrust_id);
684 }
685 
686 /** Store RTT in infra cache */
687 static void
do_infra_rtt(struct replay_runtime * runtime)688 do_infra_rtt(struct replay_runtime* runtime)
689 {
690 	struct replay_moment* now = runtime->now;
691 	int rto;
692 	size_t dplen = 0;
693 	uint8_t* dp = sldns_str2wire_dname(now->variable, &dplen);
694 	if(!dp) fatal_exit("cannot parse %s", now->variable);
695 	rto = infra_rtt_update(runtime->infra, &now->addr, now->addrlen,
696 		dp, dplen, LDNS_RR_TYPE_A, atoi(now->string),
697 		-1, runtime->now_secs);
698 	log_addr(0, "INFRA_RTT for", &now->addr, now->addrlen);
699 	log_info("INFRA_RTT(%s roundtrip %d): rto of %d", now->variable,
700 		atoi(now->string), rto);
701 	if(rto == 0) fatal_exit("infra_rtt_update failed");
702 	free(dp);
703 }
704 
705 /** perform exponential backoff on the timeout */
706 static void
expon_timeout_backoff(struct replay_runtime * runtime)707 expon_timeout_backoff(struct replay_runtime* runtime)
708 {
709 	struct fake_pending* p = runtime->pending_list;
710 	int rtt, vs;
711 	uint8_t edns_lame_known;
712 	int last_rtt, rto;
713 	if(!p) return; /* no pending packet to backoff */
714 	if(!infra_host(runtime->infra, &p->addr, p->addrlen, p->zone,
715 		p->zonelen, runtime->now_secs, &vs, &edns_lame_known, &rtt))
716 		return;
717 	last_rtt = rtt;
718 	rto = infra_rtt_update(runtime->infra, &p->addr, p->addrlen, p->zone,
719 		p->zonelen, p->qtype, -1, last_rtt, runtime->now_secs);
720 	log_info("infra_rtt_update returned rto %d", rto);
721 }
722 
723 /**
724  * Advance to the next moment.
725  */
726 static void
advance_moment(struct replay_runtime * runtime)727 advance_moment(struct replay_runtime* runtime)
728 {
729 	if(!runtime->now)
730 		runtime->now = runtime->scenario->mom_first;
731 	else 	runtime->now = runtime->now->mom_next;
732 }
733 
734 /**
735  * Perform actions or checks determined by the moment.
736  * Also advances the time by one step.
737  * @param runtime: scenario runtime information.
738  */
739 static void
do_moment_and_advance(struct replay_runtime * runtime)740 do_moment_and_advance(struct replay_runtime* runtime)
741 {
742 	struct replay_moment* mom;
743 	if(!runtime->now) {
744 		advance_moment(runtime);
745 		return;
746 	}
747 	log_info("testbound: do STEP %d %s", runtime->now->time_step,
748 		repevt_string(runtime->now->evt_type));
749 	switch(runtime->now->evt_type) {
750 	case repevt_nothing:
751 		advance_moment(runtime);
752 		break;
753 	case repevt_front_query:
754 		/* advance moment before doing the step, so that the next
755 		   moment which may check some result of the mom step
756 		   can catch those results. */
757 		mom = runtime->now;
758 		advance_moment(runtime);
759 		fake_front_query(runtime, mom);
760 		break;
761 	case repevt_front_reply:
762 		if(runtime->answer_list)
763 			log_err("testbound: There are unmatched answers.");
764 		fatal_exit("testbound: query answer not matched");
765 		break;
766 	case repevt_timeout:
767 		mom = runtime->now;
768 		advance_moment(runtime);
769 		expon_timeout_backoff(runtime);
770 		fake_pending_callback(runtime, mom, NETEVENT_TIMEOUT);
771 		break;
772 	case repevt_back_reply:
773 		mom = runtime->now;
774 		advance_moment(runtime);
775 		fake_pending_callback(runtime, mom, NETEVENT_NOERROR);
776 		break;
777 	case repevt_back_query:
778 		/* Back queries are matched when they are sent out. */
779 		log_err("No query matching the current moment was sent.");
780 		fatal_exit("testbound: back query not matched");
781 		break;
782 	case repevt_error:
783 		mom = runtime->now;
784 		advance_moment(runtime);
785 		fake_pending_callback(runtime, mom, NETEVENT_CLOSED);
786 		break;
787 	case repevt_time_passes:
788 		time_passes(runtime, runtime->now);
789 		advance_moment(runtime);
790 		break;
791 	case repevt_autotrust_check:
792 		autotrust_check(runtime, runtime->now);
793 		advance_moment(runtime);
794 		break;
795 	case repevt_tempfile_check:
796 		tempfile_check(runtime, runtime->now);
797 		advance_moment(runtime);
798 		break;
799 	case repevt_assign:
800 		moment_assign(runtime, runtime->now);
801 		advance_moment(runtime);
802 		break;
803 	case repevt_traffic:
804 		advance_moment(runtime);
805 		break;
806 	case repevt_infra_rtt:
807 		do_infra_rtt(runtime);
808 		advance_moment(runtime);
809 		break;
810 	default:
811 		fatal_exit("testbound: unknown event type %d",
812 			runtime->now->evt_type);
813 	}
814 }
815 
816 /** run the scenario in event callbacks */
817 static void
run_scenario(struct replay_runtime * runtime)818 run_scenario(struct replay_runtime* runtime)
819 {
820 	struct entry* entry = NULL;
821 	struct fake_pending* pending = NULL;
822 	int max_rounds = 5000;
823 	int rounds = 0;
824 	runtime->now = runtime->scenario->mom_first;
825 	log_info("testbound: entering fake runloop");
826 	do {
827 		/* if moment matches pending query do it. */
828 		/* else if moment matches given answer, do it */
829 		/* else if precoded_range matches pending, do it */
830 		/* else do the current moment */
831 		if(pending_matches_current(runtime, &entry, &pending)) {
832 			log_info("testbound: do STEP %d CHECK_OUT_QUERY",
833 				runtime->now->time_step);
834 			advance_moment(runtime);
835 			if(entry->copy_id)
836 				answer_callback_from_entry(runtime, entry,
837 				pending);
838 		} else if(runtime->answer_list && runtime->now &&
839 			runtime->now->evt_type == repevt_front_reply) {
840 			answer_check_it(runtime);
841 			advance_moment(runtime);
842 		} else if(pending_matches_range(runtime, &entry, &pending)) {
843 			answer_callback_from_entry(runtime, entry, pending);
844 		} else {
845 			do_moment_and_advance(runtime);
846 		}
847 		log_info("testbound: end of event stage");
848 		rounds++;
849 		if(rounds > max_rounds)
850 			fatal_exit("testbound: too many rounds, it loops.");
851 	} while(runtime->now);
852 
853 	if(runtime->pending_list) {
854 		struct fake_pending* p;
855 		log_err("testbound: there are still messages pending.");
856 		for(p = runtime->pending_list; p; p=p->next) {
857 			log_pkt("pending msg", p->pkt, p->pkt_len);
858 			log_addr(0, "pending to", &p->addr, p->addrlen);
859 		}
860 		fatal_exit("testbound: there are still messages pending.");
861 	}
862 	if(runtime->answer_list) {
863 		fatal_exit("testbound: there are unmatched answers.");
864 	}
865 	log_info("testbound: exiting fake runloop.");
866 	runtime->exit_cleanly = 1;
867 }
868 
869 /*********** Dummy routines ***********/
870 
871 struct listen_dnsport*
listen_create(struct comm_base * base,struct listen_port * ATTR_UNUSED (ports),size_t bufsize,int ATTR_UNUSED (tcp_accept_count),int ATTR_UNUSED (tcp_idle_timeout),int ATTR_UNUSED (harden_large_queries),uint32_t ATTR_UNUSED (http_max_streams),char * ATTR_UNUSED (http_endpoint),int ATTR_UNUSED (http_notls),struct tcl_list * ATTR_UNUSED (tcp_conn_limit),void * ATTR_UNUSED (sslctx),struct dt_env * ATTR_UNUSED (dtenv),comm_point_callback_type * cb,void * cb_arg)872 listen_create(struct comm_base* base, struct listen_port* ATTR_UNUSED(ports),
873 	size_t bufsize, int ATTR_UNUSED(tcp_accept_count),
874 	int ATTR_UNUSED(tcp_idle_timeout),
875 	int ATTR_UNUSED(harden_large_queries),
876 	uint32_t ATTR_UNUSED(http_max_streams),
877 	char* ATTR_UNUSED(http_endpoint),
878 	int ATTR_UNUSED(http_notls),
879 	struct tcl_list* ATTR_UNUSED(tcp_conn_limit),
880 	void* ATTR_UNUSED(sslctx), struct dt_env* ATTR_UNUSED(dtenv),
881 	comm_point_callback_type* cb, void *cb_arg)
882 {
883 	struct replay_runtime* runtime = (struct replay_runtime*)base;
884 	struct listen_dnsport* l= calloc(1, sizeof(struct listen_dnsport));
885 	if(!l)
886 		return NULL;
887 	l->base = base;
888 	l->udp_buff = sldns_buffer_new(bufsize);
889 	if(!l->udp_buff) {
890 		free(l);
891 		return NULL;
892 	}
893 	runtime->callback_query = cb;
894 	runtime->cb_arg = cb_arg;
895 	runtime->bufsize = bufsize;
896 	return l;
897 }
898 
899 void
listen_delete(struct listen_dnsport * listen)900 listen_delete(struct listen_dnsport* listen)
901 {
902 	if(!listen)
903 		return;
904 	sldns_buffer_free(listen->udp_buff);
905 	free(listen);
906 }
907 
908 struct comm_base*
comm_base_create(int ATTR_UNUSED (sigs))909 comm_base_create(int ATTR_UNUSED(sigs))
910 {
911 	/* we return the runtime structure instead. */
912 	struct replay_runtime* runtime = (struct replay_runtime*)
913 		calloc(1, sizeof(struct replay_runtime));
914 	if(!runtime)
915 		fatal_exit("out of memory in fake_event.c:comm_base_create");
916 	runtime->scenario = saved_scenario;
917 	runtime->vars = macro_store_create();
918 	if(!runtime->vars) fatal_exit("out of memory");
919 	return (struct comm_base*)runtime;
920 }
921 
922 void
comm_base_delete(struct comm_base * b)923 comm_base_delete(struct comm_base* b)
924 {
925 	struct replay_runtime* runtime = (struct replay_runtime*)b;
926 	struct fake_pending* p, *np;
927 	struct replay_answer* a, *na;
928 	struct fake_timer* t, *nt;
929 	if(!runtime)
930 		return;
931 	runtime->scenario= NULL;
932 	p = runtime->pending_list;
933 	while(p) {
934 		np = p->next;
935 		delete_fake_pending(p);
936 		p = np;
937 	}
938 	a = runtime->answer_list;
939 	while(a) {
940 		na = a->next;
941 		delete_replay_answer(a);
942 		a = na;
943 	}
944 	t = runtime->timer_list;
945 	while(t) {
946 		nt = t->next;
947 		free(t);
948 		t = nt;
949 	}
950 	macro_store_delete(runtime->vars);
951 	free(runtime);
952 }
953 
954 void
comm_base_timept(struct comm_base * b,time_t ** tt,struct timeval ** tv)955 comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv)
956 {
957 	struct replay_runtime* runtime = (struct replay_runtime*)b;
958 	*tt = &runtime->now_secs;
959 	*tv = &runtime->now_tv;
960 }
961 
962 void
comm_base_dispatch(struct comm_base * b)963 comm_base_dispatch(struct comm_base* b)
964 {
965 	struct replay_runtime* runtime = (struct replay_runtime*)b;
966 	run_scenario(runtime);
967 	if(runtime->sig_cb)
968 		(*runtime->sig_cb)(SIGTERM, runtime->sig_cb_arg);
969 	else	exit(0); /* OK exit when LIBEVENT_SIGNAL_PROBLEM exists */
970 }
971 
972 void
comm_base_exit(struct comm_base * b)973 comm_base_exit(struct comm_base* b)
974 {
975 	struct replay_runtime* runtime = (struct replay_runtime*)b;
976 	if(!runtime->exit_cleanly) {
977 		/* some sort of failure */
978 		fatal_exit("testbound: comm_base_exit was called.");
979 	}
980 }
981 
982 struct comm_signal*
comm_signal_create(struct comm_base * base,void (* callback)(int,void *),void * cb_arg)983 comm_signal_create(struct comm_base* base,
984         void (*callback)(int, void*), void* cb_arg)
985 {
986 	struct replay_runtime* runtime = (struct replay_runtime*)base;
987 	runtime->sig_cb = callback;
988 	runtime->sig_cb_arg = cb_arg;
989 	return calloc(1, sizeof(struct comm_signal));
990 }
991 
992 int
comm_signal_bind(struct comm_signal * ATTR_UNUSED (comsig),int ATTR_UNUSED (sig))993 comm_signal_bind(struct comm_signal* ATTR_UNUSED(comsig), int
994 	ATTR_UNUSED(sig))
995 {
996 	return 1;
997 }
998 
999 void
comm_signal_delete(struct comm_signal * comsig)1000 comm_signal_delete(struct comm_signal* comsig)
1001 {
1002 	free(comsig);
1003 }
1004 
1005 void
comm_point_send_reply(struct comm_reply * repinfo)1006 comm_point_send_reply(struct comm_reply* repinfo)
1007 {
1008 	struct replay_answer* ans = (struct replay_answer*)calloc(1,
1009 		sizeof(struct replay_answer));
1010 	struct replay_runtime* runtime = (struct replay_runtime*)repinfo->c->ev;
1011 	log_info("testbound: comm_point_send_reply fake");
1012 	/* dump it into the todo list */
1013 	log_assert(ans);
1014 	memcpy(&ans->repinfo, repinfo, sizeof(struct comm_reply));
1015 	ans->next = NULL;
1016 	if(runtime->answer_last)
1017 		runtime->answer_last->next = ans;
1018 	else 	runtime->answer_list = ans;
1019 	runtime->answer_last = ans;
1020 
1021 	/* try to parse packet */
1022 	ans->pkt = memdup(sldns_buffer_begin(ans->repinfo.c->buffer),
1023 		sldns_buffer_limit(ans->repinfo.c->buffer));
1024 	ans->pkt_len = sldns_buffer_limit(ans->repinfo.c->buffer);
1025 	if(!ans->pkt) fatal_exit("out of memory");
1026 	log_pkt("reply pkt: ", ans->pkt, ans->pkt_len);
1027 }
1028 
1029 void
comm_point_drop_reply(struct comm_reply * repinfo)1030 comm_point_drop_reply(struct comm_reply* repinfo)
1031 {
1032 	log_info("comm_point_drop_reply fake");
1033 	if(repinfo->c) {
1034 		sldns_buffer_free(repinfo->c->buffer);
1035 		free(repinfo->c);
1036 	}
1037 }
1038 
1039 struct outside_network*
outside_network_create(struct comm_base * base,size_t bufsize,size_t ATTR_UNUSED (num_ports),char ** ATTR_UNUSED (ifs),int ATTR_UNUSED (num_ifs),int ATTR_UNUSED (do_ip4),int ATTR_UNUSED (do_ip6),size_t ATTR_UNUSED (num_tcp),int ATTR_UNUSED (dscp),struct infra_cache * infra,struct ub_randstate * ATTR_UNUSED (rnd),int ATTR_UNUSED (use_caps_for_id),int * ATTR_UNUSED (availports),int ATTR_UNUSED (numavailports),size_t ATTR_UNUSED (unwanted_threshold),int ATTR_UNUSED (outgoing_tcp_mss),void (* unwanted_action)(void *),void * ATTR_UNUSED (unwanted_param),int ATTR_UNUSED (do_udp),void * ATTR_UNUSED (sslctx),int ATTR_UNUSED (delayclose),int ATTR_UNUSED (tls_use_sni),struct dt_env * ATTR_UNUSED (dtenv),int ATTR_UNUSED (udp_connect),int ATTR_UNUSED (max_reuse_tcp_queries),int ATTR_UNUSED (tcp_reuse_timeout),int ATTR_UNUSED (tcp_auth_query_timeout))1040 outside_network_create(struct comm_base* base, size_t bufsize,
1041 	size_t ATTR_UNUSED(num_ports), char** ATTR_UNUSED(ifs),
1042 	int ATTR_UNUSED(num_ifs), int ATTR_UNUSED(do_ip4),
1043 	int ATTR_UNUSED(do_ip6), size_t ATTR_UNUSED(num_tcp),
1044 	int ATTR_UNUSED(dscp),
1045 	struct infra_cache* infra,
1046 	struct ub_randstate* ATTR_UNUSED(rnd),
1047 	int ATTR_UNUSED(use_caps_for_id), int* ATTR_UNUSED(availports),
1048 	int ATTR_UNUSED(numavailports), size_t ATTR_UNUSED(unwanted_threshold),
1049 	int ATTR_UNUSED(outgoing_tcp_mss),
1050 	void (*unwanted_action)(void*), void* ATTR_UNUSED(unwanted_param),
1051 	int ATTR_UNUSED(do_udp), void* ATTR_UNUSED(sslctx),
1052 	int ATTR_UNUSED(delayclose), int ATTR_UNUSED(tls_use_sni),
1053 	struct dt_env* ATTR_UNUSED(dtenv), int ATTR_UNUSED(udp_connect),
1054 	int ATTR_UNUSED(max_reuse_tcp_queries), int ATTR_UNUSED(tcp_reuse_timeout),
1055 	int ATTR_UNUSED(tcp_auth_query_timeout))
1056 {
1057 	struct replay_runtime* runtime = (struct replay_runtime*)base;
1058 	struct outside_network* outnet =  calloc(1,
1059 		sizeof(struct outside_network));
1060 	(void)unwanted_action;
1061 	if(!outnet)
1062 		return NULL;
1063 	runtime->infra = infra;
1064 	outnet->base = base;
1065 	outnet->udp_buff = sldns_buffer_new(bufsize);
1066 	if(!outnet->udp_buff) {
1067 		free(outnet);
1068 		return NULL;
1069 	}
1070 	return outnet;
1071 }
1072 
1073 void
outside_network_delete(struct outside_network * outnet)1074 outside_network_delete(struct outside_network* outnet)
1075 {
1076 	if(!outnet)
1077 		return;
1078 	sldns_buffer_free(outnet->udp_buff);
1079 	free(outnet);
1080 }
1081 
1082 void
outside_network_quit_prepare(struct outside_network * ATTR_UNUSED (outnet))1083 outside_network_quit_prepare(struct outside_network* ATTR_UNUSED(outnet))
1084 {
1085 }
1086 
1087 struct pending*
pending_udp_query(struct serviced_query * sq,sldns_buffer * packet,int timeout,comm_point_callback_type * callback,void * callback_arg)1088 pending_udp_query(struct serviced_query* sq, sldns_buffer* packet,
1089 	int timeout, comm_point_callback_type* callback, void* callback_arg)
1090 {
1091 	struct replay_runtime* runtime = (struct replay_runtime*)
1092 		sq->outnet->base;
1093 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1094 		sizeof(struct fake_pending));
1095 	log_assert(pend);
1096 	pend->buffer = sldns_buffer_new(sldns_buffer_capacity(packet));
1097 	log_assert(pend->buffer);
1098 	sldns_buffer_write(pend->buffer, sldns_buffer_begin(packet),
1099 		sldns_buffer_limit(packet));
1100 	sldns_buffer_flip(pend->buffer);
1101 	memcpy(&pend->addr, &sq->addr, sq->addrlen);
1102 	pend->addrlen = sq->addrlen;
1103 	pend->callback = callback;
1104 	pend->cb_arg = callback_arg;
1105 	pend->timeout = timeout/1000;
1106 	pend->transport = transport_udp;
1107 	pend->pkt = NULL;
1108 	pend->zone = NULL;
1109 	pend->serviced = 0;
1110 	pend->runtime = runtime;
1111 	pend->pkt_len = sldns_buffer_limit(packet);
1112 	pend->pkt = memdup(sldns_buffer_begin(packet), pend->pkt_len);
1113 	if(!pend->pkt) fatal_exit("out of memory");
1114 	log_pkt("pending udp pkt: ", pend->pkt, pend->pkt_len);
1115 
1116 	/* see if it matches the current moment */
1117 	if(runtime->now && runtime->now->evt_type == repevt_back_query &&
1118 		(runtime->now->addrlen == 0 || sockaddr_cmp(
1119 			&runtime->now->addr, runtime->now->addrlen,
1120 			&pend->addr, pend->addrlen) == 0) &&
1121 		find_match(runtime->now->match, pend->pkt, pend->pkt_len,
1122 			pend->transport)) {
1123 		log_info("testbound: matched pending to event. "
1124 			"advance time between events.");
1125 		log_info("testbound: do STEP %d %s", runtime->now->time_step,
1126 			repevt_string(runtime->now->evt_type));
1127 		advance_moment(runtime);
1128 		/* still create the pending, because we need it to callback */
1129 	}
1130 	log_info("testbound: created fake pending");
1131 	/* add to list */
1132 	pend->next = runtime->pending_list;
1133 	runtime->pending_list = pend;
1134 	return (struct pending*)pend;
1135 }
1136 
1137 struct waiting_tcp*
pending_tcp_query(struct serviced_query * sq,sldns_buffer * packet,int timeout,comm_point_callback_type * callback,void * callback_arg)1138 pending_tcp_query(struct serviced_query* sq, sldns_buffer* packet,
1139 	int timeout, comm_point_callback_type* callback, void* callback_arg)
1140 {
1141 	struct replay_runtime* runtime = (struct replay_runtime*)
1142 		sq->outnet->base;
1143 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1144 		sizeof(struct fake_pending));
1145 	log_assert(pend);
1146 	pend->buffer = sldns_buffer_new(sldns_buffer_capacity(packet));
1147 	log_assert(pend->buffer);
1148 	sldns_buffer_write(pend->buffer, sldns_buffer_begin(packet),
1149 		sldns_buffer_limit(packet));
1150 	sldns_buffer_flip(pend->buffer);
1151 	memcpy(&pend->addr, &sq->addr, sq->addrlen);
1152 	pend->addrlen = sq->addrlen;
1153 	pend->callback = callback;
1154 	pend->cb_arg = callback_arg;
1155 	pend->timeout = timeout/1000;
1156 	pend->transport = transport_tcp;
1157 	pend->pkt = NULL;
1158 	pend->zone = NULL;
1159 	pend->runtime = runtime;
1160 	pend->serviced = 0;
1161 	pend->pkt_len = sldns_buffer_limit(packet);
1162 	pend->pkt = memdup(sldns_buffer_begin(packet), pend->pkt_len);
1163 	if(!pend->pkt) fatal_exit("out of memory");
1164 	log_pkt("pending tcp pkt: ", pend->pkt, pend->pkt_len);
1165 
1166 	/* see if it matches the current moment */
1167 	if(runtime->now && runtime->now->evt_type == repevt_back_query &&
1168 		(runtime->now->addrlen == 0 || sockaddr_cmp(
1169 			&runtime->now->addr, runtime->now->addrlen,
1170 			&pend->addr, pend->addrlen) == 0) &&
1171 		find_match(runtime->now->match, pend->pkt, pend->pkt_len,
1172 			pend->transport)) {
1173 		log_info("testbound: matched pending to event. "
1174 			"advance time between events.");
1175 		log_info("testbound: do STEP %d %s", runtime->now->time_step,
1176 			repevt_string(runtime->now->evt_type));
1177 		advance_moment(runtime);
1178 		/* still create the pending, because we need it to callback */
1179 	}
1180 	log_info("testbound: created fake pending");
1181 	/* add to list */
1182 	pend->next = runtime->pending_list;
1183 	runtime->pending_list = pend;
1184 	return (struct waiting_tcp*)pend;
1185 }
1186 
outnet_serviced_query(struct outside_network * outnet,struct query_info * qinfo,uint16_t flags,int dnssec,int ATTR_UNUSED (want_dnssec),int ATTR_UNUSED (nocaps),int ATTR_UNUSED (tcp_upstream),int ATTR_UNUSED (ssl_upstream),char * ATTR_UNUSED (tls_auth_name),struct sockaddr_storage * addr,socklen_t addrlen,uint8_t * zone,size_t zonelen,struct module_qstate * qstate,comm_point_callback_type * callback,void * callback_arg,sldns_buffer * ATTR_UNUSED (buff),struct module_env * env)1187 struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
1188 	struct query_info* qinfo, uint16_t flags, int dnssec,
1189 	int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
1190 	int ATTR_UNUSED(tcp_upstream), int ATTR_UNUSED(ssl_upstream),
1191 	char* ATTR_UNUSED(tls_auth_name), struct sockaddr_storage* addr,
1192 	socklen_t addrlen, uint8_t* zone, size_t zonelen,
1193 	struct module_qstate* qstate, comm_point_callback_type* callback,
1194 	void* callback_arg, sldns_buffer* ATTR_UNUSED(buff),
1195 	struct module_env* env)
1196 {
1197 	struct replay_runtime* runtime = (struct replay_runtime*)outnet->base;
1198 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1199 		sizeof(struct fake_pending));
1200 	char z[256];
1201 	log_assert(pend);
1202 	log_nametypeclass(VERB_OPS, "pending serviced query",
1203 		qinfo->qname, qinfo->qtype, qinfo->qclass);
1204 	dname_str(zone, z);
1205 	verbose(VERB_OPS, "pending serviced query zone %s flags%s%s%s%s",
1206 		z, (flags&BIT_RD)?" RD":"", (flags&BIT_CD)?" CD":"",
1207 		(flags&~(BIT_RD|BIT_CD))?" MORE":"", (dnssec)?" DO":"");
1208 
1209 	/* create packet with EDNS */
1210 	pend->buffer = sldns_buffer_new(512);
1211 	log_assert(pend->buffer);
1212 	sldns_buffer_write_u16(pend->buffer, 0); /* id */
1213 	sldns_buffer_write_u16(pend->buffer, flags);
1214 	sldns_buffer_write_u16(pend->buffer, 1); /* qdcount */
1215 	sldns_buffer_write_u16(pend->buffer, 0); /* ancount */
1216 	sldns_buffer_write_u16(pend->buffer, 0); /* nscount */
1217 	sldns_buffer_write_u16(pend->buffer, 0); /* arcount */
1218 	sldns_buffer_write(pend->buffer, qinfo->qname, qinfo->qname_len);
1219 	sldns_buffer_write_u16(pend->buffer, qinfo->qtype);
1220 	sldns_buffer_write_u16(pend->buffer, qinfo->qclass);
1221 	sldns_buffer_flip(pend->buffer);
1222 	if(1) {
1223 		struct edns_data edns;
1224 		struct edns_string_addr* client_string_addr;
1225 		if(!inplace_cb_query_call(env, qinfo, flags, addr, addrlen,
1226 			zone, zonelen, qstate, qstate->region)) {
1227 			free(pend);
1228 			return NULL;
1229 		}
1230 		/* add edns */
1231 		edns.edns_present = 1;
1232 		edns.ext_rcode = 0;
1233 		edns.edns_version = EDNS_ADVERTISED_VERSION;
1234 		edns.udp_size = EDNS_ADVERTISED_SIZE;
1235 		edns.bits = 0;
1236 		if(dnssec)
1237 			edns.bits = EDNS_DO;
1238 		edns.padding_block_size = 0;
1239 		if((client_string_addr = edns_string_addr_lookup(
1240 			&env->edns_strings->client_strings,
1241 			addr, addrlen))) {
1242 			edns_opt_list_append(&qstate->edns_opts_back_out,
1243 				env->edns_strings->client_string_opcode,
1244 				client_string_addr->string_len,
1245 				client_string_addr->string, qstate->region);
1246 		}
1247 		edns.opt_list_in = NULL;
1248 		edns.opt_list_out = qstate->edns_opts_back_out;
1249 		edns.opt_list_inplace_cb_out = NULL;
1250 		attach_edns_record(pend->buffer, &edns);
1251 	}
1252 	memcpy(&pend->addr, addr, addrlen);
1253 	pend->addrlen = addrlen;
1254 	pend->zone = memdup(zone, zonelen);
1255 	pend->zonelen = zonelen;
1256 	pend->qtype = (int)qinfo->qtype;
1257 	log_assert(pend->zone);
1258 	pend->callback = callback;
1259 	pend->cb_arg = callback_arg;
1260 	pend->timeout = UDP_AUTH_QUERY_TIMEOUT/1000;
1261 	pend->transport = transport_udp; /* pretend UDP */
1262 	pend->pkt = NULL;
1263 	pend->runtime = runtime;
1264 	pend->serviced = 1;
1265 	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1266 	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1267 	if(!pend->pkt) fatal_exit("out of memory");
1268 	/*log_pkt("pending serviced query: ", pend->pkt, pend->pkt_len);*/
1269 
1270 	/* see if it matches the current moment */
1271 	if(runtime->now && runtime->now->evt_type == repevt_back_query &&
1272 		(runtime->now->addrlen == 0 || sockaddr_cmp(
1273 			&runtime->now->addr, runtime->now->addrlen,
1274 			&pend->addr, pend->addrlen) == 0) &&
1275 		find_match(runtime->now->match, pend->pkt, pend->pkt_len,
1276 			pend->transport)) {
1277 		log_info("testbound: matched pending to event. "
1278 			"advance time between events.");
1279 		log_info("testbound: do STEP %d %s", runtime->now->time_step,
1280 			repevt_string(runtime->now->evt_type));
1281 		advance_moment(runtime);
1282 		/* still create the pending, because we need it to callback */
1283 	}
1284 	log_info("testbound: created fake pending");
1285 	/* add to list */
1286 	pend->next = runtime->pending_list;
1287 	runtime->pending_list = pend;
1288 	return (struct serviced_query*)pend;
1289 }
1290 
outnet_serviced_query_stop(struct serviced_query * sq,void * cb_arg)1291 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg)
1292 {
1293 	struct fake_pending* pend = (struct fake_pending*)sq;
1294 	struct replay_runtime* runtime = pend->runtime;
1295 	/* delete from the list */
1296 	struct fake_pending* p = runtime->pending_list, *prev=NULL;
1297 	while(p) {
1298 		if(p == pend) {
1299 			log_assert(p->cb_arg == cb_arg);
1300 			(void)cb_arg;
1301 			log_info("serviced pending delete");
1302 			if(prev)
1303 				prev->next = p->next;
1304 			else 	runtime->pending_list = p->next;
1305 			sldns_buffer_free(p->buffer);
1306 			free(p->pkt);
1307 			free(p->zone);
1308 			free(p);
1309 			return;
1310 		}
1311 		prev = p;
1312 		p = p->next;
1313 	}
1314 	log_info("double delete of pending serviced query");
1315 }
1316 
resolve_interface_names(char ** ATTR_UNUSED (ifs),int ATTR_UNUSED (num_ifs),struct config_strlist * ATTR_UNUSED (list),char *** ATTR_UNUSED (resif),int * ATTR_UNUSED (num_resif))1317 int resolve_interface_names(char** ATTR_UNUSED(ifs), int ATTR_UNUSED(num_ifs),
1318 	struct config_strlist* ATTR_UNUSED(list), char*** ATTR_UNUSED(resif),
1319 	int* ATTR_UNUSED(num_resif))
1320 {
1321 	return 1;
1322 }
1323 
listening_ports_open(struct config_file * ATTR_UNUSED (cfg),char ** ATTR_UNUSED (ifs),int ATTR_UNUSED (num_ifs),int * ATTR_UNUSED (reuseport))1324 struct listen_port* listening_ports_open(struct config_file* ATTR_UNUSED(cfg),
1325 	char** ATTR_UNUSED(ifs), int ATTR_UNUSED(num_ifs),
1326 	int* ATTR_UNUSED(reuseport))
1327 {
1328 	return calloc(1, 1);
1329 }
1330 
listening_ports_free(struct listen_port * list)1331 void listening_ports_free(struct listen_port* list)
1332 {
1333 	free(list);
1334 }
1335 
comm_point_create_local(struct comm_base * ATTR_UNUSED (base),int ATTR_UNUSED (fd),size_t ATTR_UNUSED (bufsize),comm_point_callback_type * ATTR_UNUSED (callback),void * ATTR_UNUSED (callback_arg))1336 struct comm_point* comm_point_create_local(struct comm_base* ATTR_UNUSED(base),
1337         int ATTR_UNUSED(fd), size_t ATTR_UNUSED(bufsize),
1338         comm_point_callback_type* ATTR_UNUSED(callback),
1339 	void* ATTR_UNUSED(callback_arg))
1340 {
1341 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1342 		sizeof(*fc));
1343 	if(!fc) return NULL;
1344 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1345 	return (struct comm_point*)fc;
1346 }
1347 
comm_point_create_raw(struct comm_base * ATTR_UNUSED (base),int ATTR_UNUSED (fd),int ATTR_UNUSED (writing),comm_point_callback_type * ATTR_UNUSED (callback),void * ATTR_UNUSED (callback_arg))1348 struct comm_point* comm_point_create_raw(struct comm_base* ATTR_UNUSED(base),
1349         int ATTR_UNUSED(fd), int ATTR_UNUSED(writing),
1350         comm_point_callback_type* ATTR_UNUSED(callback),
1351 	void* ATTR_UNUSED(callback_arg))
1352 {
1353 	/* no pipe comm possible */
1354 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1355 		sizeof(*fc));
1356 	if(!fc) return NULL;
1357 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1358 	return (struct comm_point*)fc;
1359 }
1360 
comm_point_start_listening(struct comm_point * ATTR_UNUSED (c),int ATTR_UNUSED (newfd),int ATTR_UNUSED (sec))1361 void comm_point_start_listening(struct comm_point* ATTR_UNUSED(c),
1362 	int ATTR_UNUSED(newfd), int ATTR_UNUSED(sec))
1363 {
1364 	/* no bg write pipe comm possible */
1365 }
1366 
comm_point_stop_listening(struct comm_point * ATTR_UNUSED (c))1367 void comm_point_stop_listening(struct comm_point* ATTR_UNUSED(c))
1368 {
1369 	/* no bg write pipe comm possible */
1370 }
1371 
1372 /* only cmd com _local gets deleted */
comm_point_delete(struct comm_point * c)1373 void comm_point_delete(struct comm_point* c)
1374 {
1375 	struct fake_commpoint* fc = (struct fake_commpoint*)c;
1376 	if(c == NULL) return;
1377 	log_assert(fc->typecode == FAKE_COMMPOINT_TYPECODE);
1378 	if(fc->type_tcp_out) {
1379 		/* remove tcp pending, so no more callbacks to it */
1380 		pending_list_delete(fc->runtime, fc->pending);
1381 	}
1382 	free(c);
1383 }
1384 
listen_get_mem(struct listen_dnsport * ATTR_UNUSED (listen))1385 size_t listen_get_mem(struct listen_dnsport* ATTR_UNUSED(listen))
1386 {
1387 	return 0;
1388 }
1389 
outnet_get_mem(struct outside_network * ATTR_UNUSED (outnet))1390 size_t outnet_get_mem(struct outside_network* ATTR_UNUSED(outnet))
1391 {
1392 	return 0;
1393 }
1394 
comm_point_get_mem(struct comm_point * ATTR_UNUSED (c))1395 size_t comm_point_get_mem(struct comm_point* ATTR_UNUSED(c))
1396 {
1397 	return 0;
1398 }
1399 
serviced_get_mem(struct serviced_query * ATTR_UNUSED (c))1400 size_t serviced_get_mem(struct serviced_query* ATTR_UNUSED(c))
1401 {
1402 	return 0;
1403 }
1404 
1405 /* fake for fptr wlist */
outnet_udp_cb(struct comm_point * ATTR_UNUSED (c),void * ATTR_UNUSED (arg),int ATTR_UNUSED (error),struct comm_reply * ATTR_UNUSED (reply_info))1406 int outnet_udp_cb(struct comm_point* ATTR_UNUSED(c),
1407 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1408         struct comm_reply *ATTR_UNUSED(reply_info))
1409 {
1410 	log_assert(0);
1411 	return 0;
1412 }
1413 
outnet_tcp_cb(struct comm_point * ATTR_UNUSED (c),void * ATTR_UNUSED (arg),int ATTR_UNUSED (error),struct comm_reply * ATTR_UNUSED (reply_info))1414 int outnet_tcp_cb(struct comm_point* ATTR_UNUSED(c),
1415 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1416         struct comm_reply *ATTR_UNUSED(reply_info))
1417 {
1418 	log_assert(0);
1419 	return 0;
1420 }
1421 
pending_udp_timer_cb(void * ATTR_UNUSED (arg))1422 void pending_udp_timer_cb(void *ATTR_UNUSED(arg))
1423 {
1424 	log_assert(0);
1425 }
1426 
pending_udp_timer_delay_cb(void * ATTR_UNUSED (arg))1427 void pending_udp_timer_delay_cb(void *ATTR_UNUSED(arg))
1428 {
1429 	log_assert(0);
1430 }
1431 
outnet_tcptimer(void * ATTR_UNUSED (arg))1432 void outnet_tcptimer(void* ATTR_UNUSED(arg))
1433 {
1434 	log_assert(0);
1435 }
1436 
comm_point_udp_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1437 void comm_point_udp_callback(int ATTR_UNUSED(fd), short ATTR_UNUSED(event),
1438 	void* ATTR_UNUSED(arg))
1439 {
1440 	log_assert(0);
1441 }
1442 
comm_point_udp_ancil_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1443 void comm_point_udp_ancil_callback(int ATTR_UNUSED(fd),
1444 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1445 {
1446 	log_assert(0);
1447 }
1448 
comm_point_tcp_accept_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1449 void comm_point_tcp_accept_callback(int ATTR_UNUSED(fd),
1450 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1451 {
1452 	log_assert(0);
1453 }
1454 
comm_point_tcp_handle_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1455 void comm_point_tcp_handle_callback(int ATTR_UNUSED(fd),
1456 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1457 {
1458 	log_assert(0);
1459 }
1460 
comm_timer_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1461 void comm_timer_callback(int ATTR_UNUSED(fd),
1462 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1463 {
1464 	log_assert(0);
1465 }
1466 
comm_signal_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1467 void comm_signal_callback(int ATTR_UNUSED(fd),
1468 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1469 {
1470 	log_assert(0);
1471 }
1472 
comm_point_http_handle_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1473 void comm_point_http_handle_callback(int ATTR_UNUSED(fd),
1474 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1475 {
1476 	log_assert(0);
1477 }
1478 
comm_point_local_handle_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1479 void comm_point_local_handle_callback(int ATTR_UNUSED(fd),
1480 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1481 {
1482 	log_assert(0);
1483 }
1484 
comm_point_raw_handle_callback(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1485 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
1486 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1487 {
1488 	log_assert(0);
1489 }
1490 
comm_base_handle_slow_accept(int ATTR_UNUSED (fd),short ATTR_UNUSED (event),void * ATTR_UNUSED (arg))1491 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
1492 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1493 {
1494 	log_assert(0);
1495 }
1496 
serviced_udp_callback(struct comm_point * ATTR_UNUSED (c),void * ATTR_UNUSED (arg),int ATTR_UNUSED (error),struct comm_reply * ATTR_UNUSED (reply_info))1497 int serviced_udp_callback(struct comm_point* ATTR_UNUSED(c),
1498 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1499         struct comm_reply* ATTR_UNUSED(reply_info))
1500 {
1501 	log_assert(0);
1502 	return 0;
1503 }
1504 
serviced_tcp_callback(struct comm_point * ATTR_UNUSED (c),void * ATTR_UNUSED (arg),int ATTR_UNUSED (error),struct comm_reply * ATTR_UNUSED (reply_info))1505 int serviced_tcp_callback(struct comm_point* ATTR_UNUSED(c),
1506 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1507         struct comm_reply* ATTR_UNUSED(reply_info))
1508 {
1509 	log_assert(0);
1510 	return 0;
1511 }
1512 
pending_cmp(const void * ATTR_UNUSED (a),const void * ATTR_UNUSED (b))1513 int pending_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1514 {
1515 	log_assert(0);
1516 	return 0;
1517 }
1518 
serviced_cmp(const void * ATTR_UNUSED (a),const void * ATTR_UNUSED (b))1519 int serviced_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1520 {
1521 	log_assert(0);
1522 	return 0;
1523 }
1524 
reuse_cmp(const void * ATTR_UNUSED (a),const void * ATTR_UNUSED (b))1525 int reuse_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1526 {
1527 	log_assert(0);
1528 	return 0;
1529 }
1530 
reuse_id_cmp(const void * ATTR_UNUSED (a),const void * ATTR_UNUSED (b))1531 int reuse_id_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1532 {
1533 	log_assert(0);
1534 	return 0;
1535 }
1536 
1537 /* timers in testbound for autotrust. statistics tested in tdir. */
comm_timer_create(struct comm_base * base,void (* cb)(void *),void * cb_arg)1538 struct comm_timer* comm_timer_create(struct comm_base* base,
1539 	void (*cb)(void*), void* cb_arg)
1540 {
1541 	struct replay_runtime* runtime = (struct replay_runtime*)base;
1542 	struct fake_timer* t = (struct fake_timer*)calloc(1, sizeof(*t));
1543 	if(!t)
1544 		fatal_exit("out of memory in fake_event.c:comm_timer_create");
1545 	t->cb = cb;
1546 	t->cb_arg = cb_arg;
1547 	fptr_ok(fptr_whitelist_comm_timer(t->cb)); /* check in advance */
1548 	t->runtime = runtime;
1549 	t->next = runtime->timer_list;
1550 	runtime->timer_list = t;
1551 	return (struct comm_timer*)t;
1552 }
1553 
comm_timer_disable(struct comm_timer * timer)1554 void comm_timer_disable(struct comm_timer* timer)
1555 {
1556 	struct fake_timer* t = (struct fake_timer*)timer;
1557 	log_info("fake timer disabled");
1558 	t->enabled = 0;
1559 }
1560 
comm_timer_set(struct comm_timer * timer,struct timeval * tv)1561 void comm_timer_set(struct comm_timer* timer, struct timeval* tv)
1562 {
1563 	struct fake_timer* t = (struct fake_timer*)timer;
1564 	t->enabled = 1;
1565 	t->tv = *tv;
1566 	log_info("fake timer set %d.%6.6d",
1567 		(int)t->tv.tv_sec, (int)t->tv.tv_usec);
1568 	timeval_add(&t->tv, &t->runtime->now_tv);
1569 }
1570 
comm_timer_delete(struct comm_timer * timer)1571 void comm_timer_delete(struct comm_timer* timer)
1572 {
1573 	struct fake_timer* t = (struct fake_timer*)timer;
1574 	struct fake_timer** pp, *p;
1575 	if(!t) return;
1576 
1577 	/* remove from linked list */
1578 	pp = &t->runtime->timer_list;
1579 	p = t->runtime->timer_list;
1580 	while(p) {
1581 		if(p == t) {
1582 			/* snip from list */
1583 			*pp = p->next;
1584 			break;
1585 		}
1586 		pp = &p->next;
1587 		p = p->next;
1588 	}
1589 
1590 	free(timer);
1591 }
1592 
comm_base_set_slow_accept_handlers(struct comm_base * ATTR_UNUSED (b),void (* stop_acc)(void *),void (* start_acc)(void *),void * ATTR_UNUSED (arg))1593 void comm_base_set_slow_accept_handlers(struct comm_base* ATTR_UNUSED(b),
1594 	void (*stop_acc)(void*), void (*start_acc)(void*),
1595 	void* ATTR_UNUSED(arg))
1596 {
1597 	/* ignore this */
1598 	(void)stop_acc;
1599 	(void)start_acc;
1600 }
1601 
comm_base_internal(struct comm_base * ATTR_UNUSED (b))1602 struct ub_event_base* comm_base_internal(struct comm_base* ATTR_UNUSED(b))
1603 {
1604 	/* no pipe comm possible in testbound */
1605 	return NULL;
1606 }
1607 
daemon_remote_exec(struct worker * ATTR_UNUSED (worker))1608 void daemon_remote_exec(struct worker* ATTR_UNUSED(worker))
1609 {
1610 }
1611 
listen_start_accept(struct listen_dnsport * ATTR_UNUSED (listen))1612 void listen_start_accept(struct listen_dnsport* ATTR_UNUSED(listen))
1613 {
1614 }
1615 
listen_stop_accept(struct listen_dnsport * ATTR_UNUSED (listen))1616 void listen_stop_accept(struct listen_dnsport* ATTR_UNUSED(listen))
1617 {
1618 }
1619 
daemon_remote_start_accept(struct daemon_remote * ATTR_UNUSED (rc))1620 void daemon_remote_start_accept(struct daemon_remote* ATTR_UNUSED(rc))
1621 {
1622 }
1623 
daemon_remote_stop_accept(struct daemon_remote * ATTR_UNUSED (rc))1624 void daemon_remote_stop_accept(struct daemon_remote* ATTR_UNUSED(rc))
1625 {
1626 }
1627 
create_udp_sock(int ATTR_UNUSED (family),int ATTR_UNUSED (socktype),struct sockaddr * ATTR_UNUSED (addr),socklen_t ATTR_UNUSED (addrlen),int ATTR_UNUSED (v6only),int * ATTR_UNUSED (inuse),int * ATTR_UNUSED (noproto),int ATTR_UNUSED (rcv),int ATTR_UNUSED (snd),int ATTR_UNUSED (listen),int * ATTR_UNUSED (reuseport),int ATTR_UNUSED (transparent),int ATTR_UNUSED (freebind),int ATTR_UNUSED (use_systemd),int ATTR_UNUSED (dscp))1628 int create_udp_sock(int ATTR_UNUSED(family), int ATTR_UNUSED(socktype),
1629 	struct sockaddr* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
1630 	int ATTR_UNUSED(v6only), int* ATTR_UNUSED(inuse),
1631 	int* ATTR_UNUSED(noproto), int ATTR_UNUSED(rcv), int ATTR_UNUSED(snd),
1632 	int ATTR_UNUSED(listen), int* ATTR_UNUSED(reuseport),
1633 	int ATTR_UNUSED(transparent), int ATTR_UNUSED(freebind),
1634 	int ATTR_UNUSED(use_systemd), int ATTR_UNUSED(dscp))
1635 {
1636 	/* if you actually print to this, it'll be stdout during test */
1637 	return 1;
1638 }
1639 
comm_point_create_udp(struct comm_base * ATTR_UNUSED (base),int ATTR_UNUSED (fd),sldns_buffer * ATTR_UNUSED (buffer),comm_point_callback_type * ATTR_UNUSED (callback),void * ATTR_UNUSED (callback_arg),struct unbound_socket * ATTR_UNUSED (socket))1640 struct comm_point* comm_point_create_udp(struct comm_base *ATTR_UNUSED(base),
1641 	int ATTR_UNUSED(fd), sldns_buffer* ATTR_UNUSED(buffer),
1642 	comm_point_callback_type* ATTR_UNUSED(callback),
1643 	void* ATTR_UNUSED(callback_arg),
1644 	struct unbound_socket* ATTR_UNUSED(socket))
1645 {
1646 	log_assert(0);
1647 	return NULL;
1648 }
1649 
comm_point_create_tcp_out(struct comm_base * ATTR_UNUSED (base),size_t ATTR_UNUSED (bufsize),comm_point_callback_type * ATTR_UNUSED (callback),void * ATTR_UNUSED (callback_arg))1650 struct comm_point* comm_point_create_tcp_out(struct comm_base*
1651 	ATTR_UNUSED(base), size_t ATTR_UNUSED(bufsize),
1652 	comm_point_callback_type* ATTR_UNUSED(callback),
1653 	void* ATTR_UNUSED(callback_arg))
1654 {
1655 	log_assert(0);
1656 	return NULL;
1657 }
1658 
outnet_comm_point_for_udp(struct outside_network * outnet,comm_point_callback_type * cb,void * cb_arg,struct sockaddr_storage * ATTR_UNUSED (to_addr),socklen_t ATTR_UNUSED (to_addrlen))1659 struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet,
1660 	comm_point_callback_type* cb, void* cb_arg,
1661 	struct sockaddr_storage* ATTR_UNUSED(to_addr),
1662 	socklen_t ATTR_UNUSED(to_addrlen))
1663 {
1664 	struct replay_runtime* runtime = (struct replay_runtime*)
1665 		outnet->base;
1666 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1667 		sizeof(*fc));
1668 	if(!fc) return NULL;
1669 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1670 	fc->type_udp_out = 1;
1671 	fc->cb = cb;
1672 	fc->cb_arg = cb_arg;
1673 	fc->runtime = runtime;
1674 	/* used by authzone transfers */
1675 	return (struct comm_point*)fc;
1676 }
1677 
outnet_comm_point_for_tcp(struct outside_network * outnet,comm_point_callback_type * cb,void * cb_arg,struct sockaddr_storage * to_addr,socklen_t to_addrlen,struct sldns_buffer * query,int timeout,int ATTR_UNUSED (ssl),char * ATTR_UNUSED (host))1678 struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet,
1679 	comm_point_callback_type* cb, void* cb_arg,
1680 	struct sockaddr_storage* to_addr, socklen_t to_addrlen,
1681 	struct sldns_buffer* query, int timeout, int ATTR_UNUSED(ssl),
1682 	char* ATTR_UNUSED(host))
1683 {
1684 	struct replay_runtime* runtime = (struct replay_runtime*)
1685 		outnet->base;
1686 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1687 		sizeof(*fc));
1688 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1689 		sizeof(struct fake_pending));
1690 	if(!fc || !pend) {
1691 		free(fc);
1692 		free(pend);
1693 		return NULL;
1694 	}
1695 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1696 	fc->type_tcp_out = 1;
1697 	fc->cb = cb;
1698 	fc->cb_arg = cb_arg;
1699 	fc->runtime = runtime;
1700 	fc->pending = pend;
1701 
1702 	/* used by authzone transfers */
1703 	/* create pending item */
1704 	pend->buffer = sldns_buffer_new(sldns_buffer_limit(query)+10);
1705 	if(!pend->buffer) {
1706 		free(fc);
1707 		free(pend);
1708 		return NULL;
1709 	}
1710 	sldns_buffer_copy(pend->buffer, query);
1711 	memcpy(&pend->addr, to_addr, to_addrlen);
1712 	pend->addrlen = to_addrlen;
1713 	pend->zone = NULL;
1714 	pend->zonelen = 0;
1715 	if(LDNS_QDCOUNT(sldns_buffer_begin(query)) > 0) {
1716 		char buf[512];
1717 		char addrbuf[128];
1718 		(void)sldns_wire2str_rrquestion_buf(sldns_buffer_at(query, LDNS_HEADER_SIZE), sldns_buffer_limit(query)-LDNS_HEADER_SIZE, buf, sizeof(buf));
1719 		addr_to_str((struct sockaddr_storage*)to_addr, to_addrlen,
1720 			addrbuf, sizeof(addrbuf));
1721 		if(verbosity >= VERB_ALGO) {
1722 			strip_end_white(buf);
1723 			log_info("tcp to %s: %s", addrbuf, buf);
1724 		}
1725 		log_assert(sldns_buffer_limit(query)-LDNS_HEADER_SIZE >= 2);
1726 		pend->qtype = (int)sldns_buffer_read_u16_at(query,
1727 			LDNS_HEADER_SIZE+
1728 			dname_valid(sldns_buffer_at(query, LDNS_HEADER_SIZE),
1729 				sldns_buffer_limit(query)-LDNS_HEADER_SIZE));
1730 	}
1731 	pend->callback = cb;
1732 	pend->cb_arg = cb_arg;
1733 	pend->timeout = timeout;
1734 	pend->transport = transport_tcp;
1735 	pend->pkt = NULL;
1736 	pend->runtime = runtime;
1737 	pend->serviced = 0;
1738 	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1739 	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1740 	if(!pend->pkt) fatal_exit("out of memory");
1741 
1742 	log_info("testbound: created fake pending for tcp_out");
1743 
1744 	/* add to list */
1745 	pend->next = runtime->pending_list;
1746 	runtime->pending_list = pend;
1747 
1748 	return (struct comm_point*)fc;
1749 }
1750 
outnet_comm_point_for_http(struct outside_network * outnet,comm_point_callback_type * cb,void * cb_arg,struct sockaddr_storage * to_addr,socklen_t to_addrlen,int timeout,int ssl,char * host,char * path,struct config_file * cfg)1751 struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet,
1752 	comm_point_callback_type* cb, void* cb_arg,
1753 	struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
1754 	int ssl, char* host, char* path, struct config_file* cfg)
1755 {
1756 	struct replay_runtime* runtime = (struct replay_runtime*)
1757 		outnet->base;
1758 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1759 		sizeof(*fc));
1760 	if(!fc) {
1761 		return NULL;
1762 	}
1763 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1764 	fc->type_http_out = 1;
1765 	fc->cb = cb;
1766 	fc->cb_arg = cb_arg;
1767 	fc->runtime = runtime;
1768 
1769 	(void)to_addr;
1770 	(void)to_addrlen;
1771 	(void)timeout;
1772 
1773 	(void)ssl;
1774 	(void)host;
1775 	(void)path;
1776 	(void)cfg;
1777 
1778 	/* handle http comm point and return contents from test script */
1779 	return (struct comm_point*)fc;
1780 }
1781 
comm_point_send_udp_msg(struct comm_point * c,sldns_buffer * packet,struct sockaddr * addr,socklen_t addrlen,int ATTR_UNUSED (is_connected))1782 int comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet,
1783 	struct sockaddr* addr, socklen_t addrlen, int ATTR_UNUSED(is_connected))
1784 {
1785 	struct fake_commpoint* fc = (struct fake_commpoint*)c;
1786 	struct replay_runtime* runtime = fc->runtime;
1787 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1788 		sizeof(struct fake_pending));
1789 	if(!pend) {
1790 		log_err("malloc failure");
1791 		return 0;
1792 	}
1793 	fc->pending = pend;
1794 	/* used by authzone transfers */
1795 	/* create pending item */
1796 	pend->buffer = sldns_buffer_new(sldns_buffer_limit(packet) + 10);
1797 	if(!pend->buffer) {
1798 		free(pend);
1799 		return 0;
1800 	}
1801 	sldns_buffer_copy(pend->buffer, packet);
1802 	memcpy(&pend->addr, addr, addrlen);
1803 	pend->addrlen = addrlen;
1804 	pend->zone = NULL;
1805 	pend->zonelen = 0;
1806 	if(LDNS_QDCOUNT(sldns_buffer_begin(packet)) > 0) {
1807 		char buf[512];
1808 		char addrbuf[128];
1809 		(void)sldns_wire2str_rrquestion_buf(sldns_buffer_at(packet, LDNS_HEADER_SIZE), sldns_buffer_limit(packet)-LDNS_HEADER_SIZE, buf, sizeof(buf));
1810 		addr_to_str((struct sockaddr_storage*)addr, addrlen,
1811 			addrbuf, sizeof(addrbuf));
1812 		if(verbosity >= VERB_ALGO) {
1813 			strip_end_white(buf);
1814 			log_info("udp to %s: %s", addrbuf, buf);
1815 		}
1816 		log_assert(sldns_buffer_limit(packet)-LDNS_HEADER_SIZE >= 2);
1817 		pend->qtype = (int)sldns_buffer_read_u16_at(packet,
1818 			LDNS_HEADER_SIZE+
1819 			dname_valid(sldns_buffer_at(packet, LDNS_HEADER_SIZE),
1820 				sldns_buffer_limit(packet)-LDNS_HEADER_SIZE));
1821 	}
1822 	pend->callback = fc->cb;
1823 	pend->cb_arg = fc->cb_arg;
1824 	pend->timeout = UDP_AUTH_QUERY_TIMEOUT/1000;
1825 	pend->transport = transport_udp;
1826 	pend->pkt = NULL;
1827 	pend->runtime = runtime;
1828 	pend->serviced = 0;
1829 	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1830 	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1831 	if(!pend->pkt) fatal_exit("out of memory");
1832 
1833 	log_info("testbound: created fake pending for send_udp_msg");
1834 
1835 	/* add to list */
1836 	pend->next = runtime->pending_list;
1837 	runtime->pending_list = pend;
1838 
1839 	return 1;
1840 }
1841 
outnet_get_tcp_fd(struct sockaddr_storage * ATTR_UNUSED (addr),socklen_t ATTR_UNUSED (addrlen),int ATTR_UNUSED (tcp_mss),int ATTR_UNUSED (dscp))1842 int outnet_get_tcp_fd(struct sockaddr_storage* ATTR_UNUSED(addr),
1843 	socklen_t ATTR_UNUSED(addrlen), int ATTR_UNUSED(tcp_mss), int ATTR_UNUSED(dscp))
1844 {
1845 	log_assert(0);
1846 	return -1;
1847 }
1848 
outnet_tcp_connect(int ATTR_UNUSED (s),struct sockaddr_storage * ATTR_UNUSED (addr),socklen_t ATTR_UNUSED (addrlen))1849 int outnet_tcp_connect(int ATTR_UNUSED(s), struct sockaddr_storage* ATTR_UNUSED(addr),
1850 	socklen_t ATTR_UNUSED(addrlen))
1851 {
1852 	log_assert(0);
1853 	return 0;
1854 }
1855 
tcp_req_info_add_meshstate(struct tcp_req_info * ATTR_UNUSED (req),struct mesh_area * ATTR_UNUSED (mesh),struct mesh_state * ATTR_UNUSED (m))1856 int tcp_req_info_add_meshstate(struct tcp_req_info* ATTR_UNUSED(req),
1857         struct mesh_area* ATTR_UNUSED(mesh), struct mesh_state* ATTR_UNUSED(m))
1858 {
1859 	log_assert(0);
1860 	return 0;
1861 }
1862 
1863 void
tcp_req_info_remove_mesh_state(struct tcp_req_info * ATTR_UNUSED (req),struct mesh_state * ATTR_UNUSED (m))1864 tcp_req_info_remove_mesh_state(struct tcp_req_info* ATTR_UNUSED(req),
1865 	struct mesh_state* ATTR_UNUSED(m))
1866 {
1867 	log_assert(0);
1868 }
1869 
1870 size_t
tcp_req_info_get_stream_buffer_size(void)1871 tcp_req_info_get_stream_buffer_size(void)
1872 {
1873 	return 0;
1874 }
1875 
1876 size_t
http2_get_query_buffer_size(void)1877 http2_get_query_buffer_size(void)
1878 {
1879 	return 0;
1880 }
1881 
1882 size_t
http2_get_response_buffer_size(void)1883 http2_get_response_buffer_size(void)
1884 {
1885 	return 0;
1886 }
1887 
http2_stream_add_meshstate(struct http2_stream * ATTR_UNUSED (h2_stream),struct mesh_area * ATTR_UNUSED (mesh),struct mesh_state * ATTR_UNUSED (m))1888 void http2_stream_add_meshstate(struct http2_stream* ATTR_UNUSED(h2_stream),
1889 	struct mesh_area* ATTR_UNUSED(mesh), struct mesh_state* ATTR_UNUSED(m))
1890 {
1891 }
1892 
1893 /*********** End of Dummy routines ***********/
1894