OpenVPN 3 Core Library
Loading...
Searching...
No Matches
durhelper.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_TIME_DURHELPER_H
13#define OPENVPN_TIME_DURHELPER_H
14
16#include <openvpn/time/time.hpp>
18
19namespace openvpn {
20inline void set_duration_parm(Time::Duration &dur,
21 const std::string &name,
22 const std::string &valstr,
23 const unsigned int min_value,
24 const bool x2, // multiply result by 2
25 const bool ms) // values are in milliseconds rather than seconds
26{
27 const unsigned int maxdur = ms ? 1000 * 60 * 60 * 24 : 60 * 60 * 24 * 7; // maximum duration -- milliseconds: 1 day, seconds: 7 days
28 unsigned int value = 0;
29 const bool status = parse_number<unsigned int>(valstr, value);
30 if (!status)
31 OPENVPN_THROW_ARG1(option_error, ERR_INVALID_OPTION_VAL, name << ": error parsing number of " << (ms ? "milliseconds" : "seconds"));
32 if (x2)
33 value *= 2;
34 if (value == 0 || value > maxdur)
35 value = maxdur;
36 if (value < min_value)
37 value = min_value;
38 dur = ms ? Time::Duration::milliseconds(value) : Time::Duration::seconds(value);
39}
40
41inline const Option *load_duration_parm(Time::Duration &dur,
42 const std::string &name,
43 const OptionList &opt,
44 const unsigned int min_value,
45 const bool x2,
46 const bool allow_ms)
47{
48 // look for milliseconds given as <name>-ms
49 if (allow_ms)
50 {
51 const Option *o = opt.get_ptr(name + "-ms");
52 if (o)
53 {
54 set_duration_parm(dur, name, o->get(1, 16), min_value, x2, true);
55 return o;
56 }
57 }
58
59 // look for seconds given as <name>
60 {
61 const Option *o = opt.get_ptr(name);
62 if (o)
63 set_duration_parm(dur, name, o->get(1, 16), allow_ms ? 1 : min_value, x2, false);
64 return o;
65 }
66}
67
68inline Time::Duration load_duration_default(const std::string &name,
69 const OptionList &opt,
70 const Time::Duration &default_duration,
71 const unsigned int min_value,
72 const bool x2,
73 const bool allow_ms)
74{
75 Time::Duration ret(default_duration);
76 load_duration_parm(ret, name, opt, min_value, x2, allow_ms);
77 return ret;
78}
79
80inline Time::Duration skew_duration(const Time::Duration &dur,
81 const Time::Duration &min,
82 const unsigned int flux_order,
83 RandomAPI &rng)
84{
85 const unsigned int range = 1 << flux_order;
86 const int delta = int(rng.rand_get<unsigned int>() & (range - 1)) - int(range >> 1);
87 const Time::Duration ret = dur + delta;
88 if (ret >= min)
89 return ret;
90 else
91 return min;
92}
93} // namespace openvpn
94
95#endif
const Option * get_ptr(const std::string &name) const
Definition options.hpp:1186
const std::string & get(const size_t index, const size_t max_len) const
Definition options.hpp:187
Abstract base class for random number generators.
Definition randapi.hpp:39
T rand_get()
Create a data object filled with random bytes.
Definition randapi.hpp:86
#define OPENVPN_THROW_ARG1(exc, arg, stuff)
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
const Option * load_duration_parm(Time::Duration &dur, const std::string &name, const OptionList &opt, const unsigned int min_value, const bool x2, const bool allow_ms)
Definition durhelper.hpp:41
void set_duration_parm(Time::Duration &dur, const std::string &name, const std::string &valstr, const unsigned int min_value, const bool x2, const bool ms)
Definition durhelper.hpp:20
Time::Duration skew_duration(const Time::Duration &dur, const Time::Duration &min, const unsigned int flux_order, RandomAPI &rng)
Definition durhelper.hpp:80
Time::Duration load_duration_default(const std::string &name, const OptionList &opt, const Time::Duration &default_duration, const unsigned int min_value, const bool x2, const bool allow_ms)
Definition durhelper.hpp:68
Implementation of the base classes for random number generators.
std::string ret