forked from battlesnake/tun-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.hpp
More file actions
63 lines (48 loc) · 2 KB
/
Config.hpp
File metadata and controls
63 lines (48 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <ostream>
#include <fstream>
#include <streambuf>
#include <string>
#include <cstddef>
#include <regex>
#include "IpAddress.hpp"
namespace IpLink {
/* Using X-macro pattern */
#define X_CONFIG \
X(uart, string, "/dev/ttyS0", string, string, "Serial device path") \
X(baud, int, 115200, strtonatural, std::to_string, "Serial baud rate") \
X(ifname, string, "uart0", string, string, "TUN interface name") \
X(mtu, int, 115200/32, strtonatural, std::to_string, "Interface MTU") \
X(addr, ip_address, "10.101.0.1/30", ip_address, std::to_string, "Local IP address") \
X(keepalive_interval, int, 500, strtonatural, std::to_string, "Keep-alive interval in milliseconds (zero to disable)") \
X(keepalive_limit, int, 3, strtonatural, std::to_string, "Number of missed keep-alive messages before assuming peer has disconnected (limit must be greater than one if enabled)") \
X(updown, bool, false, strtobool, booltostr, "Set TUN up/down in response to peer connection/disconnection (requires keep-alives to be enabled)") \
X(daemon, bool, false, strtobool, booltostr, "Fork to background") \
X(verbose, bool, false, strtobool, booltostr, "Enable extra logging") \
X(meter, bool, false, strtobool, booltostr, "Display data meter")
class Config
{
using string = std::string;
public:
struct parse_error :
std::runtime_error
{
using std::runtime_error::runtime_error;
};
#define X(name, type, def, parse, format, help) type name = type(def);
X_CONFIG;
#undef X
Config() = default;
void set(const std::string& key, const std::string& value);
void parse_args(int argc, char *argv[], std::ostream& os);
void parse_config(const std::string& config);
static void dump_var(std::ostream& os, const char *name, const char *type, const char *def, const char *help, const std::string& value, bool with_help);
void dump(std::ostream& os, bool with_help) const;
void help(std::ostream& os);
bool shown_help = false;
void validate() const;
};
#if ! defined KEEP_X_CONFIG
#undef X_CONFIG
#endif
}