}
}
-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())
{
return -1;
}
+cf.ReadString("LocalName", &localName, "");
+
cf.ReadString("LocalPort", &temp, "0");
if (ParseIntInRange(temp, 0, 65535, &localPort))
{
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; }
std::string password;
std::string serverName;
int port;
+ std::string localName;
int localPort;
uint32_t listenWebIP;
int refreshPeriod;
# 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
#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)
{
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;
}
}
Flush();
}
//---------------------------------------------------------------------------
-const string & CONFIGFILE::GetFileName() const
+const std::string & CONFIGFILE::GetFileName() const
{
return fileName;
}
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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<long long int>(val));
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<string, string>::const_iterator it(param_val.find(param));
+const std::map<std::string, std::string>::const_iterator it(param_val.find(param));
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);
//---------------------------------------------------------------------------
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<string, string>::const_iterator it = param_val.begin();
+std::map<std::string, std::string>::const_iterator it = param_val.begin();
while (it != param_val.end())
{
f << it->first << "=" << it->second << "\n";
#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<string, string, StringCaseCmp_t> param_val;
- string fileName;
+ std::map<std::string, std::string, StringCaseCmp_t> param_val;
+ std::string fileName;
mutable int error;
mutable bool changed;
}
//---------------------------------------------------------------------------
#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),
serverName(sn),
port(p),
ip(0),
- localPort(localPort),
+ localName(ln),
+ localPort(lp),
firstConnect(true),
reconnect(0),
sockr(0),
//---------------------------------------------------------------------------
void IA_CLIENT_PROT::PrepareNet()
{
-struct hostent * phe;
+/*struct hostent * phe;
unsigned long ip;
ip = inet_addr(serverName.c_str());
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
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);
//---------------------------------------------------------------------------
int IA_CLIENT_PROT::DeterminatePacketType(const char * buffer)
{
-map<string, int>::iterator pi;
+std::map<std::string, int>::iterator pi;
pi = packetTypes.find(buffer);
if (pi == packetTypes.end())
{
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;
return 0;
}
//---------------------------------------------------------------------------
-int IA_CLIENT_PROT::GetStrError(string * error) const
+int IA_CLIENT_PROT::GetStrError(std::string * error) const
{
int ret = codeError;
*error = strError;
//---------------------------------------------------------------------------
int IA_CLIENT_PROT::Process_CONN_SYN_ACK_8(const char * buffer)
{
-vector<string> dirNames;
+std::vector<std::string> dirNames;
connSynAck8 = (CONN_SYN_ACK_8*)buffer;
#ifdef ARCH_BE
#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<string> & 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<std::string> & dirName, void * data);
//---------------------------------------------------------------------------
class IA_CLIENT_PROT
#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);
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; };
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;
bool selectedDirs[DIR_NUM];
- string password;
- string login;
+ std::string password;
+ std::string login;
#ifdef WIN32
WSADATA wsaData;
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;
void * errorCbData;
void * dirNameCbData;
- map<string, int> packetTypes;
+ std::map<std::string, int> packetTypes;
CONN_SYN_8 * connSyn8;
CONN_SYN_ACK_8 * connSynAck8;