|
| 1 | +#undef getaddrinfo |
| 2 | + |
| 3 | +#include "socket.h" |
| 4 | +#include <map> |
| 5 | +#include <set> |
| 6 | +#include <fstream> |
| 7 | +#include <sstream> |
| 8 | +#include <regex> |
| 9 | + |
| 10 | +//Somewhere glua can't read? |
| 11 | +const char* whitelistDir = "../gm_socket_whitelist.txt"; |
| 12 | +std::map<std::string, std::set<std::string> > whitelist; |
| 13 | + |
| 14 | +enum : int |
| 15 | +{ |
| 16 | + PARSE_SUCCESS = 0, |
| 17 | + PARSE_CANT_READ = 1, |
| 18 | + PARSE_NO_ENTRIES = 2 |
| 19 | +}; |
| 20 | + |
| 21 | +int parseWhitelist() |
| 22 | +{ |
| 23 | + std::ifstream input(whitelistDir); |
| 24 | + if (input) |
| 25 | + { |
| 26 | + std::stringstream filereader; |
| 27 | + filereader << input.rdbuf(); |
| 28 | + std::string filedata = filereader.str(); |
| 29 | + std::regex line_parser("(?:(?!\r?\n).)+"); |
| 30 | + std::regex entry_parser("^[ \\t]*([\\w\\.-]+)\\:(\\d+)[ \\t]*$"); |
| 31 | + for (std::sregex_iterator line = std::sregex_iterator(filedata.begin(), filedata.end(), line_parser), end = std::sregex_iterator(); line != end; ++line) |
| 32 | + { |
| 33 | + const std::string& linestr = line->operator[](0); |
| 34 | + std::smatch match; |
| 35 | + if(std::regex_match(linestr, match, entry_parser)) |
| 36 | + { |
| 37 | + whitelist[match[1].str()].insert(match[2].str()); |
| 38 | + } |
| 39 | + } |
| 40 | + if (whitelist.empty()) |
| 41 | + { |
| 42 | + return PARSE_NO_ENTRIES; |
| 43 | + } |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + return PARSE_CANT_READ; |
| 48 | + } |
| 49 | + return PARSE_SUCCESS; |
| 50 | +} |
| 51 | + |
| 52 | +void clearWhitelist() |
| 53 | +{ |
| 54 | + whitelist.clear(); |
| 55 | +} |
| 56 | + |
| 57 | +bool isSafe(const char* pNodeName, const char* pServiceName) |
| 58 | +{ |
| 59 | + std::map<std::string, std::set<std::string> >::iterator domain = whitelist.find(pNodeName); |
| 60 | + return domain != whitelist.end() && domain->second.count(pServiceName)==1; |
| 61 | +} |
| 62 | + |
| 63 | +extern "C" { |
| 64 | + |
| 65 | +#ifdef _WIN32 |
| 66 | + INT WSAAPI __wrap_getaddrinfo( |
| 67 | + _In_opt_ PCSTR pNodeName, |
| 68 | + _In_opt_ PCSTR pServiceName, |
| 69 | + _In_opt_ const ADDRINFOA * pHints, |
| 70 | + _Outptr_result_maybenull_ PADDRINFOA * ppResult |
| 71 | + ) |
| 72 | +#else |
| 73 | + int __wrap_getaddrinfo (__const char *__restrict pNodeName, |
| 74 | + __const char *__restrict pServiceName, |
| 75 | + __const struct addrinfo *__restrict pHints, |
| 76 | + struct addrinfo **__restrict ppResult) |
| 77 | +#endif |
| 78 | + { |
| 79 | + if(isSafe(pNodeName, pServiceName)) |
| 80 | + { |
| 81 | + return getaddrinfo(pNodeName, pServiceName, pHints, ppResult); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + *ppResult = nullptr; |
| 86 | + return EAI_FAIL; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments