Skip to content

Commit

Permalink
WAB-7720: IPLoop TIA service restart does not properly work on multi-…
Browse files Browse the repository at this point in the history
…tunneling target.
  • Loading branch information
XiaopengZHOU committed Nov 15, 2023
1 parent 2735c52 commit c1a4dc6
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 83 deletions.
2 changes: 1 addition & 1 deletion network.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ struct iploop {
void* child;
// void* thread;
};
int map_ip_to_loopback(struct iploop *ipl, char** addr, int n, bool tia_portal);
int map_ip_to_loopback(struct iploop *ipl, char** addr, int n, char** addrTia, int nTia);
int unmap_ip_from_loopback(struct iploop* ipl);

#endif
23 changes: 20 additions & 3 deletions ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,6 @@ static const char *ssh_init(Seat *seat, Backend **backend_handle,
const char *p;
Ssh *ssh;

bool tia_portal = false;

ssh = snew(Ssh);
memset(ssh, 0, sizeof(Ssh));

Expand Down Expand Up @@ -912,16 +910,20 @@ static const char *ssh_init(Seat *seat, Backend **backend_handle,
/* Map IP to loopback if SSH tunnel flag set. */
if (conf_get_bool(ssh->conf, CONF_lport_loopback)) {
int nbAddr = 0;
int nbTia = 0;
char* key, *val;
bool addr_limit_reached = false;
char *loopback_addr[MAX_IPLOOP_ADDR];
char *tia_addr[MAX_IPLOOP_ADDR];
for (val = conf_get_str_strs(ssh->conf, CONF_portfwd, NULL, &key);
val != NULL;
val = conf_get_str_strs(ssh->conf, CONF_portfwd, key, &key)) {
char* kp, *kp2;
char address_family, type;
char *saddr;

bool tia_portal = false;

kp = key;

address_family = 'A';
Expand Down Expand Up @@ -957,22 +959,37 @@ static const char *ssh_init(Seat *seat, Backend **backend_handle,
addr_limit_reached = true;
}
if (nbAddr < MAX_IPLOOP_ADDR) {
char * saddr_tia = saddr;
bool already_in = false;
for (int idx = 0; idx < nbAddr; idx++) {
if (nullstrcmp(loopback_addr[idx], saddr) == 0) {
already_in = true;
saddr_tia = loopback_addr[idx];
}
}
if (!already_in) {
loopback_addr[nbAddr++] = saddr;
}
else
{
sfree(saddr);
}

if (tia_portal)
{
tia_addr[nbTia++] = saddr_tia;
}
}
}
}

if (map_ip_to_loopback(&ssh->ipl, loopback_addr, nbAddr, tia_portal) != 0) {
if (map_ip_to_loopback(&ssh->ipl, loopback_addr, nbAddr, tia_addr, nbTia) != 0) {
seat_connection_fatal(ssh->seat, "Cannot map IP(s) to loopback");
}

for (int idx = 0; idx < nbAddr; idx++) {
sfree(loopback_addr[idx]);
}
}

p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive);
Expand Down
85 changes: 76 additions & 9 deletions windows/VS2015-WAB/iploop/iploop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ bool RaiseEndEvent(LPCTSTR lpszBaseEventName, LPCTSTR lpszMsgBoxTitle)
return true;
}


std::vector<tstring> STRSplitIntoVector(_In_ LPCTSTR lpsz,
_In_ LPCTSTR lpszDelimit)
{
tstring str = lpsz;

std::vector<tstring> ret;

if (lpsz && lpszDelimit)
{
LPTSTR lpszNextToken = nullptr;
LPTSTR lpszToken = ::_tcstok_s(&str.front(), lpszDelimit, &lpszNextToken);
while (lpszToken)
{
ret.push_back(lpszToken);
lpszToken = ::_tcstok_s(nullptr, lpszDelimit, &lpszNextToken);
} // while (lpszToken)
} // if (lpsz && lpszDelimit)

return ret;
} // std::vector<tstring> STRSplitIntoVector(_In_ LPCTSTR lpsz,
// _In_ LPCTSTR lpszDelimit)

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
Expand All @@ -67,6 +90,7 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,

// TODO: Place code here.
OutputDebugString(lpCmdLine);
OutputDebugString(TEXT("\n"));

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
Expand All @@ -77,16 +101,26 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
MessageBox(NULL, _T("Cannot parse command line!"), szTitle, MB_ICONERROR | MB_OK);
return FALSE;
}
if (nArgs < 2) {
MessageBox(NULL, _T("iploop event_name ip1 [ip2 [...]] [/tia] [/parent process_id] [/window parent_wnd] [/begin-standalone | /end-standalone]"), szTitle, MB_ICONERROR | MB_OK);
if (nArgs < 3) {
MessageBox(NULL, _T("iploop.exe event_name addr1,[addr2[,...]] [/service name,port,addr1[,addr2[,...]] [/parent process_id] [/window parent_wnd] [/begin-standalone | /end-standalone]"), szTitle, MB_ICONERROR | MB_OK);

LocalFree(szArglistW);
return FALSE;
}

parameters.strEventNameBase = szArglistW[0];
parameters.dwGUIThreadId = GetCurrentThreadId();

for (int i = 1; i < nArgs; ++i)
{
std::vector<tstring> ips = STRSplitIntoVector(szArglistW[1], _T(","));

for (tstring& ip : ips)
{
parameters.vecstrIPs.emplace_back(std::move(ip));
}
}

for (int i = 2; i < nArgs; ++i)
{
if (!lstrcmpi(szArglistW[i], L"/begin-standalone"))
{
Expand Down Expand Up @@ -116,11 +150,39 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
return FALSE;
}
}
else if (!lstrcmpi(szArglistW[i], L"/tia"))
else if (StrStrI(szArglistW[i], L"/service") == szArglistW[i])
{
OutputDebugStringW(L"_tWinMain(): Enable TIA portal support.");
if (i < nArgs - 1)
{
++i;

std::vector<tstring> items = STRSplitIntoVector(szArglistW[i], _T(","));

if (items.size() < 2)
{
MessageBox(NULL, _T("Please provide name and IP port of service!"), szTitle, MB_ICONERROR | MB_OK);
LocalFree(szArglistW);
return FALSE;
}

std::vector<tstring> vecstrServiceIPs;

for (int i = 2, c = items.size(); i < c; i++)
{
vecstrServiceIPs.emplace_back(std::move(items[i]));
}

parameters.bTiaPortalSupport = true;
parameters.spService = std::make_unique<ManagedService>();
parameters.spService->strName = std::move(items[0]);
parameters.spService->usPort = _ttoi(items[1].c_str());
parameters.spService->vecstrIPs = std::move(vecstrServiceIPs);
}
else
{
MessageBox(NULL, _T("Please provide service parameters!"), szTitle, MB_ICONERROR | MB_OK);
LocalFree(szArglistW);
return FALSE;
}
}
else if (!lstrcmpi(szArglistW[i], L"/window"))
{
Expand All @@ -132,7 +194,7 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
DWORD dwParentWnd = 0;
iss >> dwParentWnd;
hwndParent = reinterpret_cast<HWND>(dwParentWnd);
}
}
else
{
MessageBox(NULL, _T("Please provide handle of parent window!"), szTitle, MB_ICONERROR | MB_OK);
Expand All @@ -149,9 +211,14 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
LocalFree(szArglistW);
return FALSE;
}
else if (std::find(parameters.vecstrIPs.begin(), parameters.vecstrIPs.end(), szArglistW[i]) == parameters.vecstrIPs.end())
else
{
parameters.vecstrIPs.emplace_back(szArglistW[i]);
std::wstring strMessageW;
strMessageW = L"Unknown command-line parameter: ";
strMessageW += szArglistW[i];
MessageBoxW(NULL, strMessageW.c_str(), szTitle, MB_ICONERROR | MB_OK);
LocalFree(szArglistW);
return FALSE;
}
}

Expand Down
21 changes: 20 additions & 1 deletion windows/VS2015-WAB/iploop/iploop.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@

#include "resource.h"

#if defined(_UNICODE) || defined(UNICODE)

using tstring = std::wstring;

#else // #if defined(_UNICODE) || defined(UNICODE)

using tstring = std::string;

#endif // #if defined(_UNICODE) || defined(UNICODE)

struct ManagedService
{
tstring strName;

unsigned short usPort = 0;

std::vector<std::wstring> vecstrIPs;
};

struct IPLoopThreadParameter
{
std::wstring strEventNameBase;

std::vector<std::wstring> vecstrIPs;

bool bTiaPortalSupport = false;
std::unique_ptr<ManagedService> spService;

HANDLE hParentProcess = NULL;

Expand Down
8 changes: 4 additions & 4 deletions windows/VS2015-WAB/iploop/iploop.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<ConformanceMode>false</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Iphlpapi.lib;ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Iphlpapi.lib;ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
</Link>
<PostBuildEvent>
Expand Down Expand Up @@ -127,15 +127,15 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<ConformanceMode>false</ConformanceMode>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Iphlpapi.lib;ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Iphlpapi.lib;ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
</Link>
<PostBuildEvent>
Expand Down
Loading

0 comments on commit c1a4dc6

Please sign in to comment.