added the method to get the route table

This commit is contained in:
Fedotov Anton 2021-12-02 15:08:54 +03:00
parent 340639b3c8
commit 1a16a61ced
5 changed files with 176 additions and 40 deletions

View file

@ -11,6 +11,20 @@ namespace adpinfo{
// {false,""} - no error // {false,""} - no error
// {true,"descr"} - error with description // {true,"descr"} - error with description
using RET_TYPE = std::tuple<bool, std::string>; using RET_TYPE = std::tuple<bool, std::string>;
//using ROUTE_TABLE = std::tuple<std::string, std::string, std::string>;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
* Getting the route table
*/
typedef struct route_table{
std::string szDestIp;
std::string szMaskIp;
std::string szGatewayIp;
std::string szInterfaceIp;
}route_table;
std::vector<std::tuple<std::string,std::string,std::string,std::string>>get_route_table();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* /*
* The object uses for collect the information about active network adapters/interfaces * The object uses for collect the information about active network adapters/interfaces
@ -52,6 +66,7 @@ public:
std::string_view get_adapter_route_gateway()const; std::string_view get_adapter_route_gateway()const;
std::string_view get_adapter_local_address()const; std::string_view get_adapter_local_address()const;
std::string_view get_adapter_local_gateway()const; std::string_view get_adapter_local_gateway()const;
}; };
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -7,37 +7,48 @@
#include <cassert> #include <cassert>
#include <windows.h> #include <windows.h>
//#include <atlbase.h>
//#include <wbemcli.h>
//#include <comutil.h>
#include <iphlpapi.h> #include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "iphlpapi.lib")
//#pragma comment(lib, "wbemuuid.lib")
//#pragma comment(lib, "comsuppw.lib")
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static std::string convert_wide_to_ansi(const std::wstring& widestring) { //static std::string convert_wide_to_ansi(const std::wstring& widestring) {
auto nchars = WideCharToMultiByte( // auto nchars = WideCharToMultiByte(
CP_ACP, // CP_ACP,
0, // 0,
widestring.c_str(), // widestring.c_str(),
static_cast<int>(widestring.length() + 1), // static_cast<int>(widestring.length() + 1),
nullptr, // nullptr,
0, // 0,
nullptr, // nullptr,
nullptr); // nullptr);
std::string converted_string{}; // std::string converted_string{};
converted_string.resize(nchars); // converted_string.resize(nchars);
WideCharToMultiByte(CP_ACP, // WideCharToMultiByte(CP_ACP,
0, // 0,
widestring.c_str(), // widestring.c_str(),
-1, // -1,
&converted_string[0], // &converted_string[0],
static_cast<int>(widestring.length()), // static_cast<int>(widestring.length()),
nullptr, // nullptr,
nullptr); // nullptr);
return converted_string; // return converted_string;
//}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static std::string get_founded_route(std::string_view ip_address){
MIB_IPFORWARDROW br{0};
ZeroMemory(&br, sizeof(MIB_IPFORWARDROW));
struct in_addr ia;
std::string sTmp{};
DWORD dwRes = GetBestRoute(inet_addr(ip_address.data()), 0, &br);
if( dwRes == NO_ERROR ){
ia.S_un.S_addr = (u_long) br.dwForwardDest;
sTmp = inet_ntoa(ia);
qDebug()<<"Best Route:"<< sTmp.data();
}
return sTmp;
} }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static std::string get_route_gateway() static std::string get_route_gateway()
@ -52,12 +63,9 @@ static std::string get_route_gateway()
DWORD dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder); DWORD dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICIENT_BUFFER) { if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE) malloc(dwSize))) { if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE) malloc(dwSize))) {
//printf("Malloc failed. Out of memory.\n");
return {"Out of memory"}; return {"Out of memory"};
} }
// Now get the table.
dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder); dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
} }
if (dwStatus != ERROR_SUCCESS) { if (dwStatus != ERROR_SUCCESS) {
@ -68,7 +76,6 @@ static std::string get_route_gateway()
const DWORD end = pIpForwardTable->dwNumEntries; const DWORD end = pIpForwardTable->dwNumEntries;
for (DWORD i = 0; i < end; i++) { for (DWORD i = 0; i < end; i++) {
if (pIpForwardTable->table[i].dwForwardDest == 0) { if (pIpForwardTable->table[i].dwForwardDest == 0) {
// We have found the default gateway.
IpAddr.S_un.S_addr = IpAddr.S_un.S_addr =
(u_long) pIpForwardTable->table[i].dwForwardNextHop; (u_long) pIpForwardTable->table[i].dwForwardNextHop;
strcpy_s(szGatewayIp, sizeof (szGatewayIp), inet_ntoa(IpAddr)); strcpy_s(szGatewayIp, sizeof (szGatewayIp), inet_ntoa(IpAddr));
@ -82,6 +89,80 @@ static std::string get_route_gateway()
return route_gateway; return route_gateway;
} }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static std::string get_interface_ip(DWORD index){
std::string _ipaddr{'\0'};
std::vector<BYTE> buffer{};
IP_ADAPTER_INFO *adapter_info{nullptr};
DWORD result{ERROR_BUFFER_OVERFLOW};
ULONG buffer_len = sizeof(IP_ADAPTER_INFO) * 10;
while (result == ERROR_BUFFER_OVERFLOW){
buffer.resize(buffer_len);
adapter_info = reinterpret_cast<IP_ADAPTER_INFO*>(&buffer[0]);
result = GetAdaptersInfo(adapter_info, &buffer_len);
if (result == ERROR_NO_DATA){
return _ipaddr;
}
}//end while
if (result != NO_ERROR){
return _ipaddr;
}
IP_ADAPTER_INFO *adapter_iterator = adapter_info;
while(adapter_iterator){
if (adapter_iterator->Index == index)
break;
adapter_iterator = adapter_iterator->Next;
}//end while
if (adapter_iterator != nullptr || adapter_iterator != 0x0 || adapter_iterator != NULL)
_ipaddr = std::string(adapter_iterator->IpAddressList.IpAddress.String, 16);
else
_ipaddr = "127.0.0.1";
return _ipaddr;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace adpinfo {
std::vector<std::tuple<std::string, std::string, std::string, std::string>>get_route_table(){
std::vector< std::tuple<std::string, std::string, std::string, std::string >>ret_table{};
PMIB_IPFORWARDTABLE p_table{nullptr};
struct in_addr ip_addr{};
DWORD size{};
p_table = (MIB_IPFORWARDTABLE *) HeapAlloc(GetProcessHeap(), 0, (sizeof (MIB_IPFORWARDTABLE)));
if (p_table == nullptr)
{
return ret_table;
}
if (GetIpForwardTable(p_table, &size, 0) == ERROR_INSUFFICIENT_BUFFER) {
HeapFree(GetProcessHeap(), 0, p_table);
p_table = (MIB_IPFORWARDTABLE *) HeapAlloc(GetProcessHeap(), 0, size);
if (p_table == nullptr) {
return ret_table;
}
}
DWORD ret = GetIpForwardTable(p_table, &size, 0);
if (ret == NO_ERROR) {
const int &numEntries = static_cast<int>(p_table->dwNumEntries);
for (int i = 0; i <numEntries; ++i) {
char szDestIp[128]{};
char szMaskIp[128]{};
char szGatewayIp[128]{};
char szInterfaceIp[21]{};
ip_addr.S_un.S_addr = (u_long) p_table->table[i].dwForwardDest;
strcpy_s(szDestIp, sizeof (szDestIp), inet_ntoa(ip_addr));
ip_addr.S_un.S_addr = (u_long) p_table->table[i].dwForwardMask;
strcpy_s(szMaskIp, sizeof (szMaskIp), inet_ntoa(ip_addr));
ip_addr.S_un.S_addr = (u_long) p_table->table[i].dwForwardNextHop;
strcpy_s(szGatewayIp, sizeof (szGatewayIp), inet_ntoa(ip_addr));
const auto &ifname = get_interface_ip(p_table->table[i].dwForwardIfIndex);
const auto &ifnameSize = ifname.length() + 1;
strcpy_s(szInterfaceIp, ifnameSize, ifname.data());
const auto &mt = std::make_tuple(std::string(szDestIp), std::string(szMaskIp), std::string(szGatewayIp), std::string(szInterfaceIp));
ret_table.emplace_back(mt);
}//end for
}//end if
return ret_table;
}
}//end namespace adpinfo
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace adpinfo { namespace adpinfo {
void NetAdpInfo::Adapter::set_name(std::string_view value){ void NetAdpInfo::Adapter::set_name(std::string_view value){
@ -144,13 +225,13 @@ RET_TYPE NetAdpInfo::collect_adapters_data(){
std::string lgw = adapter_iterator->GatewayList.IpAddress.String; std::string lgw = adapter_iterator->GatewayList.IpAddress.String;
if (lgw.length() == 0 || lgw.find("0.0.0.0") != std::string::npos) if (lgw.length() == 0 || lgw.find("0.0.0.0") != std::string::npos)
{ {
lgw = get_founded_route("8.8.8.8");
if (adapter_iterator->DhcpEnabled == 1) if (adapter_iterator->DhcpEnabled == 1)
{ {
lgw = adapter_iterator->DhcpServer.IpAddress.String; lgw = adapter_iterator->DhcpServer.IpAddress.String;
} }
} }
_tmp->set_local_gateway(lgw); _tmp->set_local_gateway(lgw);
//_tmp->set_local_gateway(adapter_iterator->GatewayList.IpAddress.String);
_tmp->set_route_gateway(get_route_gateway()); _tmp->set_route_gateway(get_route_gateway());
_adapters.emplace_back(_tmp); _adapters.emplace_back(_tmp);
adapter_iterator = adapter_iterator->Next; adapter_iterator = adapter_iterator->Next;

View file

@ -1,11 +1,10 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QFileInfo> #include <QFileInfo>
#include <QProcess> #include <QProcess>
//#include <QRegularExpression>
//#include <QTcpSocket>
#include <QThread> #include <QThread>
#include <chrono> #include <chrono>
#include <algorithm>
#include "debug.h" #include "debug.h"
#include "ikev2_vpn_protocol.h" #include "ikev2_vpn_protocol.h"
@ -171,6 +170,7 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE
//SetEvent(gEvent_handle); //SetEvent(gEvent_handle);
{ {
//get the network settings of adapters //get the network settings of adapters
std::this_thread::sleep_for(std::chrono::seconds(3));
std::string p1,p2,p3; std::string p1,p2,p3;
const auto ret = adpInfo.get_adapter_info(tunnelName().toStdString()); const auto ret = adpInfo.get_adapter_info(tunnelName().toStdString());
if (std::get<0>(ret) == false){ if (std::get<0>(ret) == false){
@ -183,6 +183,19 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE
qDebug()<<"My ikev2 m_routeGateway "<<m_routeGateway; qDebug()<<"My ikev2 m_routeGateway "<<m_routeGateway;
qDebug()<<"My ikev2 m_vpnLocalAddress "<<m_vpnLocalAddress; qDebug()<<"My ikev2 m_vpnLocalAddress "<<m_vpnLocalAddress;
qDebug()<<"My ikev2 m_vpnGateway "<< m_vpnGateway; qDebug()<<"My ikev2 m_vpnGateway "<< m_vpnGateway;
auto ret = adpinfo::get_route_table();
qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
{
for (const auto &itret: ret){
const auto ip = std::get<0>(itret);
const auto msk = std::get<1>(itret);
const auto gw = std::get<2>(itret);
const auto itf = std::get<3>(itret);
qDebug()<<"IP["<<ip.c_str()<<"]"<<"Mask["<<msk.c_str()<<"]"<<"gateway["<<gw.c_str()<<"]"<<"Interface["<<itf.c_str()<<"]";
}
}
qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
} }
} }
break; break;

View file

@ -246,7 +246,11 @@ void OpenVpnProtocol::onReadyReadDataFromManagementServer()
sendInitialData(); sendInitialData();
} else if (line.startsWith(">STATE")) { } else if (line.startsWith(">STATE")) {
if (line.contains("CONNECTED,SUCCESS")) { if (line.contains("CONNECTED,SUCCESS")) {
sendByteCount();
stopTimeoutTimer();
setConnectionState(VpnProtocol::Connected);
{ {
std::this_thread::sleep_for(std::chrono::seconds(3));
std::string p1,p2,p3; std::string p1,p2,p3;
const auto &ret = adpInfo.get_adapter_info("TAP-Windows Adapter V9"); const auto &ret = adpInfo.get_adapter_info("TAP-Windows Adapter V9");
if (std::get<0>(ret) == false){ if (std::get<0>(ret) == false){
@ -259,14 +263,24 @@ void OpenVpnProtocol::onReadyReadDataFromManagementServer()
qDebug()<<"My openvpn m_routeGateway "<<m_routeGateway; qDebug()<<"My openvpn m_routeGateway "<<m_routeGateway;
qDebug()<<"My openvpn m_vpnLocalAddress "<<m_vpnLocalAddress; qDebug()<<"My openvpn m_vpnLocalAddress "<<m_vpnLocalAddress;
qDebug()<<"My openvpn m_vpnGateway "<< m_vpnGateway; qDebug()<<"My openvpn m_vpnGateway "<< m_vpnGateway;
auto ret = adpinfo::get_route_table();
qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
{
for (const auto &itret: ret){
const auto ip = std::get<0>(itret);
const auto msk = std::get<1>(itret);
const auto gw = std::get<2>(itret);
const auto itf = std::get<3>(itret);
qDebug()<<"IP["<<ip.c_str()<<"]"<<"Mask["<<msk.c_str()<<"]"<<"gateway["<<gw.c_str()<<"]"<<"Interface["<<itf.c_str()<<"]";
}
}
qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
} }
else{ else{
qDebug()<<"We can't get information about active adapter:"<<QString::fromStdString(std::get<1>(ret)); qDebug()<<"We can't get information about active adapter:"<<QString::fromStdString(std::get<1>(ret));
} }
} }
sendByteCount();
stopTimeoutTimer();
setConnectionState(VpnProtocol::Connected);
continue; continue;
} else if (line.contains("EXITING,SIGTER")) { } else if (line.contains("EXITING,SIGTER")) {
//openVpnStateSigTermHandler(); //openVpnStateSigTermHandler();

View file

@ -174,19 +174,32 @@ ErrorCode WireguardProtocol::start()
setConnectionState(ConnectionState::Connected); setConnectionState(ConnectionState::Connected);
{ {
//TODO:FIXME: without some ugly sleep we have't get a adapter parametrs //TODO:FIXME: without some ugly sleep we have't get a adapter parametrs
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(3));
std::string p1,p2,p3; std::string p1{},p2{};//,p3;
const auto &ret = adpInfo.get_adapter_info("WireGuard Tunnel");//serviceName().toStdString());//("AmneziaVPN IKEv2"); const auto &ret = adpInfo.get_adapter_info("WireGuard Tunnel");//serviceName().toStdString());//("AmneziaVPN IKEv2");
if (std::get<0>(ret) == false){ if (std::get<0>(ret) == false){
p1 = adpInfo.get_adapter_route_gateway(); p1 = adpInfo.get_adapter_route_gateway();
p2 = adpInfo.get_adapter_local_address(); p2 = adpInfo.get_adapter_local_address();
p3 = adpInfo.get_adapter_local_gateway(); //p3 = adpInfo.get_adapter_local_gateway();
m_routeGateway = QString::fromStdString(p1); m_routeGateway = QString::fromStdString(p1);
m_vpnLocalAddress = QString::fromStdString(p2); m_vpnLocalAddress = QString::fromStdString(p2);
m_vpnGateway = QString::fromStdString(p3); m_vpnGateway = protocols::wireguard::defaultSubnetAddress;//QString::fromStdString(p3);
qDebug()<<"My wireguard m_routeGateway "<<m_routeGateway; qDebug()<<"My wireguard m_routeGateway "<<m_routeGateway;
qDebug()<<"My wireguard m_vpnLocalAddress "<<m_vpnLocalAddress; qDebug()<<"My wireguard m_vpnLocalAddress "<<m_vpnLocalAddress;
qDebug()<<"My wireguard m_vpnGateway "<< m_vpnGateway; qDebug()<<"My wireguard m_vpnGateway "<< m_vpnGateway;
auto ret = adpinfo::get_route_table();
qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
{
for (const auto &itret: ret){
const auto ip = std::get<0>(itret);
const auto msk = std::get<1>(itret);
const auto gw = std::get<2>(itret);
const auto itf = std::get<3>(itret);
qDebug()<<"IP["<<ip.c_str()<<"]"<<"Mask["<<msk.c_str()<<"]"<<"gateway["<<gw.c_str()<<"]"<<"Interface["<<itf.c_str()<<"]";
}
}
qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
} }
else{ else{
qDebug()<<"We can't get information about active adapter:"<<QString::fromStdString(std::get<1>(ret)); qDebug()<<"We can't get information about active adapter:"<<QString::fromStdString(std::get<1>(ret));