From a50b256dde29028a056176a2bef99096f6c5b619 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 14 Oct 2016 17:21:27 +0000 Subject: Port to the new librandom API. --- src/clients/s6-randomip.c | 9 +++------ src/include/s6-dns/s6dns.h | 4 ++-- src/libs6dns/deps-lib/s6dns | 2 ++ src/libs6dns/s6dns_engine.c | 6 +++--- src/libs6dns/s6dns_finish.c | 9 +++++++++ src/libs6dns/s6dns_init.c | 11 +++++++++++ 6 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 src/libs6dns/s6dns_finish.c create mode 100644 src/libs6dns/s6dns_init.c (limited to 'src') diff --git a/src/clients/s6-randomip.c b/src/clients/s6-randomip.c index b220374..2c1581a 100644 --- a/src/clients/s6-randomip.c +++ b/src/clients/s6-randomip.c @@ -38,19 +38,16 @@ int main (int argc, char const *const *argv) argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; if (!what) what = 1 ; what = 1 << (1 << what) ; - if (!badrandom_init()) strerr_diefu1sys(111, "init RNG") ; + if (!random_init()) strerr_diefu1sys(111, "init random generator") ; for (i = 0 ; !finite || (i < n) ; i++) { unsigned int len = what ; if (len > 16) { - char c ; - if (badrandom_string(&c, 1) < 1) - strerr_diefu1sys(111, "badrandom_string") ; + unsigned char c = random_char() ; len = (c & 1) ? 16 : 4 ; } - if (badrandom_string(ip, len) < 4) - strerr_diefu1sys(111, "badrandom_string") ; + random_string(ip, len) ; len = (len == 16) ? ip6_fmt(fmt, ip) : ip4_fmt(fmt, ip) ; fmt[len++] = '\n' ; if (buffer_put(buffer_1, fmt, len) < (int)len) diff --git a/src/include/s6-dns/s6dns.h b/src/include/s6-dns/s6dns.h index dbc1138..448f14d 100644 --- a/src/include/s6-dns/s6dns.h +++ b/src/include/s6-dns/s6dns.h @@ -12,7 +12,7 @@ #include #include -#define s6dns_init() s6dns_rci_init(&s6dns_rci_here, "/etc/resolv.conf") -#define s6dns_finish() (s6dns_engine_free(&s6dns_engine_here), s6dns_rci_free(&s6dns_rci_here)) +extern int s6dns_init (void) ; +extern void s6dns_finish (void) ; #endif diff --git a/src/libs6dns/deps-lib/s6dns b/src/libs6dns/deps-lib/s6dns index 1140b6e..b0ae95d 100644 --- a/src/libs6dns/deps-lib/s6dns +++ b/src/libs6dns/deps-lib/s6dns @@ -86,4 +86,6 @@ s6dns_debug_dumpdt_stderr.o s6dns_debug_dumpdt_post_recv.o s6dns_debug_dumpdt_pre_send.o s6dns_debug_dumpdt_post_send.o +s6dns_init.o +s6dns_finish.o -lskarnet diff --git a/src/libs6dns/s6dns_engine.c b/src/libs6dns/s6dns_engine.c index 6cf7797..f903ea9 100644 --- a/src/libs6dns/s6dns_engine.c +++ b/src/libs6dns/s6dns_engine.c @@ -71,7 +71,7 @@ static int randombind (int fd, int flag) { register unsigned int i = 0 ; for (; i < 10 ; i++) - if (socketbind46(fd, S6DNS_ENGINE_LOCAL0, 1025 + badrandom_int(64510), flag) >= 0) return 1 ; + if (socketbind46(fd, S6DNS_ENGINE_LOCAL0, 1025 + random_uint32(64510), flag) >= 0) return 1 ; return (socketbind46(fd, S6DNS_ENGINE_LOCAL0, 0, flag) >= 0) ; } @@ -86,7 +86,7 @@ static int thisudp (s6dns_engine_t *dt, tain_t const *stamp) } if (byte_diff(s6dns_ip46list_ip(&dt->servers, dt->curserver), SKALIBS_IP_SIZE, S6DNS_ENGINE_LOCAL0)) break ; } - if (badrandom_string(dt->sa.s + 2, 2) < 2) return -1 ; /* random query id */ + random_string(dt->sa.s + 2, 2) ; /* random query id */ dt->fd = socketudp46(s6dns_ip46list_is6(&dt->servers, dt->curserver)) ; if (dt->fd < 0) return -1 ; if (!randombind(dt->fd, s6dns_ip46list_is6(&dt->servers, dt->curserver))) @@ -117,7 +117,7 @@ static int thistcp (s6dns_engine_t *dt, tain_t const *stamp) for (; dt->curserver < S6DNS_MAX_SERVERS ; dt->curserver++) if (byte_diff(s6dns_ip46list_ip(&dt->servers, dt->curserver), SKALIBS_IP_SIZE, S6DNS_ENGINE_LOCAL0)) break ; if (dt->curserver >= S6DNS_MAX_SERVERS) return -2 ; - if (badrandom_string(dt->sa.s + 2, 2) < 2) return -1 ; + random_string(dt->sa.s + 2, 2) ; dt->fd = sockettcp46(s6dns_ip46list_is6(&dt->servers, dt->curserver)) ; if (dt->fd < 0) return -1 ; if (!randombind(dt->fd, s6dns_ip46list_is6(&dt->servers, dt->curserver))) diff --git a/src/libs6dns/s6dns_finish.c b/src/libs6dns/s6dns_finish.c new file mode 100644 index 0000000..3520c5d --- /dev/null +++ b/src/libs6dns/s6dns_finish.c @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include + +void s6dns_finish () +{ + s6dns_engine_free(&s6dns_engine_here) ; + s6dns_rci_free(&s6dns_rci_here) ; +} diff --git a/src/libs6dns/s6dns_init.c b/src/libs6dns/s6dns_init.c new file mode 100644 index 0000000..04f94d9 --- /dev/null +++ b/src/libs6dns/s6dns_init.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include +#include + +int s6dns_init (void) +{ + if (!random_init()) return 0 ; + if (!s6dns_rci_init(&s6dns_rci_here, "/etc/resolv.conf")) return 0 ; + return 1 ; +} -- cgit v1.2.3