Skip to content

Commit 8496ab1

Browse files
committed
Got it compiling
1 parent 91d3bef commit 8496ab1

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

projects/premake5.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local LUASOCKET_FOLDER = "../luasocket/src"
2323
CreateWorkspace({name = "socket.core"})
2424
CreateProject({serverside = true, manual_files = true})
2525
files("../source/socket.cpp")
26-
if whitelist then files("../source/whitelist.cpp") end
26+
if whitelist then files("../source/whitelist.cpp") defines({"USE_WHITELIST"}) includedirs(LUASOCKET_FOLDER) end
2727
IncludeLuaShared()
2828
links("socket")
2929

@@ -32,7 +32,7 @@ CreateWorkspace({name = "socket.core"})
3232

3333
CreateProject({serverside = false, manual_files = true})
3434
files("../source/socket.cpp")
35-
if whitelist then files("../source/whitelist.cpp") end
35+
if whitelist then files("../source/whitelist.cpp") defines({"USE_WHITELIST"}) includedirs(LUASOCKET_FOLDER) end
3636
IncludeLuaShared()
3737
links("socket")
3838

source/socket.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
extern "C" int luaopen_socket_core( lua_State *state );
44

5+
int parseWhitelist(GarrysMod::Lua::ILuaBase* LUA);
6+
57
GMOD_MODULE_OPEN( )
68
{
9+
#ifdef USE_WHITELIST
10+
if (parseWhitelist(LUA))
11+
{
12+
return 0;
13+
}
14+
#endif
15+
716
if( luaopen_socket_core( LUA->GetState( ) ) == 1 )
817
{
918
LUA->Push( -1 );

source/whitelist.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
#undef getaddrinfo
22

3+
#include <GarrysMod/Lua/Interface.h>
4+
35
#include "socket.h"
46
#include <vector>
57
#include <fstream>
8+
#include <sstream>
69
#include <regex>
710

11+
const char* whitelistDir = "../gm_socket_whitelist.txt";
812
std::vector<std::pair<std::string, std::string> > whitelist;
913

10-
void parseWhitelist()
14+
int parseWhitelist(GarrysMod::Lua::ILuaBase* LUA)
1115
{
1216
//Somewhere glua can't read?
13-
std::ifstream input("../../../gm_socket_whitelist.txt");
17+
std::ifstream input(whitelistDir);
1418
if(input)
1519
{
20+
std::stringstream filereader;
21+
filereader << input.rdbuf();
22+
std::string filedata = filereader.str();
1623
std::regex line_parser("([\\w\\.-]+)\\s+(\\d+)(?:(?:\r?\n)+|$)");
17-
for (std::sregex_iterator match = std::sregex_iterator(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>(), line_parser), end = std::sregex_iterator(); match != end; ++match)
24+
for (std::regex_iterator<std::string::iterator> match = std::regex_iterator(filedata.begin(), filedata.end(), line_parser), end = std::regex_iterator<std::string::iterator>(); match != end; ++match)
25+
{
26+
whitelist.push_back(std::pair<std::string, std::string>(match->operator[](1).str(), match->operator[](2).str()));
27+
}
28+
if (whitelist.empty())
1829
{
19-
whitelist.push_back(std::pair<std::string, std::string>(match->operator[](1).str(), match->operator[](2).str());
30+
LUA->PushString("Didn't find any valid entries in whitelist file!");
31+
LUA->Error();
32+
return 1;
2033
}
2134
}
35+
else
36+
{
37+
LUA->PushString("Failed to read whitelist file!");
38+
LUA->Error();
39+
return 1;
40+
}
41+
return 0;
2242
}
2343

24-
bool isSafe(unsigned char* pNodeName, unsigned char* pServiceName)
44+
bool isSafe(const char* pNodeName, const char* pServiceName)
2545
{
2646
for(auto i = whitelist.begin(), end = whitelist.end(); i!=end; ++i)
2747
{
@@ -35,12 +55,12 @@ bool isSafe(unsigned char* pNodeName, unsigned char* pServiceName)
3555

3656
extern "C" {
3757

38-
#ifdef WIN32_
58+
#ifdef _WIN32
3959
INT WSAAPI __wrap_getaddrinfo(
4060
_In_opt_ PCSTR pNodeName,
4161
_In_opt_ PCSTR pServiceName,
4262
_In_opt_ const ADDRINFOA * pHints,
43-
_Outptr_ PADDRINFOA * ppResult
63+
_Outptr_result_maybenull_ PADDRINFOA * ppResult
4464
)
4565
#else
4666
int __wrap_getaddrinfo (__const char *__restrict pNodeName,
@@ -55,6 +75,7 @@ extern "C" {
5575
}
5676
else
5777
{
78+
*ppResult = nullptr;
5879
return EAI_FAIL;
5980
}
6081
}

0 commit comments

Comments
 (0)