From owner-dev-commits-src-all@freebsd.org Thu Jun 3 01:00:00 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 3619064F1F6; Thu, 3 Jun 2021 01:00:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FwSH40ZK2z3Kw8; Thu, 3 Jun 2021 01:00:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E09C62095B; Thu, 3 Jun 2021 00:59:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1530xxwm020441; Thu, 3 Jun 2021 00:59:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1530xxM5020440; Thu, 3 Jun 2021 00:59:59 GMT (envelope-from git) Date: Thu, 3 Jun 2021 00:59:59 GMT Message-Id: <202106030059.1530xxM5020440@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Cy Schubert Subject: git: 735956e48aad - stable/11 - Apply upstream fix 08968baec1122a58bb90d8f97ad948a75f8a5d69: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: cy X-Git-Repository: src X-Git-Refname: refs/heads/stable/11 X-Git-Reftype: branch X-Git-Commit: 735956e48aad33a0509da08506d3b6866eb2467e Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Jun 2021 01:00:00 -0000 The branch stable/11 has been updated by cy: URL: https://cgit.FreeBSD.org/src/commit/?id=735956e48aad33a0509da08506d3b6866eb2467e commit 735956e48aad33a0509da08506d3b6866eb2467e Author: Xin LI AuthorDate: 2020-12-17 23:35:18 +0000 Commit: Cy Schubert CommitDate: 2021-06-01 19:25:27 +0000 Apply upstream fix 08968baec1122a58bb90d8f97ad948a75f8a5d69: Fix error cases when udp-connect is set and send() returns an error Approved by: git-admin (uqs) (cherry picked from commit 072fbfa38b24d202f4eac875ad2f93531dad7f7e) --- contrib/unbound/services/authzone.c | 2 +- contrib/unbound/services/outside_network.c | 15 +- contrib/unbound/util/netevent.c | 29 +- contrib/unbound/util/netevent.h | 3 +- testcode/fake_event.c | 1878 ++++++++++++++++++++++++++++ 5 files changed, 1904 insertions(+), 23 deletions(-) diff --git a/contrib/unbound/services/authzone.c b/contrib/unbound/services/authzone.c index 8fa69d27aa21..a43a25def993 100644 --- a/contrib/unbound/services/authzone.c +++ b/contrib/unbound/services/authzone.c @@ -6093,7 +6093,7 @@ xfr_probe_send_probe(struct auth_xfer* xfr, struct module_env* env, /* send udp packet */ if(!comm_point_send_udp_msg(xfr->task_probe->cp, env->scratch_buffer, - (struct sockaddr*)&addr, addrlen)) { + (struct sockaddr*)&addr, addrlen, 0)) { char zname[255+1], as[256]; dname_str(xfr->name, zname); addr_to_str(&addr, addrlen, as, sizeof(as)); diff --git a/contrib/unbound/services/outside_network.c b/contrib/unbound/services/outside_network.c index 11951adea7bc..e87aba893d98 100644 --- a/contrib/unbound/services/outside_network.c +++ b/contrib/unbound/services/outside_network.c @@ -1870,17 +1870,10 @@ randomize_and_send_udp(struct pending* pend, sldns_buffer* packet, int timeout) log_assert(pend->pc && pend->pc->cp); /* send it over the commlink */ - if(outnet->udp_connect) { - if(!comm_point_send_udp_msg(pend->pc->cp, packet, NULL, 0)) { - portcomm_loweruse(outnet, pend->pc); - return 0; - } - } else { - if(!comm_point_send_udp_msg(pend->pc->cp, packet, - (struct sockaddr*)&pend->addr, pend->addrlen)) { - portcomm_loweruse(outnet, pend->pc); - return 0; - } + if(!comm_point_send_udp_msg(pend->pc->cp, packet, + (struct sockaddr*)&pend->addr, pend->addrlen, outnet->udp_connect)) { + portcomm_loweruse(outnet, pend->pc); + return 0; } /* system calls to set timeout after sending UDP to make roundtrip diff --git a/contrib/unbound/util/netevent.c b/contrib/unbound/util/netevent.c index 8bbad15920a2..3525af39aa30 100644 --- a/contrib/unbound/util/netevent.c +++ b/contrib/unbound/util/netevent.c @@ -333,7 +333,7 @@ int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen) /* send a UDP reply */ int comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet, - struct sockaddr* addr, socklen_t addrlen) + struct sockaddr* addr, socklen_t addrlen, int is_connected) { ssize_t sent; log_assert(c->fd != -1); @@ -341,8 +341,8 @@ comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet, if(sldns_buffer_remaining(packet) == 0) log_err("error: send empty UDP packet"); #endif - if(addr) { - log_assert(addr && addrlen > 0); + log_assert(addr && addrlen > 0); + if(!is_connected) { sent = sendto(c->fd, (void*)sldns_buffer_begin(packet), sldns_buffer_remaining(packet), 0, addr, addrlen); @@ -367,9 +367,14 @@ comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet, #endif int e; fd_set_block(c->fd); - sent = sendto(c->fd, (void*)sldns_buffer_begin(packet), - sldns_buffer_remaining(packet), 0, - addr, addrlen); + if (!is_connected) { + sent = sendto(c->fd, (void*)sldns_buffer_begin(packet), + sldns_buffer_remaining(packet), 0, + addr, addrlen); + } else { + sent = send(c->fd, (void*)sldns_buffer_begin(packet), + sldns_buffer_remaining(packet), 0); + } e = errno; fd_set_nonblock(c->fd); errno = e; @@ -378,8 +383,12 @@ comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet, if(sent == -1) { if(!udp_send_errno_needs_log(addr, addrlen)) return 0; - verbose(VERB_OPS, "sendto failed: %s", sock_strerror(errno)); - log_addr(VERB_OPS, "remote address is", + if (!is_connected) { + verbose(VERB_OPS, "sendto failed: %s", sock_strerror(errno)); + } else { + verbose(VERB_OPS, "send failed: %s", sock_strerror(errno)); + } + log_addr(VERB_OPS, "remote address is", (struct sockaddr_storage*)addr, addrlen); return 0; } else if((size_t)sent != sldns_buffer_remaining(packet)) { @@ -754,7 +763,7 @@ comm_point_udp_callback(int fd, short event, void* arg) buffer = rep.c->buffer; #endif (void)comm_point_send_udp_msg(rep.c, buffer, - (struct sockaddr*)&rep.addr, rep.addrlen); + (struct sockaddr*)&rep.addr, rep.addrlen, 0); } if(!rep.c || rep.c->fd != fd) /* commpoint closed to -1 or reused for another UDP port. Note rep.c cannot be reused with TCP fd. */ @@ -3901,7 +3910,7 @@ comm_point_send_reply(struct comm_reply *repinfo) repinfo->addrlen, repinfo); else comm_point_send_udp_msg(repinfo->c, buffer, - (struct sockaddr*)&repinfo->addr, repinfo->addrlen); + (struct sockaddr*)&repinfo->addr, repinfo->addrlen, 0); #ifdef USE_DNSTAP if(repinfo->c->dtenv != NULL && repinfo->c->dtenv->log_client_response_messages) diff --git a/contrib/unbound/util/netevent.h b/contrib/unbound/util/netevent.h index daa954b6492f..4c1d9c15b2f2 100644 --- a/contrib/unbound/util/netevent.h +++ b/contrib/unbound/util/netevent.h @@ -633,10 +633,11 @@ void comm_point_drop_reply(struct comm_reply* repinfo); * @param addr: where to send it to. If NULL, send is performed, * for connected sockets, to the connected address. * @param addrlen: length of addr. + * @param is_connected: if the UDP socket is connect()ed. * @return: false on a failure. */ int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet, - struct sockaddr* addr, socklen_t addrlen); + struct sockaddr* addr, socklen_t addrlen,int is_connected); /** * Stop listening for input on the commpoint. No callbacks will happen. diff --git a/testcode/fake_event.c b/testcode/fake_event.c new file mode 100644 index 000000000000..6ad4c2cee61a --- /dev/null +++ b/testcode/fake_event.c @@ -0,0 +1,1878 @@ +/* + * 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 COPYRIGHT + * HOLDER 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 "util/net_help.h" +#include "util/data/msgparse.h" +#include "util/data/msgreply.h" +#include "util/data/msgencode.h" +#include "util/data/dname.h" +#include "util/edns.h" +#include "util/config_file.h" +#include "services/listen_dnsport.h" +#include "services/outside_network.h" +#include "services/cache/infra.h" +#include "testcode/replay.h" +#include "testcode/testpkts.h" +#include "util/log.h" +#include "util/fptr_wlist.h" +#include "sldns/sbuffer.h" +#include "sldns/wire2str.h" +#include "sldns/str2wire.h" +#include +struct worker; +struct daemon_remote; + +/** unique code to check that fake_commpoint is that structure */ +#define FAKE_COMMPOINT_TYPECODE 97347923 +/** fake commpoint, stores information */ +struct fake_commpoint { + /** typecode */ + int typecode; + /** if this is a udp outgoing type of commpoint */ + int type_udp_out; + /** if this is a tcp outgoing type of commpoint */ + int type_tcp_out; + /** if this is a http outgoing type of commpoint. */ + int type_http_out; + + /** the callback, stored for usage */ + comm_point_callback_type* cb; + /** the callback userarg, stored for usage */ + void* cb_arg; + /** runtime ptr */ + struct replay_runtime* runtime; + /** the pending entry for this commpoint (if any) */ + struct fake_pending* pending; +}; + +/** Global variable: the scenario. Saved here for when event_init is done. */ +static struct replay_scenario* saved_scenario = NULL; + +/** add timers and the values do not overflow or become negative */ +static void +timeval_add(struct timeval* d, const struct timeval* add) +{ +#ifndef S_SPLINT_S + d->tv_sec += add->tv_sec; + d->tv_usec += add->tv_usec; + if(d->tv_usec >= 1000000) { + d->tv_usec -= 1000000; + d->tv_sec++; + } +#endif +} + +void +fake_temp_file(const char* adj, const char* id, char* buf, size_t len) +{ +#ifdef USE_WINSOCK + snprintf(buf, len, "testbound_%u%s%s.tmp", + (unsigned)getpid(), adj, id); +#else + snprintf(buf, len, "/tmp/testbound_%u%s%s.tmp", + (unsigned)getpid(), adj, id); +#endif +} + +void +fake_event_init(struct replay_scenario* scen) +{ + saved_scenario = scen; +} + +void +fake_event_cleanup(void) +{ + replay_scenario_delete(saved_scenario); + saved_scenario = NULL; +} + +/** helper function that logs a sldns_pkt packet to logfile */ +static void +log_pkt(const char* desc, uint8_t* pkt, size_t len) +{ + char* str = sldns_wire2str_pkt(pkt, len); + if(!str) + fatal_exit("%s: (failed out of memory wire2str_pkt)", desc); + else { + log_info("%s%s", desc, str); + free(str); + } +} + +/** + * Returns a string describing the event type. + */ +static const char* +repevt_string(enum replay_event_type t) +{ + switch(t) { + case repevt_nothing: return "NOTHING"; + case repevt_front_query: return "QUERY"; + case repevt_front_reply: return "CHECK_ANSWER"; + case repevt_timeout: return "TIMEOUT"; + case repevt_time_passes: return "TIME_PASSES"; + case repevt_back_reply: return "REPLY"; + case repevt_back_query: return "CHECK_OUT_QUERY"; + case repevt_autotrust_check: return "CHECK_AUTOTRUST"; + case repevt_tempfile_check: return "CHECK_TEMPFILE"; + case repevt_error: return "ERROR"; + case repevt_assign: return "ASSIGN"; + case repevt_traffic: return "TRAFFIC"; + case repevt_infra_rtt: return "INFRA_RTT"; + default: return "UNKNOWN"; + } +} + +/** delete a fake pending */ +static void +delete_fake_pending(struct fake_pending* pend) +{ + if(!pend) + return; + free(pend->zone); + sldns_buffer_free(pend->buffer); + free(pend->pkt); + free(pend); +} + +/** delete a replay answer */ +static void +delete_replay_answer(struct replay_answer* a) +{ + if(!a) + return; + if(a->repinfo.c) { + sldns_buffer_free(a->repinfo.c->buffer); + free(a->repinfo.c); + } + free(a->pkt); + free(a); +} + +/** + * return: true if pending query matches the now event. + */ +static int +pending_matches_current(struct replay_runtime* runtime, + struct entry** entry, struct fake_pending **pend) +{ + struct fake_pending* p; + struct entry* e; + 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(runtime->now->addrlen != 0 && + sockaddr_cmp(&p->addr, p->addrlen, &runtime->now->addr, + runtime->now->addrlen) != 0) + continue; + if((e=find_match(runtime->now->match, p->pkt, p->pkt_len, + p->transport))) { + *entry = e; + *pend = p; + return 1; + } + } + return 0; +} + +/** + * Find the range that matches this pending message. + * @param runtime: runtime with current moment, and range list. + * @param entry: returns the pointer to entry that matches. + * @param pend: the pending that the entry must match. + * @return: true if a match is found. + */ +static int +pending_find_match(struct replay_runtime* runtime, struct entry** entry, + struct fake_pending* pend) +{ + int timenow = runtime->now->time_step; + struct replay_range* p = runtime->scenario->range_list; + while(p) { + if(p->start_step <= timenow && timenow <= p->end_step && + (p->addrlen == 0 || sockaddr_cmp(&p->addr, p->addrlen, + &pend->addr, pend->addrlen) == 0) && + (*entry = find_match(p->match, pend->pkt, pend->pkt_len, + pend->transport))) { + log_info("matched query time %d in range [%d, %d] " + "with entry line %d", timenow, + p->start_step, p->end_step, (*entry)->lineno); + if(p->addrlen != 0) + log_addr(0, "matched ip", &p->addr, p->addrlen); + log_pkt("matched pkt: ", + (*entry)->reply_list->reply_pkt, + (*entry)->reply_list->reply_len); + return 1; + } + p = p->next_range; + } + 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) +{ + struct fake_pending* p = runtime->pending_list; + /* slow, O(N*N), but it works as advertised with weird matching */ + while(p) { + if(p->tcp_pkt_counter != 0) { + /* continue tcp transfer */ + *pend = p; + return 1; + } + if(pending_find_match(runtime, entry, p)) { + *pend = p; + return 1; + } + p = p->next; + } + return 0; +} + +/** + * Remove the item from the pending list. + */ +static void +pending_list_delete(struct replay_runtime* runtime, struct fake_pending* pend) +{ + struct fake_pending** prev = &runtime->pending_list; + struct fake_pending* p = runtime->pending_list; + + while(p) { + if(p == pend) { + *prev = p->next; + delete_fake_pending(pend); + return; + } + + prev = &p->next; + p = p->next; + } +} + +/** number of replies in entry */ +static int +count_reply_packets(struct entry* entry) +{ + int count = 0; + struct reply_packet* reppkt = entry->reply_list; + while(reppkt) { + count++; + reppkt = reppkt->next; + } + return count; +} + +/** + * Fill buffer with reply from the entry. + */ +static void +fill_buffer_with_reply(sldns_buffer* buffer, struct entry* entry, uint8_t* q, + size_t qlen, int tcp_pkt_counter) +{ + struct reply_packet* reppkt; + uint8_t* c; + size_t clen; + log_assert(entry && entry->reply_list); + sldns_buffer_clear(buffer); + reppkt = entry->reply_list; + if(tcp_pkt_counter > 0) { + int i = tcp_pkt_counter; + while(reppkt && i--) + reppkt = reppkt->next; + if(!reppkt) fatal_exit("extra packet read from TCP stream but none is available"); + log_pkt("extra_packet ", reppkt->reply_pkt, reppkt->reply_len); + } + if(reppkt->reply_from_hex) { + c = sldns_buffer_begin(reppkt->reply_from_hex); + clen = sldns_buffer_limit(reppkt->reply_from_hex); + if(!c) fatal_exit("out of memory"); + } else { + c = reppkt->reply_pkt; + clen = reppkt->reply_len; + } + if(c) { + if(q) adjust_packet(entry, &c, &clen, q, qlen); + sldns_buffer_write(buffer, c, clen); + if(q) free(c); + } + sldns_buffer_flip(buffer); +} + +/** + * Perform range entry on pending message. + * @param runtime: runtime buffer size preference. + * @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) +{ + struct comm_point c; + struct comm_reply repinfo; + void* cb_arg = pend->cb_arg; + comm_point_callback_type* cb = pend->callback; + + memset(&c, 0, sizeof(c)); + c.fd = -1; + c.buffer = sldns_buffer_new(runtime->bufsize); + c.type = comm_udp; + if(pend->transport == transport_tcp) { + c.type = comm_tcp; + c.tcp_timeout_msec = 30000; + c.tcp_keepalive = runtime->tcp_seen_keepalive; + } + fill_buffer_with_reply(c.buffer, entry, pend->pkt, pend->pkt_len, + pend->tcp_pkt_counter); + repinfo.c = &c; + repinfo.addrlen = pend->addrlen; + memcpy(&repinfo.addr, &pend->addr, pend->addrlen); + if(!pend->serviced) { + if(entry && entry->reply_list->next && + pend->tcp_pkt_counter < count_reply_packets(entry)) { + /* go to next packet next time */ + pend->tcp_pkt_counter++; + } else { + pending_list_delete(runtime, pend); + } + } + if((*cb)(&c, cb_arg, NETEVENT_NOERROR, &repinfo)) { + fatal_exit("testbound: unexpected: callback returned 1"); + } + sldns_buffer_free(c.buffer); +} + +/** Check the now moment answer check event */ +static void +answer_check_it(struct replay_runtime* runtime) +{ + struct replay_answer* ans = runtime->answer_list, + *prev = NULL; + log_assert(runtime && runtime->now && + runtime->now->evt_type == repevt_front_reply); + while(ans) { + enum transport_type tr = transport_tcp; + if(ans->repinfo.c->type == comm_udp) + tr = transport_udp; + if((runtime->now->addrlen == 0 || sockaddr_cmp( + &runtime->now->addr, runtime->now->addrlen, + &ans->repinfo.addr, ans->repinfo.addrlen) == 0) && + find_match(runtime->now->match, ans->pkt, + ans->pkt_len, tr)) { + log_info("testbound matched event entry from line %d", + runtime->now->match->lineno); + log_info("testbound: do STEP %d %s", + runtime->now->time_step, + repevt_string(runtime->now->evt_type)); + if(prev) + prev->next = ans->next; + else runtime->answer_list = ans->next; + if(!ans->next) + runtime->answer_last = prev; + if(ans->repinfo.c->tcp_keepalive) + runtime->tcp_seen_keepalive = 1; + delete_replay_answer(ans); + return; + } else { + prev = ans; + ans = ans->next; + } + } + log_info("testbound: do STEP %d %s", runtime->now->time_step, + repevt_string(runtime->now->evt_type)); + fatal_exit("testbound: not matched"); +} + +/** + * Create commpoint (as return address) for a fake incoming query. + */ +static void +fake_front_query(struct replay_runtime* runtime, struct replay_moment *todo) +{ + struct comm_reply repinfo; + memset(&repinfo, 0, sizeof(repinfo)); + repinfo.c = (struct comm_point*)calloc(1, sizeof(struct comm_point)); + repinfo.addrlen = (socklen_t)sizeof(struct sockaddr_in); + if(todo->addrlen != 0) { + repinfo.addrlen = todo->addrlen; + memcpy(&repinfo.addr, &todo->addr, todo->addrlen); + } + repinfo.c->fd = -1; + repinfo.c->ev = (struct internal_event*)runtime; + repinfo.c->buffer = sldns_buffer_new(runtime->bufsize); + if(todo->match->match_transport == transport_tcp) { + repinfo.c->type = comm_tcp; + repinfo.c->tcp_timeout_msec = 30000; + repinfo.c->tcp_keepalive = runtime->tcp_seen_keepalive; + } else + repinfo.c->type = comm_udp; + fill_buffer_with_reply(repinfo.c->buffer, todo->match, NULL, 0, 0); + log_info("testbound: incoming QUERY"); + log_pkt("query pkt", todo->match->reply_list->reply_pkt, + todo->match->reply_list->reply_len); + /* call the callback for incoming queries */ + if((*runtime->callback_query)(repinfo.c, runtime->cb_arg, + NETEVENT_NOERROR, &repinfo)) { + /* send immediate reply */ + comm_point_send_reply(&repinfo); + } + /* clear it again, in case copy not done properly */ + memset(&repinfo, 0, sizeof(repinfo)); +} + +/** + * Perform callback for fake pending message. + */ +static void +fake_pending_callback(struct replay_runtime* runtime, + struct replay_moment* todo, int error) +{ + struct fake_pending* p = runtime->pending_list; + struct comm_reply repinfo; + struct comm_point c; + void* cb_arg; + comm_point_callback_type* cb; + + memset(&c, 0, sizeof(c)); + if(!p) fatal_exit("No pending queries."); + cb_arg = p->cb_arg; + cb = p->callback; + c.buffer = sldns_buffer_new(runtime->bufsize); + c.type = comm_udp; + if(p->transport == transport_tcp) { + c.type = comm_tcp; + c.tcp_timeout_msec = 30000; + c.tcp_keepalive = runtime->tcp_seen_keepalive; + } + if(todo->evt_type == repevt_back_reply && todo->match) { + fill_buffer_with_reply(c.buffer, todo->match, p->pkt, + p->pkt_len, p->tcp_pkt_counter); + } + repinfo.c = &c; + repinfo.addrlen = p->addrlen; + memcpy(&repinfo.addr, &p->addr, p->addrlen); + if(!p->serviced) { + if(todo->match && todo->match->reply_list->next && !error && + p->tcp_pkt_counter < count_reply_packets(todo->match)) { + /* go to next packet next time */ + p->tcp_pkt_counter++; + } else { + pending_list_delete(runtime, p); + } + } + if((*cb)(&c, cb_arg, error, &repinfo)) { + fatal_exit("unexpected: pending callback returned 1"); + } + /* delete the pending item. */ + sldns_buffer_free(c.buffer); +} + +/** pass time */ +static void +moment_assign(struct replay_runtime* runtime, struct replay_moment* mom) +{ + char* value = macro_process(runtime->vars, runtime, mom->string); + if(!value) + fatal_exit("could not process macro step %d", mom->time_step); + log_info("assign %s = %s", mom->variable, value); + if(!macro_assign(runtime->vars, mom->variable, value)) + fatal_exit("out of memory storing macro"); + free(value); + if(verbosity >= VERB_ALGO) + macro_print_debug(runtime->vars); +} + +/** pass time */ +static void +time_passes(struct replay_runtime* runtime, struct replay_moment* mom) +{ + struct fake_timer *t; + struct timeval tv = mom->elapse; + if(mom->string) { + char* xp = macro_process(runtime->vars, runtime, mom->string); + double sec; + if(!xp) fatal_exit("could not macro expand %s", mom->string); + verbose(VERB_ALGO, "EVAL %s", mom->string); + sec = atof(xp); + free(xp); +#ifndef S_SPLINT_S + tv.tv_sec = sec; + tv.tv_usec = (int)((sec - (double)tv.tv_sec) *1000000. + 0.5); +#endif + } + timeval_add(&runtime->now_tv, &tv); + runtime->now_secs = (time_t)runtime->now_tv.tv_sec; +#ifndef S_SPLINT_S + log_info("elapsed %d.%6.6d now %d.%6.6d", + (int)tv.tv_sec, (int)tv.tv_usec, + (int)runtime->now_tv.tv_sec, (int)runtime->now_tv.tv_usec); +#endif + /* see if any timers have fired; and run them */ + while( (t=replay_get_oldest_timer(runtime)) ) { + t->enabled = 0; + log_info("fake_timer callback"); + fptr_ok(fptr_whitelist_comm_timer(t->cb)); + (*t->cb)(t->cb_arg); + } +} + +/** check autotrust file contents */ +static void +autotrust_check(struct replay_runtime* runtime, struct replay_moment* mom) +{ + char name[1024], line[1024]; + FILE *in; + int lineno = 0, oke=1; + char* expanded; + struct config_strlist* p; + line[sizeof(line)-1] = 0; + log_assert(mom->autotrust_id); + fake_temp_file("_auto_", mom->autotrust_id, name, sizeof(name)); + in = fopen(name, "r"); + if(!in) fatal_exit("could not open %s: %s", name, strerror(errno)); + for(p=mom->file_content; p; p=p->next) { + lineno++; + if(!fgets(line, (int)sizeof(line)-1, in)) { + log_err("autotrust check failed, could not read line"); + log_err("file %s, line %d", name, lineno); + log_err("should be: %s", p->str); + fatal_exit("autotrust_check failed"); + } + if(line[0]) line[strlen(line)-1] = 0; /* remove newline */ + expanded = macro_process(runtime->vars, runtime, p->str); + if(!expanded) + fatal_exit("could not expand macro line %d", lineno); + if(verbosity >= 7 && strcmp(p->str, expanded) != 0) + log_info("expanded '%s' to '%s'", p->str, expanded); + if(strcmp(expanded, line) != 0) { + log_err("mismatch in file %s, line %d", name, lineno); + log_err("file has : %s", line); + log_err("should be: %s", expanded); + free(expanded); + oke = 0; + continue; + } + free(expanded); + fprintf(stderr, "%s:%2d ok : %s\n", name, lineno, line); + } + if(fgets(line, (int)sizeof(line)-1, in)) { + log_err("autotrust check failed, extra lines in %s after %d", + name, lineno); + do { + fprintf(stderr, "file has: %s", line); + } while(fgets(line, (int)sizeof(line)-1, in)); + oke = 0; + } + fclose(in); + if(!oke) + fatal_exit("autotrust_check STEP %d failed", mom->time_step); + log_info("autotrust %s is OK", mom->autotrust_id); +} + +/** check tempfile file contents */ +static void +tempfile_check(struct replay_runtime* runtime, struct replay_moment* mom) +{ + char name[1024], line[1024]; + FILE *in; + int lineno = 0, oke=1; + char* expanded; + struct config_strlist* p; + line[sizeof(line)-1] = 0; + log_assert(mom->autotrust_id); + fake_temp_file("_temp_", mom->autotrust_id, name, sizeof(name)); + in = fopen(name, "r"); + if(!in) fatal_exit("could not open %s: %s", name, strerror(errno)); + for(p=mom->file_content; p; p=p->next) { + lineno++; + if(!fgets(line, (int)sizeof(line)-1, in)) { + log_err("tempfile check failed, could not read line"); + log_err("file %s, line %d", name, lineno); + log_err("should be: %s", p->str); + fatal_exit("tempfile_check failed"); + } + if(line[0]) line[strlen(line)-1] = 0; /* remove newline */ + expanded = macro_process(runtime->vars, runtime, p->str); + if(!expanded) + fatal_exit("could not expand macro line %d", lineno); + if(verbosity >= 7 && strcmp(p->str, expanded) != 0) + log_info("expanded '%s' to '%s'", p->str, expanded); + if(strcmp(expanded, line) != 0) { + log_err("mismatch in file %s, line %d", name, lineno); + log_err("file has : %s", line); + log_err("should be: %s", expanded); + free(expanded); + oke = 0; + continue; + } + free(expanded); + fprintf(stderr, "%s:%2d ok : %s\n", name, lineno, line); + } + if(fgets(line, (int)sizeof(line)-1, in)) { + log_err("tempfile check failed, extra lines in %s after %d", + name, lineno); + do { + fprintf(stderr, "file has: %s", line); + } while(fgets(line, (int)sizeof(line)-1, in)); + oke = 0; + } + fclose(in); + if(!oke) + fatal_exit("tempfile_check STEP %d failed", mom->time_step); + log_info("tempfile %s is OK", mom->autotrust_id); +} + +/** Store RTT in infra cache */ +static void +do_infra_rtt(struct replay_runtime* runtime) +{ + struct replay_moment* now = runtime->now; + int rto; + size_t dplen = 0; + uint8_t* dp = sldns_str2wire_dname(now->variable, &dplen); + if(!dp) fatal_exit("cannot parse %s", now->variable); + rto = infra_rtt_update(runtime->infra, &now->addr, now->addrlen, + dp, dplen, LDNS_RR_TYPE_A, atoi(now->string), + -1, runtime->now_secs); + log_addr(0, "INFRA_RTT for", &now->addr, now->addrlen); + log_info("INFRA_RTT(%s roundtrip %d): rto of %d", now->variable, + atoi(now->string), rto); + if(rto == 0) fatal_exit("infra_rtt_update failed"); + free(dp); +} + +/** perform exponential backoff on the timeout */ +static void +expon_timeout_backoff(struct replay_runtime* runtime) +{ + struct fake_pending* p = runtime->pending_list; + int rtt, vs; + uint8_t edns_lame_known; + int last_rtt, rto; + if(!p) return; /* no pending packet to backoff */ + if(!infra_host(runtime->infra, &p->addr, p->addrlen, p->zone, + p->zonelen, runtime->now_secs, &vs, &edns_lame_known, &rtt)) + return; + last_rtt = rtt; + rto = infra_rtt_update(runtime->infra, &p->addr, p->addrlen, p->zone, + p->zonelen, p->qtype, -1, last_rtt, runtime->now_secs); + log_info("infra_rtt_update returned rto %d", rto); +} + +/** + * 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; +} + +/** + * Perform actions or checks determined by the moment. + * Also advances the time by one step. + * @param runtime: scenario runtime information. + */ +static void +do_moment_and_advance(struct replay_runtime* runtime) +{ + struct replay_moment* mom; + if(!runtime->now) { + advance_moment(runtime); + return; + } + log_info("testbound: do STEP %d %s", runtime->now->time_step, + repevt_string(runtime->now->evt_type)); + switch(runtime->now->evt_type) { + case repevt_nothing: + advance_moment(runtime); + break; + case repevt_front_query: + /* advance moment before doing the step, so that the next + moment which may check some result of the mom step + can catch those results. */ + mom = runtime->now; + advance_moment(runtime); + fake_front_query(runtime, mom); + break; + case repevt_front_reply: + if(runtime->answer_list) + log_err("testbound: There are unmatched answers."); + fatal_exit("testbound: query answer not matched"); + break; + case repevt_timeout: + mom = runtime->now; + advance_moment(runtime); + expon_timeout_backoff(runtime); + fake_pending_callback(runtime, mom, NETEVENT_TIMEOUT); + break; + case repevt_back_reply: + mom = runtime->now; + advance_moment(runtime); + fake_pending_callback(runtime, mom, NETEVENT_NOERROR); + break; + case repevt_back_query: + /* Back queries are matched when they are sent out. */ + log_err("No query matching the current moment was sent."); + fatal_exit("testbound: back query not matched"); + break; + case repevt_error: + mom = runtime->now; + advance_moment(runtime); + fake_pending_callback(runtime, mom, NETEVENT_CLOSED); + break; + case repevt_time_passes: + time_passes(runtime, runtime->now); + advance_moment(runtime); + break; + case repevt_autotrust_check: + autotrust_check(runtime, runtime->now); + advance_moment(runtime); + break; + case repevt_tempfile_check: + tempfile_check(runtime, runtime->now); + advance_moment(runtime); + break; + case repevt_assign: + moment_assign(runtime, runtime->now); + advance_moment(runtime); + break; + case repevt_traffic: + advance_moment(runtime); + break; + case repevt_infra_rtt: + do_infra_rtt(runtime); + advance_moment(runtime); + break; + default: + fatal_exit("testbound: unknown event type %d", + runtime->now->evt_type); + } +} + +/** run the scenario in event callbacks */ +static void +run_scenario(struct replay_runtime* runtime) +{ + struct entry* entry = NULL; + struct fake_pending* pending = NULL; + int max_rounds = 5000; + int rounds = 0; + runtime->now = runtime->scenario->mom_first; + log_info("testbound: entering fake runloop"); + do { + /* if moment matches pending query do it. */ + /* else if moment matches given answer, do it */ *** 1053 LINES SKIPPED ***