Skip to content

Commit 2eec8d1

Browse files
committed
Add Files.
1 parent db41ed1 commit 2eec8d1

File tree

11 files changed

+125
-21
lines changed

11 files changed

+125
-21
lines changed

common/libtcpip/netstack.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ namespace lwip {
5555
static Nat2Socket n2ss_;
5656
static SynchronizedObject lockobj_;
5757

58-
LIBTCPIP_IPV4_OUTPUT netstack::output = NULL;
59-
uint32_t netstack::IP = 0;
60-
uint32_t netstack::GW = 0;
61-
uint32_t netstack::MASK = 0;
62-
int netstack::Localhost = 0;
58+
LIBTCPIP_CLOSE_EVENT netstack::close_event = NULL;
59+
LIBTCPIP_IPV4_OUTPUT netstack::output = NULL;
60+
uint32_t netstack::IP = 0;
61+
uint32_t netstack::GW = 0;
62+
uint32_t netstack::MASK = 0;
63+
int netstack::Localhost = 0;
6364

6465
inline static void* netstack_tcp_linksocket(struct tcp_pcb* pcb, const std::shared_ptr<netstack_tcp_socket>& socket) noexcept {
6566
if (!pcb || !socket) {
@@ -654,6 +655,11 @@ namespace lwip {
654655
boost::system::error_code ec_;
655656
boost::asio::io_context::work work_(context_);
656657
context_.run(ec_);
658+
659+
LIBTCPIP_CLOSE_EVENT close_event_ = netstack::close_event;
660+
if (close_event_) {
661+
close_event_();
662+
}
657663
}).detach();
658664
return true;
659665
}
@@ -670,7 +676,7 @@ namespace lwip {
670676
if (timeout) {
671677
timeout_.reset();
672678
try {
673-
timeout_->cancel(ec);
679+
timeout->cancel(ec);
674680
}
675681
catch (std::exception&) {}
676682
}

common/libtcpip/netstack.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace boost {
3434

3535
namespace lwip {
3636
typedef std::function<bool(void* packet, int size)> LIBTCPIP_IPV4_OUTPUT;
37+
typedef std::function<void()> LIBTCPIP_CLOSE_EVENT;
3738

3839
class netstack {
3940
public:
@@ -42,10 +43,13 @@ namespace lwip {
4243

4344
public:
4445
static LIBTCPIP_IPV4_OUTPUT output;
46+
static LIBTCPIP_CLOSE_EVENT close_event;
4547
static uint32_t IP;
4648
static uint32_t GW;
4749
static uint32_t MASK;
4850
static int Localhost;
51+
52+
public:
4953
static bool input(const void* packet, int size) noexcept;
5054
static bool link(int nat, uint32_t& srcAddr, int& srcPort, uint32_t& dstAddr, int& dstPort) noexcept;
5155
};

main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct
3636
{
3737
uint32_t LeaseTimeInSeconds;
3838
bool Lwip;
39+
bool HostedNetwork;
3940
std::string ComponentId;
4041
boost::asio::ip::address IPAddress;
4142
boost::asio::ip::address GatewayServer;
@@ -123,6 +124,7 @@ bool PppApplication::PreparedLoopbackEnvironment(bool client_or_server, const st
123124
network_interface->GatewayServer.to_string(),
124125
network_interface->SubmaskAddress.to_string(),
125126
network_interface->LeaseTimeInSeconds,
127+
network_interface->HostedNetwork,
126128
Ipep::AddressesTransformToStrings(network_interface->DnsAddresses));
127129
if (NULL == tap)
128130
{
@@ -261,11 +263,13 @@ void PppApplication::PrintHelpInformation() noexcept
261263
messages += " --tun-ip=10.0.0.2 \\\r\n";
262264
messages += " --tun-gw=10.0.0.0 \\\r\n";
263265
messages += " --tun-mask=30 \\\r\n";
266+
messages += " --tun-host=[true|false] \\\r\n";
264267
messages += " --tun-lease-time-in-seconds=7200 \\\r\n";
265268
#else
266269
messages += " --tun-ip=10.0.0.2 \\\r\n";
267270
messages += " --tun-gw=10.0.0.1 \\\r\n";
268271
messages += " --tun-mask=30 \\\r\n";
272+
messages += " --tun-host=[true|false] \\\r\n";
269273
#endif
270274

271275
messages += " --dns=8.8.8.8,8.8.4.4 \\\r\n";
@@ -380,7 +384,7 @@ std::shared_ptr<NetworkInterface> PppApplication::GetNetworkInterface(int argc,
380384
ni->IPAddress = GetNetworkAddress("--tun-ip", "10.0.0.2", argc, argv);
381385
ni->GatewayServer = GetNetworkAddress("--tun-gw", "10.0.0.0", argc, argv);
382386
ni->SubmaskAddress = GetNetworkAddress("--tun-mask", "255.255.255.252", argc, argv);
383-
387+
384388
// DHCP-MASQ lease time in seconds.
385389
ni->LeaseTimeInSeconds = strtoul(ppp::GetCommandArgument("--tun-lease-time-in-seconds", argc, argv).data(), NULL, 10);
386390
if (ni->LeaseTimeInSeconds < 1)
@@ -394,7 +398,9 @@ std::shared_ptr<NetworkInterface> PppApplication::GetNetworkInterface(int argc,
394398
ni->SubmaskAddress = GetNetworkAddress("--tun-mask", "255.255.255.252", argc, argv);
395399
#endif
396400

401+
// Enabled the vEthernet bearer network to take over the Layer L2/L3 vEthernet traffic of the entire operating system.
397402
ni->IPAddress = Ipep::FixedIPAddress(ni->IPAddress, ni->GatewayServer, ni->SubmaskAddress);
403+
ni->HostedNetwork = ppp::ToBoolean(ppp::GetCommandArgument("--tun-host", argc, argv).data());
398404

399405
#ifdef _WIN32
400406
ni->ComponentId = ppp::tap::TapWindows::FindComponentId(ppp::GetCommandArgument("--tun", argc, argv));

ppp/ethernet/VEthernet.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ using ppp::net::packet::BufferSegment;
1717

1818
namespace ppp
1919
{
20+
namespace threading
21+
{
22+
void Executors_NetstackAllocExitAwaitable() noexcept;
23+
}
24+
2025
namespace ethernet
2126
{
2227
VEthernet::VEthernet(const std::shared_ptr<boost::asio::io_context>& context, bool lwip) noexcept
@@ -180,15 +185,19 @@ namespace ppp
180185
}
181186

182187
public:
183-
bool try_open_loopback() noexcept
188+
bool try_open_loopback() noexcept
184189
{
185190
std::lock_guard<std::mutex> scope(syncobj_);
186-
if (opened_)
191+
if (opened_)
187192
{
188193
return true;
189194
}
190195

191196
opened_ = lwip::netstack::open();
197+
if (opened_)
198+
{
199+
ppp::threading::Executors_NetstackAllocExitAwaitable();
200+
}
192201
return opened_;
193202
}
194203

ppp/tap/ITap.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace ppp
166166
#endif
167167
}
168168

169-
std::shared_ptr<ITap> ITap::Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, const std::vector<uint32_t>& dns_addresses) noexcept
169+
std::shared_ptr<ITap> ITap::Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, bool hosted_network, const std::vector<uint32_t>& dns_addresses) noexcept
170170
{
171171
if (NULL == context)
172172
{
@@ -198,13 +198,13 @@ namespace ppp
198198
}
199199

200200
#ifdef _WIN32
201-
return ppp::tap::TapWindows::Create(context, dev, ip, gw, mask, lease_time_in_seconds, dns_addresses);
201+
return ppp::tap::TapWindows::Create(context, dev, ip, gw, mask, lease_time_in_seconds, hosted_network, dns_addresses);
202202
#else
203-
return ppp::tap::TapLinux::Create(context, tun, ip, gw, mask, lease_time_in_seconds, dns_addresses);
203+
return ppp::tap::TapLinux::Create(context, tun, ip, gw, mask, lease_time_in_seconds, hosted_network, dns_addresses);
204204
#endif
205205
}
206206

207-
std::shared_ptr<ITap> ITap::Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, const std::string& ip, const std::string& gw, const std::string& mask, uint32_t lease_time_in_seconds, const std::vector<std::string>& dns_addresses) noexcept {
207+
std::shared_ptr<ITap> ITap::Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, const std::string& ip, const std::string& gw, const std::string& mask, uint32_t lease_time_in_seconds, bool hosted_network, const std::vector<std::string>& dns_addresses) noexcept {
208208
std::vector<uint32_t> dns_addresses_stloc;
209209
Ipep::ToAddresses(dns_addresses, dns_addresses_stloc);
210210

@@ -214,6 +214,7 @@ namespace ppp
214214
inet_addr(gw.data()),
215215
inet_addr(mask.data()),
216216
lease_time_in_seconds,
217+
hosted_network,
217218
dns_addresses_stloc);
218219
}
219220

ppp/tap/ITap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ namespace ppp
6464
public:
6565
static std::shared_ptr<ITap> From(const std::shared_ptr<boost::asio::io_context>& context, void* tun, uint32_t address, uint32_t gw, uint32_t mask) noexcept;
6666
static std::string FindAnyDevice() noexcept;
67-
static std::shared_ptr<ITap> Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, const std::vector<uint32_t>& dns_addresses) noexcept;
68-
static std::shared_ptr<ITap> Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, const std::string& ip, const std::string& gw, const std::string& mask, uint32_t lease_time_in_seconds, const std::vector<std::string>& dns_addresses) noexcept;
67+
static std::shared_ptr<ITap> Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, bool hosted_network, const std::vector<uint32_t>& dns_addresses) noexcept;
68+
static std::shared_ptr<ITap> Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& dev, const std::string& ip, const std::string& gw, const std::string& mask, uint32_t lease_time_in_seconds, bool hosted_network, const std::vector<std::string>& dns_addresses) noexcept;
6969

7070
protected:
7171
inline std::shared_ptr<boost::asio::posix::stream_descriptor> GetStream() noexcept { return _stream; }

ppp/threading/Executors.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <ppp/threading/Executors.h>
22
#include <ppp/threading/Timer.h>
33
#include <ppp/threading/Thread.h>
4+
#include <common/libtcpip/netstack.h>
45

56
#ifdef _WIN32
67
#include <windows/ppp/win32/Win32Native.h>
@@ -32,10 +33,21 @@ namespace ppp
3233
ExecutorTable ContextTable;
3334
ExecutorThreadTable Threads;
3435
ExecutorBufferArrayTable Buffers;
36+
std::shared_ptr<Executors::Awaitable> NetstackExitAwaitable;
3537

3638
public:
3739
ExecutorsInternal() noexcept
3840
{
41+
lwip::netstack::close_event =
42+
[]() noexcept
43+
{
44+
std::shared_ptr<Executors::Awaitable> awaitable = std::move(Internal.NetstackExitAwaitable);
45+
if (NULL != awaitable)
46+
{
47+
Internal.NetstackExitAwaitable = NULL;
48+
awaitable->Processed();
49+
}
50+
};
3951
SetThreadPriorityToMaxLevel();
4052
SetProcessPriorityToMaxLevel();
4153
}
@@ -185,6 +197,21 @@ namespace ppp
185197
Executors_DeleteTickByDefaultContext();
186198
Executors_DeleteCachedBuffer(context.get());
187199
}
200+
201+
static void Executors_NetstackTryExit() noexcept
202+
{
203+
lwip::netstack::close();
204+
std::shared_ptr<Executors::Awaitable> awaitable = Internal.NetstackExitAwaitable;
205+
if (NULL != awaitable)
206+
{
207+
awaitable->Await();
208+
}
209+
}
210+
211+
void Executors_NetstackAllocExitAwaitable() noexcept
212+
{
213+
Internal.NetstackExitAwaitable = make_shared_object<Executors::Awaitable>();
214+
}
188215

189216
void Executors::GetAllContexts(std::vector<ContextPtr>& contexts) noexcept
190217
{
@@ -494,6 +521,7 @@ namespace ppp
494521
}
495522
}
496523

524+
Executors_NetstackTryExit();
497525
Exit(Default);
498526
}
499527

windows/ppp/tap/TapWindows.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ namespace ppp
100100
IPEndPoint gwEP(gw, 0);
101101
if (IPEndPoint::IsInvalid(gwEP))
102102
{
103-
return false;
103+
std::string interface_name = ppp::win32::network::GetInterfaceName(interface_index);
104+
if (interface_name.empty())
105+
{
106+
return false;
107+
}
108+
109+
return ppp::win32::network::SetIPAddresses(interface_name, ipEP.ToAddressString(), maskEP.ToAddressString());
104110
}
105111

106112
if (!ppp::win32::network::SetIPAddresses(interface_index, { ipEP.ToAddressString() }, { maskEP.ToAddressString() }))
@@ -116,7 +122,7 @@ namespace ppp
116122
return ppp::win32::network::GetAllComponentIds(componentIds);
117123
}
118124

119-
std::shared_ptr<ITap> TapWindows::Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& componentId, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, const std::vector<uint32_t>& dns_addresses)
125+
std::shared_ptr<ITap> TapWindows::Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& componentId, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, bool hosted_network, const std::vector<uint32_t>& dns_addresses)
120126
{
121127
if (NULL == context)
122128
{
@@ -193,10 +199,16 @@ namespace ppp
193199
std::vector<std::string> mask_stloc;
194200
Ipep::ToAddresses({ mask }, mask_stloc);
195201

196-
ok = ok && ppp::win32::network::SetIPAddresses(interface_index, ips_stloc, mask_stloc);
197-
ok = ok && ppp::win32::network::SetDefaultIPGateway(interface_index, gw_stloc);
198-
ok = ok && ppp::win32::network::SetDnsAddresses(interface_index, dns_addresses_stloc);
202+
if (hosted_network)
203+
{
204+
ok = ok && SetAddresses(interface_index, ip, mask, gw);
205+
}
206+
else
207+
{
208+
ok = ok && SetAddresses(interface_index, ip, mask, IPEndPoint::NoneAddress);
209+
}
199210

211+
ok = ok && SetDnsAddresses(interface_index, dns_addresses_stloc);
200212
if (!ok)
201213
{
202214
tap->Close();

windows/ppp/tap/TapWindows.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace ppp
1717
static bool SetAddresses(int interface_index, uint32_t ip, uint32_t mask, uint32_t gw) noexcept;
1818
static bool SetDnsAddresses(int interface_index, std::vector<uint32_t>& servers) noexcept;
1919
static bool SetDnsAddresses(int interface_index, std::vector<std::string>& servers) noexcept;
20-
static std::shared_ptr<ITap> Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& componentId, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, const std::vector<uint32_t>& dns_addresses);
20+
static std::shared_ptr<ITap> Create(const std::shared_ptr<boost::asio::io_context>& context, const std::string& componentId, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t lease_time_in_seconds, bool hosted_network, const std::vector<uint32_t>& dns_addresses);
2121
static bool InstallDriver(const std::string& path, const std::string& declareTapName) noexcept;
2222
static bool UninstallDriver(const std::string& path) noexcept;
2323

windows/ppp/win32/network/NetworkInterface.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,43 @@ namespace ppp
11521152
std::map<int, MOF_Win32_NetworkAdapter> network_adapters;
11531153
return GetAdapterNameByIndexWMI(interface_index, network_adapters);
11541154
}
1155+
1156+
bool SetIPAddresses(const std::string& interface_name, const std::string& ip, const std::string& mask) noexcept
1157+
{
1158+
if (interface_name.empty() || ip.empty() || mask.empty())
1159+
{
1160+
return false;
1161+
}
1162+
1163+
PROCESS_INFORMATION pi;
1164+
ZeroMemory(&pi, sizeof(pi));
1165+
1166+
STARTUPINFOA si;
1167+
ZeroMemory(&si, sizeof(si));
1168+
si.cb = sizeof(si);
1169+
1170+
char command[1000];
1171+
snprintf(command, sizeof(command), "netsh interface ipv4 set address name=\"%s\" static %s %s", interface_name.data(), ip.data(), mask.data());
1172+
1173+
if (!CreateProcessA(NULL, command,
1174+
NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
1175+
{
1176+
return false;
1177+
}
1178+
1179+
DWORD dwExitCode = INFINITE;
1180+
if (WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_OBJECT_0)
1181+
{
1182+
if (!GetExitCodeProcess(pi.hProcess, &dwExitCode))
1183+
{
1184+
dwExitCode = INFINITE;
1185+
}
1186+
}
1187+
1188+
CloseHandle(pi.hProcess);
1189+
CloseHandle(pi.hThread);
1190+
return dwExitCode == ERROR_SUCCESS;
1191+
}
11551192
}
11561193
}
11571194
}

0 commit comments

Comments
 (0)