aboutsummaryrefslogtreecommitdiff
path: root/primedev/wsockproxy/dllmain.cpp
blob: 5a606e45d3589273e689cad77443ee84e7d38035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "loader.h"

#include <filesystem>

FARPROC p[73];
HMODULE hL = 0;

static wchar_t wsockPath[4096];

BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID)
{
	if (reason == DLL_PROCESS_ATTACH)
	{
		// Tell the OS we don't need to know about threads
        DisableThreadLibraryCalls(hInst);

		if (!ProvisionNorthstar()) // does not call InitialiseNorthstar yet, will do it on LauncherMain hook
			return true;

        GetSystemDirectoryW(wsockPath, 4096);
		swprintf_s(wsockPath, 4096, L"%s\\wsock32.dll", wsockPath);

		hL = LoadLibraryExW(wsockPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
		if (!hL)
		{
			LibraryLoadError(GetLastError(), L"wsock32.dll", wsockPath);
			return false;
		}

		// load the functions to proxy
		// it's only some of them, because in case of wsock32 most of the functions can actually be natively redirected
		// (see wsock32.def and https://source.winehq.org/WineAPI/wsock32.html)
		p[1] = GetProcAddress(hL, "EnumProtocolsA");
		p[2] = GetProcAddress(hL, "EnumProtocolsW");
		p[4] = GetProcAddress(hL, "GetAddressByNameA");
		p[5] = GetProcAddress(hL, "GetAddressByNameW");
		p[17] = GetProcAddress(hL, "WEP");
		p[30] = GetProcAddress(hL, "WSARecvEx");
		p[36] = GetProcAddress(hL, "__WSAFDIsSet");
		p[45] = GetProcAddress(hL, "getnetbyname");
		p[52] = GetProcAddress(hL, "getsockopt");
		p[56] = GetProcAddress(hL, "inet_network");
		p[67] = GetProcAddress(hL, "s_perror");
		p[72] = GetProcAddress(hL, "setsockopt");
	}

	if (reason == DLL_PROCESS_DETACH)
	{
		FreeLibrary(hL);
		return true;
	}

	return true;
}

extern "C"
{
	FARPROC PA = NULL;
	int RunASM();

	void PROXY_EnumProtocolsA() { p[1](); }
	void PROXY_EnumProtocolsW() { p[2](); }
	void PROXY_GetAddressByNameA() { p[4](); }
	void PROXY_GetAddressByNameW() { p[5](); }
	void PROXY_WEP() { p[17](); }
	void PROXY_WSARecvEx() { p[30](); }
	void PROXY___WSAFDIsSet() { p[36](); }
	void PROXY_getnetbyname() { p[45](); }
	void PROXY_getsockopt() { p[52](); }
	void PROXY_inet_network() { p[56](); }
	void PROXY_s_perror() { p[67](); }
	void PROXY_setsockopt() { p[72](); }
}