2007-02-08 16:03:26 +00:00
|
|
|
/*
|
|
|
|
* testcode/fake_event.c - fake event handling that replays existing scenario.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007, NLnet Labs. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is open source.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Neither the name of the NLNET LABS nor the names of its contributors may
|
|
|
|
* be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Event service that replays a scenario.
|
|
|
|
* This implements the same exported symbols as the files:
|
|
|
|
* util/netevent.c
|
|
|
|
* services/listen_dnsport.c
|
|
|
|
* services/outside_network.c
|
|
|
|
* But these do not actually access the network or events, instead
|
|
|
|
* the scenario is played.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "testcode/fake_event.h"
|
|
|
|
#include "util/netevent.h"
|
|
|
|
#include "services/listen_dnsport.h"
|
|
|
|
#include "services/outside_network.h"
|
|
|
|
#include "testcode/replay.h"
|
2007-02-09 16:39:47 +00:00
|
|
|
#include "testcode/ldns-testpkts.h"
|
2007-02-08 16:03:26 +00:00
|
|
|
|
2007-02-09 16:39:47 +00:00
|
|
|
/** Global variable: the scenario. Saved here for when event_init is done. */
|
|
|
|
static struct replay_scenario* saved_scenario = NULL;
|
2007-02-08 16:03:26 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
fake_event_init(struct replay_scenario* scen)
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
saved_scenario = scen;
|
2007-02-08 16:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fake_event_cleanup()
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
replay_scenario_delete(saved_scenario);
|
|
|
|
saved_scenario = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return: true if pending query matches the now event.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pending_matches_current(struct replay_runtime* runtime)
|
|
|
|
{
|
|
|
|
struct fake_pending* p;
|
|
|
|
if(!runtime->now || runtime->now->evt_type != repevt_back_query
|
|
|
|
|| !runtime->pending_list)
|
|
|
|
return 0;
|
|
|
|
/* see if any of the pending queries matches */
|
|
|
|
for(p = runtime->pending_list; p; p = p->next) {
|
|
|
|
if(find_match(runtime->now->match, p->pkt, p->transport))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if outgoing pending query matches an entry.
|
|
|
|
* @param runtime: runtime.
|
|
|
|
* @param entry: if true, the entry that matches is returned.
|
|
|
|
* @param pend: if true, the outgoing message that matches is returned.
|
|
|
|
* return: true if pending query matches the now event.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pending_matches_range(struct replay_runtime* runtime,
|
|
|
|
struct entry** entry, struct fake_pending** pend)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform range entry on pending message.
|
|
|
|
* @param runtime: runtime, needed?.
|
|
|
|
* @param entry: entry that codes for the reply to do.
|
|
|
|
* @param pend: pending query that is answered, callback called.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
answer_callback_from_entry(struct replay_runtime* runtime,
|
|
|
|
struct entry* entry, struct fake_pending* pend)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform actions or checks determined by the moment.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_moment(struct replay_runtime* runtime)
|
|
|
|
{
|
|
|
|
if(!runtime->now)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance to the next moment.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
advance_moment(struct replay_runtime* runtime)
|
|
|
|
{
|
|
|
|
if(!runtime->now)
|
|
|
|
runtime->now = runtime->scenario->mom_first;
|
|
|
|
else runtime->now = runtime->now->mom_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** run the scenario in event callbacks */
|
|
|
|
static void
|
|
|
|
run_scenario(struct replay_runtime* runtime)
|
|
|
|
{
|
|
|
|
struct entry* entry = NULL;
|
|
|
|
struct fake_pending* pending = NULL;
|
|
|
|
runtime->now = NULL;
|
|
|
|
do {
|
|
|
|
/* if moment matches pending query do it. */
|
|
|
|
/* else if precoded_range matches pending, do it */
|
|
|
|
/* else do the current moment */
|
|
|
|
if(pending_matches_current(runtime)) {
|
|
|
|
advance_moment(runtime);
|
|
|
|
} else if(pending_matches_range(runtime, &entry, &pending)) {
|
|
|
|
answer_callback_from_entry(runtime, entry, pending);
|
|
|
|
} else {
|
|
|
|
do_moment(runtime);
|
|
|
|
advance_moment(runtime);
|
|
|
|
}
|
|
|
|
} while(runtime->now);
|
2007-02-08 16:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********** Dummy routines ***********/
|
|
|
|
|
2007-02-08 16:47:06 +00:00
|
|
|
struct listen_dnsport*
|
2007-02-09 16:39:47 +00:00
|
|
|
listen_create(struct comm_base* base,
|
2007-02-08 16:47:06 +00:00
|
|
|
int ATTR_UNUSED(num_ifs), const char* ATTR_UNUSED(ifs[]),
|
|
|
|
const char* ATTR_UNUSED(port),
|
|
|
|
int ATTR_UNUSED(do_ip4), int ATTR_UNUSED(do_ip6),
|
|
|
|
int ATTR_UNUSED(do_udp), int ATTR_UNUSED(do_tcp),
|
2007-02-09 16:39:47 +00:00
|
|
|
size_t bufsize, comm_point_callback_t* cb, void* cb_arg)
|
2007-02-08 16:47:06 +00:00
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
struct replay_runtime* runtime = (struct replay_runtime*)base;
|
|
|
|
struct listen_dnsport* l= calloc(1, sizeof(struct listen_dnsport));
|
|
|
|
if(!l)
|
|
|
|
return NULL;
|
|
|
|
l->base = base;
|
|
|
|
l->udp_buff = ldns_buffer_new(bufsize);
|
|
|
|
if(!l->udp_buff) {
|
|
|
|
free(l);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
runtime->callback_query = cb;
|
|
|
|
runtime->cb_arg = cb_arg;
|
|
|
|
return l;
|
2007-02-08 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
listen_delete(struct listen_dnsport* listen)
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
if(!listen)
|
|
|
|
return;
|
|
|
|
ldns_buffer_free(listen->udp_buff);
|
2007-02-08 16:47:06 +00:00
|
|
|
free(listen);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct comm_base* comm_base_create()
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
/* we return the runtime structure instead. */
|
|
|
|
struct replay_runtime* runtime = (struct replay_runtime*)
|
|
|
|
calloc(1, sizeof(struct replay_runtime));
|
|
|
|
runtime->scenario = saved_scenario;
|
|
|
|
return (struct comm_base*)runtime;
|
2007-02-08 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void comm_base_delete(struct comm_base* b)
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
struct replay_runtime* runtime = (struct replay_runtime*)b;
|
|
|
|
free(runtime);
|
2007-02-08 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void comm_base_dispatch(struct comm_base* b)
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
struct replay_runtime* runtime = (struct replay_runtime*)b;
|
|
|
|
run_scenario(runtime);
|
2007-02-08 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void comm_base_exit(struct comm_base* ATTR_UNUSED(b))
|
|
|
|
{
|
|
|
|
/* some sort of failure */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-02-09 16:39:47 +00:00
|
|
|
struct comm_signal* comm_signal_create(struct comm_base* base,
|
|
|
|
void (*callback)(int, void*), void* cb_arg)
|
2007-02-08 16:47:06 +00:00
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
struct replay_runtime* runtime = (struct replay_runtime*)base;
|
|
|
|
runtime->sig_cb = callback;
|
|
|
|
runtime->sig_cb_arg = cb_arg;
|
|
|
|
return calloc(1, sizeof(struct comm_signal));
|
2007-02-08 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int comm_signal_bind(struct comm_signal* ATTR_UNUSED(comsig), int
|
|
|
|
ATTR_UNUSED(sig))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void comm_signal_delete(struct comm_signal* comsig)
|
|
|
|
{
|
|
|
|
free(comsig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
comm_point_send_reply(struct comm_reply* repinfo)
|
|
|
|
{
|
|
|
|
/* TODO see if this is checked */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
comm_point_drop_reply(struct comm_reply* repinfo)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
|
|
|
|
struct outside_network*
|
2007-02-09 16:39:47 +00:00
|
|
|
outside_network_create(struct comm_base* base, size_t bufsize,
|
|
|
|
size_t ATTR_UNUSED(num_ports),
|
2007-02-08 16:47:06 +00:00
|
|
|
const char** ATTR_UNUSED(ifs), int ATTR_UNUSED(num_ifs),
|
|
|
|
int ATTR_UNUSED(do_ip4), int ATTR_UNUSED(do_ip6),
|
|
|
|
int ATTR_UNUSED(port_base))
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
struct outside_network* outnet = calloc(1,
|
|
|
|
sizeof(struct outside_network));
|
|
|
|
if(!outnet)
|
|
|
|
return NULL;
|
|
|
|
outnet->base = base;
|
|
|
|
outnet->udp_buff = ldns_buffer_new(bufsize);
|
|
|
|
if(!outnet->udp_buff)
|
|
|
|
return NULL;
|
|
|
|
return outnet;
|
2007-02-08 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
outside_network_delete(struct outside_network* outnet)
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
if(!outnet)
|
|
|
|
return;
|
|
|
|
ldns_buffer_free(outnet->udp_buff);
|
2007-02-08 16:47:06 +00:00
|
|
|
free(outnet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pending_udp_query(struct outside_network* outnet, ldns_buffer* packet,
|
|
|
|
struct sockaddr_storage* addr, socklen_t addrlen, int timeout,
|
|
|
|
comm_point_callback_t* callback, void* callback_arg)
|
|
|
|
{
|
2007-02-09 16:39:47 +00:00
|
|
|
/* TODO create it */
|
2007-02-08 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
2007-02-08 16:03:26 +00:00
|
|
|
/*********** End of Dummy routines ***********/
|