1369938Sgit2svn/*
2369938Sgit2svn * testcode/fake_event.c - fake event handling that replays existing scenario.
3369938Sgit2svn *
4369938Sgit2svn * Copyright (c) 2007, NLnet Labs. All rights reserved.
5369938Sgit2svn *
6369938Sgit2svn * This software is open source.
7369938Sgit2svn *
8369938Sgit2svn * Redistribution and use in source and binary forms, with or without
9369938Sgit2svn * modification, are permitted provided that the following conditions
10369938Sgit2svn * are met:
11369938Sgit2svn *
12369938Sgit2svn * Redistributions of source code must retain the above copyright notice,
13369938Sgit2svn * this list of conditions and the following disclaimer.
14369938Sgit2svn *
15369938Sgit2svn * Redistributions in binary form must reproduce the above copyright notice,
16369938Sgit2svn * this list of conditions and the following disclaimer in the documentation
17369938Sgit2svn * and/or other materials provided with the distribution.
18369938Sgit2svn *
19369938Sgit2svn * Neither the name of the NLNET LABS nor the names of its contributors may
20369938Sgit2svn * be used to endorse or promote products derived from this software without
21369938Sgit2svn * specific prior written permission.
22369938Sgit2svn *
23369938Sgit2svn * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24369938Sgit2svn * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25369938Sgit2svn * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26369938Sgit2svn * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27369938Sgit2svn * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28369938Sgit2svn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29369938Sgit2svn * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30369938Sgit2svn * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31369938Sgit2svn * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32369938Sgit2svn * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33369938Sgit2svn * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34369938Sgit2svn */
35369938Sgit2svn
36369938Sgit2svn/**
37369938Sgit2svn * \file
38369938Sgit2svn * Event service that replays a scenario.
39369938Sgit2svn * This implements the same exported symbols as the files:
40369938Sgit2svn * util/netevent.c
41369938Sgit2svn * services/listen_dnsport.c
42369938Sgit2svn * services/outside_network.c
43369938Sgit2svn * But these do not actually access the network or events, instead
44369938Sgit2svn * the scenario is played.
45369938Sgit2svn */
46369938Sgit2svn
47369938Sgit2svn#include "config.h"
48369938Sgit2svn#include "testcode/fake_event.h"
49369938Sgit2svn#include "util/netevent.h"
50369938Sgit2svn#include "util/net_help.h"
51369938Sgit2svn#include "util/data/msgparse.h"
52369938Sgit2svn#include "util/data/msgreply.h"
53369938Sgit2svn#include "util/data/msgencode.h"
54369938Sgit2svn#include "util/data/dname.h"
55369938Sgit2svn#include "util/edns.h"
56369938Sgit2svn#include "util/config_file.h"
57369938Sgit2svn#include "services/listen_dnsport.h"
58369938Sgit2svn#include "services/outside_network.h"
59369938Sgit2svn#include "services/cache/infra.h"
60369938Sgit2svn#include "testcode/replay.h"
61369938Sgit2svn#include "testcode/testpkts.h"
62369938Sgit2svn#include "util/log.h"
63369938Sgit2svn#include "util/fptr_wlist.h"
64369938Sgit2svn#include "sldns/sbuffer.h"
65369938Sgit2svn#include "sldns/wire2str.h"
66369938Sgit2svn#include "sldns/str2wire.h"
67369938Sgit2svn#include <signal.h>
68369938Sgit2svnstruct worker;
69369938Sgit2svnstruct daemon_remote;
70369938Sgit2svn
71369938Sgit2svn/** unique code to check that fake_commpoint is that structure */
72369938Sgit2svn#define FAKE_COMMPOINT_TYPECODE 97347923
73369938Sgit2svn/** fake commpoint, stores information */
74369938Sgit2svnstruct fake_commpoint {
75369938Sgit2svn	/** typecode */
76369938Sgit2svn	int typecode;
77369938Sgit2svn	/** if this is a udp outgoing type of commpoint */
78369938Sgit2svn	int type_udp_out;
79369938Sgit2svn	/** if this is a tcp outgoing type of commpoint */
80369938Sgit2svn	int type_tcp_out;
81369938Sgit2svn	/** if this is a http outgoing type of commpoint. */
82369938Sgit2svn	int type_http_out;
83369938Sgit2svn
84369938Sgit2svn	/** the callback, stored for usage */
85369938Sgit2svn	comm_point_callback_type* cb;
86369938Sgit2svn	/** the callback userarg, stored for usage */
87369938Sgit2svn	void* cb_arg;
88369938Sgit2svn	/** runtime ptr */
89369938Sgit2svn	struct replay_runtime* runtime;
90369938Sgit2svn	/** the pending entry for this commpoint (if any) */
91369938Sgit2svn	struct fake_pending* pending;
92369938Sgit2svn};
93369938Sgit2svn
94369938Sgit2svn/** Global variable: the scenario. Saved here for when event_init is done. */
95369938Sgit2svnstatic struct replay_scenario* saved_scenario = NULL;
96369938Sgit2svn
97369938Sgit2svn/** add timers and the values do not overflow or become negative */
98369938Sgit2svnstatic void
99369938Sgit2svntimeval_add(struct timeval* d, const struct timeval* add)
100369938Sgit2svn{
101369938Sgit2svn#ifndef S_SPLINT_S
102369938Sgit2svn	d->tv_sec += add->tv_sec;
103369938Sgit2svn	d->tv_usec += add->tv_usec;
104369938Sgit2svn	if(d->tv_usec >= 1000000) {
105369938Sgit2svn		d->tv_usec -= 1000000;
106369938Sgit2svn		d->tv_sec++;
107369938Sgit2svn	}
108369938Sgit2svn#endif
109369938Sgit2svn}
110369938Sgit2svn
111369938Sgit2svnvoid
112369938Sgit2svnfake_temp_file(const char* adj, const char* id, char* buf, size_t len)
113369938Sgit2svn{
114369938Sgit2svn#ifdef USE_WINSOCK
115369938Sgit2svn	snprintf(buf, len, "testbound_%u%s%s.tmp",
116369938Sgit2svn		(unsigned)getpid(), adj, id);
117369938Sgit2svn#else
118369938Sgit2svn	snprintf(buf, len, "/tmp/testbound_%u%s%s.tmp",
119369938Sgit2svn		(unsigned)getpid(), adj, id);
120369938Sgit2svn#endif
121369938Sgit2svn}
122369938Sgit2svn
123369938Sgit2svnvoid
124369938Sgit2svnfake_event_init(struct replay_scenario* scen)
125369938Sgit2svn{
126369938Sgit2svn	saved_scenario = scen;
127369938Sgit2svn}
128369938Sgit2svn
129369938Sgit2svnvoid
130369938Sgit2svnfake_event_cleanup(void)
131369938Sgit2svn{
132369938Sgit2svn	replay_scenario_delete(saved_scenario);
133369938Sgit2svn	saved_scenario = NULL;
134369938Sgit2svn}
135369938Sgit2svn
136369938Sgit2svn/** helper function that logs a sldns_pkt packet to logfile */
137369938Sgit2svnstatic void
138369938Sgit2svnlog_pkt(const char* desc, uint8_t* pkt, size_t len)
139369938Sgit2svn{
140369938Sgit2svn	char* str = sldns_wire2str_pkt(pkt, len);
141369938Sgit2svn	if(!str)
142369938Sgit2svn		fatal_exit("%s: (failed out of memory wire2str_pkt)", desc);
143369938Sgit2svn	else {
144369938Sgit2svn		log_info("%s%s", desc, str);
145369938Sgit2svn		free(str);
146369938Sgit2svn	}
147369938Sgit2svn}
148369938Sgit2svn
149369938Sgit2svn/**
150369938Sgit2svn * Returns a string describing the event type.
151369938Sgit2svn */
152369938Sgit2svnstatic const char*
153369938Sgit2svnrepevt_string(enum replay_event_type t)
154369938Sgit2svn{
155369938Sgit2svn	switch(t) {
156369938Sgit2svn	case repevt_nothing:	 return "NOTHING";
157369938Sgit2svn	case repevt_front_query: return "QUERY";
158369938Sgit2svn	case repevt_front_reply: return "CHECK_ANSWER";
159369938Sgit2svn	case repevt_timeout:	 return "TIMEOUT";
160369938Sgit2svn	case repevt_time_passes: return "TIME_PASSES";
161369938Sgit2svn	case repevt_back_reply:  return "REPLY";
162369938Sgit2svn	case repevt_back_query:  return "CHECK_OUT_QUERY";
163369938Sgit2svn	case repevt_autotrust_check: return "CHECK_AUTOTRUST";
164369938Sgit2svn	case repevt_tempfile_check: return "CHECK_TEMPFILE";
165369938Sgit2svn	case repevt_error:	 return "ERROR";
166369938Sgit2svn	case repevt_assign:	 return "ASSIGN";
167369938Sgit2svn	case repevt_traffic:	 return "TRAFFIC";
168369938Sgit2svn	case repevt_infra_rtt:	 return "INFRA_RTT";
169369938Sgit2svn	default:		 return "UNKNOWN";
170369938Sgit2svn	}
171369938Sgit2svn}
172369938Sgit2svn
173369938Sgit2svn/** delete a fake pending */
174369938Sgit2svnstatic void
175369938Sgit2svndelete_fake_pending(struct fake_pending* pend)
176369938Sgit2svn{
177369938Sgit2svn	if(!pend)
178369938Sgit2svn		return;
179369938Sgit2svn	free(pend->zone);
180369938Sgit2svn	sldns_buffer_free(pend->buffer);
181369938Sgit2svn	free(pend->pkt);
182369938Sgit2svn	free(pend);
183369938Sgit2svn}
184369938Sgit2svn
185369938Sgit2svn/** delete a replay answer */
186369938Sgit2svnstatic void
187369938Sgit2svndelete_replay_answer(struct replay_answer* a)
188369938Sgit2svn{
189369938Sgit2svn	if(!a)
190369938Sgit2svn		return;
191369938Sgit2svn	if(a->repinfo.c) {
192369938Sgit2svn		sldns_buffer_free(a->repinfo.c->buffer);
193369938Sgit2svn		free(a->repinfo.c);
194369938Sgit2svn	}
195369938Sgit2svn	free(a->pkt);
196369938Sgit2svn	free(a);
197369938Sgit2svn}
198369938Sgit2svn
199369938Sgit2svn/**
200369938Sgit2svn * return: true if pending query matches the now event.
201369938Sgit2svn */
202369938Sgit2svnstatic int
203369938Sgit2svnpending_matches_current(struct replay_runtime* runtime,
204369938Sgit2svn	struct entry** entry, struct fake_pending **pend)
205369938Sgit2svn{
206369938Sgit2svn	struct fake_pending* p;
207369938Sgit2svn	struct entry* e;
208369938Sgit2svn	if(!runtime->now || runtime->now->evt_type != repevt_back_query
209369938Sgit2svn		|| !runtime->pending_list)
210369938Sgit2svn		return 0;
211369938Sgit2svn	/* see if any of the pending queries matches */
212369938Sgit2svn	for(p = runtime->pending_list; p; p = p->next) {
213369938Sgit2svn		if(runtime->now->addrlen != 0 &&
214369938Sgit2svn			sockaddr_cmp(&p->addr, p->addrlen, &runtime->now->addr,
215369938Sgit2svn			runtime->now->addrlen) != 0)
216369938Sgit2svn			continue;
217369938Sgit2svn		if((e=find_match(runtime->now->match, p->pkt, p->pkt_len,
218369938Sgit2svn			p->transport))) {
219369938Sgit2svn			*entry = e;
220369938Sgit2svn			*pend = p;
221369938Sgit2svn			return 1;
222369938Sgit2svn		}
223369938Sgit2svn	}
224369938Sgit2svn	return 0;
225369938Sgit2svn}
226369938Sgit2svn
227369938Sgit2svn/**
228369938Sgit2svn * Find the range that matches this pending message.
229369938Sgit2svn * @param runtime: runtime with current moment, and range list.
230369938Sgit2svn * @param entry: returns the pointer to entry that matches.
231369938Sgit2svn * @param pend: the pending that the entry must match.
232369938Sgit2svn * @return: true if a match is found.
233369938Sgit2svn */
234369938Sgit2svnstatic int
235369938Sgit2svnpending_find_match(struct replay_runtime* runtime, struct entry** entry,
236369938Sgit2svn	struct fake_pending* pend)
237369938Sgit2svn{
238369938Sgit2svn	int timenow = runtime->now->time_step;
239369938Sgit2svn	struct replay_range* p = runtime->scenario->range_list;
240369938Sgit2svn	while(p) {
241369938Sgit2svn		if(p->start_step <= timenow && timenow <= p->end_step &&
242369938Sgit2svn		  (p->addrlen == 0 || sockaddr_cmp(&p->addr, p->addrlen,
243369938Sgit2svn		  	&pend->addr, pend->addrlen) == 0) &&
244369938Sgit2svn		  (*entry = find_match(p->match, pend->pkt, pend->pkt_len,
245369938Sgit2svn		 	 pend->transport))) {
246369938Sgit2svn			log_info("matched query time %d in range [%d, %d] "
247369938Sgit2svn				"with entry line %d", timenow,
248369938Sgit2svn				p->start_step, p->end_step, (*entry)->lineno);
249369938Sgit2svn			if(p->addrlen != 0)
250369938Sgit2svn				log_addr(0, "matched ip", &p->addr, p->addrlen);
251369938Sgit2svn			log_pkt("matched pkt: ",
252369938Sgit2svn				(*entry)->reply_list->reply_pkt,
253369938Sgit2svn				(*entry)->reply_list->reply_len);
254369938Sgit2svn			return 1;
255369938Sgit2svn		}
256369938Sgit2svn		p = p->next_range;
257369938Sgit2svn	}
258369938Sgit2svn	return 0;
259369938Sgit2svn}
260369938Sgit2svn
261369938Sgit2svn/**
262369938Sgit2svn * See if outgoing pending query matches an entry.
263369938Sgit2svn * @param runtime: runtime.
264369938Sgit2svn * @param entry: if true, the entry that matches is returned.
265369938Sgit2svn * @param pend: if true, the outgoing message that matches is returned.
266369938Sgit2svn * @return: true if pending query matches the now event.
267369938Sgit2svn */
268369938Sgit2svnstatic int
269369938Sgit2svnpending_matches_range(struct replay_runtime* runtime,
270369938Sgit2svn	struct entry** entry, struct fake_pending** pend)
271369938Sgit2svn{
272369938Sgit2svn	struct fake_pending* p = runtime->pending_list;
273369938Sgit2svn	/* slow, O(N*N), but it works as advertised with weird matching */
274369938Sgit2svn	while(p) {
275369938Sgit2svn		if(p->tcp_pkt_counter != 0) {
276369938Sgit2svn			/* continue tcp transfer */
277369938Sgit2svn			*pend = p;
278369938Sgit2svn			return 1;
279369938Sgit2svn		}
280369938Sgit2svn		if(pending_find_match(runtime, entry, p)) {
281369938Sgit2svn			*pend = p;
282369938Sgit2svn			return 1;
283369938Sgit2svn		}
284369938Sgit2svn		p = p->next;
285369938Sgit2svn	}
286369938Sgit2svn	return 0;
287369938Sgit2svn}
288369938Sgit2svn
289369938Sgit2svn/**
290369938Sgit2svn * Remove the item from the pending list.
291369938Sgit2svn */
292369938Sgit2svnstatic void
293369938Sgit2svnpending_list_delete(struct replay_runtime* runtime, struct fake_pending* pend)
294369938Sgit2svn{
295369938Sgit2svn	struct fake_pending** prev = &runtime->pending_list;
296369938Sgit2svn	struct fake_pending* p = runtime->pending_list;
297369938Sgit2svn
298369938Sgit2svn	while(p) {
299369938Sgit2svn		if(p == pend) {
300369938Sgit2svn			*prev = p->next;
301369938Sgit2svn			delete_fake_pending(pend);
302369938Sgit2svn			return;
303369938Sgit2svn		}
304369938Sgit2svn
305369938Sgit2svn		prev = &p->next;
306369938Sgit2svn		p = p->next;
307369938Sgit2svn	}
308369938Sgit2svn}
309369938Sgit2svn
310369938Sgit2svn/** number of replies in entry */
311369938Sgit2svnstatic int
312369938Sgit2svncount_reply_packets(struct entry* entry)
313369938Sgit2svn{
314369938Sgit2svn	int count = 0;
315369938Sgit2svn	struct reply_packet* reppkt = entry->reply_list;
316369938Sgit2svn	while(reppkt) {
317369938Sgit2svn		count++;
318369938Sgit2svn		reppkt = reppkt->next;
319369938Sgit2svn	}
320369938Sgit2svn	return count;
321369938Sgit2svn}
322369938Sgit2svn
323369938Sgit2svn/**
324369938Sgit2svn * Fill buffer with reply from the entry.
325369938Sgit2svn */
326369938Sgit2svnstatic void
327369938Sgit2svnfill_buffer_with_reply(sldns_buffer* buffer, struct entry* entry, uint8_t* q,
328369938Sgit2svn	size_t qlen, int tcp_pkt_counter)
329369938Sgit2svn{
330369938Sgit2svn	struct reply_packet* reppkt;
331369938Sgit2svn	uint8_t* c;
332369938Sgit2svn	size_t clen;
333369938Sgit2svn	log_assert(entry && entry->reply_list);
334369938Sgit2svn	sldns_buffer_clear(buffer);
335369938Sgit2svn	reppkt = entry->reply_list;
336369938Sgit2svn	if(tcp_pkt_counter > 0) {
337369938Sgit2svn		int i = tcp_pkt_counter;
338369938Sgit2svn		while(reppkt && i--)
339369938Sgit2svn			reppkt = reppkt->next;
340369938Sgit2svn		if(!reppkt) fatal_exit("extra packet read from TCP stream but none is available");
341369938Sgit2svn		log_pkt("extra_packet ", reppkt->reply_pkt, reppkt->reply_len);
342369938Sgit2svn	}
343369938Sgit2svn	if(reppkt->reply_from_hex) {
344369938Sgit2svn		c = sldns_buffer_begin(reppkt->reply_from_hex);
345369938Sgit2svn		clen = sldns_buffer_limit(reppkt->reply_from_hex);
346369938Sgit2svn		if(!c) fatal_exit("out of memory");
347369938Sgit2svn	} else {
348369938Sgit2svn		c = reppkt->reply_pkt;
349369938Sgit2svn		clen = reppkt->reply_len;
350369938Sgit2svn	}
351369938Sgit2svn	if(c) {
352369938Sgit2svn		if(q) adjust_packet(entry, &c, &clen, q, qlen);
353369938Sgit2svn		sldns_buffer_write(buffer, c, clen);
354369938Sgit2svn		if(q) free(c);
355369938Sgit2svn	}
356369938Sgit2svn	sldns_buffer_flip(buffer);
357369938Sgit2svn}
358369938Sgit2svn
359369938Sgit2svn/**
360369938Sgit2svn * Perform range entry on pending message.
361369938Sgit2svn * @param runtime: runtime buffer size preference.
362369938Sgit2svn * @param entry: entry that codes for the reply to do.
363369938Sgit2svn * @param pend: pending query that is answered, callback called.
364369938Sgit2svn */
365369938Sgit2svnstatic void
366369938Sgit2svnanswer_callback_from_entry(struct replay_runtime* runtime,
367369938Sgit2svn        struct entry* entry, struct fake_pending* pend)
368369938Sgit2svn{
369369938Sgit2svn	struct comm_point c;
370369938Sgit2svn	struct comm_reply repinfo;
371369938Sgit2svn	void* cb_arg = pend->cb_arg;
372369938Sgit2svn	comm_point_callback_type* cb = pend->callback;
373369938Sgit2svn
374369938Sgit2svn	memset(&c, 0, sizeof(c));
375369938Sgit2svn	c.fd = -1;
376369938Sgit2svn	c.buffer = sldns_buffer_new(runtime->bufsize);
377369938Sgit2svn	c.type = comm_udp;
378369938Sgit2svn	if(pend->transport == transport_tcp) {
379369938Sgit2svn		c.type = comm_tcp;
380369938Sgit2svn		c.tcp_timeout_msec = 30000;
381369938Sgit2svn		c.tcp_keepalive = runtime->tcp_seen_keepalive;
382369938Sgit2svn	}
383369938Sgit2svn	fill_buffer_with_reply(c.buffer, entry, pend->pkt, pend->pkt_len,
384369938Sgit2svn		pend->tcp_pkt_counter);
385369938Sgit2svn	repinfo.c = &c;
386369938Sgit2svn	repinfo.addrlen = pend->addrlen;
387369938Sgit2svn	memcpy(&repinfo.addr, &pend->addr, pend->addrlen);
388369938Sgit2svn	if(!pend->serviced) {
389369938Sgit2svn		if(entry && entry->reply_list->next &&
390369938Sgit2svn			pend->tcp_pkt_counter < count_reply_packets(entry)) {
391369938Sgit2svn			/* go to next packet next time */
392369938Sgit2svn			pend->tcp_pkt_counter++;
393369938Sgit2svn		} else {
394369938Sgit2svn			pending_list_delete(runtime, pend);
395369938Sgit2svn		}
396369938Sgit2svn	}
397369938Sgit2svn	if((*cb)(&c, cb_arg, NETEVENT_NOERROR, &repinfo)) {
398369938Sgit2svn		fatal_exit("testbound: unexpected: callback returned 1");
399369938Sgit2svn	}
400369938Sgit2svn	sldns_buffer_free(c.buffer);
401369938Sgit2svn}
402369938Sgit2svn
403369938Sgit2svn/** Check the now moment answer check event */
404369938Sgit2svnstatic void
405369938Sgit2svnanswer_check_it(struct replay_runtime* runtime)
406369938Sgit2svn{
407369938Sgit2svn	struct replay_answer* ans = runtime->answer_list,
408369938Sgit2svn		*prev = NULL;
409369938Sgit2svn	log_assert(runtime && runtime->now &&
410369938Sgit2svn		runtime->now->evt_type == repevt_front_reply);
411369938Sgit2svn	while(ans) {
412369938Sgit2svn		enum transport_type tr = transport_tcp;
413369938Sgit2svn		if(ans->repinfo.c->type == comm_udp)
414369938Sgit2svn			tr = transport_udp;
415369938Sgit2svn		if((runtime->now->addrlen == 0 || sockaddr_cmp(
416369938Sgit2svn			&runtime->now->addr, runtime->now->addrlen,
417369938Sgit2svn			&ans->repinfo.addr, ans->repinfo.addrlen) == 0) &&
418369938Sgit2svn			find_match(runtime->now->match, ans->pkt,
419369938Sgit2svn				ans->pkt_len, tr)) {
420369938Sgit2svn			log_info("testbound matched event entry from line %d",
421369938Sgit2svn				runtime->now->match->lineno);
422369938Sgit2svn			log_info("testbound: do STEP %d %s",
423369938Sgit2svn				runtime->now->time_step,
424369938Sgit2svn				repevt_string(runtime->now->evt_type));
425369938Sgit2svn			if(prev)
426369938Sgit2svn				prev->next = ans->next;
427369938Sgit2svn			else 	runtime->answer_list = ans->next;
428369938Sgit2svn			if(!ans->next)
429369938Sgit2svn				runtime->answer_last = prev;
430369938Sgit2svn			if(ans->repinfo.c->tcp_keepalive)
431369938Sgit2svn				runtime->tcp_seen_keepalive = 1;
432369938Sgit2svn			delete_replay_answer(ans);
433369938Sgit2svn			return;
434369938Sgit2svn		} else {
435369938Sgit2svn			prev = ans;
436369938Sgit2svn			ans = ans->next;
437369938Sgit2svn		}
438369938Sgit2svn	}
439369938Sgit2svn	log_info("testbound: do STEP %d %s", runtime->now->time_step,
440369938Sgit2svn		repevt_string(runtime->now->evt_type));
441369938Sgit2svn	fatal_exit("testbound: not matched");
442369938Sgit2svn}
443369938Sgit2svn
444369938Sgit2svn/**
445369938Sgit2svn * Create commpoint (as return address) for a fake incoming query.
446369938Sgit2svn */
447369938Sgit2svnstatic void
448369938Sgit2svnfake_front_query(struct replay_runtime* runtime, struct replay_moment *todo)
449369938Sgit2svn{
450369938Sgit2svn	struct comm_reply repinfo;
451369938Sgit2svn	memset(&repinfo, 0, sizeof(repinfo));
452369938Sgit2svn	repinfo.c = (struct comm_point*)calloc(1, sizeof(struct comm_point));
453369938Sgit2svn	repinfo.addrlen = (socklen_t)sizeof(struct sockaddr_in);
454369938Sgit2svn	if(todo->addrlen != 0) {
455369938Sgit2svn		repinfo.addrlen = todo->addrlen;
456369938Sgit2svn		memcpy(&repinfo.addr, &todo->addr, todo->addrlen);
457369938Sgit2svn	}
458369938Sgit2svn	repinfo.c->fd = -1;
459369938Sgit2svn	repinfo.c->ev = (struct internal_event*)runtime;
460369938Sgit2svn	repinfo.c->buffer = sldns_buffer_new(runtime->bufsize);
461369938Sgit2svn	if(todo->match->match_transport == transport_tcp) {
462369938Sgit2svn		repinfo.c->type = comm_tcp;
463369938Sgit2svn		repinfo.c->tcp_timeout_msec = 30000;
464369938Sgit2svn		repinfo.c->tcp_keepalive = runtime->tcp_seen_keepalive;
465369938Sgit2svn	} else
466369938Sgit2svn		repinfo.c->type = comm_udp;
467369938Sgit2svn	fill_buffer_with_reply(repinfo.c->buffer, todo->match, NULL, 0, 0);
468369938Sgit2svn	log_info("testbound: incoming QUERY");
469369938Sgit2svn	log_pkt("query pkt", todo->match->reply_list->reply_pkt,
470369938Sgit2svn		todo->match->reply_list->reply_len);
471369938Sgit2svn	/* call the callback for incoming queries */
472369938Sgit2svn	if((*runtime->callback_query)(repinfo.c, runtime->cb_arg,
473369938Sgit2svn		NETEVENT_NOERROR, &repinfo)) {
474369938Sgit2svn		/* send immediate reply */
475369938Sgit2svn		comm_point_send_reply(&repinfo);
476369938Sgit2svn	}
477369938Sgit2svn	/* clear it again, in case copy not done properly */
478369938Sgit2svn	memset(&repinfo, 0, sizeof(repinfo));
479369938Sgit2svn}
480369938Sgit2svn
481369938Sgit2svn/**
482369938Sgit2svn * Perform callback for fake pending message.
483369938Sgit2svn */
484369938Sgit2svnstatic void
485369938Sgit2svnfake_pending_callback(struct replay_runtime* runtime,
486369938Sgit2svn	struct replay_moment* todo, int error)
487369938Sgit2svn{
488369938Sgit2svn	struct fake_pending* p = runtime->pending_list;
489369938Sgit2svn	struct comm_reply repinfo;
490369938Sgit2svn	struct comm_point c;
491369938Sgit2svn	void* cb_arg;
492369938Sgit2svn	comm_point_callback_type* cb;
493369938Sgit2svn
494369938Sgit2svn	memset(&c, 0, sizeof(c));
495369938Sgit2svn	if(!p) fatal_exit("No pending queries.");
496369938Sgit2svn	cb_arg = p->cb_arg;
497369938Sgit2svn	cb = p->callback;
498369938Sgit2svn	c.buffer = sldns_buffer_new(runtime->bufsize);
499369938Sgit2svn	c.type = comm_udp;
500369938Sgit2svn	if(p->transport == transport_tcp) {
501369938Sgit2svn		c.type = comm_tcp;
502369938Sgit2svn		c.tcp_timeout_msec = 30000;
503369938Sgit2svn		c.tcp_keepalive = runtime->tcp_seen_keepalive;
504369938Sgit2svn	}
505369938Sgit2svn	if(todo->evt_type == repevt_back_reply && todo->match) {
506369938Sgit2svn		fill_buffer_with_reply(c.buffer, todo->match, p->pkt,
507369938Sgit2svn			p->pkt_len, p->tcp_pkt_counter);
508369938Sgit2svn	}
509369938Sgit2svn	repinfo.c = &c;
510369938Sgit2svn	repinfo.addrlen = p->addrlen;
511369938Sgit2svn	memcpy(&repinfo.addr, &p->addr, p->addrlen);
512369938Sgit2svn	if(!p->serviced) {
513369938Sgit2svn		if(todo->match && todo->match->reply_list->next && !error &&
514369938Sgit2svn			p->tcp_pkt_counter < count_reply_packets(todo->match)) {
515369938Sgit2svn			/* go to next packet next time */
516369938Sgit2svn			p->tcp_pkt_counter++;
517369938Sgit2svn		} else {
518369938Sgit2svn			pending_list_delete(runtime, p);
519369938Sgit2svn		}
520369938Sgit2svn	}
521369938Sgit2svn	if((*cb)(&c, cb_arg, error, &repinfo)) {
522369938Sgit2svn		fatal_exit("unexpected: pending callback returned 1");
523369938Sgit2svn	}
524369938Sgit2svn	/* delete the pending item. */
525369938Sgit2svn	sldns_buffer_free(c.buffer);
526369938Sgit2svn}
527369938Sgit2svn
528369938Sgit2svn/** pass time */
529369938Sgit2svnstatic void
530369938Sgit2svnmoment_assign(struct replay_runtime* runtime, struct replay_moment* mom)
531369938Sgit2svn{
532369938Sgit2svn	char* value = macro_process(runtime->vars, runtime, mom->string);
533369938Sgit2svn	if(!value)
534369938Sgit2svn		fatal_exit("could not process macro step %d", mom->time_step);
535369938Sgit2svn	log_info("assign %s = %s", mom->variable, value);
536369938Sgit2svn	if(!macro_assign(runtime->vars, mom->variable, value))
537369938Sgit2svn		fatal_exit("out of memory storing macro");
538369938Sgit2svn	free(value);
539369938Sgit2svn	if(verbosity >= VERB_ALGO)
540369938Sgit2svn		macro_print_debug(runtime->vars);
541369938Sgit2svn}
542369938Sgit2svn
543369938Sgit2svn/** pass time */
544369938Sgit2svnstatic void
545369938Sgit2svntime_passes(struct replay_runtime* runtime, struct replay_moment* mom)
546369938Sgit2svn{
547369938Sgit2svn	struct fake_timer *t;
548369938Sgit2svn	struct timeval tv = mom->elapse;
549369938Sgit2svn	if(mom->string) {
550369938Sgit2svn		char* xp = macro_process(runtime->vars, runtime, mom->string);
551369938Sgit2svn		double sec;
552369938Sgit2svn		if(!xp) fatal_exit("could not macro expand %s", mom->string);
553369938Sgit2svn		verbose(VERB_ALGO, "EVAL %s", mom->string);
554369938Sgit2svn		sec = atof(xp);
555369938Sgit2svn		free(xp);
556369938Sgit2svn#ifndef S_SPLINT_S
557369938Sgit2svn		tv.tv_sec = sec;
558369938Sgit2svn		tv.tv_usec = (int)((sec - (double)tv.tv_sec) *1000000. + 0.5);
559369938Sgit2svn#endif
560369938Sgit2svn	}
561369938Sgit2svn	timeval_add(&runtime->now_tv, &tv);
562369938Sgit2svn	runtime->now_secs = (time_t)runtime->now_tv.tv_sec;
563369938Sgit2svn#ifndef S_SPLINT_S
564369938Sgit2svn	log_info("elapsed %d.%6.6d  now %d.%6.6d",
565369938Sgit2svn		(int)tv.tv_sec, (int)tv.tv_usec,
566369938Sgit2svn		(int)runtime->now_tv.tv_sec, (int)runtime->now_tv.tv_usec);
567369938Sgit2svn#endif
568369938Sgit2svn	/* see if any timers have fired; and run them */
569369938Sgit2svn	while( (t=replay_get_oldest_timer(runtime)) ) {
570369938Sgit2svn		t->enabled = 0;
571369938Sgit2svn		log_info("fake_timer callback");
572369938Sgit2svn		fptr_ok(fptr_whitelist_comm_timer(t->cb));
573369938Sgit2svn		(*t->cb)(t->cb_arg);
574369938Sgit2svn	}
575369938Sgit2svn}
576369938Sgit2svn
577369938Sgit2svn/** check autotrust file contents */
578369938Sgit2svnstatic void
579369938Sgit2svnautotrust_check(struct replay_runtime* runtime, struct replay_moment* mom)
580369938Sgit2svn{
581369938Sgit2svn	char name[1024], line[1024];
582369938Sgit2svn	FILE *in;
583369938Sgit2svn	int lineno = 0, oke=1;
584369938Sgit2svn	char* expanded;
585369938Sgit2svn	struct config_strlist* p;
586369938Sgit2svn	line[sizeof(line)-1] = 0;
587369938Sgit2svn	log_assert(mom->autotrust_id);
588369938Sgit2svn	fake_temp_file("_auto_", mom->autotrust_id, name, sizeof(name));
589369938Sgit2svn	in = fopen(name, "r");
590369938Sgit2svn	if(!in) fatal_exit("could not open %s: %s", name, strerror(errno));
591369938Sgit2svn	for(p=mom->file_content; p; p=p->next) {
592369938Sgit2svn		lineno++;
593369938Sgit2svn		if(!fgets(line, (int)sizeof(line)-1, in)) {
594369938Sgit2svn			log_err("autotrust check failed, could not read line");
595369938Sgit2svn			log_err("file %s, line %d", name, lineno);
596369938Sgit2svn			log_err("should be: %s", p->str);
597369938Sgit2svn			fatal_exit("autotrust_check failed");
598369938Sgit2svn		}
599369938Sgit2svn		if(line[0]) line[strlen(line)-1] = 0; /* remove newline */
600369938Sgit2svn		expanded = macro_process(runtime->vars, runtime, p->str);
601369938Sgit2svn		if(!expanded)
602369938Sgit2svn			fatal_exit("could not expand macro line %d", lineno);
603369938Sgit2svn		if(verbosity >= 7 && strcmp(p->str, expanded) != 0)
604369938Sgit2svn			log_info("expanded '%s' to '%s'", p->str, expanded);
605369938Sgit2svn		if(strcmp(expanded, line) != 0) {
606369938Sgit2svn			log_err("mismatch in file %s, line %d", name, lineno);
607369938Sgit2svn			log_err("file has : %s", line);
608369938Sgit2svn			log_err("should be: %s", expanded);
609369938Sgit2svn			free(expanded);
610369938Sgit2svn			oke = 0;
611369938Sgit2svn			continue;
612369938Sgit2svn		}
613369938Sgit2svn		free(expanded);
614369938Sgit2svn		fprintf(stderr, "%s:%2d ok : %s\n", name, lineno, line);
615369938Sgit2svn	}
616369938Sgit2svn	if(fgets(line, (int)sizeof(line)-1, in)) {
617369938Sgit2svn		log_err("autotrust check failed, extra lines in %s after %d",
618369938Sgit2svn			name, lineno);
619369938Sgit2svn		do {
620369938Sgit2svn			fprintf(stderr, "file has: %s", line);
621369938Sgit2svn		} while(fgets(line, (int)sizeof(line)-1, in));
622369938Sgit2svn		oke = 0;
623369938Sgit2svn	}
624369938Sgit2svn	fclose(in);
625369938Sgit2svn	if(!oke)
626369938Sgit2svn		fatal_exit("autotrust_check STEP %d failed", mom->time_step);
627369938Sgit2svn	log_info("autotrust %s is OK", mom->autotrust_id);
628369938Sgit2svn}
629369938Sgit2svn
630369938Sgit2svn/** check tempfile file contents */
631369938Sgit2svnstatic void
632369938Sgit2svntempfile_check(struct replay_runtime* runtime, struct replay_moment* mom)
633369938Sgit2svn{
634369938Sgit2svn	char name[1024], line[1024];
635369938Sgit2svn	FILE *in;
636369938Sgit2svn	int lineno = 0, oke=1;
637369938Sgit2svn	char* expanded;
638369938Sgit2svn	struct config_strlist* p;
639369938Sgit2svn	line[sizeof(line)-1] = 0;
640369938Sgit2svn	log_assert(mom->autotrust_id);
641369938Sgit2svn	fake_temp_file("_temp_", mom->autotrust_id, name, sizeof(name));
642369938Sgit2svn	in = fopen(name, "r");
643369938Sgit2svn	if(!in) fatal_exit("could not open %s: %s", name, strerror(errno));
644369938Sgit2svn	for(p=mom->file_content; p; p=p->next) {
645369938Sgit2svn		lineno++;
646369938Sgit2svn		if(!fgets(line, (int)sizeof(line)-1, in)) {
647369938Sgit2svn			log_err("tempfile check failed, could not read line");
648369938Sgit2svn			log_err("file %s, line %d", name, lineno);
649369938Sgit2svn			log_err("should be: %s", p->str);
650369938Sgit2svn			fatal_exit("tempfile_check failed");
651369938Sgit2svn		}
652369938Sgit2svn		if(line[0]) line[strlen(line)-1] = 0; /* remove newline */
653369938Sgit2svn		expanded = macro_process(runtime->vars, runtime, p->str);
654369938Sgit2svn		if(!expanded)
655369938Sgit2svn			fatal_exit("could not expand macro line %d", lineno);
656369938Sgit2svn		if(verbosity >= 7 && strcmp(p->str, expanded) != 0)
657369938Sgit2svn			log_info("expanded '%s' to '%s'", p->str, expanded);
658369938Sgit2svn		if(strcmp(expanded, line) != 0) {
659369938Sgit2svn			log_err("mismatch in file %s, line %d", name, lineno);
660369938Sgit2svn			log_err("file has : %s", line);
661369938Sgit2svn			log_err("should be: %s", expanded);
662369938Sgit2svn			free(expanded);
663369938Sgit2svn			oke = 0;
664369938Sgit2svn			continue;
665369938Sgit2svn		}
666369938Sgit2svn		free(expanded);
667369938Sgit2svn		fprintf(stderr, "%s:%2d ok : %s\n", name, lineno, line);
668369938Sgit2svn	}
669369938Sgit2svn	if(fgets(line, (int)sizeof(line)-1, in)) {
670369938Sgit2svn		log_err("tempfile check failed, extra lines in %s after %d",
671369938Sgit2svn			name, lineno);
672369938Sgit2svn		do {
673369938Sgit2svn			fprintf(stderr, "file has: %s", line);
674369938Sgit2svn		} while(fgets(line, (int)sizeof(line)-1, in));
675369938Sgit2svn		oke = 0;
676369938Sgit2svn	}
677369938Sgit2svn	fclose(in);
678369938Sgit2svn	if(!oke)
679369938Sgit2svn		fatal_exit("tempfile_check STEP %d failed", mom->time_step);
680369938Sgit2svn	log_info("tempfile %s is OK", mom->autotrust_id);
681369938Sgit2svn}
682369938Sgit2svn
683369938Sgit2svn/** Store RTT in infra cache */
684369938Sgit2svnstatic void
685369938Sgit2svndo_infra_rtt(struct replay_runtime* runtime)
686369938Sgit2svn{
687369938Sgit2svn	struct replay_moment* now = runtime->now;
688369938Sgit2svn	int rto;
689369938Sgit2svn	size_t dplen = 0;
690369938Sgit2svn	uint8_t* dp = sldns_str2wire_dname(now->variable, &dplen);
691369938Sgit2svn	if(!dp) fatal_exit("cannot parse %s", now->variable);
692369938Sgit2svn	rto = infra_rtt_update(runtime->infra, &now->addr, now->addrlen,
693369938Sgit2svn		dp, dplen, LDNS_RR_TYPE_A, atoi(now->string),
694369938Sgit2svn		-1, runtime->now_secs);
695369938Sgit2svn	log_addr(0, "INFRA_RTT for", &now->addr, now->addrlen);
696369938Sgit2svn	log_info("INFRA_RTT(%s roundtrip %d): rto of %d", now->variable,
697369938Sgit2svn		atoi(now->string), rto);
698369938Sgit2svn	if(rto == 0) fatal_exit("infra_rtt_update failed");
699369938Sgit2svn	free(dp);
700369938Sgit2svn}
701369938Sgit2svn
702369938Sgit2svn/** perform exponential backoff on the timeout */
703369938Sgit2svnstatic void
704369938Sgit2svnexpon_timeout_backoff(struct replay_runtime* runtime)
705369938Sgit2svn{
706369938Sgit2svn	struct fake_pending* p = runtime->pending_list;
707369938Sgit2svn	int rtt, vs;
708369938Sgit2svn	uint8_t edns_lame_known;
709369938Sgit2svn	int last_rtt, rto;
710369938Sgit2svn	if(!p) return; /* no pending packet to backoff */
711369938Sgit2svn	if(!infra_host(runtime->infra, &p->addr, p->addrlen, p->zone,
712369938Sgit2svn		p->zonelen, runtime->now_secs, &vs, &edns_lame_known, &rtt))
713369938Sgit2svn		return;
714369938Sgit2svn	last_rtt = rtt;
715369938Sgit2svn	rto = infra_rtt_update(runtime->infra, &p->addr, p->addrlen, p->zone,
716369938Sgit2svn		p->zonelen, p->qtype, -1, last_rtt, runtime->now_secs);
717369938Sgit2svn	log_info("infra_rtt_update returned rto %d", rto);
718369938Sgit2svn}
719369938Sgit2svn
720369938Sgit2svn/**
721369938Sgit2svn * Advance to the next moment.
722369938Sgit2svn */
723369938Sgit2svnstatic void
724369938Sgit2svnadvance_moment(struct replay_runtime* runtime)
725369938Sgit2svn{
726369938Sgit2svn	if(!runtime->now)
727369938Sgit2svn		runtime->now = runtime->scenario->mom_first;
728369938Sgit2svn	else 	runtime->now = runtime->now->mom_next;
729369938Sgit2svn}
730369938Sgit2svn
731369938Sgit2svn/**
732369938Sgit2svn * Perform actions or checks determined by the moment.
733369938Sgit2svn * Also advances the time by one step.
734369938Sgit2svn * @param runtime: scenario runtime information.
735369938Sgit2svn */
736369938Sgit2svnstatic void
737369938Sgit2svndo_moment_and_advance(struct replay_runtime* runtime)
738369938Sgit2svn{
739369938Sgit2svn	struct replay_moment* mom;
740369938Sgit2svn	if(!runtime->now) {
741369938Sgit2svn		advance_moment(runtime);
742369938Sgit2svn		return;
743369938Sgit2svn	}
744369938Sgit2svn	log_info("testbound: do STEP %d %s", runtime->now->time_step,
745369938Sgit2svn		repevt_string(runtime->now->evt_type));
746369938Sgit2svn	switch(runtime->now->evt_type) {
747369938Sgit2svn	case repevt_nothing:
748369938Sgit2svn		advance_moment(runtime);
749369938Sgit2svn		break;
750369938Sgit2svn	case repevt_front_query:
751369938Sgit2svn		/* advance moment before doing the step, so that the next
752369938Sgit2svn		   moment which may check some result of the mom step
753369938Sgit2svn		   can catch those results. */
754369938Sgit2svn		mom = runtime->now;
755369938Sgit2svn		advance_moment(runtime);
756369938Sgit2svn		fake_front_query(runtime, mom);
757369938Sgit2svn		break;
758369938Sgit2svn	case repevt_front_reply:
759369938Sgit2svn		if(runtime->answer_list)
760369938Sgit2svn			log_err("testbound: There are unmatched answers.");
761369938Sgit2svn		fatal_exit("testbound: query answer not matched");
762369938Sgit2svn		break;
763369938Sgit2svn	case repevt_timeout:
764369938Sgit2svn		mom = runtime->now;
765369938Sgit2svn		advance_moment(runtime);
766369938Sgit2svn		expon_timeout_backoff(runtime);
767369938Sgit2svn		fake_pending_callback(runtime, mom, NETEVENT_TIMEOUT);
768369938Sgit2svn		break;
769369938Sgit2svn	case repevt_back_reply:
770369938Sgit2svn		mom = runtime->now;
771369938Sgit2svn		advance_moment(runtime);
772369938Sgit2svn		fake_pending_callback(runtime, mom, NETEVENT_NOERROR);
773369938Sgit2svn		break;
774369938Sgit2svn	case repevt_back_query:
775369938Sgit2svn		/* Back queries are matched when they are sent out. */
776369938Sgit2svn		log_err("No query matching the current moment was sent.");
777369938Sgit2svn		fatal_exit("testbound: back query not matched");
778369938Sgit2svn		break;
779369938Sgit2svn	case repevt_error:
780369938Sgit2svn		mom = runtime->now;
781369938Sgit2svn		advance_moment(runtime);
782369938Sgit2svn		fake_pending_callback(runtime, mom, NETEVENT_CLOSED);
783369938Sgit2svn		break;
784369938Sgit2svn	case repevt_time_passes:
785369938Sgit2svn		time_passes(runtime, runtime->now);
786369938Sgit2svn		advance_moment(runtime);
787369938Sgit2svn		break;
788369938Sgit2svn	case repevt_autotrust_check:
789369938Sgit2svn		autotrust_check(runtime, runtime->now);
790369938Sgit2svn		advance_moment(runtime);
791369938Sgit2svn		break;
792369938Sgit2svn	case repevt_tempfile_check:
793369938Sgit2svn		tempfile_check(runtime, runtime->now);
794369938Sgit2svn		advance_moment(runtime);
795369938Sgit2svn		break;
796369938Sgit2svn	case repevt_assign:
797369938Sgit2svn		moment_assign(runtime, runtime->now);
798369938Sgit2svn		advance_moment(runtime);
799369938Sgit2svn		break;
800369938Sgit2svn	case repevt_traffic:
801369938Sgit2svn		advance_moment(runtime);
802369938Sgit2svn		break;
803369938Sgit2svn	case repevt_infra_rtt:
804369938Sgit2svn		do_infra_rtt(runtime);
805369938Sgit2svn		advance_moment(runtime);
806369938Sgit2svn		break;
807369938Sgit2svn	default:
808369938Sgit2svn		fatal_exit("testbound: unknown event type %d",
809369938Sgit2svn			runtime->now->evt_type);
810369938Sgit2svn	}
811369938Sgit2svn}
812369938Sgit2svn
813369938Sgit2svn/** run the scenario in event callbacks */
814369938Sgit2svnstatic void
815369938Sgit2svnrun_scenario(struct replay_runtime* runtime)
816369938Sgit2svn{
817369938Sgit2svn	struct entry* entry = NULL;
818369938Sgit2svn	struct fake_pending* pending = NULL;
819369938Sgit2svn	int max_rounds = 5000;
820369938Sgit2svn	int rounds = 0;
821369938Sgit2svn	runtime->now = runtime->scenario->mom_first;
822369938Sgit2svn	log_info("testbound: entering fake runloop");
823369938Sgit2svn	do {
824369938Sgit2svn		/* if moment matches pending query do it. */
825369938Sgit2svn		/* else if moment matches given answer, do it */
826369938Sgit2svn		/* else if precoded_range matches pending, do it */
827369938Sgit2svn		/* else do the current moment */
828369938Sgit2svn		if(pending_matches_current(runtime, &entry, &pending)) {
829369938Sgit2svn			log_info("testbound: do STEP %d CHECK_OUT_QUERY",
830369938Sgit2svn				runtime->now->time_step);
831369938Sgit2svn			advance_moment(runtime);
832369938Sgit2svn			if(entry->copy_id)
833369938Sgit2svn				answer_callback_from_entry(runtime, entry,
834369938Sgit2svn				pending);
835369938Sgit2svn		} else if(runtime->answer_list && runtime->now &&
836369938Sgit2svn			runtime->now->evt_type == repevt_front_reply) {
837369938Sgit2svn			answer_check_it(runtime);
838369938Sgit2svn			advance_moment(runtime);
839369938Sgit2svn		} else if(pending_matches_range(runtime, &entry, &pending)) {
840369938Sgit2svn			answer_callback_from_entry(runtime, entry, pending);
841369938Sgit2svn		} else {
842369938Sgit2svn			do_moment_and_advance(runtime);
843369938Sgit2svn		}
844369938Sgit2svn		log_info("testbound: end of event stage");
845369938Sgit2svn		rounds++;
846369938Sgit2svn		if(rounds > max_rounds)
847369938Sgit2svn			fatal_exit("testbound: too many rounds, it loops.");
848369938Sgit2svn	} while(runtime->now);
849369938Sgit2svn
850369938Sgit2svn	if(runtime->pending_list) {
851369938Sgit2svn		struct fake_pending* p;
852369938Sgit2svn		log_err("testbound: there are still messages pending.");
853369938Sgit2svn		for(p = runtime->pending_list; p; p=p->next) {
854369938Sgit2svn			log_pkt("pending msg", p->pkt, p->pkt_len);
855369938Sgit2svn			log_addr(0, "pending to", &p->addr, p->addrlen);
856369938Sgit2svn		}
857369938Sgit2svn		fatal_exit("testbound: there are still messages pending.");
858369938Sgit2svn	}
859369938Sgit2svn	if(runtime->answer_list) {
860369938Sgit2svn		fatal_exit("testbound: there are unmatched answers.");
861369938Sgit2svn	}
862369938Sgit2svn	log_info("testbound: exiting fake runloop.");
863369938Sgit2svn	runtime->exit_cleanly = 1;
864369938Sgit2svn}
865369938Sgit2svn
866369938Sgit2svn/*********** Dummy routines ***********/
867369938Sgit2svn
868369938Sgit2svnstruct listen_dnsport*
869369938Sgit2svnlisten_create(struct comm_base* base, struct listen_port* ATTR_UNUSED(ports),
870369938Sgit2svn	size_t bufsize, int ATTR_UNUSED(tcp_accept_count),
871369938Sgit2svn	int ATTR_UNUSED(tcp_idle_timeout),
872369938Sgit2svn	int ATTR_UNUSED(harden_large_queries),
873369938Sgit2svn	uint32_t ATTR_UNUSED(http_max_streams),
874369938Sgit2svn	char* ATTR_UNUSED(http_endpoint),
875369938Sgit2svn	int ATTR_UNUSED(http_notls),
876369938Sgit2svn	struct tcl_list* ATTR_UNUSED(tcp_conn_limit),
877369938Sgit2svn	void* ATTR_UNUSED(sslctx), struct dt_env* ATTR_UNUSED(dtenv),
878369938Sgit2svn	comm_point_callback_type* cb, void *cb_arg)
879369938Sgit2svn{
880369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)base;
881369938Sgit2svn	struct listen_dnsport* l= calloc(1, sizeof(struct listen_dnsport));
882369938Sgit2svn	if(!l)
883369938Sgit2svn		return NULL;
884369938Sgit2svn	l->base = base;
885369938Sgit2svn	l->udp_buff = sldns_buffer_new(bufsize);
886369938Sgit2svn	if(!l->udp_buff) {
887369938Sgit2svn		free(l);
888369938Sgit2svn		return NULL;
889369938Sgit2svn	}
890369938Sgit2svn	runtime->callback_query = cb;
891369938Sgit2svn	runtime->cb_arg = cb_arg;
892369938Sgit2svn	runtime->bufsize = bufsize;
893369938Sgit2svn	return l;
894369938Sgit2svn}
895369938Sgit2svn
896369938Sgit2svnvoid
897369938Sgit2svnlisten_delete(struct listen_dnsport* listen)
898369938Sgit2svn{
899369938Sgit2svn	if(!listen)
900369938Sgit2svn		return;
901369938Sgit2svn	sldns_buffer_free(listen->udp_buff);
902369938Sgit2svn	free(listen);
903369938Sgit2svn}
904369938Sgit2svn
905369938Sgit2svnstruct comm_base*
906369938Sgit2svncomm_base_create(int ATTR_UNUSED(sigs))
907369938Sgit2svn{
908369938Sgit2svn	/* we return the runtime structure instead. */
909369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)
910369938Sgit2svn		calloc(1, sizeof(struct replay_runtime));
911369938Sgit2svn	runtime->scenario = saved_scenario;
912369938Sgit2svn	runtime->vars = macro_store_create();
913369938Sgit2svn	if(!runtime->vars) fatal_exit("out of memory");
914369938Sgit2svn	return (struct comm_base*)runtime;
915369938Sgit2svn}
916369938Sgit2svn
917369938Sgit2svnvoid
918369938Sgit2svncomm_base_delete(struct comm_base* b)
919369938Sgit2svn{
920369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)b;
921369938Sgit2svn	struct fake_pending* p, *np;
922369938Sgit2svn	struct replay_answer* a, *na;
923369938Sgit2svn	struct fake_timer* t, *nt;
924369938Sgit2svn	if(!runtime)
925369938Sgit2svn		return;
926369938Sgit2svn	runtime->scenario= NULL;
927369938Sgit2svn	p = runtime->pending_list;
928369938Sgit2svn	while(p) {
929369938Sgit2svn		np = p->next;
930369938Sgit2svn		delete_fake_pending(p);
931369938Sgit2svn		p = np;
932369938Sgit2svn	}
933369938Sgit2svn	a = runtime->answer_list;
934369938Sgit2svn	while(a) {
935369938Sgit2svn		na = a->next;
936369938Sgit2svn		delete_replay_answer(a);
937369938Sgit2svn		a = na;
938369938Sgit2svn	}
939369938Sgit2svn	t = runtime->timer_list;
940369938Sgit2svn	while(t) {
941369938Sgit2svn		nt = t->next;
942369938Sgit2svn		free(t);
943369938Sgit2svn		t = nt;
944369938Sgit2svn	}
945369938Sgit2svn	macro_store_delete(runtime->vars);
946369938Sgit2svn	free(runtime);
947369938Sgit2svn}
948369938Sgit2svn
949369938Sgit2svnvoid
950369938Sgit2svncomm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv)
951369938Sgit2svn{
952369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)b;
953369938Sgit2svn	*tt = &runtime->now_secs;
954369938Sgit2svn	*tv = &runtime->now_tv;
955369938Sgit2svn}
956369938Sgit2svn
957369938Sgit2svnvoid
958369938Sgit2svncomm_base_dispatch(struct comm_base* b)
959369938Sgit2svn{
960369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)b;
961369938Sgit2svn	run_scenario(runtime);
962369938Sgit2svn	if(runtime->sig_cb)
963369938Sgit2svn		(*runtime->sig_cb)(SIGTERM, runtime->sig_cb_arg);
964369938Sgit2svn	else	exit(0); /* OK exit when LIBEVENT_SIGNAL_PROBLEM exists */
965369938Sgit2svn}
966369938Sgit2svn
967369938Sgit2svnvoid
968369938Sgit2svncomm_base_exit(struct comm_base* b)
969369938Sgit2svn{
970369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)b;
971369938Sgit2svn	if(!runtime->exit_cleanly) {
972369938Sgit2svn		/* some sort of failure */
973369938Sgit2svn		fatal_exit("testbound: comm_base_exit was called.");
974369938Sgit2svn	}
975369938Sgit2svn}
976369938Sgit2svn
977369938Sgit2svnstruct comm_signal*
978369938Sgit2svncomm_signal_create(struct comm_base* base,
979369938Sgit2svn        void (*callback)(int, void*), void* cb_arg)
980369938Sgit2svn{
981369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)base;
982369938Sgit2svn	runtime->sig_cb = callback;
983369938Sgit2svn	runtime->sig_cb_arg = cb_arg;
984369938Sgit2svn	return calloc(1, sizeof(struct comm_signal));
985369938Sgit2svn}
986369938Sgit2svn
987369938Sgit2svnint
988369938Sgit2svncomm_signal_bind(struct comm_signal* ATTR_UNUSED(comsig), int
989369938Sgit2svn	ATTR_UNUSED(sig))
990369938Sgit2svn{
991369938Sgit2svn	return 1;
992369938Sgit2svn}
993369938Sgit2svn
994369938Sgit2svnvoid
995369938Sgit2svncomm_signal_delete(struct comm_signal* comsig)
996369938Sgit2svn{
997369938Sgit2svn	free(comsig);
998369938Sgit2svn}
999369938Sgit2svn
1000369938Sgit2svnvoid
1001369938Sgit2svncomm_point_send_reply(struct comm_reply* repinfo)
1002369938Sgit2svn{
1003369938Sgit2svn	struct replay_answer* ans = (struct replay_answer*)calloc(1,
1004369938Sgit2svn		sizeof(struct replay_answer));
1005369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)repinfo->c->ev;
1006369938Sgit2svn	log_info("testbound: comm_point_send_reply fake");
1007369938Sgit2svn	/* dump it into the todo list */
1008369938Sgit2svn	log_assert(ans);
1009369938Sgit2svn	memcpy(&ans->repinfo, repinfo, sizeof(struct comm_reply));
1010369938Sgit2svn	ans->next = NULL;
1011369938Sgit2svn	if(runtime->answer_last)
1012369938Sgit2svn		runtime->answer_last->next = ans;
1013369938Sgit2svn	else 	runtime->answer_list = ans;
1014369938Sgit2svn	runtime->answer_last = ans;
1015369938Sgit2svn
1016369938Sgit2svn	/* try to parse packet */
1017369938Sgit2svn	ans->pkt = memdup(sldns_buffer_begin(ans->repinfo.c->buffer),
1018369938Sgit2svn		sldns_buffer_limit(ans->repinfo.c->buffer));
1019369938Sgit2svn	ans->pkt_len = sldns_buffer_limit(ans->repinfo.c->buffer);
1020369938Sgit2svn	if(!ans->pkt) fatal_exit("out of memory");
1021369938Sgit2svn	log_pkt("reply pkt: ", ans->pkt, ans->pkt_len);
1022369938Sgit2svn}
1023369938Sgit2svn
1024369938Sgit2svnvoid
1025369938Sgit2svncomm_point_drop_reply(struct comm_reply* repinfo)
1026369938Sgit2svn{
1027369938Sgit2svn	log_info("comm_point_drop_reply fake");
1028369938Sgit2svn	if(repinfo->c) {
1029369938Sgit2svn		sldns_buffer_free(repinfo->c->buffer);
1030369938Sgit2svn		free(repinfo->c);
1031369938Sgit2svn	}
1032369938Sgit2svn}
1033369938Sgit2svn
1034369938Sgit2svnstruct outside_network*
1035369938Sgit2svnoutside_network_create(struct comm_base* base, size_t bufsize,
1036369938Sgit2svn	size_t ATTR_UNUSED(num_ports), char** ATTR_UNUSED(ifs),
1037369938Sgit2svn	int ATTR_UNUSED(num_ifs), int ATTR_UNUSED(do_ip4),
1038369938Sgit2svn	int ATTR_UNUSED(do_ip6), size_t ATTR_UNUSED(num_tcp),
1039369938Sgit2svn	int ATTR_UNUSED(dscp),
1040369938Sgit2svn	struct infra_cache* infra,
1041369938Sgit2svn	struct ub_randstate* ATTR_UNUSED(rnd),
1042369938Sgit2svn	int ATTR_UNUSED(use_caps_for_id), int* ATTR_UNUSED(availports),
1043369938Sgit2svn	int ATTR_UNUSED(numavailports), size_t ATTR_UNUSED(unwanted_threshold),
1044369938Sgit2svn	int ATTR_UNUSED(outgoing_tcp_mss),
1045369938Sgit2svn	void (*unwanted_action)(void*), void* ATTR_UNUSED(unwanted_param),
1046369938Sgit2svn	int ATTR_UNUSED(do_udp), void* ATTR_UNUSED(sslctx),
1047369938Sgit2svn	int ATTR_UNUSED(delayclose), int ATTR_UNUSED(tls_use_sni),
1048369938Sgit2svn	struct dt_env* ATTR_UNUSED(dtenv), int ATTR_UNUSED(udp_connect))
1049369938Sgit2svn{
1050369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)base;
1051369938Sgit2svn	struct outside_network* outnet =  calloc(1,
1052369938Sgit2svn		sizeof(struct outside_network));
1053369938Sgit2svn	(void)unwanted_action;
1054369938Sgit2svn	if(!outnet)
1055369938Sgit2svn		return NULL;
1056369938Sgit2svn	runtime->infra = infra;
1057369938Sgit2svn	outnet->base = base;
1058369938Sgit2svn	outnet->udp_buff = sldns_buffer_new(bufsize);
1059369938Sgit2svn	if(!outnet->udp_buff) {
1060369938Sgit2svn		free(outnet);
1061369938Sgit2svn		return NULL;
1062369938Sgit2svn	}
1063369938Sgit2svn	return outnet;
1064369938Sgit2svn}
1065369938Sgit2svn
1066369938Sgit2svnvoid
1067369938Sgit2svnoutside_network_delete(struct outside_network* outnet)
1068369938Sgit2svn{
1069369938Sgit2svn	if(!outnet)
1070369938Sgit2svn		return;
1071369938Sgit2svn	sldns_buffer_free(outnet->udp_buff);
1072369938Sgit2svn	free(outnet);
1073369938Sgit2svn}
1074369938Sgit2svn
1075369938Sgit2svnvoid
1076369938Sgit2svnoutside_network_quit_prepare(struct outside_network* ATTR_UNUSED(outnet))
1077369938Sgit2svn{
1078369938Sgit2svn}
1079369938Sgit2svn
1080369938Sgit2svnstruct pending*
1081369938Sgit2svnpending_udp_query(struct serviced_query* sq, sldns_buffer* packet,
1082369938Sgit2svn	int timeout, comm_point_callback_type* callback, void* callback_arg)
1083369938Sgit2svn{
1084369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)
1085369938Sgit2svn		sq->outnet->base;
1086369938Sgit2svn	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1087369938Sgit2svn		sizeof(struct fake_pending));
1088369938Sgit2svn	log_assert(pend);
1089369938Sgit2svn	pend->buffer = sldns_buffer_new(sldns_buffer_capacity(packet));
1090369938Sgit2svn	log_assert(pend->buffer);
1091369938Sgit2svn	sldns_buffer_write(pend->buffer, sldns_buffer_begin(packet),
1092369938Sgit2svn		sldns_buffer_limit(packet));
1093369938Sgit2svn	sldns_buffer_flip(pend->buffer);
1094369938Sgit2svn	memcpy(&pend->addr, &sq->addr, sq->addrlen);
1095369938Sgit2svn	pend->addrlen = sq->addrlen;
1096369938Sgit2svn	pend->callback = callback;
1097369938Sgit2svn	pend->cb_arg = callback_arg;
1098369938Sgit2svn	pend->timeout = timeout/1000;
1099369938Sgit2svn	pend->transport = transport_udp;
1100369938Sgit2svn	pend->pkt = NULL;
1101369938Sgit2svn	pend->zone = NULL;
1102369938Sgit2svn	pend->serviced = 0;
1103369938Sgit2svn	pend->runtime = runtime;
1104369938Sgit2svn	pend->pkt_len = sldns_buffer_limit(packet);
1105369938Sgit2svn	pend->pkt = memdup(sldns_buffer_begin(packet), pend->pkt_len);
1106369938Sgit2svn	if(!pend->pkt) fatal_exit("out of memory");
1107369938Sgit2svn	log_pkt("pending udp pkt: ", pend->pkt, pend->pkt_len);
1108369938Sgit2svn
1109369938Sgit2svn	/* see if it matches the current moment */
1110369938Sgit2svn	if(runtime->now && runtime->now->evt_type == repevt_back_query &&
1111369938Sgit2svn		(runtime->now->addrlen == 0 || sockaddr_cmp(
1112369938Sgit2svn			&runtime->now->addr, runtime->now->addrlen,
1113369938Sgit2svn			&pend->addr, pend->addrlen) == 0) &&
1114369938Sgit2svn		find_match(runtime->now->match, pend->pkt, pend->pkt_len,
1115369938Sgit2svn			pend->transport)) {
1116369938Sgit2svn		log_info("testbound: matched pending to event. "
1117369938Sgit2svn			"advance time between events.");
1118369938Sgit2svn		log_info("testbound: do STEP %d %s", runtime->now->time_step,
1119369938Sgit2svn			repevt_string(runtime->now->evt_type));
1120369938Sgit2svn		advance_moment(runtime);
1121369938Sgit2svn		/* still create the pending, because we need it to callback */
1122369938Sgit2svn	}
1123369938Sgit2svn	log_info("testbound: created fake pending");
1124369938Sgit2svn	/* add to list */
1125369938Sgit2svn	pend->next = runtime->pending_list;
1126369938Sgit2svn	runtime->pending_list = pend;
1127369938Sgit2svn	return (struct pending*)pend;
1128369938Sgit2svn}
1129369938Sgit2svn
1130369938Sgit2svnstruct waiting_tcp*
1131369938Sgit2svnpending_tcp_query(struct serviced_query* sq, sldns_buffer* packet,
1132369938Sgit2svn	int timeout, comm_point_callback_type* callback, void* callback_arg)
1133369938Sgit2svn{
1134369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)
1135369938Sgit2svn		sq->outnet->base;
1136369938Sgit2svn	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1137369938Sgit2svn		sizeof(struct fake_pending));
1138369938Sgit2svn	log_assert(pend);
1139369938Sgit2svn	pend->buffer = sldns_buffer_new(sldns_buffer_capacity(packet));
1140369938Sgit2svn	log_assert(pend->buffer);
1141369938Sgit2svn	sldns_buffer_write(pend->buffer, sldns_buffer_begin(packet),
1142369938Sgit2svn		sldns_buffer_limit(packet));
1143369938Sgit2svn	sldns_buffer_flip(pend->buffer);
1144369938Sgit2svn	memcpy(&pend->addr, &sq->addr, sq->addrlen);
1145369938Sgit2svn	pend->addrlen = sq->addrlen;
1146369938Sgit2svn	pend->callback = callback;
1147369938Sgit2svn	pend->cb_arg = callback_arg;
1148369938Sgit2svn	pend->timeout = timeout/1000;
1149369938Sgit2svn	pend->transport = transport_tcp;
1150369938Sgit2svn	pend->pkt = NULL;
1151369938Sgit2svn	pend->zone = NULL;
1152369938Sgit2svn	pend->runtime = runtime;
1153369938Sgit2svn	pend->serviced = 0;
1154369938Sgit2svn	pend->pkt_len = sldns_buffer_limit(packet);
1155369938Sgit2svn	pend->pkt = memdup(sldns_buffer_begin(packet), pend->pkt_len);
1156369938Sgit2svn	if(!pend->pkt) fatal_exit("out of memory");
1157369938Sgit2svn	log_pkt("pending tcp pkt: ", pend->pkt, pend->pkt_len);
1158369938Sgit2svn
1159369938Sgit2svn	/* see if it matches the current moment */
1160369938Sgit2svn	if(runtime->now && runtime->now->evt_type == repevt_back_query &&
1161369938Sgit2svn		(runtime->now->addrlen == 0 || sockaddr_cmp(
1162369938Sgit2svn			&runtime->now->addr, runtime->now->addrlen,
1163369938Sgit2svn			&pend->addr, pend->addrlen) == 0) &&
1164369938Sgit2svn		find_match(runtime->now->match, pend->pkt, pend->pkt_len,
1165369938Sgit2svn			pend->transport)) {
1166369938Sgit2svn		log_info("testbound: matched pending to event. "
1167369938Sgit2svn			"advance time between events.");
1168369938Sgit2svn		log_info("testbound: do STEP %d %s", runtime->now->time_step,
1169369938Sgit2svn			repevt_string(runtime->now->evt_type));
1170369938Sgit2svn		advance_moment(runtime);
1171369938Sgit2svn		/* still create the pending, because we need it to callback */
1172369938Sgit2svn	}
1173369938Sgit2svn	log_info("testbound: created fake pending");
1174369938Sgit2svn	/* add to list */
1175369938Sgit2svn	pend->next = runtime->pending_list;
1176369938Sgit2svn	runtime->pending_list = pend;
1177369938Sgit2svn	return (struct waiting_tcp*)pend;
1178369938Sgit2svn}
1179369938Sgit2svn
1180369938Sgit2svnstruct serviced_query* outnet_serviced_query(struct outside_network* outnet,
1181369938Sgit2svn	struct query_info* qinfo, uint16_t flags, int dnssec,
1182369938Sgit2svn	int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
1183369938Sgit2svn	int ATTR_UNUSED(tcp_upstream), int ATTR_UNUSED(ssl_upstream),
1184369938Sgit2svn	char* ATTR_UNUSED(tls_auth_name), struct sockaddr_storage* addr,
1185369938Sgit2svn	socklen_t addrlen, uint8_t* zone, size_t zonelen,
1186369938Sgit2svn	struct module_qstate* qstate, comm_point_callback_type* callback,
1187369938Sgit2svn	void* callback_arg, sldns_buffer* ATTR_UNUSED(buff),
1188369938Sgit2svn	struct module_env* env)
1189369938Sgit2svn{
1190369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)outnet->base;
1191369938Sgit2svn	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1192369938Sgit2svn		sizeof(struct fake_pending));
1193369938Sgit2svn	char z[256];
1194369938Sgit2svn	log_assert(pend);
1195369938Sgit2svn	log_nametypeclass(VERB_OPS, "pending serviced query",
1196369938Sgit2svn		qinfo->qname, qinfo->qtype, qinfo->qclass);
1197369938Sgit2svn	dname_str(zone, z);
1198369938Sgit2svn	verbose(VERB_OPS, "pending serviced query zone %s flags%s%s%s%s",
1199369938Sgit2svn		z, (flags&BIT_RD)?" RD":"", (flags&BIT_CD)?" CD":"",
1200369938Sgit2svn		(flags&~(BIT_RD|BIT_CD))?" MORE":"", (dnssec)?" DO":"");
1201369938Sgit2svn
1202369938Sgit2svn	/* create packet with EDNS */
1203369938Sgit2svn	pend->buffer = sldns_buffer_new(512);
1204369938Sgit2svn	log_assert(pend->buffer);
1205369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, 0); /* id */
1206369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, flags);
1207369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, 1); /* qdcount */
1208369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, 0); /* ancount */
1209369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, 0); /* nscount */
1210369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, 0); /* arcount */
1211369938Sgit2svn	sldns_buffer_write(pend->buffer, qinfo->qname, qinfo->qname_len);
1212369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, qinfo->qtype);
1213369938Sgit2svn	sldns_buffer_write_u16(pend->buffer, qinfo->qclass);
1214369938Sgit2svn	sldns_buffer_flip(pend->buffer);
1215369938Sgit2svn	if(1) {
1216369938Sgit2svn		struct edns_data edns;
1217369938Sgit2svn		struct edns_string_addr* client_string_addr;
1218369938Sgit2svn		if(!inplace_cb_query_call(env, qinfo, flags, addr, addrlen,
1219369938Sgit2svn			zone, zonelen, qstate, qstate->region)) {
1220369938Sgit2svn			free(pend);
1221369938Sgit2svn			return NULL;
1222369938Sgit2svn		}
1223369938Sgit2svn		/* add edns */
1224369938Sgit2svn		edns.edns_present = 1;
1225369938Sgit2svn		edns.ext_rcode = 0;
1226369938Sgit2svn		edns.edns_version = EDNS_ADVERTISED_VERSION;
1227369938Sgit2svn		edns.udp_size = EDNS_ADVERTISED_SIZE;
1228369938Sgit2svn		edns.bits = 0;
1229369938Sgit2svn		if(dnssec)
1230369938Sgit2svn			edns.bits = EDNS_DO;
1231369938Sgit2svn		if((client_string_addr = edns_string_addr_lookup(
1232369938Sgit2svn			&env->edns_strings->client_strings,
1233369938Sgit2svn			addr, addrlen))) {
1234369938Sgit2svn			edns_opt_list_append(&qstate->edns_opts_back_out,
1235369938Sgit2svn				env->edns_strings->client_string_opcode,
1236369938Sgit2svn				client_string_addr->string_len,
1237369938Sgit2svn				client_string_addr->string, qstate->region);
1238369938Sgit2svn		}
1239369938Sgit2svn		edns.opt_list = qstate->edns_opts_back_out;
1240369938Sgit2svn		attach_edns_record(pend->buffer, &edns);
1241369938Sgit2svn	}
1242369938Sgit2svn	memcpy(&pend->addr, addr, addrlen);
1243369938Sgit2svn	pend->addrlen = addrlen;
1244369938Sgit2svn	pend->zone = memdup(zone, zonelen);
1245369938Sgit2svn	pend->zonelen = zonelen;
1246369938Sgit2svn	pend->qtype = (int)qinfo->qtype;
1247369938Sgit2svn	log_assert(pend->zone);
1248369938Sgit2svn	pend->callback = callback;
1249369938Sgit2svn	pend->cb_arg = callback_arg;
1250369938Sgit2svn	pend->timeout = UDP_AUTH_QUERY_TIMEOUT/1000;
1251369938Sgit2svn	pend->transport = transport_udp; /* pretend UDP */
1252369938Sgit2svn	pend->pkt = NULL;
1253369938Sgit2svn	pend->runtime = runtime;
1254369938Sgit2svn	pend->serviced = 1;
1255369938Sgit2svn	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1256369938Sgit2svn	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1257369938Sgit2svn	if(!pend->pkt) fatal_exit("out of memory");
1258369938Sgit2svn	/*log_pkt("pending serviced query: ", pend->pkt, pend->pkt_len);*/
1259369938Sgit2svn
1260369938Sgit2svn	/* see if it matches the current moment */
1261369938Sgit2svn	if(runtime->now && runtime->now->evt_type == repevt_back_query &&
1262369938Sgit2svn		(runtime->now->addrlen == 0 || sockaddr_cmp(
1263369938Sgit2svn			&runtime->now->addr, runtime->now->addrlen,
1264369938Sgit2svn			&pend->addr, pend->addrlen) == 0) &&
1265369938Sgit2svn		find_match(runtime->now->match, pend->pkt, pend->pkt_len,
1266369938Sgit2svn			pend->transport)) {
1267369938Sgit2svn		log_info("testbound: matched pending to event. "
1268369938Sgit2svn			"advance time between events.");
1269369938Sgit2svn		log_info("testbound: do STEP %d %s", runtime->now->time_step,
1270369938Sgit2svn			repevt_string(runtime->now->evt_type));
1271369938Sgit2svn		advance_moment(runtime);
1272369938Sgit2svn		/* still create the pending, because we need it to callback */
1273369938Sgit2svn	}
1274369938Sgit2svn	log_info("testbound: created fake pending");
1275369938Sgit2svn	/* add to list */
1276369938Sgit2svn	pend->next = runtime->pending_list;
1277369938Sgit2svn	runtime->pending_list = pend;
1278369938Sgit2svn	return (struct serviced_query*)pend;
1279369938Sgit2svn}
1280369938Sgit2svn
1281369938Sgit2svnvoid outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg)
1282369938Sgit2svn{
1283369938Sgit2svn	struct fake_pending* pend = (struct fake_pending*)sq;
1284369938Sgit2svn	struct replay_runtime* runtime = pend->runtime;
1285369938Sgit2svn	/* delete from the list */
1286369938Sgit2svn	struct fake_pending* p = runtime->pending_list, *prev=NULL;
1287369938Sgit2svn	while(p) {
1288369938Sgit2svn		if(p == pend) {
1289369938Sgit2svn			log_assert(p->cb_arg == cb_arg);
1290369938Sgit2svn			(void)cb_arg;
1291369938Sgit2svn			log_info("serviced pending delete");
1292369938Sgit2svn			if(prev)
1293369938Sgit2svn				prev->next = p->next;
1294369938Sgit2svn			else 	runtime->pending_list = p->next;
1295369938Sgit2svn			sldns_buffer_free(p->buffer);
1296369938Sgit2svn			free(p->pkt);
1297369938Sgit2svn			free(p->zone);
1298369938Sgit2svn			free(p);
1299369938Sgit2svn			return;
1300369938Sgit2svn		}
1301369938Sgit2svn		prev = p;
1302369938Sgit2svn		p = p->next;
1303369938Sgit2svn	}
1304369938Sgit2svn	log_info("double delete of pending serviced query");
1305369938Sgit2svn}
1306369938Sgit2svn
1307369938Sgit2svnint resolve_interface_names(struct config_file* ATTR_UNUSED(cfg),
1308369938Sgit2svn	char*** ATTR_UNUSED(resif), int* ATTR_UNUSED(num_resif))
1309369938Sgit2svn{
1310369938Sgit2svn	return 1;
1311369938Sgit2svn}
1312369938Sgit2svn
1313369938Sgit2svnstruct listen_port* listening_ports_open(struct config_file* ATTR_UNUSED(cfg),
1314369938Sgit2svn	char** ATTR_UNUSED(ifs), int ATTR_UNUSED(num_ifs),
1315369938Sgit2svn	int* ATTR_UNUSED(reuseport))
1316369938Sgit2svn{
1317369938Sgit2svn	return calloc(1, 1);
1318369938Sgit2svn}
1319369938Sgit2svn
1320369938Sgit2svnvoid listening_ports_free(struct listen_port* list)
1321369938Sgit2svn{
1322369938Sgit2svn	free(list);
1323369938Sgit2svn}
1324369938Sgit2svn
1325369938Sgit2svnstruct comm_point* comm_point_create_local(struct comm_base* ATTR_UNUSED(base),
1326369938Sgit2svn        int ATTR_UNUSED(fd), size_t ATTR_UNUSED(bufsize),
1327369938Sgit2svn        comm_point_callback_type* ATTR_UNUSED(callback),
1328369938Sgit2svn	void* ATTR_UNUSED(callback_arg))
1329369938Sgit2svn{
1330369938Sgit2svn	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1331369938Sgit2svn		sizeof(*fc));
1332369938Sgit2svn	if(!fc) return NULL;
1333369938Sgit2svn	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1334369938Sgit2svn	return (struct comm_point*)fc;
1335369938Sgit2svn}
1336369938Sgit2svn
1337369938Sgit2svnstruct comm_point* comm_point_create_raw(struct comm_base* ATTR_UNUSED(base),
1338369938Sgit2svn        int ATTR_UNUSED(fd), int ATTR_UNUSED(writing),
1339369938Sgit2svn        comm_point_callback_type* ATTR_UNUSED(callback),
1340369938Sgit2svn	void* ATTR_UNUSED(callback_arg))
1341369938Sgit2svn{
1342369938Sgit2svn	/* no pipe comm possible */
1343369938Sgit2svn	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1344369938Sgit2svn		sizeof(*fc));
1345369938Sgit2svn	if(!fc) return NULL;
1346369938Sgit2svn	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1347369938Sgit2svn	return (struct comm_point*)fc;
1348369938Sgit2svn}
1349369938Sgit2svn
1350369938Sgit2svnvoid comm_point_start_listening(struct comm_point* ATTR_UNUSED(c),
1351369938Sgit2svn	int ATTR_UNUSED(newfd), int ATTR_UNUSED(sec))
1352369938Sgit2svn{
1353369938Sgit2svn	/* no bg write pipe comm possible */
1354369938Sgit2svn}
1355369938Sgit2svn
1356369938Sgit2svnvoid comm_point_stop_listening(struct comm_point* ATTR_UNUSED(c))
1357369938Sgit2svn{
1358369938Sgit2svn	/* no bg write pipe comm possible */
1359369938Sgit2svn}
1360369938Sgit2svn
1361369938Sgit2svn/* only cmd com _local gets deleted */
1362369938Sgit2svnvoid comm_point_delete(struct comm_point* c)
1363369938Sgit2svn{
1364369938Sgit2svn	struct fake_commpoint* fc = (struct fake_commpoint*)c;
1365369938Sgit2svn	if(c == NULL) return;
1366369938Sgit2svn	log_assert(fc->typecode == FAKE_COMMPOINT_TYPECODE);
1367369938Sgit2svn	if(fc->type_tcp_out) {
1368369938Sgit2svn		/* remove tcp pending, so no more callbacks to it */
1369369938Sgit2svn		pending_list_delete(fc->runtime, fc->pending);
1370369938Sgit2svn	}
1371369938Sgit2svn	free(c);
1372369938Sgit2svn}
1373369938Sgit2svn
1374369938Sgit2svnsize_t listen_get_mem(struct listen_dnsport* ATTR_UNUSED(listen))
1375369938Sgit2svn{
1376369938Sgit2svn	return 0;
1377369938Sgit2svn}
1378369938Sgit2svn
1379369938Sgit2svnsize_t outnet_get_mem(struct outside_network* ATTR_UNUSED(outnet))
1380369938Sgit2svn{
1381369938Sgit2svn	return 0;
1382369938Sgit2svn}
1383369938Sgit2svn
1384369938Sgit2svnsize_t comm_point_get_mem(struct comm_point* ATTR_UNUSED(c))
1385369938Sgit2svn{
1386369938Sgit2svn	return 0;
1387369938Sgit2svn}
1388369938Sgit2svn
1389369938Sgit2svnsize_t serviced_get_mem(struct serviced_query* ATTR_UNUSED(c))
1390369938Sgit2svn{
1391369938Sgit2svn	return 0;
1392369938Sgit2svn}
1393369938Sgit2svn
1394369938Sgit2svn/* fake for fptr wlist */
1395369938Sgit2svnint outnet_udp_cb(struct comm_point* ATTR_UNUSED(c),
1396369938Sgit2svn	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1397369938Sgit2svn        struct comm_reply *ATTR_UNUSED(reply_info))
1398369938Sgit2svn{
1399369938Sgit2svn	log_assert(0);
1400369938Sgit2svn	return 0;
1401369938Sgit2svn}
1402369938Sgit2svn
1403369938Sgit2svnint outnet_tcp_cb(struct comm_point* ATTR_UNUSED(c),
1404369938Sgit2svn	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1405369938Sgit2svn        struct comm_reply *ATTR_UNUSED(reply_info))
1406369938Sgit2svn{
1407369938Sgit2svn	log_assert(0);
1408369938Sgit2svn	return 0;
1409369938Sgit2svn}
1410369938Sgit2svn
1411369938Sgit2svnvoid pending_udp_timer_cb(void *ATTR_UNUSED(arg))
1412369938Sgit2svn{
1413369938Sgit2svn	log_assert(0);
1414369938Sgit2svn}
1415369938Sgit2svn
1416369938Sgit2svnvoid pending_udp_timer_delay_cb(void *ATTR_UNUSED(arg))
1417369938Sgit2svn{
1418369938Sgit2svn	log_assert(0);
1419369938Sgit2svn}
1420369938Sgit2svn
1421369938Sgit2svnvoid outnet_tcptimer(void* ATTR_UNUSED(arg))
1422369938Sgit2svn{
1423369938Sgit2svn	log_assert(0);
1424369938Sgit2svn}
1425369938Sgit2svn
1426369938Sgit2svnvoid comm_point_udp_callback(int ATTR_UNUSED(fd), short ATTR_UNUSED(event),
1427369938Sgit2svn	void* ATTR_UNUSED(arg))
1428369938Sgit2svn{
1429369938Sgit2svn	log_assert(0);
1430369938Sgit2svn}
1431369938Sgit2svn
1432369938Sgit2svnvoid comm_point_udp_ancil_callback(int ATTR_UNUSED(fd),
1433369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1434369938Sgit2svn{
1435369938Sgit2svn	log_assert(0);
1436369938Sgit2svn}
1437369938Sgit2svn
1438369938Sgit2svnvoid comm_point_tcp_accept_callback(int ATTR_UNUSED(fd),
1439369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1440369938Sgit2svn{
1441369938Sgit2svn	log_assert(0);
1442369938Sgit2svn}
1443369938Sgit2svn
1444369938Sgit2svnvoid comm_point_tcp_handle_callback(int ATTR_UNUSED(fd),
1445369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1446369938Sgit2svn{
1447369938Sgit2svn	log_assert(0);
1448369938Sgit2svn}
1449369938Sgit2svn
1450369938Sgit2svnvoid comm_timer_callback(int ATTR_UNUSED(fd),
1451369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1452369938Sgit2svn{
1453369938Sgit2svn	log_assert(0);
1454369938Sgit2svn}
1455369938Sgit2svn
1456369938Sgit2svnvoid comm_signal_callback(int ATTR_UNUSED(fd),
1457369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1458369938Sgit2svn{
1459369938Sgit2svn	log_assert(0);
1460369938Sgit2svn}
1461369938Sgit2svn
1462369938Sgit2svnvoid comm_point_http_handle_callback(int ATTR_UNUSED(fd),
1463369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1464369938Sgit2svn{
1465369938Sgit2svn	log_assert(0);
1466369938Sgit2svn}
1467369938Sgit2svn
1468369938Sgit2svnvoid comm_point_local_handle_callback(int ATTR_UNUSED(fd),
1469369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1470369938Sgit2svn{
1471369938Sgit2svn	log_assert(0);
1472369938Sgit2svn}
1473369938Sgit2svn
1474369938Sgit2svnvoid comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
1475369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1476369938Sgit2svn{
1477369938Sgit2svn	log_assert(0);
1478369938Sgit2svn}
1479369938Sgit2svn
1480369938Sgit2svnvoid comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
1481369938Sgit2svn	short ATTR_UNUSED(event), void* ATTR_UNUSED(arg))
1482369938Sgit2svn{
1483369938Sgit2svn	log_assert(0);
1484369938Sgit2svn}
1485369938Sgit2svn
1486369938Sgit2svnint serviced_udp_callback(struct comm_point* ATTR_UNUSED(c),
1487369938Sgit2svn	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1488369938Sgit2svn        struct comm_reply* ATTR_UNUSED(reply_info))
1489369938Sgit2svn{
1490369938Sgit2svn	log_assert(0);
1491369938Sgit2svn	return 0;
1492369938Sgit2svn}
1493369938Sgit2svn
1494369938Sgit2svnint serviced_tcp_callback(struct comm_point* ATTR_UNUSED(c),
1495369938Sgit2svn	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1496369938Sgit2svn        struct comm_reply* ATTR_UNUSED(reply_info))
1497369938Sgit2svn{
1498369938Sgit2svn	log_assert(0);
1499369938Sgit2svn	return 0;
1500369938Sgit2svn}
1501369938Sgit2svn
1502369938Sgit2svnint pending_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1503369938Sgit2svn{
1504369938Sgit2svn	log_assert(0);
1505369938Sgit2svn	return 0;
1506369938Sgit2svn}
1507369938Sgit2svn
1508369938Sgit2svnint serviced_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1509369938Sgit2svn{
1510369938Sgit2svn	log_assert(0);
1511369938Sgit2svn	return 0;
1512369938Sgit2svn}
1513369938Sgit2svn
1514369938Sgit2svnint reuse_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1515369938Sgit2svn{
1516369938Sgit2svn	log_assert(0);
1517369938Sgit2svn	return 0;
1518369938Sgit2svn}
1519369938Sgit2svn
1520369938Sgit2svnint reuse_id_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1521369938Sgit2svn{
1522369938Sgit2svn	log_assert(0);
1523369938Sgit2svn	return 0;
1524369938Sgit2svn}
1525369938Sgit2svn
1526369938Sgit2svn/* timers in testbound for autotrust. statistics tested in tdir. */
1527369938Sgit2svnstruct comm_timer* comm_timer_create(struct comm_base* base,
1528369938Sgit2svn	void (*cb)(void*), void* cb_arg)
1529369938Sgit2svn{
1530369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)base;
1531369938Sgit2svn	struct fake_timer* t = (struct fake_timer*)calloc(1, sizeof(*t));
1532369938Sgit2svn	t->cb = cb;
1533369938Sgit2svn	t->cb_arg = cb_arg;
1534369938Sgit2svn	fptr_ok(fptr_whitelist_comm_timer(t->cb)); /* check in advance */
1535369938Sgit2svn	t->runtime = runtime;
1536369938Sgit2svn	t->next = runtime->timer_list;
1537369938Sgit2svn	runtime->timer_list = t;
1538369938Sgit2svn	return (struct comm_timer*)t;
1539369938Sgit2svn}
1540369938Sgit2svn
1541369938Sgit2svnvoid comm_timer_disable(struct comm_timer* timer)
1542369938Sgit2svn{
1543369938Sgit2svn	struct fake_timer* t = (struct fake_timer*)timer;
1544369938Sgit2svn	log_info("fake timer disabled");
1545369938Sgit2svn	t->enabled = 0;
1546369938Sgit2svn}
1547369938Sgit2svn
1548369938Sgit2svnvoid comm_timer_set(struct comm_timer* timer, struct timeval* tv)
1549369938Sgit2svn{
1550369938Sgit2svn	struct fake_timer* t = (struct fake_timer*)timer;
1551369938Sgit2svn	t->enabled = 1;
1552369938Sgit2svn	t->tv = *tv;
1553369938Sgit2svn	log_info("fake timer set %d.%6.6d",
1554369938Sgit2svn		(int)t->tv.tv_sec, (int)t->tv.tv_usec);
1555369938Sgit2svn	timeval_add(&t->tv, &t->runtime->now_tv);
1556369938Sgit2svn}
1557369938Sgit2svn
1558369938Sgit2svnvoid comm_timer_delete(struct comm_timer* timer)
1559369938Sgit2svn{
1560369938Sgit2svn	struct fake_timer* t = (struct fake_timer*)timer;
1561369938Sgit2svn	struct fake_timer** pp, *p;
1562369938Sgit2svn	if(!t) return;
1563369938Sgit2svn
1564369938Sgit2svn	/* remove from linked list */
1565369938Sgit2svn	pp = &t->runtime->timer_list;
1566369938Sgit2svn	p = t->runtime->timer_list;
1567369938Sgit2svn	while(p) {
1568369938Sgit2svn		if(p == t) {
1569369938Sgit2svn			/* snip from list */
1570369938Sgit2svn			*pp = p->next;
1571369938Sgit2svn			break;
1572369938Sgit2svn		}
1573369938Sgit2svn		pp = &p->next;
1574369938Sgit2svn		p = p->next;
1575369938Sgit2svn	}
1576369938Sgit2svn
1577369938Sgit2svn	free(timer);
1578369938Sgit2svn}
1579369938Sgit2svn
1580369938Sgit2svnvoid comm_base_set_slow_accept_handlers(struct comm_base* ATTR_UNUSED(b),
1581369938Sgit2svn	void (*stop_acc)(void*), void (*start_acc)(void*),
1582369938Sgit2svn	void* ATTR_UNUSED(arg))
1583369938Sgit2svn{
1584369938Sgit2svn	/* ignore this */
1585369938Sgit2svn	(void)stop_acc;
1586369938Sgit2svn	(void)start_acc;
1587369938Sgit2svn}
1588369938Sgit2svn
1589369938Sgit2svnstruct ub_event_base* comm_base_internal(struct comm_base* ATTR_UNUSED(b))
1590369938Sgit2svn{
1591369938Sgit2svn	/* no pipe comm possible in testbound */
1592369938Sgit2svn	return NULL;
1593369938Sgit2svn}
1594369938Sgit2svn
1595369938Sgit2svnvoid daemon_remote_exec(struct worker* ATTR_UNUSED(worker))
1596369938Sgit2svn{
1597369938Sgit2svn}
1598369938Sgit2svn
1599369938Sgit2svnvoid listen_start_accept(struct listen_dnsport* ATTR_UNUSED(listen))
1600369938Sgit2svn{
1601369938Sgit2svn}
1602369938Sgit2svn
1603369938Sgit2svnvoid listen_stop_accept(struct listen_dnsport* ATTR_UNUSED(listen))
1604369938Sgit2svn{
1605369938Sgit2svn}
1606369938Sgit2svn
1607369938Sgit2svnvoid daemon_remote_start_accept(struct daemon_remote* ATTR_UNUSED(rc))
1608369938Sgit2svn{
1609369938Sgit2svn}
1610369938Sgit2svn
1611369938Sgit2svnvoid daemon_remote_stop_accept(struct daemon_remote* ATTR_UNUSED(rc))
1612369938Sgit2svn{
1613369938Sgit2svn}
1614369938Sgit2svn
1615369938Sgit2svnint create_udp_sock(int ATTR_UNUSED(family), int ATTR_UNUSED(socktype),
1616369938Sgit2svn	struct sockaddr* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
1617369938Sgit2svn	int ATTR_UNUSED(v6only), int* ATTR_UNUSED(inuse),
1618369938Sgit2svn	int* ATTR_UNUSED(noproto), int ATTR_UNUSED(rcv), int ATTR_UNUSED(snd),
1619369938Sgit2svn	int ATTR_UNUSED(listen), int* ATTR_UNUSED(reuseport),
1620369938Sgit2svn	int ATTR_UNUSED(transparent), int ATTR_UNUSED(freebind),
1621369938Sgit2svn	int ATTR_UNUSED(use_systemd), int ATTR_UNUSED(dscp))
1622369938Sgit2svn{
1623369938Sgit2svn	/* if you actually print to this, it'll be stdout during test */
1624369938Sgit2svn	return 1;
1625369938Sgit2svn}
1626369938Sgit2svn
1627369938Sgit2svnstruct comm_point* comm_point_create_udp(struct comm_base *ATTR_UNUSED(base),
1628369938Sgit2svn	int ATTR_UNUSED(fd), sldns_buffer* ATTR_UNUSED(buffer),
1629369938Sgit2svn	comm_point_callback_type* ATTR_UNUSED(callback),
1630369938Sgit2svn	void* ATTR_UNUSED(callback_arg))
1631369938Sgit2svn{
1632369938Sgit2svn	log_assert(0);
1633369938Sgit2svn	return NULL;
1634369938Sgit2svn}
1635369938Sgit2svn
1636369938Sgit2svnstruct comm_point* comm_point_create_tcp_out(struct comm_base*
1637369938Sgit2svn	ATTR_UNUSED(base), size_t ATTR_UNUSED(bufsize),
1638369938Sgit2svn	comm_point_callback_type* ATTR_UNUSED(callback),
1639369938Sgit2svn	void* ATTR_UNUSED(callback_arg))
1640369938Sgit2svn{
1641369938Sgit2svn	log_assert(0);
1642369938Sgit2svn	return NULL;
1643369938Sgit2svn}
1644369938Sgit2svn
1645369938Sgit2svnstruct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet,
1646369938Sgit2svn	comm_point_callback_type* cb, void* cb_arg,
1647369938Sgit2svn	struct sockaddr_storage* ATTR_UNUSED(to_addr),
1648369938Sgit2svn	socklen_t ATTR_UNUSED(to_addrlen))
1649369938Sgit2svn{
1650369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)
1651369938Sgit2svn		outnet->base;
1652369938Sgit2svn	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1653369938Sgit2svn		sizeof(*fc));
1654369938Sgit2svn	if(!fc) return NULL;
1655369938Sgit2svn	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1656369938Sgit2svn	fc->type_udp_out = 1;
1657369938Sgit2svn	fc->cb = cb;
1658369938Sgit2svn	fc->cb_arg = cb_arg;
1659369938Sgit2svn	fc->runtime = runtime;
1660369938Sgit2svn	/* used by authzone transfers */
1661369938Sgit2svn	return (struct comm_point*)fc;
1662369938Sgit2svn}
1663369938Sgit2svn
1664369938Sgit2svnstruct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet,
1665369938Sgit2svn	comm_point_callback_type* cb, void* cb_arg,
1666369938Sgit2svn	struct sockaddr_storage* to_addr, socklen_t to_addrlen,
1667369938Sgit2svn	struct sldns_buffer* query, int timeout, int ATTR_UNUSED(ssl),
1668369938Sgit2svn	char* ATTR_UNUSED(host))
1669369938Sgit2svn{
1670369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)
1671369938Sgit2svn		outnet->base;
1672369938Sgit2svn	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1673369938Sgit2svn		sizeof(*fc));
1674369938Sgit2svn	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1675369938Sgit2svn		sizeof(struct fake_pending));
1676369938Sgit2svn	if(!fc || !pend) {
1677369938Sgit2svn		free(fc);
1678369938Sgit2svn		free(pend);
1679369938Sgit2svn		return NULL;
1680369938Sgit2svn	}
1681369938Sgit2svn	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1682369938Sgit2svn	fc->type_tcp_out = 1;
1683369938Sgit2svn	fc->cb = cb;
1684369938Sgit2svn	fc->cb_arg = cb_arg;
1685369938Sgit2svn	fc->runtime = runtime;
1686369938Sgit2svn	fc->pending = pend;
1687369938Sgit2svn
1688369938Sgit2svn	/* used by authzone transfers */
1689369938Sgit2svn	/* create pending item */
1690369938Sgit2svn	pend->buffer = sldns_buffer_new(sldns_buffer_limit(query)+10);
1691369938Sgit2svn	if(!pend->buffer) {
1692369938Sgit2svn		free(fc);
1693369938Sgit2svn		free(pend);
1694369938Sgit2svn		return NULL;
1695369938Sgit2svn	}
1696369938Sgit2svn	sldns_buffer_copy(pend->buffer, query);
1697369938Sgit2svn	memcpy(&pend->addr, to_addr, to_addrlen);
1698369938Sgit2svn	pend->addrlen = to_addrlen;
1699369938Sgit2svn	pend->zone = NULL;
1700369938Sgit2svn	pend->zonelen = 0;
1701369938Sgit2svn	if(LDNS_QDCOUNT(sldns_buffer_begin(query)) > 0) {
1702369938Sgit2svn		char buf[512];
1703369938Sgit2svn		char addrbuf[128];
1704369938Sgit2svn		(void)sldns_wire2str_rrquestion_buf(sldns_buffer_at(query, LDNS_HEADER_SIZE), sldns_buffer_limit(query)-LDNS_HEADER_SIZE, buf, sizeof(buf));
1705369938Sgit2svn		addr_to_str((struct sockaddr_storage*)to_addr, to_addrlen,
1706369938Sgit2svn			addrbuf, sizeof(addrbuf));
1707369938Sgit2svn		if(verbosity >= VERB_ALGO) {
1708369938Sgit2svn			if(buf[0] != 0) buf[strlen(buf)-1] = 0; /* del newline*/
1709369938Sgit2svn			log_info("tcp to %s: %s", addrbuf, buf);
1710369938Sgit2svn		}
1711369938Sgit2svn		log_assert(sldns_buffer_limit(query)-LDNS_HEADER_SIZE >= 2);
1712369938Sgit2svn		pend->qtype = (int)sldns_buffer_read_u16_at(query,
1713369938Sgit2svn			LDNS_HEADER_SIZE+
1714369938Sgit2svn			dname_valid(sldns_buffer_at(query, LDNS_HEADER_SIZE),
1715369938Sgit2svn				sldns_buffer_limit(query)-LDNS_HEADER_SIZE));
1716369938Sgit2svn	}
1717369938Sgit2svn	pend->callback = cb;
1718369938Sgit2svn	pend->cb_arg = cb_arg;
1719369938Sgit2svn	pend->timeout = timeout;
1720369938Sgit2svn	pend->transport = transport_tcp;
1721369938Sgit2svn	pend->pkt = NULL;
1722369938Sgit2svn	pend->runtime = runtime;
1723369938Sgit2svn	pend->serviced = 0;
1724369938Sgit2svn	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1725369938Sgit2svn	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1726369938Sgit2svn	if(!pend->pkt) fatal_exit("out of memory");
1727369938Sgit2svn
1728369938Sgit2svn	log_info("testbound: created fake pending for tcp_out");
1729369938Sgit2svn
1730369938Sgit2svn	/* add to list */
1731369938Sgit2svn	pend->next = runtime->pending_list;
1732369938Sgit2svn	runtime->pending_list = pend;
1733369938Sgit2svn
1734369938Sgit2svn	return (struct comm_point*)fc;
1735369938Sgit2svn}
1736369938Sgit2svn
1737369938Sgit2svnstruct comm_point* outnet_comm_point_for_http(struct outside_network* outnet,
1738369938Sgit2svn	comm_point_callback_type* cb, void* cb_arg,
1739369938Sgit2svn	struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
1740369938Sgit2svn	int ssl, char* host, char* path)
1741369938Sgit2svn{
1742369938Sgit2svn	struct replay_runtime* runtime = (struct replay_runtime*)
1743369938Sgit2svn		outnet->base;
1744369938Sgit2svn	struct fake_commpoint* fc = (struct fake_commpoint*)calloc(1,
1745369938Sgit2svn		sizeof(*fc));
1746369938Sgit2svn	if(!fc) {
1747369938Sgit2svn		return NULL;
1748369938Sgit2svn	}
1749369938Sgit2svn	fc->typecode = FAKE_COMMPOINT_TYPECODE;
1750369938Sgit2svn	fc->type_http_out = 1;
1751369938Sgit2svn	fc->cb = cb;
1752369938Sgit2svn	fc->cb_arg = cb_arg;
1753369938Sgit2svn	fc->runtime = runtime;
1754369938Sgit2svn
1755369938Sgit2svn	(void)to_addr;
1756369938Sgit2svn	(void)to_addrlen;
1757369938Sgit2svn	(void)timeout;
1758369938Sgit2svn
1759369938Sgit2svn	(void)ssl;
1760369938Sgit2svn	(void)host;
1761369938Sgit2svn	(void)path;
1762369938Sgit2svn
1763369938Sgit2svn	/* handle http comm point and return contents from test script */
1764369938Sgit2svn	return (struct comm_point*)fc;
1765369938Sgit2svn}
1766369938Sgit2svn
1767369938Sgit2svnint comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet,
1768369938Sgit2svn	struct sockaddr* addr, socklen_t addrlen, int ATTR_UNUSED(is_connected))
1769369938Sgit2svn{
1770369938Sgit2svn	struct fake_commpoint* fc = (struct fake_commpoint*)c;
1771369938Sgit2svn	struct replay_runtime* runtime = fc->runtime;
1772369938Sgit2svn	struct fake_pending* pend = (struct fake_pending*)calloc(1,
1773369938Sgit2svn		sizeof(struct fake_pending));
1774369938Sgit2svn	if(!pend) {
1775369938Sgit2svn		log_err("malloc failure");
1776369938Sgit2svn		return 0;
1777369938Sgit2svn	}
1778369938Sgit2svn	fc->pending = pend;
1779369938Sgit2svn	/* used by authzone transfers */
1780369938Sgit2svn	/* create pending item */
1781369938Sgit2svn	pend->buffer = sldns_buffer_new(sldns_buffer_limit(packet) + 10);
1782369938Sgit2svn	if(!pend->buffer) {
1783369938Sgit2svn		free(pend);
1784369938Sgit2svn		return 0;
1785369938Sgit2svn	}
1786369938Sgit2svn	sldns_buffer_copy(pend->buffer, packet);
1787369938Sgit2svn	memcpy(&pend->addr, addr, addrlen);
1788369938Sgit2svn	pend->addrlen = addrlen;
1789369938Sgit2svn	pend->zone = NULL;
1790369938Sgit2svn	pend->zonelen = 0;
1791369938Sgit2svn	if(LDNS_QDCOUNT(sldns_buffer_begin(packet)) > 0) {
1792369938Sgit2svn		char buf[512];
1793369938Sgit2svn		char addrbuf[128];
1794369938Sgit2svn		(void)sldns_wire2str_rrquestion_buf(sldns_buffer_at(packet, LDNS_HEADER_SIZE), sldns_buffer_limit(packet)-LDNS_HEADER_SIZE, buf, sizeof(buf));
1795369938Sgit2svn		addr_to_str((struct sockaddr_storage*)addr, addrlen,
1796369938Sgit2svn			addrbuf, sizeof(addrbuf));
1797369938Sgit2svn		if(verbosity >= VERB_ALGO) {
1798369938Sgit2svn			if(buf[0] != 0) buf[strlen(buf)-1] = 0; /* del newline*/
1799369938Sgit2svn			log_info("udp to %s: %s", addrbuf, buf);
1800369938Sgit2svn		}
1801369938Sgit2svn		log_assert(sldns_buffer_limit(packet)-LDNS_HEADER_SIZE >= 2);
1802369938Sgit2svn		pend->qtype = (int)sldns_buffer_read_u16_at(packet,
1803369938Sgit2svn			LDNS_HEADER_SIZE+
1804369938Sgit2svn			dname_valid(sldns_buffer_at(packet, LDNS_HEADER_SIZE),
1805369938Sgit2svn				sldns_buffer_limit(packet)-LDNS_HEADER_SIZE));
1806369938Sgit2svn	}
1807369938Sgit2svn	pend->callback = fc->cb;
1808369938Sgit2svn	pend->cb_arg = fc->cb_arg;
1809369938Sgit2svn	pend->timeout = UDP_AUTH_QUERY_TIMEOUT/1000;
1810369938Sgit2svn	pend->transport = transport_udp;
1811369938Sgit2svn	pend->pkt = NULL;
1812369938Sgit2svn	pend->runtime = runtime;
1813369938Sgit2svn	pend->serviced = 0;
1814369938Sgit2svn	pend->pkt_len = sldns_buffer_limit(pend->buffer);
1815369938Sgit2svn	pend->pkt = memdup(sldns_buffer_begin(pend->buffer), pend->pkt_len);
1816369938Sgit2svn	if(!pend->pkt) fatal_exit("out of memory");
1817369938Sgit2svn
1818369938Sgit2svn	log_info("testbound: created fake pending for send_udp_msg");
1819369938Sgit2svn
1820369938Sgit2svn	/* add to list */
1821369938Sgit2svn	pend->next = runtime->pending_list;
1822369938Sgit2svn	runtime->pending_list = pend;
1823369938Sgit2svn
1824369938Sgit2svn	return 1;
1825369938Sgit2svn}
1826369938Sgit2svn
1827369938Sgit2svnint outnet_get_tcp_fd(struct sockaddr_storage* ATTR_UNUSED(addr),
1828369938Sgit2svn	socklen_t ATTR_UNUSED(addrlen), int ATTR_UNUSED(tcp_mss), int ATTR_UNUSED(dscp))
1829369938Sgit2svn{
1830369938Sgit2svn	log_assert(0);
1831369938Sgit2svn	return -1;
1832369938Sgit2svn}
1833369938Sgit2svn
1834369938Sgit2svnint outnet_tcp_connect(int ATTR_UNUSED(s), struct sockaddr_storage* ATTR_UNUSED(addr),
1835369938Sgit2svn	socklen_t ATTR_UNUSED(addrlen))
1836369938Sgit2svn{
1837369938Sgit2svn	log_assert(0);
1838369938Sgit2svn	return 0;
1839369938Sgit2svn}
1840369938Sgit2svn
1841369938Sgit2svnint tcp_req_info_add_meshstate(struct tcp_req_info* ATTR_UNUSED(req),
1842369938Sgit2svn        struct mesh_area* ATTR_UNUSED(mesh), struct mesh_state* ATTR_UNUSED(m))
1843369938Sgit2svn{
1844369938Sgit2svn	log_assert(0);
1845369938Sgit2svn	return 0;
1846369938Sgit2svn}
1847369938Sgit2svn
1848369938Sgit2svnvoid
1849369938Sgit2svntcp_req_info_remove_mesh_state(struct tcp_req_info* ATTR_UNUSED(req),
1850369938Sgit2svn	struct mesh_state* ATTR_UNUSED(m))
1851369938Sgit2svn{
1852369938Sgit2svn	log_assert(0);
1853369938Sgit2svn}
1854369938Sgit2svn
1855369938Sgit2svnsize_t
1856369938Sgit2svntcp_req_info_get_stream_buffer_size(void)
1857369938Sgit2svn{
1858369938Sgit2svn	return 0;
1859369938Sgit2svn}
1860369938Sgit2svn
1861369938Sgit2svnsize_t
1862369938Sgit2svnhttp2_get_query_buffer_size(void)
1863369938Sgit2svn{
1864369938Sgit2svn	return 0;
1865369938Sgit2svn}
1866369938Sgit2svn
1867369938Sgit2svnsize_t
1868369938Sgit2svnhttp2_get_response_buffer_size(void)
1869369938Sgit2svn{
1870369938Sgit2svn	return 0;
1871369938Sgit2svn}
1872369938Sgit2svn
1873369938Sgit2svnvoid http2_stream_add_meshstate(struct http2_stream* ATTR_UNUSED(h2_stream),
1874369938Sgit2svn	struct mesh_area* ATTR_UNUSED(mesh), struct mesh_state* ATTR_UNUSED(m))
1875369938Sgit2svn{
1876369938Sgit2svn}
1877369938Sgit2svn
1878369938Sgit2svn/*********** End of Dummy routines ***********/
1879