*
*******************************************************************/
+#include "configproto.h"
+
+#include "stg/admins.h"
+#include "stg/logger.h"
+#include "stg/common.h"
+#include "stg/blowfish.h"
+
#include <cerrno>
#include <csignal>
#include <cstdio> // snprintf
+#include <cstring> // strerror
-#include "configproto.h"
-#include "blowfish.h"
+#include <unistd.h> // close
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
#ifndef ENODATA
// FreeBSD 4.* - suxx
//-----------------------------------------------------------------------------
int CONFIGPROTO::Prepare()
{
-list<string> ansList; //óÀÄÁ ÂÕÄÅÔ ÐÏÍÅÝÅÎ ÏÔ×ÅÔ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ËÌÉÅÎÔÏ×
int res;
struct sockaddr_in listenAddr;
if (listenSocket < 0)
{
errorStr = "Create NET_CONFIGURATOR socket failed.";
+ logger("Cannot create a socket: %s", strerror(errno));
return -1;
}
if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
{
- errorStr = "Setsockopt failed. " + string(strerror(errno));
+ errorStr = "Setsockopt failed. " + std::string(strerror(errno));
+ logger("setsockopt error: %s", strerror(errno));
return -1;
}
if (res == -1)
{
errorStr = "Bind admin socket failed";
+ logger("Cannot bind the socket: %s", strerror(errno));
return -1;
}
if (res == -1)
{
errorStr = "Listen admin socket failed";
+ logger("Cannot listen the socket: %s", strerror(errno));
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
-// æÕÎËÃÉÑ ÏÂÝÅÎÉÑ Ó ËÏÎÆÉÇÕÒÁÔÏÒÏÍ
-void * CONFIGPROTO::Run(void * a)
+void CONFIGPROTO::Run()
{
-/*
- * Function Name:ReciveSendConf
- * Parameters: void * a ÕËÁÚÁÔÅÌØ ÎÁ ÜËÚÅÍÐÌÑÒ ËÌÁÓÓÁ CONFIGPROTO
- * Description: üÔÁ ÆÕÎËÃÉÑ ÏÂÅÓÐÅÞÉ×ÁÅÔ ÓÅÔÅ×ÏÅ ×ÚÁÉÍÏÄÅÊÓÔ×ÉÅ
- * Ó íÅÎÅÄÖÅÒÏÍ ëÌÉÅÎÔÏ×. ÷ ÅÅ ÚÁÄÁÞÉ ×ÈÏÄÉÔ: ÐÒÉÅÍ ÚÁÐÒÏÓÏ× ÐÏ TCP/IP
- * ÉÈ ÒÁÓÛÉÆÒÏ×ËÁ, ÐÅÒÅÄÁÞÁ ÐÒÉÎÑÔÙÈ ÄÁÎÎÙÈ ÁÎÁÌÉÚÁÔÏÒÕ É ÏÔÐÒÁ×ËÁ ÏÔ×ÅÔÁ ÎÁÚÁÄ.
- * Returns: ×ÏÚ×ÒÁÝÁÅÔ NULL
- */
-
-CONFIGPROTO * cp = (CONFIGPROTO*)a;
-cp->state = confHdr;
+state = confHdr;
-while (cp->nonstop)
+while (nonstop)
{
- cp->state = confHdr;
+ state = confHdr;
struct sockaddr_in outerAddr;
socklen_t outerAddrLen(sizeof(outerAddr));
- int outerSocket = accept(cp->listenSocket,
+ int outerSocket = accept(listenSocket,
(struct sockaddr*)(&outerAddr),
&outerAddrLen);
- if (!cp->nonstop)
- {
- continue;
- }
+ if (!nonstop)
+ break;
- if (outerSocket == -1)
+ if (outerSocket < 0)
{
+ logger("accept error: %s", strerror(errno));
printfd(__FILE__, "accept failed\n");
- usleep(100000);
continue;
}
- cp->adminIP = *(unsigned int*)&(outerAddr.sin_addr);
+ adminIP = *(unsigned int*)&(outerAddr.sin_addr);
- printfd(__FILE__, "Connection accepted from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
-
- if (cp->state == confHdr)
+ if (state == confHdr)
{
- if (cp->RecvHdr(outerSocket) < 0)
+ if (RecvHdr(outerSocket) < 0)
{
close(outerSocket);
continue;
}
- if (cp->state == confLogin)
+ if (state == confLogin)
{
- if (cp->SendHdrAnswer(outerSocket, ans_ok) < 0)
+ if (SendHdrAnswer(outerSocket, ans_ok) < 0)
{
close(outerSocket);
continue;
}
-
- if (cp->RecvLogin(outerSocket) < 0)
+ if (RecvLogin(outerSocket) < 0)
{
close(outerSocket);
continue;
}
-
- if (cp->state == confLoginCipher)
+ if (state == confLoginCipher)
{
- if (cp->SendLoginAnswer(outerSocket, ans_ok) < 0)
+ if (SendLoginAnswer(outerSocket) < 0)
{
close(outerSocket);
continue;
}
- if (cp->RecvLoginS(outerSocket) < 0)
+ if (RecvLoginS(outerSocket) < 0)
{
close(outerSocket);
continue;
}
-
- if (cp->state == confData)
+ if (state == confData)
{
- if (cp->SendLoginSAnswer(outerSocket, ans_ok) < 0)
+ if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
{
close(outerSocket);
continue;
}
- if (cp->RecvData(outerSocket) < 0)
+ if (RecvData(outerSocket) < 0)
{
close(outerSocket);
continue;
}
- cp->state = confHdr;
+ state = confHdr;
}
else
{
- if (cp->SendLoginSAnswer(outerSocket, ans_err) < 0)
+ if (SendLoginSAnswer(outerSocket, ans_err) < 0)
{
close(outerSocket);
continue;
}
- cp->WriteLogAccessFailed(cp->adminIP);
+ WriteLogAccessFailed(adminIP);
}
}
else
{
- cp->WriteLogAccessFailed(cp->adminIP);
+ WriteLogAccessFailed(adminIP);
}
}
else
{
- cp->WriteLogAccessFailed(cp->adminIP);
- if (cp->SendHdrAnswer(outerSocket, ans_err) < 0)
+ WriteLogAccessFailed(adminIP);
+ if (SendHdrAnswer(outerSocket, ans_err) < 0)
{
close(outerSocket);
continue;
}
else
{
- cp->WriteLogAccessFailed(cp->adminIP);
+ WriteLogAccessFailed(adminIP);
}
+ printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
close(outerSocket);
}
-
-return NULL;
}
//-----------------------------------------------------------------------------
int CONFIGPROTO::RecvHdr(int sock)
{
char buf[sizeof(STG_HEADER)];
memset(buf, 0, sizeof(STG_HEADER));
-int ret;
-int stgHdrLen = strlen(STG_HEADER);
-for (int i = 0; i < stgHdrLen; i++)
+size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
+size_t pos = 0;
+while (pos < stgHdrLen)
{
- ret = recv(sock, &buf[i], 1, 0);
+ if (!WaitPackets(sock))
+ {
+ state = confHdr;
+ SendError("Bad request");
+ return -1;
+ }
+ ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
if (ret <= 0)
{
+ if (ret < 0)
+ logger("recv error: %s", strerror(errno));
state = confHdr;
return -1;
}
+ pos += ret;
}
if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
//-----------------------------------------------------------------------------
int CONFIGPROTO::SendHdrAnswer(int sock, int err)
{
-int ret;
-
if (err)
{
- ret = send(sock, ERR_HEADER, sizeof(ERR_HEADER)-1, 0);
- if (ret < 0)
+ if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
{
- WriteServLog("send ERR_HEADER error in SendHdrAnswer.");
+ logger("send error: %s", strerror(errno));
return -1;
}
}
else
{
- ret = send(sock, OK_HEADER, sizeof(OK_HEADER)-1, 0);
- if (ret < 0)
+ if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
{
- WriteServLog("send OK_HEADER error in SendHdrAnswer.");
+ logger("send error: %s", strerror(errno));
return -1;
}
}
//-----------------------------------------------------------------------------
int CONFIGPROTO::RecvLogin(int sock)
{
-char login[ADM_LOGIN_LEN+1];
-int ret;
+char login[ADM_LOGIN_LEN + 1];
memset(login, 0, ADM_LOGIN_LEN + 1);
-ret = recv(sock, login, ADM_LOGIN_LEN, 0);
+size_t pos = 0;
+while (pos < ADM_LOGIN_LEN) {
+ if (!WaitPackets(sock))
+ {
+ state = confHdr;
+ return ENODATA;
+ }
-if (ret < 0)
- {
- // Error in network
- close(sock);
- state = confHdr;
- return ENODATA;
- }
+ ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
-if (ret < ADM_LOGIN_LEN)
- {
- // Error in protocol
- close(sock);
- state = confHdr;
- return ENODATA;
- }
+ if (ret <= 0)
+ {
+ // Error in network
+ logger("recv error: %s", strerror(errno));
+ state = confHdr;
+ return ENODATA;
+ }
+
+ pos += ret;
+}
-if (admins->FindAdmin(login, &currAdmin))
+if (admins->Find(login, &currAdmin))
{
// Admin not found
- close(sock);
state = confHdr;
return ENODATA;
}
-currAdmin.SetAdminIP(adminIP);
+
+currAdmin->SetIP(adminIP);
adminLogin = login;
state = confLoginCipher;
return 0;
}
//-----------------------------------------------------------------------------
-int CONFIGPROTO::SendLoginAnswer(int sock, int)
+int CONFIGPROTO::SendLoginAnswer(int sock)
{
-int ret;
-
-ret = send(sock, OK_LOGIN, sizeof(OK_LOGIN)-1, 0);
-if (ret < 0)
+if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
{
- WriteServLog("Send OK_LOGIN error in SendLoginAnswer.");
+ logger("Send OK_LOGIN error in SendLoginAnswer.");
return -1;
}
return 0;
int CONFIGPROTO::RecvLoginS(int sock)
{
char loginS[ADM_LOGIN_LEN + 1];
-char login[ADM_LOGIN_LEN + 1];
-int ret;
-BLOWFISH_CTX ctx;
memset(loginS, 0, ADM_LOGIN_LEN + 1);
-int total = 0;
-
-while (total < ADM_LOGIN_LEN)
+size_t pos = 0;
+while (pos < ADM_LOGIN_LEN)
{
- ret = recv(sock, &loginS[total], ADM_LOGIN_LEN - total, 0);
+ if (!WaitPackets(sock))
+ {
+ state = confHdr;
+ return ENODATA;
+ }
+
+ ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
- if (ret < 0)
+ if (ret <= 0)
{
// Network error
printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
- close(sock);
+ logger("recv error: %s", strerror(errno));
state = confHdr;
return ENODATA;
}
- total += ret;
+ pos += ret;
}
-if (currAdmin.GetLogin() == "")
+if (currAdmin->GetLogin().empty())
{
state = confHdr;
return ENODATA;
}
-EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
+BLOWFISH_CTX ctx;
+EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
-for (int i = 0; i < ADM_LOGIN_LEN/8; i++)
- {
- DecodeString(login + i*8, loginS + i*8, &ctx);
- }
+char login[ADM_LOGIN_LEN + 1];
+for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
+ DecodeString(login + i * 8, loginS + i * 8, &ctx);
if (currAdmin == admins->GetNoAdmin())
{
return 0;
}
-if (strncmp(currAdmin.GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
+if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
{
state = confHdr;
return ENODATA;
}
state = confData;
+adminPassword = currAdmin->GetPassword();
return 0;
}
//-----------------------------------------------------------------------------
int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
{
-int ret;
-
if (err)
{
- ret = send(sock, ERR_LOGINS, sizeof(ERR_LOGINS)-1, 0);
- if (ret < 0)
+ if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
{
- WriteServLog("send ERR_LOGIN error in SendLoginAnswer.");
+ logger("send error: %s", strerror(errno));
return -1;
}
}
else
{
- ret = send(sock, OK_LOGINS, sizeof(OK_LOGINS)-1, 0);
- if (ret < 0)
+ if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
{
- WriteServLog("send OK_LOGINS error in SendLoginSAnswer.");
+ logger("send error: %s", strerror(errno));
return -1;
}
}
//-----------------------------------------------------------------------------
int CONFIGPROTO::RecvData(int sock)
{
-int ret;
-char bufferS[8];
-char buffer[9];
-
-buffer[8] = 0;
-
requestList.clear();
BLOWFISH_CTX ctx;
-EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
+EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
while (1)
{
- int total = 0;
bool done = false;
- while (total < 8)
+ char bufferS[8];
+ size_t pos = 0;
+ while (pos < sizeof(bufferS))
{
- ret = recv(sock, &bufferS[total], 8 - total, 0);
+ if (!WaitPackets(sock))
+ {
+ done = true;
+ break;
+ }
+
+ ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
if (ret < 0)
{
// Network error
- close(sock);
- return 0;
+ logger("recv error: %s", strerror(errno));
+ printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
+ return -1;
}
- if (ret < 8)
+ if (ret == 0)
{
- if (memchr(buffer, 0, ret) != NULL)
- {
- done = true;
- break;
- }
+ done = true;
+ break;
}
- total += ret;
+ pos += ret;
}
+ char buffer[8];
+ buffer[7] = 0;
+
DecodeString(buffer, bufferS, &ctx);
- requestList.push_back(std::string(buffer, total));
+ requestList.push_back(std::string(buffer, pos));
- if (done || memchr(buffer, 0, total) != NULL)
+ if (done || memchr(buffer, 0, pos) != NULL)
{
- // ëÏÎÅà ÐÏÓÙÌËÉ
+ // End of data
if (ParseCommand())
{
SendError("Bad command");
return SendDataAnswer(sock);
}
}
-return 0;
+//return 0;
}
//-----------------------------------------------------------------------------
int CONFIGPROTO::SendDataAnswer(int sock)
{
-list<string>::iterator li;
+std::list<std::string>::iterator li;
li = answerList.begin();
BLOWFISH_CTX ctx;
char buffS[8];
int n = 0;
int k = 0;
-int ret = 0;
-EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
+EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
while (li != answerList.end())
{
while ((*li).c_str()[k])
{
- buff[n%8] = (*li).c_str()[k];
+ buff[n % 8] = (*li).c_str()[k];
n++;
k++;
- if (n%8 == 0)
+ if (n % 8 == 0)
{
EncodeString(buffS, buff, &ctx);
- ret = send(sock, buffS, 8, 0);
- if (ret < 0)
- {
+ if (send(sock, buffS, 8, 0) < 0)
return -1;
- }
}
}
k = 0;// new node
- li++;
+ ++li;
}
if (answerList.empty()) {
return 0;
}
-buff[n%8] = 0;
+buff[n % 8] = 0;
EncodeString(buffS, buff, &ctx);
answerList.clear();
-return send(sock, buffS, 8, 0);
+return static_cast<int>(send(sock, buffS, 8, 0));
}
//-----------------------------------------------------------------------------
void CONFIGPROTO::SendError(const char * text)
//-----------------------------------------------------------------------------
void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
{
-WriteServLog("Admin's connect failed. IP %s", inet_ntostring(ip).c_str());
+logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
}
//-----------------------------------------------------------------------------