2020-07-28 17:54:42 +00:00
|
|
|
#include <lokimq/lokimq.h>
|
2020-07-29 15:43:48 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2020-07-28 17:54:42 +00:00
|
|
|
#include <cxxopts.hpp>
|
|
|
|
#include <future>
|
2020-07-29 15:43:48 +00:00
|
|
|
#include <vector>
|
2020-08-10 15:07:33 +00:00
|
|
|
#include <array>
|
2020-08-10 18:42:58 +00:00
|
|
|
#include <net/net.hpp>
|
2020-08-19 14:40:58 +00:00
|
|
|
#include <sys/wait.h>
|
2020-07-29 15:43:48 +00:00
|
|
|
|
2020-08-10 15:06:51 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// add the unholy windows headers for iphlpapi
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <iphlpapi.h>
|
2020-08-11 22:27:50 +00:00
|
|
|
#include <strsafe.h>
|
2020-08-10 15:06:51 +00:00
|
|
|
#endif
|
|
|
|
|
2020-07-29 15:43:48 +00:00
|
|
|
/// do a lokimq request on an lmq instance blocking style
|
|
|
|
/// returns a json object parsed from the result
|
|
|
|
std::optional<nlohmann::json>
|
|
|
|
LMQ_Request(
|
|
|
|
lokimq::LokiMQ& lmq,
|
|
|
|
const lokimq::ConnectionID& id,
|
|
|
|
std::string_view method,
|
|
|
|
std::optional<nlohmann::json> args = std::nullopt)
|
|
|
|
{
|
|
|
|
std::promise<std::optional<std::string>> result_promise;
|
|
|
|
|
|
|
|
auto handleRequest = [&result_promise](bool success, std::vector<std::string> result) {
|
|
|
|
if ((not success) or result.empty())
|
|
|
|
{
|
|
|
|
result_promise.set_value(std::nullopt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result_promise.set_value(result[0]);
|
|
|
|
};
|
|
|
|
if (args.has_value())
|
|
|
|
{
|
|
|
|
lmq.request(id, method, handleRequest, args->dump());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lmq.request(id, method, handleRequest);
|
|
|
|
}
|
|
|
|
auto ftr = result_promise.get_future();
|
|
|
|
const auto str = ftr.get();
|
|
|
|
if (str.has_value())
|
|
|
|
return nlohmann::json::parse(*str);
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get every ip address that is a gateway that isn't owned by interface with name ifname
|
|
|
|
std::vector<std::string>
|
|
|
|
GetGatewaysNotOnInterface(std::string ifname);
|
|
|
|
|
|
|
|
/// add route to ipaddr via gateway ip
|
|
|
|
void
|
|
|
|
AddRoute(std::string ipaddr, std::string gateway);
|
|
|
|
|
|
|
|
/// delete route to ipaddr via gateway ip
|
|
|
|
void
|
|
|
|
DelRoute(std::string ipaddr, std::string gateway);
|
|
|
|
|
|
|
|
/// add default route via interface with name ifname
|
|
|
|
void
|
|
|
|
AddDefaultRouteViaInterface(std::string ifname);
|
|
|
|
|
|
|
|
/// delete default route via interface with name ifname
|
|
|
|
void
|
|
|
|
DelDefaultRouteViaInterface(std::string ifname);
|
2020-07-28 17:54:42 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
cxxopts::Options opts("lokinet-vpn", "LokiNET vpn control utility");
|
|
|
|
|
|
|
|
opts.add_options()("v,verbose", "Verbose", cxxopts::value<bool>())(
|
|
|
|
"h,help", "help", cxxopts::value<bool>())("up", "put vpn up", cxxopts::value<bool>())(
|
|
|
|
"down", "put vpn down", cxxopts::value<bool>())(
|
|
|
|
"exit", "specify exit node address", cxxopts::value<std::string>())(
|
2020-07-29 15:43:48 +00:00
|
|
|
"rpc", "rpc url for lokinet", cxxopts::value<std::string>())(
|
|
|
|
"endpoint", "endpoint to use", cxxopts::value<std::string>())(
|
|
|
|
"token", "exit auth token to use", cxxopts::value<std::string>());
|
2020-07-28 17:54:42 +00:00
|
|
|
|
|
|
|
lokimq::address rpcURL("tcp://127.0.0.1:1190");
|
|
|
|
std::string exitAddress;
|
2020-07-29 15:43:48 +00:00
|
|
|
std::string endpoint = "default";
|
|
|
|
std::optional<std::string> token;
|
2020-07-28 17:54:42 +00:00
|
|
|
lokimq::LogLevel logLevel = lokimq::LogLevel::warn;
|
|
|
|
bool goUp = false;
|
|
|
|
bool goDown = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto result = opts.parse(argc, argv);
|
|
|
|
|
|
|
|
if (result.count("help") > 0)
|
|
|
|
{
|
|
|
|
std::cout << opts.help() << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.count("verbose") > 0)
|
|
|
|
{
|
|
|
|
logLevel = lokimq::LogLevel::debug;
|
|
|
|
}
|
|
|
|
if (result.count("rpc") > 0)
|
|
|
|
{
|
|
|
|
rpcURL = lokimq::address(result["rpc"].as<std::string>());
|
|
|
|
}
|
|
|
|
if (result.count("exit") > 0)
|
|
|
|
{
|
|
|
|
exitAddress = result["exit"].as<std::string>();
|
|
|
|
}
|
|
|
|
goUp = result.count("up") > 0;
|
|
|
|
goDown = result.count("down") > 0;
|
2020-07-29 15:43:48 +00:00
|
|
|
|
|
|
|
if (result.count("endpoint") > 0)
|
|
|
|
{
|
|
|
|
endpoint = result["endpoint"].as<std::string>();
|
|
|
|
}
|
|
|
|
if (result.count("token") > 0)
|
|
|
|
{
|
|
|
|
token = result["token"].as<std::string>();
|
|
|
|
}
|
2020-07-28 17:54:42 +00:00
|
|
|
}
|
|
|
|
catch (const cxxopts::option_not_exists_exception& ex)
|
|
|
|
{
|
|
|
|
std::cerr << ex.what();
|
|
|
|
std::cout << opts.help() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-29 15:43:48 +00:00
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
std::cout << ex.what() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-28 17:54:42 +00:00
|
|
|
if ((not goUp) and (not goDown))
|
|
|
|
{
|
|
|
|
std::cout << opts.help() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (goUp and exitAddress.empty())
|
|
|
|
{
|
|
|
|
std::cout << "no exit address provided" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lokimq::LokiMQ lmq{[](lokimq::LogLevel lvl, const char* file, int line, std::string msg) {
|
|
|
|
std::cout << lvl << " [" << file << ":" << line << "] " << msg << std::endl;
|
|
|
|
},
|
|
|
|
logLevel};
|
|
|
|
|
|
|
|
lmq.start();
|
|
|
|
|
|
|
|
std::promise<bool> connectPromise;
|
|
|
|
|
|
|
|
const auto connID = lmq.connect_remote(
|
|
|
|
rpcURL,
|
|
|
|
[&connectPromise](auto) { connectPromise.set_value(true); },
|
|
|
|
[&connectPromise](auto, std::string_view msg) {
|
|
|
|
std::cout << "failed to connect to lokinet RPC: " << msg << std::endl;
|
|
|
|
connectPromise.set_value(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
auto ftr = connectPromise.get_future();
|
|
|
|
if (not ftr.get())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:43:48 +00:00
|
|
|
std::vector<std::string> firstHops;
|
|
|
|
std::string ifname;
|
|
|
|
|
|
|
|
const auto maybe_status = LMQ_Request(lmq, connID, "llarp.status");
|
|
|
|
if (not maybe_status.has_value())
|
|
|
|
{
|
|
|
|
std::cout << "call to llarp.status failed" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// extract first hops
|
|
|
|
const auto& links = maybe_status->at("result")["links"]["outbound"];
|
|
|
|
for (const auto& link : links)
|
|
|
|
{
|
|
|
|
const auto& sessions = link["sessions"]["established"];
|
|
|
|
for (const auto& session : sessions)
|
|
|
|
{
|
|
|
|
std::string addr = session["remoteAddr"];
|
|
|
|
const auto pos = addr.find(":");
|
|
|
|
firstHops.push_back(addr.substr(0, pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// get interface name
|
2020-08-10 19:41:01 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// strip off the "::ffff."
|
2020-08-10 19:47:26 +00:00
|
|
|
ifname = maybe_status->at("result")["services"][endpoint]["ifaddr"];
|
2020-08-10 20:18:31 +00:00
|
|
|
const auto pos = ifname.find("/");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
ifname = ifname.substr(0, pos);
|
|
|
|
}
|
2020-08-10 19:41:01 +00:00
|
|
|
#else
|
2020-07-29 15:43:48 +00:00
|
|
|
ifname = maybe_status->at("result")["services"][endpoint]["ifname"];
|
2020-08-10 19:41:01 +00:00
|
|
|
#endif
|
2020-07-29 15:43:48 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
std::cout << "failed to parse result: " << ex.what() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (goUp)
|
|
|
|
{
|
|
|
|
std::optional<nlohmann::json> maybe_result;
|
|
|
|
if (token.has_value())
|
|
|
|
{
|
|
|
|
maybe_result = LMQ_Request(
|
|
|
|
lmq,
|
|
|
|
connID,
|
|
|
|
"llarp.exit",
|
|
|
|
nlohmann::json{{"exit", exitAddress}, {"range", "0.0.0.0/0"}, {"token", *token}});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maybe_result = LMQ_Request(
|
|
|
|
lmq, connID, "llarp.exit", nlohmann::json{{"exit", exitAddress}, {"range", "0.0.0.0/0"}});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not maybe_result.has_value())
|
|
|
|
{
|
|
|
|
std::cout << "could not add exit" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maybe_result->contains("error") and maybe_result->at("error").is_string())
|
|
|
|
{
|
|
|
|
std::cout << maybe_result->at("error").get<std::string>() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto gateways = GetGatewaysNotOnInterface(ifname);
|
|
|
|
if (gateways.empty())
|
|
|
|
{
|
|
|
|
std::cout << "cannot determine default gateway" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const auto ourGateway = gateways[0];
|
|
|
|
for (const auto& ip : firstHops)
|
|
|
|
{
|
|
|
|
AddRoute(ip, ourGateway);
|
|
|
|
}
|
|
|
|
AddDefaultRouteViaInterface(ifname);
|
|
|
|
}
|
|
|
|
if (goDown)
|
|
|
|
{
|
|
|
|
DelDefaultRouteViaInterface(ifname);
|
|
|
|
const auto gateways = GetGatewaysNotOnInterface(ifname);
|
2020-08-10 18:42:58 +00:00
|
|
|
if (gateways.empty())
|
|
|
|
{
|
|
|
|
std::cout << "cannot determine default gateway" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-29 15:43:48 +00:00
|
|
|
const auto ourGateway = gateways[0];
|
|
|
|
for (const auto& ip : firstHops)
|
|
|
|
{
|
|
|
|
DelRoute(ip, ourGateway);
|
|
|
|
}
|
|
|
|
LMQ_Request(lmq, connID, "llarp.exit", nlohmann::json{{"range", "0.0.0.0/0"}, {"unmap", true}});
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:54:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 15:43:48 +00:00
|
|
|
|
2020-08-10 20:48:13 +00:00
|
|
|
void
|
|
|
|
Execute(std::string cmd)
|
|
|
|
{
|
|
|
|
std::cout << cmd << std::endl;
|
2020-08-19 14:40:58 +00:00
|
|
|
std::vector<std::string> parts_str;
|
|
|
|
std::vector<const char *> parts_raw;
|
|
|
|
std::stringstream in(cmd);
|
|
|
|
for(std::string part; std::getline(in, part, ' '); )
|
|
|
|
{
|
|
|
|
if(part.empty())
|
|
|
|
continue;
|
|
|
|
parts_str.push_back(part);
|
|
|
|
}
|
|
|
|
for(const auto & part : parts_str)
|
|
|
|
{
|
|
|
|
parts_raw.push_back(part.c_str());
|
|
|
|
}
|
|
|
|
parts_raw.push_back(nullptr);
|
|
|
|
const auto pid = fork();
|
|
|
|
if(pid == -1)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("failed to fork");
|
|
|
|
}
|
|
|
|
else if(pid == 0)
|
|
|
|
{
|
|
|
|
char * const * args = (char * const *) &parts_raw[1];
|
|
|
|
exit(execv(parts_raw[0], args));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
waitpid(pid, 0, 0);
|
|
|
|
}
|
|
|
|
|
2020-08-10 20:48:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 15:43:48 +00:00
|
|
|
void
|
|
|
|
AddRoute(std::string ip, std::string gateway)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
#ifdef __linux__
|
|
|
|
ss << "ip route add " << ip << "/32 via " << gateway;
|
2020-08-10 15:06:51 +00:00
|
|
|
#elif _WIN32
|
2020-08-10 20:48:13 +00:00
|
|
|
ss << "route ADD " << ip << " MASK 255.255.255.255 " << gateway << " METRIC 2";
|
2020-08-10 18:15:39 +00:00
|
|
|
#elif __APPLE__
|
2020-08-10 17:13:28 +00:00
|
|
|
ss << "route -n add -host " << ip << " " << gateway;
|
2020-07-29 15:43:48 +00:00
|
|
|
#else
|
|
|
|
#error unsupported platform
|
|
|
|
#endif
|
2020-08-10 20:48:13 +00:00
|
|
|
Execute(ss.str());
|
2020-07-29 15:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelRoute(std::string ip, std::string gateway)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
#ifdef __linux__
|
|
|
|
ss << "ip route del " << ip << "/32 via " << gateway;
|
2020-08-10 15:06:51 +00:00
|
|
|
#elif _WIN32
|
2020-08-10 20:48:13 +00:00
|
|
|
ss << "route DELETE " << ip << " MASK 255.255.255.255 " << gateway << " METRIC 2";
|
2020-08-10 18:15:39 +00:00
|
|
|
#elif __APPLE__
|
2020-08-10 17:13:28 +00:00
|
|
|
ss << "route -n delete -host " << ip << " " << gateway;
|
2020-07-29 15:43:48 +00:00
|
|
|
#else
|
|
|
|
#error unsupported platform
|
|
|
|
#endif
|
2020-08-10 20:48:13 +00:00
|
|
|
Execute(ss.str());
|
2020-07-29 15:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddDefaultRouteViaInterface(std::string ifname)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2020-08-11 19:31:03 +00:00
|
|
|
Execute("ip route add default dev " + ifname);
|
2020-08-10 15:06:51 +00:00
|
|
|
#elif _WIN32
|
2020-08-11 22:27:50 +00:00
|
|
|
ifname.back()++;
|
2020-08-11 19:31:03 +00:00
|
|
|
Execute("route ADD 0.0.0.0 MASK 128.0.0.0 " + ifname);
|
|
|
|
Execute("route ADD 128.0.0.0 MASK 128.0.0.0 " + ifname);
|
2020-08-10 18:15:39 +00:00
|
|
|
#elif __APPLE__
|
2020-08-11 19:31:03 +00:00
|
|
|
Execute("route -n add -net 0.0.0.0 -interface " + ifname);
|
2020-07-29 15:43:48 +00:00
|
|
|
#else
|
|
|
|
#error unsupported platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelDefaultRouteViaInterface(std::string ifname)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2020-08-11 19:31:03 +00:00
|
|
|
Execute("ip route del default dev " + ifname);
|
2020-08-10 15:06:51 +00:00
|
|
|
#elif _WIN32
|
2020-08-11 22:27:50 +00:00
|
|
|
ifname.back()++;
|
2020-08-11 19:31:03 +00:00
|
|
|
Execute("route DELETE 0.0.0.0 MASK 128.0.0.0 " + ifname);
|
|
|
|
Execute("route DELETE 128.0.0.0 MASK 128.0.0.0 " + ifname);
|
2020-08-10 18:15:39 +00:00
|
|
|
#elif __APPLE__
|
2020-08-11 19:31:03 +00:00
|
|
|
Execute("route -n delete -net 0.0.0.0 -interface " + ifname);
|
2020-08-10 17:13:28 +00:00
|
|
|
#else
|
2020-07-29 15:43:48 +00:00
|
|
|
#error unsupported platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
GetGatewaysNotOnInterface(std::string ifname)
|
|
|
|
{
|
2020-08-10 15:06:51 +00:00
|
|
|
std::vector<std::string> gateways;
|
2020-07-29 15:43:48 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
FILE* p = popen("ip route", "r");
|
|
|
|
if (p == nullptr)
|
2020-08-10 15:06:51 +00:00
|
|
|
return gateways;
|
2020-07-29 15:43:48 +00:00
|
|
|
char* line = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read = 0;
|
|
|
|
while ((read = getline(&line, &len, p)) != -1)
|
|
|
|
{
|
|
|
|
std::string line_str(line, len);
|
|
|
|
std::vector<std::string> words;
|
|
|
|
std::istringstream instr(line_str);
|
|
|
|
for (std::string word; std::getline(instr, word, ' ');)
|
|
|
|
{
|
|
|
|
words.emplace_back(std::move(word));
|
|
|
|
}
|
|
|
|
if (words[0] == "default" and words[1] == "via" and words[3] == "dev" and words[4] != ifname)
|
|
|
|
{
|
|
|
|
gateways.emplace_back(std::move(words[2]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(p);
|
|
|
|
return gateways;
|
2020-08-10 15:06:51 +00:00
|
|
|
#elif _WIN32
|
|
|
|
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
|
|
|
|
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
|
|
|
|
PMIB_IPFORWARDTABLE pIpForwardTable;
|
2020-08-10 15:31:30 +00:00
|
|
|
DWORD dwSize = 0;
|
|
|
|
DWORD dwRetVal = 0;
|
2020-08-10 15:06:51 +00:00
|
|
|
|
|
|
|
pIpForwardTable = (MIB_IPFORWARDTABLE*)MALLOC(sizeof(MIB_IPFORWARDTABLE));
|
|
|
|
if (pIpForwardTable == nullptr)
|
|
|
|
return gateways;
|
2020-08-10 19:59:58 +00:00
|
|
|
|
|
|
|
if (GetIpForwardTable(pIpForwardTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
|
2020-08-10 15:06:51 +00:00
|
|
|
{
|
|
|
|
FREE(pIpForwardTable);
|
2020-08-10 19:59:58 +00:00
|
|
|
pIpForwardTable = (MIB_IPFORWARDTABLE*)MALLOC(dwSize);
|
|
|
|
if (pIpForwardTable == nullptr)
|
|
|
|
{
|
|
|
|
return gateways;
|
|
|
|
}
|
2020-08-10 15:06:51 +00:00
|
|
|
}
|
2020-08-10 19:59:58 +00:00
|
|
|
|
|
|
|
if ((dwRetVal = GetIpForwardTable(pIpForwardTable, &dwSize, 0)) == NO_ERROR)
|
2020-08-10 15:06:51 +00:00
|
|
|
{
|
2020-08-10 19:59:58 +00:00
|
|
|
for (int i = 0; i < (int)pIpForwardTable->dwNumEntries; i++)
|
2020-08-10 15:06:51 +00:00
|
|
|
{
|
2020-08-10 19:59:58 +00:00
|
|
|
struct in_addr gateway, interface_addr;
|
|
|
|
gateway.S_un.S_addr = (u_long)pIpForwardTable->table[i].dwForwardDest;
|
|
|
|
interface_addr.S_un.S_addr = (u_long)pIpForwardTable->table[i].dwForwardNextHop;
|
|
|
|
std::array<char, 128> interface_str{};
|
2020-08-11 22:27:50 +00:00
|
|
|
StringCchCopy(interface_str.data(), interface_str.size(), inet_ntoa(interface_addr));
|
2020-08-10 19:59:58 +00:00
|
|
|
std::string interface_name{interface_str.data()};
|
|
|
|
if ((!gateway.S_un.S_addr) and interface_name != ifname)
|
|
|
|
{
|
|
|
|
gateways.push_back(std::move(interface_name));
|
|
|
|
}
|
2020-08-10 15:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-10 19:59:58 +00:00
|
|
|
FREE(pIpForwardTable);
|
2020-08-10 15:06:51 +00:00
|
|
|
#undef MALLOC
|
|
|
|
#undef FREE
|
|
|
|
return gateways;
|
2020-08-10 18:15:39 +00:00
|
|
|
#elif __APPLE__
|
2020-08-10 18:45:19 +00:00
|
|
|
const auto maybe = llarp::GetIFAddr(ifname);
|
|
|
|
if (not maybe.has_value())
|
2020-08-10 18:42:58 +00:00
|
|
|
return gateways;
|
|
|
|
const auto interface = maybe->toString();
|
2020-08-10 17:13:28 +00:00
|
|
|
// mac os is so godawful man
|
|
|
|
FILE* p = popen("netstat -rn -f inet", "r");
|
|
|
|
if (p == nullptr)
|
|
|
|
return gateways;
|
|
|
|
char* line = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read = 0;
|
|
|
|
while ((read = getline(&line, &len, p)) != -1)
|
|
|
|
{
|
|
|
|
std::string line_str(line, len);
|
2020-08-10 18:18:20 +00:00
|
|
|
if (line_str.find("default") == 0)
|
2020-08-10 17:13:28 +00:00
|
|
|
{
|
|
|
|
line_str = line_str.substr(7);
|
|
|
|
while (line_str[0] == ' ')
|
|
|
|
{
|
|
|
|
line_str = line_str.substr(1);
|
|
|
|
}
|
|
|
|
const auto pos = line_str.find(" ");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2020-08-10 18:18:20 +00:00
|
|
|
auto gateway = line_str.substr(0, pos);
|
2020-08-10 18:42:58 +00:00
|
|
|
if (gateway != interface)
|
2020-08-10 18:18:20 +00:00
|
|
|
gateways.emplace_back(std::move(gateway));
|
2020-08-10 17:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(p);
|
|
|
|
return gateways;
|
2020-07-29 15:43:48 +00:00
|
|
|
#else
|
|
|
|
#error unsupported platform
|
|
|
|
#endif
|
|
|
|
}
|