]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/utils: moved isaac cspring here
authorMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 16 Jun 2015 00:09:15 +0000 (02:09 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 16 Jun 2015 00:09:15 +0000 (02:09 +0200)
lib/layer/iterate.c
lib/nsrep.c
lib/utils.c
lib/utils.h

index 2c0ff2f166a7503799e9fc132d824e3c37cb47d1..af27d5c58f31a5d951b3df567ac37614ba60f5a4 100644 (file)
@@ -19,7 +19,6 @@
 #include <libknot/descriptor.h>
 #include <libknot/rrtype/rdname.h>
 
-#include "ccan/isaac/isaac.h"
 #include "lib/layer/iterate.h"
 #include "lib/resolve.h"
 #include "lib/rplan.h"
 #include "lib/nsrep.h"
 #include "lib/module.h"
 
-#define SEED_SIZE 256
 #define DEBUG_MSG(fmt...) QRDEBUG(kr_rplan_current(&req->rplan), "iter", fmt)
 
 /* Iterator often walks through packet section, this is an abstraction. */
 typedef int (*rr_callback_t)(const knot_rrset_t *, unsigned, struct kr_request *);
 
-/** @internal CSPRNG context */
-static isaac_ctx ISAAC;
-
-/** @internal Reseed isaac context. */
-int iterate_init(struct kr_module *module)
-{
-       uint8_t seed[SEED_SIZE];
-       kr_randseed((char *)seed, sizeof(seed));
-       isaac_reseed(&ISAAC, seed, sizeof(seed));
-       return kr_ok();
-}
-
 /** Return minimized QNAME/QTYPE for current zone cut. */
 static const knot_dname_t *minimized_qname(struct kr_query *query, uint16_t *qtype)
 {
@@ -368,7 +354,7 @@ int kr_make_query(struct kr_query *query, knot_pkt_t *pkt)
        }
 
        /* Query built, expect answer. */
-       query->id = isaac_next_uint(&ISAAC, UINT16_MAX);
+       query->id = kr_rand_uint(UINT16_MAX);
        knot_wire_set_id(pkt->wire, query->id);
        pkt->parsed = pkt->size;
        return kr_ok();
index 0ec6d780a90d77b22b68f11318e22640f6bd54e7..6973fb4900c36cd4c83eabcfd48cac0177168aad 100644 (file)
@@ -124,7 +124,7 @@ static int eval_nsrep(const char *k, void *v, void *baton)
                ns->reputation = reputation;
        } else {
                /* With 5% chance, probe server with a probability given by its RTT / MAX_RTT */
-               unsigned roll = rand() % KR_NS_MAX_SCORE;
+               unsigned roll = kr_rand_uint(KR_NS_MAX_SCORE);
                if ((roll % 100 < 5) && (roll >= score)) {
                        update_nsrep(ns, (const knot_dname_t *)k, addr, score);
                        ns->reputation = reputation;
index 9c36726be1bca6a931febfaf70ad32aee916436d..3b1f36b5d29d7a51097bf75d7f18ea68fda41c79 100644 (file)
 #include <stdio.h>
 #include <sys/time.h>
 
+#include "ccan/isaac/isaac.h"
 #include "lib/defines.h"
 #include "lib/utils.h"
 #include "lib/generic/array.h"
 
+/** @internal CSPRNG context */
+static isaac_ctx ISAAC;
+static bool isaac_seeded = false;
+#define SEED_SIZE 256
+
 /*
  * Macros.
  */
@@ -95,7 +101,7 @@ static int seed_file(FILE *fp, char *buf, size_t buflen)
        return 0;
 }
 
-int kr_randseed(char *buf, size_t buflen)
+static int randseed(char *buf, size_t buflen)
 {
     /* This is adapted from Tor's crypto_seed_rng() */
     static const char *filenames[] = {
@@ -115,6 +121,23 @@ int kr_randseed(char *buf, size_t buflen)
     return 0;
 }
 
+int kr_rand_reseed(void)
+{
+       uint8_t seed[SEED_SIZE];
+       randseed((char *)seed, sizeof(seed));
+       isaac_reseed(&ISAAC, seed, sizeof(seed));
+       return kr_ok();
+}
+
+unsigned kr_rand_uint(unsigned max)
+{
+       if (!isaac_seeded) {
+               kr_rand_reseed();
+               isaac_seeded = true;
+       }
+       return isaac_next_uint(&ISAAC, max);
+}
+
 int mm_reserve(void *baton, char **mem, size_t elm_size, size_t want, size_t *have)
 {
     if (*have >= want) {
index 3d1c83a3b017750ed414a803b5c70fa6e73aebc3..0d2bcba8db9781e7f514ee48ec08ca36af3737fa 100644 (file)
@@ -37,8 +37,11 @@ extern void _cleanup_fclose(FILE **p);
 /** Concatenate N strings. */
 char* kr_strcatdup(unsigned n, ...);
 
-/** Fill buffer with random seed. */
-int kr_randseed(char *buf, size_t buflen);
+/** Reseed CSPRNG context. */
+int kr_rand_reseed(void);
+
+/** Get pseudo-random value. */
+unsigned kr_rand_uint(unsigned max);
 
 /** Memory reservation routine for mm_ctx_t */
 int mm_reserve(void *baton, char **mem, size_t elm_size, size_t want, size_t *have);