File: gw.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (203 lines) | stat: -rw-r--r-- 5,509 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

// Find default gateways on Linux using ip route command

#ifndef OPENVPN_NETCONF_LINUX_GW_H
#define OPENVPN_NETCONF_LINUX_GW_H

#include <string>
#include <limits>

#include <openvpn/common/exception.hpp>
#include <openvpn/common/number.hpp>
#include <openvpn/common/split.hpp>
#include <openvpn/common/splitlines.hpp>
#include <openvpn/common/process.hpp>
#include <openvpn/addr/route.hpp>

namespace openvpn {
class LinuxGW
{
  public:
    OPENVPN_EXCEPTION(linux_gw_error);

    LinuxGW(const std::string &ip_route_show_txt, const bool ignore_errors)
    {
        int best_metric = std::numeric_limits<int>::max();

        SplitLines sl(ip_route_show_txt);
        while (sl())
        {
            const std::string &line = sl.line_ref();

            try
            {
                // parse an output line generated by "ip [-6] route show"
                const std::vector<std::string> v = Split::by_space<std::vector<std::string>, NullLex, SpaceMatch, Split::NullLimit>(line);

                // blank line?
                if (v.empty())
                    continue;

                // only interested in default routes
                if (v[0] != "default")
                    continue;

                // parse out route information
                enum RouteInfo
                {
                    INITIAL,
                    VIA,
                    DEV,
                    METRIC,
                };

                std::string d;
                IP::Addr a;
                int m = std::numeric_limits<int>::max();

                RouteInfo ri = INITIAL;
                for (const auto &term : v)
                {
                    switch (ri)
                    {
                    case INITIAL:
                        if (term == "via")
                            ri = VIA;
                        else if (term == "dev")
                            ri = DEV;
                        else if (term == "metric")
                            ri = METRIC;
                        else
                            ri = INITIAL;
                        break;
                    case VIA:
                        a = IP::Addr(term, "via");
                        ri = INITIAL;
                        break;
                    case DEV:
                        d = validate_dev(term);
                        ri = INITIAL;
                        break;
                    case METRIC:
                        m = parse_number_throw<int>(term, "bad metric");
                        ri = INITIAL;
                        break;
                    }
                }

                // best metric?
                if (m < best_metric || best_metric == std::numeric_limits<int>::max())
                {
                    best_metric = m;
                    dev_ = std::move(d);
                    addr_ = a;
                }
            }
            catch (const std::exception &e)
            {
                if (!ignore_errors)
                    OPENVPN_THROW(linux_gw_error, "error parsing line: " << line << " : " << e.what());
            }
        }
    }

    static std::string ip_route_show(const bool ipv6)
    {
        RedirectPipe::InOut pipe;
        Argv argv;
        argv.emplace_back("/sbin/ip");
        if (ipv6)
            argv.emplace_back("-6");
        argv.emplace_back("route");
        argv.emplace_back("show");
        const int status = system_cmd(argv[0], argv, nullptr, pipe, 0, nullptr);
        if (status != 0)
            OPENVPN_THROW(linux_gw_error, "command returned error status " << status << " : " << argv.to_string());
        return pipe.out;
    }

    const std::string &dev() const
    {
        return dev_;
    }

    const IP::Addr &addr() const
    {
        return addr_;
    }

    bool defined() const
    {
        return !dev_.empty() && addr_.defined();
    }

    std::string to_string() const
    {
        return dev_ + '/' + addr_.to_string();
    }

  private:
    std::string validate_dev(const std::string &dev)
    {
        if (dev.empty())
            OPENVPN_THROW_EXCEPTION("dev is empty");
        return dev;
    }

    std::string dev_;
    IP::Addr addr_;
};

struct LinuxGW46
{
    LinuxGW46(const bool ignore_errors)
        : v4(LinuxGW::ip_route_show(false), ignore_errors),
          v6(LinuxGW::ip_route_show(true), ignore_errors)
    {
    }

    std::string to_string() const
    {
        std::string ret = "[";
        if (v4.defined())
        {
            ret += "4:";
            ret += v4.to_string();
        }
        if (v6.defined())
        {
            if (v4.defined())
                ret += ' ';
            ret += "6:";
            ret += v6.to_string();
        }
        ret += "]";
        return ret;
    }

    std::string dev() const
    {
        if (v4.defined())
            return v4.dev();
        else if (v6.defined())
            return v6.dev();
        else
            throw LinuxGW::linux_gw_error("cannot determine gateway interface");
    }

    LinuxGW v4;
    LinuxGW v6;
};
} // namespace openvpn

#endif