133 lines
3.5 KiB
C++
Raw Normal View History

// pipe.cc
// 8/24/2013 jichi
// Branch: ITH_DLL/pipe.cpp, rev 66
// 8/24/2013 TODO: Clean up this file
#ifdef _MSC_VER
# pragma warning (disable:4100) // C4100: unreference formal parameter
#endif // _MSC_VER
#include "src/hijack/texthook.h"
#include "src/engine/match.h"
#include "src/util/util.h"
#include "src/main.h"
#include "include/defs.h"
2018-05-20 13:11:55 -04:00
#include "src/util/growl.h"
#include "ithsys/ithsys.h"
#include <cstdio> // for swprintf
2018-05-20 16:10:07 -04:00
HANDLE hookPipe;
2018-06-13 21:52:45 -04:00
extern HMODULE currentModule;
2018-05-20 13:11:55 -04:00
DWORD WINAPI PipeManager(LPVOID unused)
{
enum { STANDARD_WAIT = 50 };
2018-05-20 13:11:55 -04:00
while (::running)
{
DWORD count;
2018-07-12 13:59:05 -04:00
BYTE buffer[PIPE_BUFFER_SIZE];
2018-05-20 13:11:55 -04:00
HANDLE hostPipe = ::hookPipe = INVALID_HANDLE_VALUE,
pipeAcquisitionMutex = CreateMutexW(nullptr, TRUE, ITH_GRANTPIPE_MUTEX);
while (::hookPipe == INVALID_HANDLE_VALUE || hostPipe == INVALID_HANDLE_VALUE)
{
Sleep(STANDARD_WAIT);
if (::hookPipe == INVALID_HANDLE_VALUE)
{
::hookPipe = CreateFileW(ITH_TEXT_PIPE, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
}
if (hostPipe == INVALID_HANDLE_VALUE)
{
hostPipe = CreateFileW(ITH_COMMAND_PIPE, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
}
}
2018-06-13 21:52:45 -04:00
*(DWORD*)buffer = GetCurrentProcessId();
2018-07-13 20:51:09 -04:00
WriteFile(::hookPipe, buffer, sizeof(DWORD), &count, nullptr);
2018-05-20 13:11:55 -04:00
ReleaseMutex(pipeAcquisitionMutex);
CloseHandle(pipeAcquisitionMutex);
ConsoleOutput("vnrcli:WaitForPipe: pipe connected");
2018-07-19 02:02:42 -04:00
Engine::Hijack();
2018-05-20 13:11:55 -04:00
while (::running)
{
2018-05-21 04:05:09 -04:00
if (!ReadFile(hostPipe, buffer, PIPE_BUFFER_SIZE / 2, &count, nullptr)) // Artikash 5/21/2018: why / 2? wchar_t?
2018-05-20 13:11:55 -04:00
{
break;
}
DWORD command = *(DWORD*)buffer;
switch (command)
{
case HOST_COMMAND_NEW_HOOK:
buffer[count] = 0;
NewHook(*(HookParam *)(buffer + sizeof(DWORD)), // Hook parameter
(LPSTR)(buffer + 4 + sizeof(HookParam)), // Hook name
0
);
2018-05-20 13:11:55 -04:00
break;
2018-05-20 16:10:07 -04:00
case HOST_COMMAND_REMOVE_HOOK:
{
TextHook *in = hookman;
for (int i = 0; i < currentHook; in++)
{
if (in->Address()) i++;
if (in->Address() == *(DWORD *)(buffer + sizeof(DWORD))) // Hook address
2018-05-20 16:10:07 -04:00
{
break;
}
}
if (in->Address())
{
in->ClearHook();
}
}
break;
2018-05-20 13:11:55 -04:00
case HOST_COMMAND_DETACH:
::running = false;
break;
}
}
CloseHandle(::hookPipe);
CloseHandle(hostPipe);
2018-05-20 13:11:55 -04:00
}
2018-06-13 21:52:45 -04:00
FreeLibraryAndExitThread(::currentModule, 0);
2018-05-20 13:11:55 -04:00
return 0;
}
void ConsoleOutput(LPCSTR text)
{
BYTE buffer[PIPE_BUFFER_SIZE];
*(DWORD*)buffer = HOST_NOTIFICATION;
*(DWORD*)(buffer + sizeof(DWORD)) = HOST_NOTIFICATION_TEXT;
strcpy((char*)buffer + sizeof(DWORD) * 2, text);
2018-07-13 20:51:09 -04:00
DWORD unused;
WriteFile(::hookPipe, buffer, strlen(text) + sizeof(DWORD) * 2, &unused, nullptr);
}
2018-07-03 07:38:46 -04:00
void NotifyHookInsert(HookParam hp, LPCSTR name)
{
2018-07-03 07:38:46 -04:00
BYTE buffer[PIPE_BUFFER_SIZE];
2018-05-20 16:10:07 -04:00
*(DWORD*)buffer = HOST_NOTIFICATION;
*(DWORD*)(buffer + sizeof(DWORD)) = HOST_NOTIFICATION_NEWHOOK;
*(HookParam*)(buffer + sizeof(DWORD) * 2) = hp;
strcpy((char*)buffer + sizeof(DWORD) * 2 + sizeof(HookParam), name);
2018-07-13 20:51:09 -04:00
DWORD unused;
WriteFile(::hookPipe, buffer, strlen(name) + sizeof(DWORD) * 2 + sizeof(HookParam), &unused, nullptr);
2018-05-20 16:10:07 -04:00
return;
}
void NotifyHookRemove(DWORD addr)
{
BYTE buffer[sizeof(DWORD) * 3];
*(DWORD*)buffer = HOST_NOTIFICATION;
*(DWORD*)(buffer + sizeof(DWORD)) = HOST_NOTIFICATION_RMVHOOK;
*(DWORD*)(buffer + sizeof(DWORD) * 2) = addr;
DWORD unused;
WriteFile(::hookPipe, buffer, sizeof(DWORD) * 3, &unused, nullptr);
return;
}
// EOF