xref: /netbsd-src/external/bsd/unbound/dist/testcode/fake_event.c (revision 867d70fc718005c0918b8b8b2f9d7f2d52d0a0db)
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
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
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
125 fake_event_init(struct replay_scenario* scen)
126 {
127 	saved_scenario = scen;
128 }
129 
130 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
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*
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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*
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
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*
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
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
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
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
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*
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
993 comm_signal_bind(struct comm_signal* ATTR_UNUSED(comsig), int
994 	ATTR_UNUSED(sig))
995 {
996 	return 1;
997 }
998 
999 void
1000 comm_signal_delete(struct comm_signal* comsig)
1001 {
1002 	free(comsig);
1003 }
1004 
1005 void
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
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*
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
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
1083 outside_network_quit_prepare(struct outside_network* ATTR_UNUSED(outnet))
1084 {
1085 }
1086 
1087 struct pending*
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*
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 
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(check_ratelimit),
1191 	int ATTR_UNUSED(tcp_upstream), int ATTR_UNUSED(ssl_upstream),
1192 	char* ATTR_UNUSED(tls_auth_name), struct sockaddr_storage* addr,
1193 	socklen_t addrlen, uint8_t* zone, size_t zonelen,
1194 	struct module_qstate* qstate, comm_point_callback_type* callback,
1195 	void* callback_arg, sldns_buffer* ATTR_UNUSED(buff),
1196 	struct module_env* env, int* ATTR_UNUSED(was_ratelimited))
1197 {
1198 	struct replay_runtime* runtime = (struct replay_runtime*)outnet->base;
1199 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1200 		sizeof(struct fake_pending));
1201 	char z[256];
1202 	log_assert(pend);
1203 	log_nametypeclass(VERB_OPS, "pending serviced query",
1204 		qinfo->qname, qinfo->qtype, qinfo->qclass);
1205 	dname_str(zone, z);
1206 	verbose(VERB_OPS, "pending serviced query zone %s flags%s%s%s%s",
1207 		z, (flags&BIT_RD)?" RD":"", (flags&BIT_CD)?" CD":"",
1208 		(flags&~(BIT_RD|BIT_CD))?" MORE":"", (dnssec)?" DO":"");
1209 
1210 	/* create packet with EDNS */
1211 	pend->buffer = sldns_buffer_new(512);
1212 	log_assert(pend->buffer);
1213 	sldns_buffer_write_u16(pend->buffer, 0); /* id */
1214 	sldns_buffer_write_u16(pend->buffer, flags);
1215 	sldns_buffer_write_u16(pend->buffer, 1); /* qdcount */
1216 	sldns_buffer_write_u16(pend->buffer, 0); /* ancount */
1217 	sldns_buffer_write_u16(pend->buffer, 0); /* nscount */
1218 	sldns_buffer_write_u16(pend->buffer, 0); /* arcount */
1219 	sldns_buffer_write(pend->buffer, qinfo->qname, qinfo->qname_len);
1220 	sldns_buffer_write_u16(pend->buffer, qinfo->qtype);
1221 	sldns_buffer_write_u16(pend->buffer, qinfo->qclass);
1222 	sldns_buffer_flip(pend->buffer);
1223 	if(1) {
1224 		struct edns_data edns;
1225 		struct edns_string_addr* client_string_addr;
1226 		struct edns_option* backed_up_opt_list =
1227 			qstate->edns_opts_back_out;
1228 		struct edns_option* per_upstream_opt_list = NULL;
1229 		/* If we have an already populated EDNS option list make a copy
1230 		 * since we may now add upstream specific EDNS options. */
1231 		if(qstate->edns_opts_back_out) {
1232 			per_upstream_opt_list = edns_opt_copy_region(
1233 				qstate->edns_opts_back_out, qstate->region);
1234 			if(!per_upstream_opt_list) {
1235 				free(pend);
1236 				fatal_exit("out of memory");
1237 			}
1238 			qstate->edns_opts_back_out = per_upstream_opt_list;
1239 		}
1240 		if(!inplace_cb_query_call(env, qinfo, flags, addr, addrlen,
1241 			zone, zonelen, qstate, qstate->region)) {
1242 			free(pend);
1243 			return NULL;
1244 		}
1245 		/* Restore the option list; we can explicitly use the copied
1246 		 * one from now on. */
1247 		per_upstream_opt_list = qstate->edns_opts_back_out;
1248 		qstate->edns_opts_back_out = backed_up_opt_list;
1249 		if((client_string_addr = edns_string_addr_lookup(
1250 			&env->edns_strings->client_strings,
1251 			addr, addrlen))) {
1252 			edns_opt_list_append(&per_upstream_opt_list,
1253 				env->edns_strings->client_string_opcode,
1254 				client_string_addr->string_len,
1255 				client_string_addr->string, qstate->region);
1256 		}
1257 		/* add edns */
1258 		edns.edns_present = 1;
1259 		edns.ext_rcode = 0;
1260 		edns.edns_version = EDNS_ADVERTISED_VERSION;
1261 		edns.udp_size = EDNS_ADVERTISED_SIZE;
1262 		edns.bits = 0;
1263 		if(dnssec)
1264 			edns.bits = EDNS_DO;
1265 		edns.padding_block_size = 0;
1266 		edns.opt_list_in = NULL;
1267 		edns.opt_list_out = per_upstream_opt_list;
1268 		edns.opt_list_inplace_cb_out = NULL;
1269 		attach_edns_record(pend->buffer, &edns);
1270 	}
1271 	memcpy(&pend->addr, addr, addrlen);
1272 	pend->addrlen = addrlen;
1273 	pend->zone = memdup(zone, zonelen);
1274 	pend->zonelen = zonelen;
1275 	pend->qtype = (int)qinfo->qtype;
1276 	log_assert(pend->zone);
1277 	pend->callback = callback;
1278 	pend->cb_arg = callback_arg;
1279 	pend->timeout = UDP_AUTH_QUERY_TIMEOUT/1000;
1280 	pend->transport = transport_udp; /* pretend UDP */
1281 	pend->pkt = NULL;
1282 	pend->runtime = runtime;
1283 	pend->serviced = 1;
1284 	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1285 	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1286 	if(!pend->pkt) fatal_exit("out of memory");
1287 	/*log_pkt("pending serviced query: ", pend->pkt, pend->pkt_len);*/
1288 
1289 	/* see if it matches the current moment */
1290 	if(runtime->now && runtime->now->evt_type == repevt_back_query &&
1291 		(runtime->now->addrlen == 0 || sockaddr_cmp(
1292 			&runtime->now->addr, runtime->now->addrlen,
1293 			&pend->addr, pend->addrlen) == 0) &&
1294 		find_match(runtime->now->match, pend->pkt, pend->pkt_len,
1295 			pend->transport)) {
1296 		log_info("testbound: matched pending to event. "
1297 			"advance time between events.");
1298 		log_info("testbound: do STEP %d %s", runtime->now->time_step,
1299 			repevt_string(runtime->now->evt_type));
1300 		advance_moment(runtime);
1301 		/* still create the pending, because we need it to callback */
1302 	}
1303 	log_info("testbound: created fake pending");
1304 	/* add to list */
1305 	pend->next = runtime->pending_list;
1306 	runtime->pending_list = pend;
1307 	return (struct serviced_query*)pend;
1308 }
1309 
1310 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg)
1311 {
1312 	struct fake_pending* pend = (struct fake_pending*)sq;
1313 	struct replay_runtime* runtime = pend->runtime;
1314 	/* delete from the list */
1315 	struct fake_pending* p = runtime->pending_list, *prev=NULL;
1316 	while(p) {
1317 		if(p == pend) {
1318 			log_assert(p->cb_arg == cb_arg);
1319 			(void)cb_arg;
1320 			log_info("serviced pending delete");
1321 			if(prev)
1322 				prev->next = p->next;
1323 			else 	runtime->pending_list = p->next;
1324 			sldns_buffer_free(p->buffer);
1325 			free(p->pkt);
1326 			free(p->zone);
1327 			free(p);
1328 			return;
1329 		}
1330 		prev = p;
1331 		p = p->next;
1332 	}
1333 	log_info("double delete of pending serviced query");
1334 }
1335 
1336 int resolve_interface_names(char** ATTR_UNUSED(ifs), int ATTR_UNUSED(num_ifs),
1337 	struct config_strlist* ATTR_UNUSED(list), char*** ATTR_UNUSED(resif),
1338 	int* ATTR_UNUSED(num_resif))
1339 {
1340 	return 1;
1341 }
1342 
1343 struct listen_port* listening_ports_open(struct config_file* ATTR_UNUSED(cfg),
1344 	char** ATTR_UNUSED(ifs), int ATTR_UNUSED(num_ifs),
1345 	int* ATTR_UNUSED(reuseport))
1346 {
1347 	return calloc(1, 1);
1348 }
1349 
1350 void listening_ports_free(struct listen_port* list)
1351 {
1352 	free(list);
1353 }
1354 
1355 struct comm_point* comm_point_create_local(struct comm_base* ATTR_UNUSED(base),
1356         int ATTR_UNUSED(fd), size_t ATTR_UNUSED(bufsize),
1357         comm_point_callback_type* ATTR_UNUSED(callback),
1358 	void* ATTR_UNUSED(callback_arg))
1359 {
1360 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1361 		sizeof(*fc));
1362 	if(!fc) return NULL;
1363 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1364 	return (struct comm_point*)fc;
1365 }
1366 
1367 struct comm_point* comm_point_create_raw(struct comm_base* ATTR_UNUSED(base),
1368         int ATTR_UNUSED(fd), int ATTR_UNUSED(writing),
1369         comm_point_callback_type* ATTR_UNUSED(callback),
1370 	void* ATTR_UNUSED(callback_arg))
1371 {
1372 	/* no pipe comm possible */
1373 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1374 		sizeof(*fc));
1375 	if(!fc) return NULL;
1376 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1377 	return (struct comm_point*)fc;
1378 }
1379 
1380 void comm_point_start_listening(struct comm_point* ATTR_UNUSED(c),
1381 	int ATTR_UNUSED(newfd), int ATTR_UNUSED(sec))
1382 {
1383 	/* no bg write pipe comm possible */
1384 }
1385 
1386 void comm_point_stop_listening(struct comm_point* ATTR_UNUSED(c))
1387 {
1388 	/* no bg write pipe comm possible */
1389 }
1390 
1391 /* only cmd com _local gets deleted */
1392 void comm_point_delete(struct comm_point* c)
1393 {
1394 	struct fake_commpoint* fc = (struct fake_commpoint*)c;
1395 	if(c == NULL) return;
1396 	log_assert(fc->typecode == FAKE_COMMPOINT_TYPECODE);
1397 	if(fc->type_tcp_out) {
1398 		/* remove tcp pending, so no more callbacks to it */
1399 		pending_list_delete(fc->runtime, fc->pending);
1400 	}
1401 	free(c);
1402 }
1403 
1404 size_t listen_get_mem(struct listen_dnsport* ATTR_UNUSED(listen))
1405 {
1406 	return 0;
1407 }
1408 
1409 size_t outnet_get_mem(struct outside_network* ATTR_UNUSED(outnet))
1410 {
1411 	return 0;
1412 }
1413 
1414 size_t comm_point_get_mem(struct comm_point* ATTR_UNUSED(c))
1415 {
1416 	return 0;
1417 }
1418 
1419 size_t serviced_get_mem(struct serviced_query* ATTR_UNUSED(c))
1420 {
1421 	return 0;
1422 }
1423 
1424 /* fake for fptr wlist */
1425 int outnet_udp_cb(struct comm_point* ATTR_UNUSED(c),
1426 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1427         struct comm_reply *ATTR_UNUSED(reply_info))
1428 {
1429 	log_assert(0);
1430 	return 0;
1431 }
1432 
1433 int outnet_tcp_cb(struct comm_point* ATTR_UNUSED(c),
1434 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1435         struct comm_reply *ATTR_UNUSED(reply_info))
1436 {
1437 	log_assert(0);
1438 	return 0;
1439 }
1440 
1441 void pending_udp_timer_cb(void *ATTR_UNUSED(arg))
1442 {
1443 	log_assert(0);
1444 }
1445 
1446 void serviced_timer_cb(void *ATTR_UNUSED(arg))
1447 {
1448 	log_assert(0);
1449 }
1450 
1451 void pending_udp_timer_delay_cb(void *ATTR_UNUSED(arg))
1452 {
1453 	log_assert(0);
1454 }
1455 
1456 void outnet_tcptimer(void* ATTR_UNUSED(arg))
1457 {
1458 	log_assert(0);
1459 }
1460 
1461 void comm_point_udp_callback(int ATTR_UNUSED(fd), short ATTR_UNUSED(event),
1462 	void* ATTR_UNUSED(arg))
1463 {
1464 	log_assert(0);
1465 }
1466 
1467 void comm_point_udp_ancil_callback(int ATTR_UNUSED(fd),
1468 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1469 {
1470 	log_assert(0);
1471 }
1472 
1473 void comm_point_tcp_accept_callback(int ATTR_UNUSED(fd),
1474 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1475 {
1476 	log_assert(0);
1477 }
1478 
1479 void comm_point_tcp_handle_callback(int ATTR_UNUSED(fd),
1480 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1481 {
1482 	log_assert(0);
1483 }
1484 
1485 void comm_timer_callback(int ATTR_UNUSED(fd),
1486 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1487 {
1488 	log_assert(0);
1489 }
1490 
1491 void comm_signal_callback(int ATTR_UNUSED(fd),
1492 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1493 {
1494 	log_assert(0);
1495 }
1496 
1497 void comm_point_http_handle_callback(int ATTR_UNUSED(fd),
1498 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1499 {
1500 	log_assert(0);
1501 }
1502 
1503 void comm_point_local_handle_callback(int ATTR_UNUSED(fd),
1504 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1505 {
1506 	log_assert(0);
1507 }
1508 
1509 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
1510 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1511 {
1512 	log_assert(0);
1513 }
1514 
1515 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
1516 	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1517 {
1518 	log_assert(0);
1519 }
1520 
1521 int serviced_udp_callback(struct comm_point* ATTR_UNUSED(c),
1522 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1523         struct comm_reply* ATTR_UNUSED(reply_info))
1524 {
1525 	log_assert(0);
1526 	return 0;
1527 }
1528 
1529 int serviced_tcp_callback(struct comm_point* ATTR_UNUSED(c),
1530 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1531         struct comm_reply* ATTR_UNUSED(reply_info))
1532 {
1533 	log_assert(0);
1534 	return 0;
1535 }
1536 
1537 int pending_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1538 {
1539 	log_assert(0);
1540 	return 0;
1541 }
1542 
1543 int serviced_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1544 {
1545 	log_assert(0);
1546 	return 0;
1547 }
1548 
1549 int reuse_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1550 {
1551 	log_assert(0);
1552 	return 0;
1553 }
1554 
1555 int reuse_id_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1556 {
1557 	log_assert(0);
1558 	return 0;
1559 }
1560 
1561 /* timers in testbound for autotrust. statistics tested in tdir. */
1562 struct comm_timer* comm_timer_create(struct comm_base* base,
1563 	void (*cb)(void*), void* cb_arg)
1564 {
1565 	struct replay_runtime* runtime = (struct replay_runtime*)base;
1566 	struct fake_timer* t = (struct fake_timer*)calloc(1, sizeof(*t));
1567 	if(!t)
1568 		fatal_exit("out of memory in fake_event.c:comm_timer_create");
1569 	t->cb = cb;
1570 	t->cb_arg = cb_arg;
1571 	fptr_ok(fptr_whitelist_comm_timer(t->cb)); /* check in advance */
1572 	t->runtime = runtime;
1573 	t->next = runtime->timer_list;
1574 	runtime->timer_list = t;
1575 	return (struct comm_timer*)t;
1576 }
1577 
1578 void comm_timer_disable(struct comm_timer* timer)
1579 {
1580 	struct fake_timer* t = (struct fake_timer*)timer;
1581 	log_info("fake timer disabled");
1582 	t->enabled = 0;
1583 }
1584 
1585 void comm_timer_set(struct comm_timer* timer, struct timeval* tv)
1586 {
1587 	struct fake_timer* t = (struct fake_timer*)timer;
1588 	t->enabled = 1;
1589 	t->tv = *tv;
1590 	log_info("fake timer set %d.%6.6d",
1591 		(int)t->tv.tv_sec, (int)t->tv.tv_usec);
1592 	timeval_add(&t->tv, &t->runtime->now_tv);
1593 }
1594 
1595 void comm_timer_delete(struct comm_timer* timer)
1596 {
1597 	struct fake_timer* t = (struct fake_timer*)timer;
1598 	struct fake_timer** pp, *p;
1599 	if(!t) return;
1600 
1601 	/* remove from linked list */
1602 	pp = &t->runtime->timer_list;
1603 	p = t->runtime->timer_list;
1604 	while(p) {
1605 		if(p == t) {
1606 			/* snip from list */
1607 			*pp = p->next;
1608 			break;
1609 		}
1610 		pp = &p->next;
1611 		p = p->next;
1612 	}
1613 
1614 	free(timer);
1615 }
1616 
1617 void comm_base_set_slow_accept_handlers(struct comm_base* ATTR_UNUSED(b),
1618 	void (*stop_acc)(void*), void (*start_acc)(void*),
1619 	void* ATTR_UNUSED(arg))
1620 {
1621 	/* ignore this */
1622 	(void)stop_acc;
1623 	(void)start_acc;
1624 }
1625 
1626 struct ub_event_base* comm_base_internal(struct comm_base* ATTR_UNUSED(b))
1627 {
1628 	/* no pipe comm possible in testbound */
1629 	return NULL;
1630 }
1631 
1632 void daemon_remote_exec(struct worker* ATTR_UNUSED(worker))
1633 {
1634 }
1635 
1636 void listen_start_accept(struct listen_dnsport* ATTR_UNUSED(listen))
1637 {
1638 }
1639 
1640 void listen_stop_accept(struct listen_dnsport* ATTR_UNUSED(listen))
1641 {
1642 }
1643 
1644 void daemon_remote_start_accept(struct daemon_remote* ATTR_UNUSED(rc))
1645 {
1646 }
1647 
1648 void daemon_remote_stop_accept(struct daemon_remote* ATTR_UNUSED(rc))
1649 {
1650 }
1651 
1652 int create_udp_sock(int ATTR_UNUSED(family), int ATTR_UNUSED(socktype),
1653 	struct sockaddr* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
1654 	int ATTR_UNUSED(v6only), int* ATTR_UNUSED(inuse),
1655 	int* ATTR_UNUSED(noproto), int ATTR_UNUSED(rcv), int ATTR_UNUSED(snd),
1656 	int ATTR_UNUSED(listen), int* ATTR_UNUSED(reuseport),
1657 	int ATTR_UNUSED(transparent), int ATTR_UNUSED(freebind),
1658 	int ATTR_UNUSED(use_systemd), int ATTR_UNUSED(dscp))
1659 {
1660 	/* if you actually print to this, it'll be stdout during test */
1661 	return 1;
1662 }
1663 
1664 struct comm_point* comm_point_create_udp(struct comm_base *ATTR_UNUSED(base),
1665 	int ATTR_UNUSED(fd), sldns_buffer* ATTR_UNUSED(buffer),
1666 	comm_point_callback_type* ATTR_UNUSED(callback),
1667 	void* ATTR_UNUSED(callback_arg),
1668 	struct unbound_socket* ATTR_UNUSED(socket))
1669 {
1670 	log_assert(0);
1671 	return NULL;
1672 }
1673 
1674 struct comm_point* comm_point_create_tcp_out(struct comm_base*
1675 	ATTR_UNUSED(base), size_t ATTR_UNUSED(bufsize),
1676 	comm_point_callback_type* ATTR_UNUSED(callback),
1677 	void* ATTR_UNUSED(callback_arg))
1678 {
1679 	log_assert(0);
1680 	return NULL;
1681 }
1682 
1683 struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet,
1684 	comm_point_callback_type* cb, void* cb_arg,
1685 	struct sockaddr_storage* ATTR_UNUSED(to_addr),
1686 	socklen_t ATTR_UNUSED(to_addrlen))
1687 {
1688 	struct replay_runtime* runtime = (struct replay_runtime*)
1689 		outnet->base;
1690 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1691 		sizeof(*fc));
1692 	if(!fc) return NULL;
1693 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1694 	fc->type_udp_out = 1;
1695 	fc->cb = cb;
1696 	fc->cb_arg = cb_arg;
1697 	fc->runtime = runtime;
1698 	/* used by authzone transfers */
1699 	return (struct comm_point*)fc;
1700 }
1701 
1702 struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet,
1703 	comm_point_callback_type* cb, void* cb_arg,
1704 	struct sockaddr_storage* to_addr, socklen_t to_addrlen,
1705 	struct sldns_buffer* query, int timeout, int ATTR_UNUSED(ssl),
1706 	char* ATTR_UNUSED(host))
1707 {
1708 	struct replay_runtime* runtime = (struct replay_runtime*)
1709 		outnet->base;
1710 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1711 		sizeof(*fc));
1712 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1713 		sizeof(struct fake_pending));
1714 	if(!fc || !pend) {
1715 		free(fc);
1716 		free(pend);
1717 		return NULL;
1718 	}
1719 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1720 	fc->type_tcp_out = 1;
1721 	fc->cb = cb;
1722 	fc->cb_arg = cb_arg;
1723 	fc->runtime = runtime;
1724 	fc->pending = pend;
1725 
1726 	/* used by authzone transfers */
1727 	/* create pending item */
1728 	pend->buffer = sldns_buffer_new(sldns_buffer_limit(query)+10);
1729 	if(!pend->buffer) {
1730 		free(fc);
1731 		free(pend);
1732 		return NULL;
1733 	}
1734 	sldns_buffer_copy(pend->buffer, query);
1735 	memcpy(&pend->addr, to_addr, to_addrlen);
1736 	pend->addrlen = to_addrlen;
1737 	pend->zone = NULL;
1738 	pend->zonelen = 0;
1739 	if(LDNS_QDCOUNT(sldns_buffer_begin(query)) > 0) {
1740 		char buf[512];
1741 		char addrbuf[128];
1742 		(void)sldns_wire2str_rrquestion_buf(sldns_buffer_at(query, LDNS_HEADER_SIZE), sldns_buffer_limit(query)-LDNS_HEADER_SIZE, buf, sizeof(buf));
1743 		addr_to_str((struct sockaddr_storage*)to_addr, to_addrlen,
1744 			addrbuf, sizeof(addrbuf));
1745 		if(verbosity >= VERB_ALGO) {
1746 			strip_end_white(buf);
1747 			log_info("tcp to %s: %s", addrbuf, buf);
1748 		}
1749 		log_assert(sldns_buffer_limit(query)-LDNS_HEADER_SIZE >= 2);
1750 		pend->qtype = (int)sldns_buffer_read_u16_at(query,
1751 			LDNS_HEADER_SIZE+
1752 			dname_valid(sldns_buffer_at(query, LDNS_HEADER_SIZE),
1753 				sldns_buffer_limit(query)-LDNS_HEADER_SIZE));
1754 	}
1755 	pend->callback = cb;
1756 	pend->cb_arg = cb_arg;
1757 	pend->timeout = timeout;
1758 	pend->transport = transport_tcp;
1759 	pend->pkt = NULL;
1760 	pend->runtime = runtime;
1761 	pend->serviced = 0;
1762 	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1763 	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1764 	if(!pend->pkt) fatal_exit("out of memory");
1765 
1766 	log_info("testbound: created fake pending for tcp_out");
1767 
1768 	/* add to list */
1769 	pend->next = runtime->pending_list;
1770 	runtime->pending_list = pend;
1771 
1772 	return (struct comm_point*)fc;
1773 }
1774 
1775 struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet,
1776 	comm_point_callback_type* cb, void* cb_arg,
1777 	struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
1778 	int ssl, char* host, char* path, struct config_file* cfg)
1779 {
1780 	struct replay_runtime* runtime = (struct replay_runtime*)
1781 		outnet->base;
1782 	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1783 		sizeof(*fc));
1784 	if(!fc) {
1785 		return NULL;
1786 	}
1787 	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1788 	fc->type_http_out = 1;
1789 	fc->cb = cb;
1790 	fc->cb_arg = cb_arg;
1791 	fc->runtime = runtime;
1792 
1793 	(void)to_addr;
1794 	(void)to_addrlen;
1795 	(void)timeout;
1796 
1797 	(void)ssl;
1798 	(void)host;
1799 	(void)path;
1800 	(void)cfg;
1801 
1802 	/* handle http comm point and return contents from test script */
1803 	return (struct comm_point*)fc;
1804 }
1805 
1806 int comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet,
1807 	struct sockaddr* addr, socklen_t addrlen, int ATTR_UNUSED(is_connected))
1808 {
1809 	struct fake_commpoint* fc = (struct fake_commpoint*)c;
1810 	struct replay_runtime* runtime = fc->runtime;
1811 	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1812 		sizeof(struct fake_pending));
1813 	if(!pend) {
1814 		log_err("malloc failure");
1815 		return 0;
1816 	}
1817 	fc->pending = pend;
1818 	/* used by authzone transfers */
1819 	/* create pending item */
1820 	pend->buffer = sldns_buffer_new(sldns_buffer_limit(packet) + 10);
1821 	if(!pend->buffer) {
1822 		free(pend);
1823 		return 0;
1824 	}
1825 	sldns_buffer_copy(pend->buffer, packet);
1826 	memcpy(&pend->addr, addr, addrlen);
1827 	pend->addrlen = addrlen;
1828 	pend->zone = NULL;
1829 	pend->zonelen = 0;
1830 	if(LDNS_QDCOUNT(sldns_buffer_begin(packet)) > 0) {
1831 		char buf[512];
1832 		char addrbuf[128];
1833 		(void)sldns_wire2str_rrquestion_buf(sldns_buffer_at(packet, LDNS_HEADER_SIZE), sldns_buffer_limit(packet)-LDNS_HEADER_SIZE, buf, sizeof(buf));
1834 		addr_to_str((struct sockaddr_storage*)addr, addrlen,
1835 			addrbuf, sizeof(addrbuf));
1836 		if(verbosity >= VERB_ALGO) {
1837 			strip_end_white(buf);
1838 			log_info("udp to %s: %s", addrbuf, buf);
1839 		}
1840 		log_assert(sldns_buffer_limit(packet)-LDNS_HEADER_SIZE >= 2);
1841 		pend->qtype = (int)sldns_buffer_read_u16_at(packet,
1842 			LDNS_HEADER_SIZE+
1843 			dname_valid(sldns_buffer_at(packet, LDNS_HEADER_SIZE),
1844 				sldns_buffer_limit(packet)-LDNS_HEADER_SIZE));
1845 	}
1846 	pend->callback = fc->cb;
1847 	pend->cb_arg = fc->cb_arg;
1848 	pend->timeout = UDP_AUTH_QUERY_TIMEOUT/1000;
1849 	pend->transport = transport_udp;
1850 	pend->pkt = NULL;
1851 	pend->runtime = runtime;
1852 	pend->serviced = 0;
1853 	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1854 	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1855 	if(!pend->pkt) fatal_exit("out of memory");
1856 
1857 	log_info("testbound: created fake pending for send_udp_msg");
1858 
1859 	/* add to list */
1860 	pend->next = runtime->pending_list;
1861 	runtime->pending_list = pend;
1862 
1863 	return 1;
1864 }
1865 
1866 int outnet_get_tcp_fd(struct sockaddr_storage* ATTR_UNUSED(addr),
1867 	socklen_t ATTR_UNUSED(addrlen), int ATTR_UNUSED(tcp_mss), int ATTR_UNUSED(dscp))
1868 {
1869 	log_assert(0);
1870 	return -1;
1871 }
1872 
1873 int outnet_tcp_connect(int ATTR_UNUSED(s), struct sockaddr_storage* ATTR_UNUSED(addr),
1874 	socklen_t ATTR_UNUSED(addrlen))
1875 {
1876 	log_assert(0);
1877 	return 0;
1878 }
1879 
1880 int tcp_req_info_add_meshstate(struct tcp_req_info* ATTR_UNUSED(req),
1881         struct mesh_area* ATTR_UNUSED(mesh), struct mesh_state* ATTR_UNUSED(m))
1882 {
1883 	log_assert(0);
1884 	return 0;
1885 }
1886 
1887 void
1888 tcp_req_info_remove_mesh_state(struct tcp_req_info* ATTR_UNUSED(req),
1889 	struct mesh_state* ATTR_UNUSED(m))
1890 {
1891 	log_assert(0);
1892 }
1893 
1894 size_t
1895 tcp_req_info_get_stream_buffer_size(void)
1896 {
1897 	return 0;
1898 }
1899 
1900 size_t
1901 http2_get_query_buffer_size(void)
1902 {
1903 	return 0;
1904 }
1905 
1906 size_t
1907 http2_get_response_buffer_size(void)
1908 {
1909 	return 0;
1910 }
1911 
1912 void http2_stream_add_meshstate(struct http2_stream* ATTR_UNUSED(h2_stream),
1913 	struct mesh_area* ATTR_UNUSED(mesh), struct mesh_state* ATTR_UNUSED(m))
1914 {
1915 }
1916 
1917 /*********** End of Dummy routines ***********/
1918