diff options
Diffstat (limited to 'src/cache')
-rw-r--r-- | src/cache/cache.c | 2 | ||||
-rw-r--r-- | src/cache/clientaccess.c | 2 | ||||
-rw-r--r-- | src/cache/conf.c | 28 | ||||
-rw-r--r-- | src/cache/shibari-cache-internal.h | 27 | ||||
-rw-r--r-- | src/cache/shibari-cache.c | 79 | ||||
-rw-r--r-- | src/cache/tcpconnection.c | 5 | ||||
-rw-r--r-- | src/cache/udpqueue.c | 16 |
7 files changed, 89 insertions, 70 deletions
diff --git a/src/cache/cache.c b/src/cache/cache.c index 186450c..7080240 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -21,7 +21,7 @@ void cache_dump (void) if (!dcache_save(&cache, g->dumpfile)) { strerr_warnwu2sys("save cache contents to ", g->dumpfile) ; - unlink_void(file) ; + unlink_void(g->dumpfile) ; } } } diff --git a/src/cache/clientaccess.c b/src/cache/clientaccess.c index 0dc5270..7a87fa3 100644 --- a/src/cache/clientaccess.c +++ b/src/cache/clientaccess.c @@ -11,7 +11,7 @@ static inline int check (char const *key, size_t keylen) { cdb_data data ; - return cdb_find(&confdb, &data, key, keylen) ; + return cdb_find(&g->confdb, &data, key, keylen) ; } int clientaccess_ip4 (char const *ip) diff --git a/src/cache/conf.c b/src/cache/conf.c index c2f3d6b..68edef5 100644 --- a/src/cache/conf.c +++ b/src/cache/conf.c @@ -1,8 +1,10 @@ /* ISC license. */ +#include <stddef.h> #include <errno.h> #include <string.h> +#include <skalibs/uint16.h> #include <skalibs/uint32.h> #include <skalibs/uint64.h> #include <skalibs/cdb.h> @@ -11,10 +13,10 @@ #include <skalibs/posixishard.h> -int conf_getb (cdb const *c, char const *key, size_t keylen, cdb_data *data) +int conf_getb (char const *key, size_t keylen, cdb_data *data) { if (keylen > 4096) return (errno = EINVAL, 0) ; - switch (cdb_find(c, data, key, keylen)) + switch (cdb_find(&g->confdb, data, key, keylen)) { case -1 : return (errno = EILSEQ, 0) ; case 0 : return (errno = ENOENT, 0) ; @@ -22,42 +24,42 @@ int conf_getb (cdb const *c, char const *key, size_t keylen, cdb_data *data) } } -int conf_get (cdb const *c, char const *key, cdb_data *data) +int conf_get (char const *key, cdb_data *data) { - return conf_getb(c, key, strlen(key), data) ; + return conf_getb(key, strlen(key), data) ; } -int conf_get_uint16 (cdb const *c, char const *key, uint16_t *value) +int conf_get_uint16 (char const *key, uint16_t *value) { cdb_data data ; - if (!conf_get(conf, key, &data)) return 0 ; + if (!conf_get(key, &data)) return 0 ; if (data.len != 2) return (errno = EPROTO, 0) ; uint16_unpack_big(data.s, value) ; return 1 ; } -int conf_get_uint32 (cdb const *c, char const *key, uint32_t *value) +int conf_get_uint32 (char const *key, uint32_t *value) { cdb_data data ; - if (!conf_get(conf, key, &data)) return 0 ; + if (!conf_get(key, &data)) return 0 ; if (data.len != 4) return (errno = EPROTO, 0) ; uint32_unpack_big(data.s, value) ; return 1 ; } -int conf_get_uint64 (cdb const *c, char const *key, uint64_t *value) +int conf_get_uint64 (char const *key, uint64_t *value) { cdb_data data ; - if (!conf_get(conf, key, &data)) return 0 ; + if (!conf_get(key, &data)) return 0 ; if (data.len != 8) return (errno = EPROTO, 0) ; uint64_unpack_big(data.s, value) ; return 1 ; } -char const *conf_get_string (cdb const *c, char const *key) +char const *conf_get_string (char const *key) { cdb_data data ; - if (!conf_get(conf, key, &data)) return 0 ; - if (!data.len || data.s[data.len - 1]) return (errno = EPROTO, 0) ; + if (!conf_get(key, &data)) return 0 ; + if (!data.len || data.s[data.len - 1]) return (errno = EPROTO, NULL) ; return data.s ; } diff --git a/src/cache/shibari-cache-internal.h b/src/cache/shibari-cache-internal.h index 4f6476d..cd21fd8 100644 --- a/src/cache/shibari-cache-internal.h +++ b/src/cache/shibari-cache-internal.h @@ -28,18 +28,18 @@ extern void cache_load (void) ; /* clientaccess */ extern int clientaccess_ip4 (char const *) ; -#if SKALIBS_IPV6_ENABLED +#ifdef SKALIBS_IPV6_ENABLED extern int clientaccess_ip6 (char const *) ; #endif /* conf */ -extern int conf_getb (cdb const *, char const *, size_t, cdb_data *) ; -extern int conf_get (cdb const *, char const *, cdb_data *) ; -extern int conf_get_uint16 (cdb const *, char const *, uint16_t *) ; -extern int conf_get_uint32 (cdb const *, char const *, uint32_t *) ; -extern int conf_get_uint64 (cdb const *, char const *, uint64_t *) ; -extern char const *conf_get_string (cdb const *, char const *) ; +extern int conf_getb (char const *, size_t, cdb_data *) ; +extern int conf_get (char const *, cdb_data *) ; +extern int conf_get_uint16 (char const *, uint16_t *) ; +extern int conf_get_uint32 (char const *, uint32_t *) ; +extern int conf_get_uint64 (char const *, uint64_t *) ; +extern char const *conf_get_string (char const *) ; /* query */ @@ -63,7 +63,7 @@ struct query_s extern void query_fail (query *) ; extern void query_success (query *) ; -extern void query_new (uint8_t, uint16_t, char const *, uint16_t, char const *, uint16_t) ; +extern int query_new (uint8_t, uint16_t, char const *, uint16_t, char const *, uint16_t) ; /* tcpconnection */ @@ -80,12 +80,13 @@ struct tcpconnection_s uint16_t next ; uint16_t xindex ; } ; -#define TCPCONNECTION_ZERO { .out = BUFALLOC_ZERO, .in = STRALLOC_ZERO, .instate = 0, .rdeadline = TAIN_INFINITE, .wdeadline = TAIN_INFINITE, .prev = 0, .next = 0. .xindex = UINT32_MAX } +#define TCPCONNECTION_ZERO { .out = BUFALLOC_ZERO, .in = STRALLOC_ZERO, .instate = 0, .rdeadline = TAIN_INFINITE, .wdeadline = TAIN_INFINITE, .prev = 0, .next = 0, .xindex = UINT16_MAX } #define ntcp (genset_n(&g->tcpconnections) - 1) #define TCPCONNECTION(i) genset_p(tcpconnection, &g->tcpconnections, (i)) #define tcpstart (TCPCONNECTION(g->tcpsentinel)->next) extern void tcpconnection_drop (tcpconnection *) ; +extern int tcpconnection_flush (tcpconnection *) ; extern int tcpconnection_new (uint8_t, uint16_t, int, char const *, uint16_t) ; @@ -102,7 +103,7 @@ struct udp4msg_s #ifdef SKALIBS_IPV6_ENABLED typedef struct udp6msg_s udp6msg, *udp6msg_ref ; -struct udp4msg_s +struct udp6msg_s { char ip[16] ; uint16_t port ; @@ -120,15 +121,15 @@ struct udpqueue_s tain deadline ; uint16_t xindex ; } ; -#define UDPQUEUE_ZERO { .fd = -1, .storage = STRALLOC_ZERO, .messages = GENALLOC_ZERO, .deadline = TAIN_INFINITE, .xindex = UINT32_MAX } +#define UDPQUEUE_ZERO { .fd = -1, .storage = STRALLOC_ZERO, .messages = GENALLOC_ZERO, .deadline = TAIN_INFINITE, .xindex = UINT16_MAX } extern void udpqueue_drop (udpqueue *) ; -extern int udpqueue_add4 (udpqueue *, char const *, uint16_t) ; +extern int udpqueue_add4 (udpqueue *, char const *, uint16_t, char const *, uint16_t) ; extern int udpqueue_flush4 (udpqueue *) ; #ifdef SKALIBS_IPV6_ENABLED -extern int udpqueue_add6 (udpqueue *, char const *, uint16_t) ; +extern int udpqueue_add6 (udpqueue *, char const *, uint16_t, char const *, uint16_t) ; extern int udpqueue_flush6 (udpqueue *) ; #endif diff --git a/src/cache/shibari-cache.c b/src/cache/shibari-cache.c index 85f74bc..f7e4256 100644 --- a/src/cache/shibari-cache.c +++ b/src/cache/shibari-cache.c @@ -14,8 +14,10 @@ #include <skalibs/uint64.h> #include <skalibs/types.h> #include <skalibs/fmtscan.h> +#include <skalibs/error.h> #include <skalibs/strerr.h> #include <skalibs/sgetopt.h> +#include <skalibs/allreadwrite.h> #include <skalibs/tai.h> #include <skalibs/socket.h> #include <skalibs/cdb.h> @@ -23,11 +25,11 @@ #include <skalibs/iopause.h> #include <skalibs/selfpipe.h> #include <skalibs/ip46.h> -#inclide <skalibs/genalloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/netstring.h> #include <shibari/config.h> #include <shibari/common.h> -#include <shibari/cache.h> #include "shibari-cache-internal.h" @@ -38,43 +40,44 @@ global *g = 0 ; -static int flagwantfinaldump = 1 ; static tain lameduckt = TAIN_INFINITE_RELATIVE ; +static int flagwantfinaldump = 1 ; +static unsigned int cont = 2 ; static inline void conf_init (char const *conffile, uint16_t *n4, uint16_t *n6, char const **ip4, char const **ip6, uint16_t *maxtcp, uint16_t *maxqueries) { cdb_data data ; uint32_t u ; - if (!conf_get_uint16(&g->confdb, "G:logv", &g->verbosity)) + if (!conf_get_uint16("G:logv", &g->verbosity)) strerr_diefu4sys(102, "read ", "G:logv", " configuration key from ", conffile) ; { uint64_t cachesize ; - if (!conf_get_uint64(&g->confdb, "G:cachesize", &cachesize)) + if (!conf_get_uint64("G:cachesize", &cachesize)) strerr_diefu4sys(102, "read ", "G:cachesize", " configuration key from ", conffile) ; if (cachesize < 4096) strerr_dief2x(102, "invalid G:cachesize in ", conffile) ; cache_init(cachesize) ; } - if (!conf_get_uint16(&g->confdb, "G:maxtcp", maxtcp)) + if (!conf_get_uint16("G:maxtcp", maxtcp)) strerr_diefu4sys(102, "read ", "G:maxtcp", " configuration key from ", conffile) ; if (*maxtcp > 4096 || *maxtcp < 1) strerr_dief2x(102, "invalid G:maxtcp in ", conffile) ; - if (!conf_get_uint16(&g->confdb, "G:maxqueries", maxqueries)) + if (!conf_get_uint16("G:maxqueries", maxqueries)) strerr_diefu4sys(102, "read ", "G:maxqueries", " configuration key from ", conffile) ; if (*maxqueries > 8192 || *maxqueries < 1) strerr_dief2x(102, "invalid G:maxqueries in ", conffile) ; - if (!conf_get_uint32(&g->confdb, "G:rtimeout", &u)) + if (!conf_get_uint32("G:rtimeout", &u)) if (u) tain_from_millisecs(&g->rtto, u) ; strerr_diefu4sys(102, "read ", "G:rtimeout", " configuration key from ", conffile) ; - if (!conf_get_uint32(&g->confdb, "G:wtimeout", &u)) + if (!conf_get_uint32("G:wtimeout", &u)) strerr_diefu4sys(102, "read ", "G:wtimeout", " configuration key from ", conffile) ; if (u) tain_from_millisecs(&g->wtto, u) ; - g->dumpfile = conf_get_string(&g->confdb, "G:cachefile") ; + g->dumpfile = conf_get_string("G:cachefile") ; if (!g->dumpfile && errno != ENOENT) strerr_diefu4sys(102, "read ", "G:cachefile", " configuration key from ", conffile) ; - if (!conf_get(&g->confdb, "G:listen4", &data)) + if (!conf_get("G:listen4", &data)) strerr_diefu4sys(102, "read ", "G:listen4", " configuration key from ", conffile) ; if (data.len & 3) strerr_diefu4sys(102, "invalid ", "G:listen4", " key in ", conffile) ; @@ -83,7 +86,7 @@ static inline void conf_init (char const *conffile, uint16_t *n4, uint16_t *n6, *n4 = data.len >> 2 ; *ip4 = data.s ; #ifdef SKALIBS_IPV6_ENABLED - if (!conf_get(&g.confdb, "G:listen6", &data)) + if (!conf_get("G:listen6", &data)) strerr_diefu4sys(102, "read ", "G:listen6", " configuration key from ", conffile) ; if (data.len & 15) strerr_diefu4sys(102, "invalid ", "G:listen6", " key in ", conffile) ; @@ -92,7 +95,7 @@ static inline void conf_init (char const *conffile, uint16_t *n4, uint16_t *n6, *n6 = data.len >> 4 ; *ip6 = data.s ; #endif - if (!*n4 && !*n6) strerr_dief1x(102, "no listen addresses configured" ; + if (!*n4 && !*n6) strerr_dief1x(102, "no listen addresses configured") ; } static inline void handle_signals (void) @@ -118,7 +121,7 @@ static inline void handle_signals (void) int main (int argc, char const *const *argv) { global globals = GLOBAL_ZERO ; - char const *conffile = SHIBARI_SYSCONFDIR "/shibari-cache.conf.cdb" ; + char const *conffile = SHIBARI_SYSCONFPREFIX "/shibari-cache.conf.cdb" ; uint16_t n4 = 0, n6 = 0, maxtcp, maxqueries ; char const *ip4 = 0, *ip6 = 0 ; unsigned int cont = 2 ; @@ -165,7 +168,7 @@ int main (int argc, char const *const *argv) close(1) ; if (!cdb_init(&g->confdb, conffile)) strerr_diefu2sys(111, "open ", conffile) ; - conf_init(conffile, &n4, &n6, &ip4, &ip6, &maxtcp, &maxqueries, &dumpfile) ; + conf_init(conffile, &n4, &n6, &ip4, &ip6, &maxtcp, &maxqueries) ; sfd = selfpipe_init() ; if (sfd == -1) strerr_diefu1sys(111, "create selfpipe") ; @@ -194,10 +197,10 @@ int main (int argc, char const *const *argv) memset(udpq4, 0, n4 * sizeof(udpqueue)) ; memset(udpq6, 0, n6 * sizeof(udpqueue)) ; - GENSET_init(g->tcpconnections, tcpconnection, tcpconnection_storage, tcpconnection_freelist, maxtcp + 1) ; - g->tcpsentinel = genset_new(g->tcpconnections) ; - GENSET_init(g->queries, query, query_storage, query_freelist, maxqueries + 1) ; - g->qsentinel = genset_new(g->queries) ; + GENSET_init(&g->tcpconnections, tcpconnection, tcpconnection_storage, tcpconnection_freelist, maxtcp + 1) ; + g->tcpsentinel = genset_new(&g->tcpconnections) ; + GENSET_init(&g->queries, query, query_storage, query_freelist, maxqueries + 1) ; + g->qsentinel = genset_new(&g->queries) ; { static const tcpconnection tcpconnection_zero = TCPCONNECTION_ZERO ; static const query query_zero = QUERY_ZERO ; @@ -380,9 +383,9 @@ int main (int argc, char const *const *argv) tcpconnection_drop(p) ; } for (uint16_t i = 0 ; i < n4 ; i++) - if (!tain_future(&udp4q[i].deadline)) udpqueue_drop(udp4q + i) ; + if (!tain_future(&udpq4[i].deadline)) udpqueue_drop(udpq4 + i) ; for (uint16_t i = 0 ; i < n6 ; i++) - if (!tain_future(&udp6q[i].deadline)) udpqueue_drop(udp6q + i) ; + if (!tain_future(&udpq6[i].deadline)) udpqueue_drop(udpq6 + i) ; } @@ -470,8 +473,8 @@ int main (int argc, char const *const *argv) { char fmtip[IP4_FMT] ; char fmtport[UINT16_FMT] ; - fmtip[ip4_fmt(fmtip, ip] = 0 ; - fmtport[uint16_fmt(fmtport, port] = 0 ; + fmtip[ip4_fmt(fmtip, ip)] = 0 ; + fmtport[uint16_fmt(fmtport, port)] = 0 ; strerr_warnwu4sys("process new UDP query from ip ", fmtip, " port ", fmtport) ; } } @@ -506,8 +509,8 @@ int main (int argc, char const *const *argv) { char fmtip[IP4_FMT] ; char fmtport[UINT16_FMT] ; - fmtip[ip4_fmt(fmtip, ip] = 0 ; - fmtport[uint16_fmt(fmtport, port] = 0 ; + fmtip[ip4_fmt(fmtip, ip)] = 0 ; + fmtport[uint16_fmt(fmtport, port)] = 0 ; strerr_warnwu4sys("process new UDP query from ip ", fmtip, " port ", fmtport) ; } } @@ -524,15 +527,23 @@ int main (int argc, char const *const *argv) int l = sanitize_read(mininetstring_read(bufalloc_fd(&p->out), &p->in, &p->instate)) ; if (l == -1) { i = p->prev ; tcpconnection_drop(p) ; } if (l <= 0) continue ; - if (sa.len < 12 || sa.len > 65536) { i = p->prev ; tcpconnection_drop(p) ; continue ; } - query_new(2, i, 0, 0, sa.s, sa.len) ; - sa.len = 0 ; + if (p->in.len < 12 || p->in.len > 65536) { i = p->prev ; tcpconnection_drop(p) ; continue ; } + if (!query_new(2, i, 0, 0, p->in.s, p->in.len)) + { + if (g->verbosity) + { + char fmt[UINT16_FMT] ; + fmt[uint16_fmt(fmt, i)] = 0 ; + strerr_warnwu2sys("process TCP query on connection ", fmt) ; + } + } + p->in.len = 0 ; } } for (uint16_t i = 0 ; i < n4 ; i++) if (tcp4xindex[i] < UINT16_MAX) { - if (x[tcp4index[i]].revents & IOPAUSE_READ) + if (x[tcp4xindex[i]].revents & IOPAUSE_READ) { uint16_t n = MAXSAME ; while (n-- && ntcp < maxtcp) @@ -543,9 +554,9 @@ int main (int argc, char const *const *argv) if (fd == -1) { if (error_isagain(errno)) break ; - strerr_diefu4sys("create new TCP connection") ; + strerr_diefu1sys(111, "create new TCP connection") ; } - if (!clientaccess_ip4(ip)) { fd_close(fd) ; continue ; } + if (!clientaccess_ip4(ip)) { close(fd) ; continue ; } tcpconnection_new(0, i, fd, ip, port) ; } } @@ -554,7 +565,7 @@ int main (int argc, char const *const *argv) #ifdef SKALIBS_IPV6_ENABLED for (uint16_t i = 0 ; i < n6 ; i++) if (tcp6xindex[i] < UINT16_MAX) { - if (x[tcp6index[i]].revents & IOPAUSE_READ) + if (x[tcp6xindex[i]].revents & IOPAUSE_READ) { uint16_t n = MAXSAME ; while (n-- && ntcp < maxtcp) @@ -565,9 +576,9 @@ int main (int argc, char const *const *argv) if (fd == -1) { if (error_isagain(errno)) break ; - strerr_diefu4sys("create new TCP connection") ; + strerr_diefu1sys(111, "create new TCP connection") ; } - if (!clientaccess_ip6(ip)) { fd_close(fd) ; continue ; } + if (!clientaccess_ip6(ip)) { close(fd) ; continue ; } tcpconnection_new(1, i, fd, ip, port) ; } } diff --git a/src/cache/tcpconnection.c b/src/cache/tcpconnection.c index d42060f..bc77781 100644 --- a/src/cache/tcpconnection.c +++ b/src/cache/tcpconnection.c @@ -6,6 +6,11 @@ void tcpconnection_drop (tcpconnection *tc) { } +int tcpconnection_flush (tcpconnection *tc) +{ + return 1 ; +} + int tcpconnection_new (uint8_t source, uint16_t i, int fd, char const *ip, uint16_t port) { return 1 ; diff --git a/src/cache/udpqueue.c b/src/cache/udpqueue.c index 8e1443a..8750d18 100644 --- a/src/cache/udpqueue.c +++ b/src/cache/udpqueue.c @@ -31,9 +31,9 @@ int udpqueue_add4 (udpqueue *q, char const *ip, uint16_t port, char const *s, ui int udpqueue_flush4 (udpqueue *q) { - size_t n = genalloc_n(udp4msg, &q->messages) ; + size_t n = genalloc_len(udp4msg, &q->messages) ; size_t shead = 0, head = 0 ; - ssize_t r ; + ssize_t r = 1 ; while (head < n) { udp4msg const *msg = genalloc_s(udp4msg, &q->messages) + head ; @@ -47,8 +47,8 @@ int udpqueue_flush4 (udpqueue *q) adjust: memmove(q->storage.s, q->storage.s + shead, q->storage.len - shead) ; q->storage.len -= shead ; - memmove(genalloc_s(&udp4msg, &q->messages), genalloc_s(&udp4msg, &q->messages) + head, (n - head) * sizeof(udp4msg)) ; - genalloc_setlen(&udp4msg, &q->messages, n - head) ; + memmove(genalloc_s(udp4msg, &q->messages), genalloc_s(udp4msg, &q->messages) + head, (n - head) * sizeof(udp4msg)) ; + genalloc_setlen(udp4msg, &q->messages, n - head) ; if (shead) tain_add_g(&q->deadline, &g->wtto) ; return sanitize_read(r) ; } @@ -68,9 +68,9 @@ int udpqueue_add6 (udpqueue *q, char const *ip, uint16_t port, char const *s, ui int udpqueue_flush6 (udpqueue *q) { - size_t n = genalloc_n(udp6msg, &q->messages) ; + size_t n = genalloc_len(udp6msg, &q->messages) ; size_t shead = 0, head = 0 ; - ssize_t r ; + ssize_t r = 1 ; while (head < n) { udp6msg const *msg = genalloc_s(udp4msg, &q->messages) + head ; @@ -84,8 +84,8 @@ int udpqueue_flush6 (udpqueue *q) adjust: memmove(q->storage.s, q->storage.s + shead, q->storage.len - shead) ; q->storage.len -= shead ; - memmove(genalloc_s(&udp6msg, &q->messages), genalloc_s(&udp6msg, &q->messages) + head, (n - head) * sizeof(udp6msg)) ; - genalloc_setlen(&udp6msg, &q->messages, n - head) ; + memmove(genalloc_s(udp6msg, &q->messages), genalloc_s(udp6msg, &q->messages) + head, (n - head) * sizeof(udp6msg)) ; + genalloc_setlen(udp6msg, &q->messages, n - head) ; if (shead) tain_add_g(&q->deadline, &g->wtto) ; return sanitize_read(r) ; } |