From: Maxim Mamontov Date: Tue, 15 Jan 2013 19:58:14 +0000 (+0200) Subject: Allowed to bind for a particular address/hostname for sgauth. X-Git-Tag: 2.409~375 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/d49162286e811faacd7d7f3277da61efdfb635db Allowed to bind for a particular address/hostname for sgauth. --- diff --git a/projects/sgauth/main.cpp b/projects/sgauth/main.cpp index 2af78b71..d0ab3c35 100644 --- a/projects/sgauth/main.cpp +++ b/projects/sgauth/main.cpp @@ -200,7 +200,7 @@ if (settings.GetDaemon()) } } -clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort()); +clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalName(), settings.GetLocalPort()); if (!settings.GetNoWeb()) { diff --git a/projects/sgauth/settings_impl.cpp b/projects/sgauth/settings_impl.cpp index f8536420..1a7b8dcd 100644 --- a/projects/sgauth/settings_impl.cpp +++ b/projects/sgauth/settings_impl.cpp @@ -85,6 +85,8 @@ if (ParseIntInRange(temp, 1, 65535, &port)) return -1; } +cf.ReadString("LocalName", &localName, ""); + cf.ReadString("LocalPort", &temp, "0"); if (ParseIntInRange(temp, 0, 65535, &localPort)) { diff --git a/projects/sgauth/settings_impl.h b/projects/sgauth/settings_impl.h index 9354e18d..253b69ea 100644 --- a/projects/sgauth/settings_impl.h +++ b/projects/sgauth/settings_impl.h @@ -37,6 +37,7 @@ public: const std::string & GetServerName() const { return serverName; } uint16_t GetServerPort() const { return port; } + const std::string & GetLocalName() const { return localName; } uint16_t GetLocalPort() const { return localPort; } const std::string & GetLogin() const { return login; } @@ -56,6 +57,7 @@ private: std::string password; std::string serverName; int port; + std::string localName; int localPort; uint32_t listenWebIP; int refreshPeriod; diff --git a/projects/sgauth/sgauth.conf b/projects/sgauth/sgauth.conf index b1fe4352..bb5fca32 100644 --- a/projects/sgauth/sgauth.conf +++ b/projects/sgauth/sgauth.conf @@ -20,6 +20,12 @@ ServerPort = 5555 # Default: Login = test +# Local host to bind +# Parameter: optional +# Values: IP address or DNS name +# Default: 0.0.0.0 +LocalName = localhost + # Port on which sgauth interacts with Stargazer # Parameter: optional # Value: 1 ... 65535 diff --git a/stglibs/conffiles.lib/conffiles.cpp b/stglibs/conffiles.lib/conffiles.cpp index 263c9933..3c3f3917 100644 --- a/stglibs/conffiles.lib/conffiles.cpp +++ b/stglibs/conffiles.lib/conffiles.cpp @@ -42,21 +42,53 @@ #include "stg/conffiles.h" -using namespace std; +namespace +{ +//--------------------------------------------------------------------------- +std::string TrimL(std::string val) +{ +size_t pos = val.find_first_not_of(" \t"); +if (pos == std::string::npos) + { + val.erase(val.begin(), val.end()); + } +else + { + val.erase(0, pos); + } +return val; +} +//--------------------------------------------------------------------------- +std::string TrimR(std::string val) +{ +size_t pos = val.find_last_not_of(" \t"); +if (pos != std::string::npos) + { + val.erase(pos + 1); + } +return val; +} +//--------------------------------------------------------------------------- +std::string Trim(std::string val) +{ +return TrimR(TrimL(val)); +} +//--------------------------------------------------------------------------- +} //--------------------------------------------------------------------------- -bool StringCaseCmp(const string & str1, const string & str2) +bool StringCaseCmp(const std::string & str1, const std::string & str2) { return (strcasecmp(str1.c_str(), str2.c_str()) < 0); } //--------------------------------------------------------------------------- -CONFIGFILE::CONFIGFILE(const string & fn, bool nook) +CONFIGFILE::CONFIGFILE(const std::string & fn, bool nook) : param_val(StringCaseCmp), fileName(fn), error(0), changed(false) { -ifstream f(fileName.c_str()); +std::ifstream f(fileName.c_str()); if (!f) { @@ -65,25 +97,25 @@ if (!f) return; } -string line; +std::string line; while (getline(f, line)) { size_t pos = line.find('#'); - if (pos != string::npos) + if (pos != std::string::npos) line.resize(pos); - if (line.find_first_not_of(" \t\r") == string::npos) + if (line.find_first_not_of(" \t\r") == std::string::npos) continue; pos = line.find_first_of('='); - if (pos == string::npos) + if (pos == std::string::npos) { error = -1; return; } - string parameter = line.substr(0, pos); - string value = line.substr(pos + 1); + std::string parameter = Trim(line.substr(0, pos)); + std::string value = Trim(line.substr(pos + 1)); param_val[parameter] = value; } } @@ -93,7 +125,7 @@ CONFIGFILE::~CONFIGFILE() Flush(); } //--------------------------------------------------------------------------- -const string & CONFIGFILE::GetFileName() const +const std::string & CONFIGFILE::GetFileName() const { return fileName; } @@ -105,9 +137,9 @@ error = 0; return e; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadString(const string & param, string * val, const string & defaultVal) const +int CONFIGFILE::ReadString(const std::string & param, std::string * val, const std::string & defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -119,15 +151,15 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -void CONFIGFILE::WriteString(const string & param, const string &val) +void CONFIGFILE::WriteString(const std::string & param, const std::string &val) { param_val[param] = val; changed = true; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadTime(const string & param, time_t * val, time_t defaultVal) const +int CONFIGFILE::ReadTime(const std::string & param, time_t * val, time_t defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -145,9 +177,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadInt(const string & param, int * val, int defaultVal) const +int CONFIGFILE::ReadInt(const std::string & param, int * val, int defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -165,9 +197,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadUInt(const string & param, unsigned int * val, unsigned int defaultVal) const +int CONFIGFILE::ReadUInt(const std::string & param, unsigned int * val, unsigned int defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -185,9 +217,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadLongInt(const string & param, long int * val, long int defaultVal) const +int CONFIGFILE::ReadLongInt(const std::string & param, long int * val, long int defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -205,9 +237,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadULongInt(const string & param, unsigned long int * val, unsigned long int defaultVal) const +int CONFIGFILE::ReadULongInt(const std::string & param, unsigned long int * val, unsigned long int defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -225,9 +257,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadLongLongInt(const string & param, int64_t * val, int64_t defaultVal) const +int CONFIGFILE::ReadLongLongInt(const std::string & param, int64_t * val, int64_t defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -245,9 +277,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadULongLongInt(const string & param, uint64_t * val, uint64_t defaultVal) const +int CONFIGFILE::ReadULongLongInt(const std::string & param, uint64_t * val, uint64_t defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -265,9 +297,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadShortInt(const string & param, short int * val, short int defaultVal) const +int CONFIGFILE::ReadShortInt(const std::string & param, short int * val, short int defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -285,9 +317,9 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadUShortInt(const string & param, unsigned short int * val, unsigned short int defaultVal) const +int CONFIGFILE::ReadUShortInt(const std::string & param, unsigned short int * val, unsigned short int defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -305,7 +337,7 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -void CONFIGFILE::WriteInt(const string & param, int64_t val) +void CONFIGFILE::WriteInt(const std::string & param, int64_t val) { char buf[32]; snprintf(buf, sizeof(buf), "%lld", static_cast(val)); @@ -313,9 +345,9 @@ param_val[param] = buf; changed = true; } //--------------------------------------------------------------------------- -int CONFIGFILE::ReadDouble(const string & param, double * val, double defaultVal) const +int CONFIGFILE::ReadDouble(const std::string & param, double * val, double defaultVal) const { -const map::const_iterator it(param_val.find(param)); +const std::map::const_iterator it(param_val.find(param)); if (it != param_val.end()) { @@ -333,7 +365,7 @@ if (it != param_val.end()) return -1; } //--------------------------------------------------------------------------- -void CONFIGFILE::WriteDouble(const string & param, double val) +void CONFIGFILE::WriteDouble(const std::string & param, double val) { char s[30]; snprintf(s, 30, "%f", val); @@ -343,14 +375,14 @@ changed = true; //--------------------------------------------------------------------------- int CONFIGFILE::Flush(const std::string & path) const { -ofstream f(path.c_str()); +std::ofstream f(path.c_str()); if (!f.is_open()) { error = EIO; return EIO; } -map::const_iterator it = param_val.begin(); +std::map::const_iterator it = param_val.begin(); while (it != param_val.end()) { f << it->first << "=" << it->second << "\n"; diff --git a/stglibs/conffiles.lib/include/stg/conffiles.h b/stglibs/conffiles.lib/include/stg/conffiles.h index 6909c6c9..be7acafe 100644 --- a/stglibs/conffiles.lib/include/stg/conffiles.h +++ b/stglibs/conffiles.lib/include/stg/conffiles.h @@ -37,44 +37,43 @@ #include "stg/os_int.h" -using namespace std; //--------------------------------------------------------------------------- -typedef bool (*StringCaseCmp_t)(const string & str1, const string & str2); +typedef bool (*StringCaseCmp_t)(const std::string & str1, const std::string & str2); class CONFIGFILE { public: - CONFIGFILE(const string & fn, bool nook = false); + CONFIGFILE(const std::string & fn, bool nook = false); ~CONFIGFILE(); - const string & GetFileName() const; + const std::string & GetFileName() const; // æÕÎËÃÉÉ Read* ×ÏÚ×ÒÁÝÁÀÔ 0 ÐÒÉ ÕÓÐÅÛÎÏÍ ÓÞÉÔÙ×ÁÎÉÉ // É EINVAL ÐÒÉ ÏÔÓÕÔÓ×ÉÉ ÐÁÒÁÍÅÔÒÁ É ×ÙÓÔÁ×ÌÑÀÔ defaulValue - //int ReadString(const string & param, char * val, int * maxLen, const char * defaultVal) const; - int ReadString(const string & param, string * val, const string & defaultVal) const; - int ReadTime(const string & param, time_t *, time_t) const; - int ReadShortInt(const string & param, short int *, short int) const; - int ReadInt(const string & param, int *, int) const; - int ReadLongInt(const string & param, long int *, long int) const; - int ReadLongLongInt(const string & param, int64_t *, int64_t) const; - int ReadUShortInt(const string & param, unsigned short int *, unsigned short int) const; - int ReadUInt(const string & param, unsigned int *, unsigned int) const; - int ReadULongInt(const string & param, unsigned long int *, unsigned long int) const; - int ReadULongLongInt(const string & param, uint64_t *, uint64_t) const; - int ReadDouble(const string & param, double * val, double defaultVal) const; + //int ReadString(const std::string & param, char * val, int * maxLen, const char * defaultVal) const; + int ReadString(const std::string & param, std::string * val, const std::string & defaultVal) const; + int ReadTime(const std::string & param, time_t *, time_t) const; + int ReadShortInt(const std::string & param, short int *, short int) const; + int ReadInt(const std::string & param, int *, int) const; + int ReadLongInt(const std::string & param, long int *, long int) const; + int ReadLongLongInt(const std::string & param, int64_t *, int64_t) const; + int ReadUShortInt(const std::string & param, unsigned short int *, unsigned short int) const; + int ReadUInt(const std::string & param, unsigned int *, unsigned int) const; + int ReadULongInt(const std::string & param, unsigned long int *, unsigned long int) const; + int ReadULongLongInt(const std::string & param, uint64_t *, uint64_t) const; + int ReadDouble(const std::string & param, double * val, double defaultVal) const; - void WriteString(const string & param, const char * val) { return WriteString(param, std::string(val)); } - void WriteString(const string & param, const string & val); - void WriteInt(const string & param, int64_t val); - void WriteDouble(const string & param, double val); + void WriteString(const std::string & param, const char * val) { return WriteString(param, std::string(val)); } + void WriteString(const std::string & param, const std::string & val); + void WriteInt(const std::string & param, int64_t val); + void WriteDouble(const std::string & param, double val); int Error() const; int Flush() const; private: - map param_val; - string fileName; + std::map param_val; + std::string fileName; mutable int error; mutable bool changed; diff --git a/stglibs/ia.lib/ia.cpp b/stglibs/ia.lib/ia.cpp index d6cb09ef..aa12a012 100644 --- a/stglibs/ia.lib/ia.cpp +++ b/stglibs/ia.lib/ia.cpp @@ -108,10 +108,36 @@ return 0; } //--------------------------------------------------------------------------- #endif + +namespace +{ + +bool HostNameToIP(const std::string & hostName, uint32_t & ip) +{ +ip = inet_addr(hostName.c_str()); +if (ip == INADDR_NONE) + { + hostent * phe = gethostbyname(hostName.c_str()); + if (phe) + { + ip = *((uint32_t *)phe->h_addr_list[0]); + } + else + { + return false; + } + } + +return true; +} + +} + //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- -IA_CLIENT_PROT::IA_CLIENT_PROT(const string & sn, unsigned short p, uint16_t localPort) +IA_CLIENT_PROT::IA_CLIENT_PROT(const std::string & sn, unsigned short p, + const std::string & ln, uint16_t lp) : action(IA_NONE), phase(1), phaseTime(0), @@ -122,7 +148,8 @@ IA_CLIENT_PROT::IA_CLIENT_PROT(const string & sn, unsigned short p, uint16_t loc serverName(sn), port(p), ip(0), - localPort(localPort), + localName(ln), + localPort(lp), firstConnect(true), reconnect(0), sockr(0), @@ -190,7 +217,7 @@ servAddr.sin_addr.s_addr = ip; //--------------------------------------------------------------------------- void IA_CLIENT_PROT::PrepareNet() { -struct hostent * phe; +/*struct hostent * phe; unsigned long ip; ip = inet_addr(serverName.c_str()); @@ -208,6 +235,16 @@ if (ip == INADDR_NONE) if (pErrorCb != NULL) pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData); } + }*/ + +if (!HostNameToIP(serverName, ip)) + { + ip = 0; + strError = std::string("Unknown host ") + "\'" + serverName + "\'"; + codeError = IA_GETHOSTBYNAME_ERROR; + if (pErrorCb != NULL) + pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData); + return; } #ifndef WIN32 @@ -225,7 +262,24 @@ if (localPort) localAddrR.sin_port = htons(localPort); else localAddrR.sin_port = htons(port); -localAddrR.sin_addr.s_addr = inet_addr("0.0.0.0"); + +if (!localName.empty()) + { + if (!HostNameToIP(localName, localIP)) + { + strError = std::string("Unknown host ") + "\'" + serverName + "\'"; + codeError = IA_GETHOSTBYNAME_ERROR; + if (pErrorCb != NULL) + pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData); + localIP = INADDR_ANY; + } + } +else + { + localIP = INADDR_ANY; + } + +localAddrR.sin_addr.s_addr = localIP; servAddr.sin_family = AF_INET; servAddr.sin_port = htons(port); @@ -268,7 +322,7 @@ WSACleanup(); //--------------------------------------------------------------------------- int IA_CLIENT_PROT::DeterminatePacketType(const char * buffer) { -map::iterator pi; +std::map::iterator pi; pi = packetTypes.find(buffer); if (pi == packetTypes.end()) { @@ -543,19 +597,19 @@ void IA_CLIENT_PROT::GetStat(LOADSTAT * ls) memcpy(ls, &stat, sizeof(stat)); } //--------------------------------------------------------------------------- -void IA_CLIENT_PROT::SetServer(const string & sn, unsigned short p) +void IA_CLIENT_PROT::SetServer(const std::string & sn, unsigned short p) { serverName = sn; port = p; PrepareNet(); } //--------------------------------------------------------------------------- -void IA_CLIENT_PROT::SetLogin(const string & l) +void IA_CLIENT_PROT::SetLogin(const std::string & l) { login = l; } //--------------------------------------------------------------------------- -void IA_CLIENT_PROT::SetPassword(const string & p) +void IA_CLIENT_PROT::SetPassword(const std::string & p) { password = p; @@ -583,7 +637,7 @@ action = IA_DISCONNECT; return 0; } //--------------------------------------------------------------------------- -int IA_CLIENT_PROT::GetStrError(string * error) const +int IA_CLIENT_PROT::GetStrError(std::string * error) const { int ret = codeError; *error = strError; @@ -594,7 +648,7 @@ return ret; //--------------------------------------------------------------------------- int IA_CLIENT_PROT::Process_CONN_SYN_ACK_8(const char * buffer) { -vector dirNames; +std::vector dirNames; connSynAck8 = (CONN_SYN_ACK_8*)buffer; #ifdef ARCH_BE diff --git a/stglibs/ia.lib/include/stg/ia.h b/stglibs/ia.lib/include/stg/ia.h index 9c8ea37f..c37f4394 100644 --- a/stglibs/ia.lib/include/stg/ia.h +++ b/stglibs/ia.lib/include/stg/ia.h @@ -51,13 +51,11 @@ #define IA_PROTO_VER (8) #define IA_PROTO_PROXY_VER (101) -using namespace std; - typedef void (*tpStatusChangedCb)(int status, void * data); typedef void (*tpStatChangedCb)(const LOADSTAT & stat, void * data); -typedef void (*tpCallBackInfoFn)(const string & message, int infoType, int showTime, int sendTime, void * data); -typedef void (*tpCallBackErrorFn)(const string & message, int netError, void * data); -typedef void (*tpCallBackDirNameFn)(const vector & dirName, void * data); +typedef void (*tpCallBackInfoFn)(const std::string & message, int infoType, int showTime, int sendTime, void * data); +typedef void (*tpCallBackErrorFn)(const std::string & message, int netError, void * data); +typedef void (*tpCallBackDirNameFn)(const std::vector & dirName, void * data); //--------------------------------------------------------------------------- class IA_CLIENT_PROT @@ -69,16 +67,16 @@ friend void * RunL(void * data); #endif public: - IA_CLIENT_PROT(const string & sn, uint16_t p, uint16_t localPort = 0); + IA_CLIENT_PROT(const std::string & sn, uint16_t p, const std::string & localName = "", uint16_t localPort = 0); ~IA_CLIENT_PROT(); void Start(); void Stop(); void GetStat(LOADSTAT * ls); - void SetServer(const string & sn, unsigned short port); - void SetLogin(const string & login); - void SetPassword(const string & password); + void SetServer(const std::string & sn, unsigned short port); + void SetLogin(const std::string & login); + void SetPassword(const std::string & password); void SetEnabledDirs(const bool * selectedDirs); void SetStatusChangedCb(tpStatusChangedCb p, void * data); @@ -95,9 +93,9 @@ public: int GetReconnect() const { return reconnect; }; void SetReconnect(int r) { reconnect = r; }; char GetProtoVer() const { return proxyMode ? IA_PROTO_PROXY_VER : IA_PROTO_VER; }; - void GetMessageText(string * text) const { *text = messageText; }; - void GetInfoText(string * text) const { *text = infoText; }; - int GetStrError(string * error) const; + void GetMessageText(std::string * text) const { *text = messageText; }; + void GetInfoText(std::string * text) const { *text = infoText; }; + int GetStrError(std::string * error) const; void SetProxyMode(bool on) { proxyMode = on; }; bool GetProxyMode() const { return proxyMode; }; @@ -134,9 +132,9 @@ private: int action; int phase; int phaseTime; - string messageText; - string infoText; - mutable string strError; + std::string messageText; + std::string infoText; + mutable std::string strError; mutable int codeError; bool nonstop; bool isNetPrepared; @@ -147,8 +145,8 @@ private: bool selectedDirs[DIR_NUM]; - string password; - string login; + std::string password; + std::string login; #ifdef WIN32 WSADATA wsaData; @@ -156,9 +154,11 @@ private: pthread_t thread; #endif - string serverName; + std::string serverName; uint16_t port; uint32_t ip; + std::string localName; + uint32_t localIP; uint32_t localPort; struct sockaddr_in servAddr; @@ -183,7 +183,7 @@ private: void * errorCbData; void * dirNameCbData; - map packetTypes; + std::map packetTypes; CONN_SYN_8 * connSyn8; CONN_SYN_ACK_8 * connSynAck8;