From a788cfec66d6e672fd9bf2e1038d075b5318459a Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 25 Jul 2024 06:10:28 +0000 Subject: More skeleton Signed-off-by: Laurent Bercot --- src/cache/cache.c | 19 ++++++++++++++++++- src/cache/deps-exe/shibari-cache | 1 + src/cache/dns.c | 38 ++++++++++++++++++++++++++++++++++++++ src/cache/query.c | 2 +- src/cache/shibari-cache-internal.h | 10 ++++++++++ src/cache/shibari-cache.c | 6 +++--- 6 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/cache/dns.c (limited to 'src') diff --git a/src/cache/cache.c b/src/cache/cache.c index 7080240..e8796f7 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -1,9 +1,13 @@ /* ISC license. */ -#include +#include + +#include #include #include +#include + #include #include "shibari-cache-internal.h" @@ -34,3 +38,16 @@ void cache_load (void) strerr_warnwu2sys("load cache contents from ", g->dumpfile) ; } } + +int cache_search (s6dns_domain_t const *name, uint16_t qtype, dcache_key_t *data) +{ + dcache_node_t *node ; + char key[name->len + 1] ; + uint16_pack_big(key, qtype) ; + memcpy(key + 2, name->s, name->len - 1) ; + node = dcache_search(&cache, key, name->len + 1) ; + if (!node) return 0 ; + data->s = node->key.s + node->key.len ; + data->len = node->datalen ; + return 1 ; +} diff --git a/src/cache/deps-exe/shibari-cache b/src/cache/deps-exe/shibari-cache index e363f19..44d74a0 100644 --- a/src/cache/deps-exe/shibari-cache +++ b/src/cache/deps-exe/shibari-cache @@ -1,6 +1,7 @@ cache.o clientaccess.o conf.o +dns.o log.o query.o tcpconnection.o diff --git a/src/cache/dns.c b/src/cache/dns.c new file mode 100644 index 0000000..ce5b611 --- /dev/null +++ b/src/cache/dns.c @@ -0,0 +1,38 @@ +/* ISC license. */ + +#include + +#include + +#include "shibari-cache-internal.h" + +int dns_newquery (uint8_t source, uint16_t i, char const *ip, uint16_t port, char const *buf, uint16_t len) +{ + dcache_key_t data ; + s6dns_message_header_t hdr ; + s6dns_message_counts_t counts ; + s6dns_domain_t name ; + unsigned int pos ; + unsigned int rcode ; + uint16_t qtype ; + char key[257] ; + + if (!s6dns_message_parse_init(&hdr, &counts, buf, len, &pos)) return 1 ; + if (hdr.opcode) { rcode = 4 ; goto err ; } + if (!hdr.rd) { rcode = 1 ; goto err ; } + if (!s6dns_message_parse_question(&counts, &name, &qtype, buf, len, &pos) + || !s6dns_domain_encode(&name)) + { + rcode = errno == ENOTSUP ? 4 : 1 ; + goto answer ; + } + + if (cache_search(&name, qtype, &data)) goto got ; + return 1 ; + + answer: + return 1 ; + err: + got : + return 1 ; +} diff --git a/src/cache/query.c b/src/cache/query.c index 6c4bda8..8f5e423 100644 --- a/src/cache/query.c +++ b/src/cache/query.c @@ -38,7 +38,7 @@ uint16_t query_succeed (uint16_t id) return query_delete(q) ; } -int query_new (uint8_t source, uint16_t i, char const *ip, uint16_t port, char const *s, uint16_t len) +int query_new (uint8_t source, uint16_t i, char const *ip, uint16_t port, char const *buf, uint16_t len) { return 1 ; } diff --git a/src/cache/shibari-cache-internal.h b/src/cache/shibari-cache-internal.h index 6c42055..c8819fd 100644 --- a/src/cache/shibari-cache-internal.h +++ b/src/cache/shibari-cache-internal.h @@ -15,14 +15,18 @@ #include #include +#include #include +#include + /* cache */ extern void cache_init (uint64_t) ; extern void cache_dump (void) ; extern void cache_load (void) ; +extern int cache_search (s6dns_domain_t const *, uint16_t, dcache_key_t *) ; /* clientaccess */ @@ -32,6 +36,7 @@ extern int clientaccess_ip4 (char const *) ; extern int clientaccess_ip6 (char const *) ; #endif + /* conf */ extern int conf_getb (char const *, size_t, cdb_data *) ; @@ -42,6 +47,11 @@ extern int conf_get_uint64 (char const *, uint64_t *) ; extern char const *conf_get_string (char const *) ; + /* dns */ + +extern int dns_newquery (uint8_t, uint16_t, char const *, uint16_t, char const *, uint16_t) ; + + /* log */ extern void log_newtcp4 (char const *, uint16_t) ; diff --git a/src/cache/shibari-cache.c b/src/cache/shibari-cache.c index cd0e4f4..ea705bd 100644 --- a/src/cache/shibari-cache.c +++ b/src/cache/shibari-cache.c @@ -458,7 +458,7 @@ int main (int argc, char const *const *argv) if (!len) break ; if (len < 12 || len > 512) continue ; if (!clientaccess_ip4(ip)) continue ; - if (!query_new(0, i, ip, port, buf, len)) + if (!dns_newquery(0, i, ip, port, buf, len)) { if (g->verbosity) { @@ -494,7 +494,7 @@ int main (int argc, char const *const *argv) if (!len) break ; if (len < 12 || len > 512) continue ; if (!clientaccess_ip6(ip)) continue ; - if (!query_new(1, i, ip, port, buf, len)) + if (!dns_newquery(1, i, ip, port, buf, len)) { if (g->verbosity) { @@ -522,7 +522,7 @@ int main (int argc, char const *const *argv) if (l == -1) { i = tcpconnection_delete(p) ; break ; } if (!l) break ; if (p->in.len < 12 || p->in.len > 65536) { i = tcpconnection_delete(p) ; break ; } - if (!query_new(2, i, 0, 0, p->in.s, p->in.len)) + if (!dns_newquery(2, i, 0, 0, p->in.s, p->in.len)) { if (g->verbosity) { -- cgit v1.2.3