mirror of
https://github.com/matrix-construct/construct
synced 2025-01-13 16:33:53 +01:00
ircd: Add std::random subsystem and utils.
This commit is contained in:
parent
b6d51b89dd
commit
48148c640c
6 changed files with 200 additions and 35 deletions
|
@ -293,6 +293,7 @@ RB_CHK_SYSHEADER([atomic], [ATOMIC])
|
|||
RB_CHK_SYSHEADER([thread], [THREAD])
|
||||
RB_CHK_SYSHEADER([mutex], [MUTEX])
|
||||
RB_CHK_SYSHEADER([condition_variable], [CONDITION_VARIABLE])
|
||||
RB_CHK_SYSHEADER([random], [RANDOM])
|
||||
|
||||
dnl experimental
|
||||
RB_CHK_SYSHEADER([string_view], [STRING_VIEW])
|
||||
|
|
85
include/ircd/rand.h
Normal file
85
include/ircd/rand.h
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Charybdis Development Team
|
||||
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice is present in all copies.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_UTIL_RANDOM_H
|
||||
|
||||
namespace ircd {
|
||||
namespace rand {
|
||||
|
||||
extern std::random_device device;
|
||||
extern std::mt19937_64 mt;
|
||||
|
||||
namespace dict
|
||||
{
|
||||
extern const std::string alnum;
|
||||
extern const std::string alpha;
|
||||
extern const std::string upper;
|
||||
extern const std::string lower;
|
||||
extern const std::string numeric;
|
||||
}
|
||||
|
||||
uint64_t integer();
|
||||
uint64_t integer(const uint64_t &min, const uint64_t &max);
|
||||
|
||||
// Random character from dictionary
|
||||
char character(const std::string &dict = dict::alnum);
|
||||
|
||||
// Random string of len from dictionary; char buffers null terminated, uchar not.
|
||||
template<size_t size> string_view string(const std::string &dict, const size_t &len, char (&buf)[size]);
|
||||
string_view string(const std::string &dict, const size_t &len, char *const &buf, const size_t &max);
|
||||
string_view string(const std::string &dict, const size_t &len, uint8_t *const &buf);
|
||||
std::string string(const std::string &dict, const size_t &len);
|
||||
|
||||
} // namespace rand
|
||||
} // namespace ircd
|
||||
|
||||
template<size_t size>
|
||||
ircd::string_view
|
||||
ircd::rand::string(const std::string &dict,
|
||||
const size_t &len,
|
||||
char (&buf)[size])
|
||||
{
|
||||
return string(dict, len, buf, size);
|
||||
}
|
||||
|
||||
// Random character from dictionary
|
||||
inline char
|
||||
ircd::rand::character(const std::string &dict)
|
||||
{
|
||||
assert(!dict.empty());
|
||||
return dict.at(integer(0, dict.size() - 1));
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
ircd::rand::integer(const uint64_t &min,
|
||||
const uint64_t &max)
|
||||
{
|
||||
std::uniform_int_distribution<uint64_t> dist{min, max};
|
||||
return dist(mt);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
ircd::rand::integer()
|
||||
{
|
||||
return mt();
|
||||
}
|
|
@ -82,6 +82,7 @@ extern "C" {
|
|||
#include <RB_INC_THREAD
|
||||
#include <RB_INC_MUTEX
|
||||
#include <RB_INC_CONDITION_VARIABLE
|
||||
#include <RB_INC_RANDOM
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
|
@ -176,6 +177,7 @@ struct client;
|
|||
} // namespace ircd
|
||||
|
||||
#include "util.h"
|
||||
#include "rand.h"
|
||||
#include "timer.h"
|
||||
#include "allocator.h"
|
||||
#include "info.h"
|
||||
|
|
|
@ -61,7 +61,9 @@ libircd_la_SOURCES = \
|
|||
json.cc \
|
||||
parse.cc \
|
||||
listen.cc \
|
||||
matrix.cc
|
||||
matrix.cc \
|
||||
rand.cc \
|
||||
###
|
||||
|
||||
if JS
|
||||
libircd_la_SOURCES += \
|
||||
|
|
37
ircd/ircd.cc
37
ircd/ircd.cc
|
@ -37,8 +37,7 @@ namespace ircd
|
|||
main_exit_cb main_exit_func; // Called when main context exits
|
||||
ctx::ctx *main_context; // Reference to main context
|
||||
|
||||
void seed_random();
|
||||
void init_system();
|
||||
void init_rlimit();
|
||||
void main_exiting() noexcept;
|
||||
void main();
|
||||
}
|
||||
|
@ -51,7 +50,7 @@ ircd::init(boost::asio::io_service &io_service,
|
|||
ircd::ios = &io_service;
|
||||
main_finish = false;
|
||||
_main_exited = false;
|
||||
init_system();
|
||||
init_rlimit();
|
||||
|
||||
// The log is available, but it is console-only until conf opens files.
|
||||
log::init();
|
||||
|
@ -153,19 +152,6 @@ ircd::at_main_exit(main_exit_cb main_exit_func)
|
|||
ircd::main_exit_func = std::move(main_exit_func);
|
||||
}
|
||||
|
||||
namespace ircd
|
||||
{
|
||||
void init_random();
|
||||
void init_rlimit();
|
||||
}
|
||||
|
||||
void
|
||||
ircd::init_system()
|
||||
{
|
||||
init_rlimit();
|
||||
init_random();
|
||||
}
|
||||
|
||||
void
|
||||
#ifdef HAVE_SYS_RESOURCE_H
|
||||
ircd::init_rlimit()
|
||||
|
@ -190,24 +176,7 @@ catch(const std::exception &e)
|
|||
ircd::init_rlimit()
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
ircd::init_random()
|
||||
{
|
||||
union
|
||||
{
|
||||
char seed_char[4];
|
||||
int seed_int;
|
||||
};
|
||||
|
||||
std::ifstream s("/dev/urandom");
|
||||
if(s.good())
|
||||
s.read(seed_char, sizeof(seed_char));
|
||||
|
||||
srand(seed_int);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// namespace ircd {
|
||||
|
||||
|
|
106
ircd/rand.cc
Normal file
106
ircd/rand.cc
Normal file
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Charybdis Development Team
|
||||
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice is present in all copies.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*
|
||||
*/
|
||||
|
||||
decltype(ircd::rand::device) ircd::rand::device
|
||||
{
|
||||
// on linux: uses RDRND or /dev/urandom
|
||||
// on windows: TODO: verify construction source
|
||||
};
|
||||
|
||||
decltype(ircd::rand::mt) ircd::rand::mt
|
||||
{
|
||||
device()
|
||||
};
|
||||
|
||||
decltype(ircd::rand::dict::alnum) ircd::rand::dict::alnum
|
||||
{
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
};
|
||||
|
||||
decltype(ircd::rand::dict::alpha) ircd::rand::dict::alpha
|
||||
{
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
};
|
||||
|
||||
decltype(ircd::rand::dict::upper) ircd::rand::dict::upper
|
||||
{
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
};
|
||||
|
||||
decltype(ircd::rand::dict::lower) ircd::rand::dict::lower
|
||||
{
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
};
|
||||
|
||||
decltype(ircd::rand::dict::numeric) ircd::rand::dict::numeric
|
||||
{
|
||||
"0123456789"
|
||||
};
|
||||
|
||||
std::string
|
||||
ircd::rand::string(const std::string &dict,
|
||||
const size_t &len)
|
||||
{
|
||||
std::string ret(len, char());
|
||||
string(dict, len, reinterpret_cast<uint8_t *>(&ret.front()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::rand::string(const std::string &dict,
|
||||
const size_t &len,
|
||||
char *const &buf,
|
||||
const size_t &max)
|
||||
{
|
||||
if(unlikely(!max))
|
||||
return { buf, max };
|
||||
|
||||
const auto size
|
||||
{
|
||||
std::min(len, max - 1)
|
||||
};
|
||||
|
||||
buf[size] = '\0';
|
||||
return string(dict, size, reinterpret_cast<uint8_t *>(buf));
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::rand::string(const std::string &dict,
|
||||
const size_t &len,
|
||||
uint8_t *const &buf)
|
||||
{
|
||||
std::uniform_int_distribution<size_t> dist
|
||||
{
|
||||
0, dict.size() - 1
|
||||
};
|
||||
|
||||
std::generate(buf, buf + len, [&dict, &dist]
|
||||
() -> uint8_t
|
||||
{
|
||||
return dict.at(dist(mt));
|
||||
});
|
||||
|
||||
return string_view
|
||||
{
|
||||
reinterpret_cast<const char *>(buf), len
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue