-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow more whitespace Change whitelist format to domain:port Tighten the space/tab regex a bit Clear the whitelist when module closes Use map and set instead of vector Don't need to pass in lua interface Use errcodes instead Fixed lua error usage Move comment Got it compiling Update premake5.lua These aren't external Progress Started whitelist code
- Loading branch information
Showing
3 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#undef getaddrinfo | ||
|
||
#include "socket.h" | ||
#include <map> | ||
#include <set> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <regex> | ||
|
||
//Somewhere glua can't read? | ||
const char* whitelistDir = "../gm_socket_whitelist.txt"; | ||
std::map<std::string, std::set<std::string> > whitelist; | ||
|
||
enum : int | ||
{ | ||
PARSE_SUCCESS = 0, | ||
PARSE_CANT_READ = 1, | ||
PARSE_NO_ENTRIES = 2 | ||
}; | ||
|
||
int parseWhitelist() | ||
{ | ||
std::ifstream input(whitelistDir); | ||
if (input) | ||
{ | ||
std::stringstream filereader; | ||
filereader << input.rdbuf(); | ||
std::string filedata = filereader.str(); | ||
std::regex line_parser("(?:(?!\r?\n).)+"); | ||
std::regex entry_parser("^[ \\t]*([\\w\\.-]+)\\:(\\d+)[ \\t]*$"); | ||
for (std::sregex_iterator line = std::sregex_iterator(filedata.begin(), filedata.end(), line_parser), end = std::sregex_iterator(); line != end; ++line) | ||
{ | ||
const std::string& linestr = line->operator[](0); | ||
std::smatch match; | ||
if(std::regex_match(linestr, match, entry_parser)) | ||
{ | ||
whitelist[match[1].str()].insert(match[2].str()); | ||
} | ||
} | ||
if (whitelist.empty()) | ||
{ | ||
return PARSE_NO_ENTRIES; | ||
} | ||
} | ||
else | ||
{ | ||
return PARSE_CANT_READ; | ||
} | ||
return PARSE_SUCCESS; | ||
} | ||
|
||
void clearWhitelist() | ||
{ | ||
whitelist.clear(); | ||
} | ||
|
||
bool isSafe(const char* pNodeName, const char* pServiceName) | ||
{ | ||
std::map<std::string, std::set<std::string> >::iterator domain = whitelist.find(pNodeName); | ||
return domain != whitelist.end() && domain->second.count(pServiceName)==1; | ||
} | ||
|
||
extern "C" { | ||
|
||
#ifdef _WIN32 | ||
INT WSAAPI __wrap_getaddrinfo( | ||
_In_opt_ PCSTR pNodeName, | ||
_In_opt_ PCSTR pServiceName, | ||
_In_opt_ const ADDRINFOA * pHints, | ||
_Outptr_result_maybenull_ PADDRINFOA * ppResult | ||
) | ||
#else | ||
int __wrap_getaddrinfo (__const char *__restrict pNodeName, | ||
__const char *__restrict pServiceName, | ||
__const struct addrinfo *__restrict pHints, | ||
struct addrinfo **__restrict ppResult) | ||
#endif | ||
{ | ||
if(isSafe(pNodeName, pServiceName)) | ||
{ | ||
return getaddrinfo(pNodeName, pServiceName, pHints, ppResult); | ||
} | ||
else | ||
{ | ||
*ppResult = nullptr; | ||
return EAI_FAIL; | ||
} | ||
} | ||
|
||
} |