From 3dfd70b31d34532a4d2e92fb4d4ee611c1518f18 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 9 Dec 2010 15:37:57 +0200 Subject: [PATCH 1/1] =?utf8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?utf8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BA=D0=BE=D0=B4=D0=B0=20=D0=BA?= =?utf8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20NETTRANSACT=20=D0=B8=20SE?= =?utf8?q?RVCONF=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA?= =?utf8?q?=D0=B8=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D0=B8?= =?utf8?q?=20=D0=BF=D1=80=D0=BE=D1=82=D0=BE=D0=BA=D0=BE=D0=BB=D0=B0=20?= =?utf8?q?=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=82?= =?utf8?q?=D0=BE=D1=80=D0=B0,=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=BE=D0=B2?= =?utf8?q?=D1=8B=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=BF?= =?utf8?q?=D1=80=D0=B5=D0=B4=D1=81=D1=82=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D1=8B=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=B0=D0=BC=D0=B8=20C++?= =?utf8?q?=20=D0=B0=20=D0=BD=D0=B5=20=D0=BE=D0=B1=D1=8B=D1=87=D0=BD=D1=8B?= =?utf8?q?=D0=BC=D0=B8=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=B0=D0=BC=D0=B8?= =?utf8?q?=20C=20=D0=BA=D0=B0=D0=BA=20=D1=80=D0=B0=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- stglibs/srvconf.lib/netunit.cpp | 55 ++++++++++++++--------------- stglibs/srvconf.lib/netunit.h | 7 ++-- stglibs/srvconf.lib/servconf.cpp | 60 ++++++++++++++------------------ stglibs/srvconf.lib/servconf.h | 10 +++--- 4 files changed, 63 insertions(+), 69 deletions(-) diff --git a/stglibs/srvconf.lib/netunit.cpp b/stglibs/srvconf.lib/netunit.cpp index ec796f7f..8d559193 100644 --- a/stglibs/srvconf.lib/netunit.cpp +++ b/stglibs/srvconf.lib/netunit.cpp @@ -25,12 +25,13 @@ */ //--------------------------------------------------------------------------- -#include -#include #include #include #include +#include +#include + #include "netunit.h" #include "common.h" @@ -56,15 +57,13 @@ NETTRANSACT::NETTRANSACT() RxCallBack(NULL), dataRxCallBack(NULL) { - memset(server, 0, SERVER_NAME_LEN); memset(login, 0, ADM_LOGIN_LEN); memset(password, 0, ADM_PASSWD_LEN); - memset(errorMsg, 0, MAX_ERR_STR_LEN); } //----------------------------------------------------------------------------- void NETTRANSACT::EnDecryptInit(const char * passwd, int, BLOWFISH_CTX *ctx) { -unsigned char * keyL = NULL; // ��� ������ +unsigned char * keyL = NULL; keyL = new unsigned char[PASSWD_LEN]; @@ -94,7 +93,7 @@ int ret; outerSocket = socket(PF_INET, SOCK_STREAM, 0); if (outerSocket < 0) { - strcpy(errorMsg, CREATE_SOCKET_ERROR); + errorMsg = CREATE_SOCKET_ERROR; return st_conn_fail; } @@ -105,14 +104,14 @@ struct hostent he; struct hostent * phe; unsigned long ip; -ip = inet_addr(server); +ip = inet_addr(server.c_str()); if (ip == INADDR_NONE) { - phe = gethostbyname(server); + phe = gethostbyname(server.c_str()); if (phe == NULL) { - sprintf(errorMsg, "DNS error.\nCan not reslove %s", server); + errorMsg = "DNS error.\nCan not reslove " + server; return st_dns_err; } @@ -127,7 +126,7 @@ ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr)); if (ret < 0) { - strcpy(errorMsg, CONNECT_FAILED); + errorMsg = CONNECT_FAILED; close(outerSocket); return st_conn_fail; } @@ -200,7 +199,7 @@ int ret; ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0); if (ret <= 0) { - strcpy(errorMsg, SEND_HEADER_ERROR); + errorMsg = SEND_HEADER_ERROR; return st_send_fail; } @@ -215,7 +214,7 @@ int ret; ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0); if (ret <= 0) { - strcpy(errorMsg, RECV_HEADER_ANSWER_ERROR); + errorMsg = RECV_HEADER_ANSWER_ERROR; return st_recv_fail; } @@ -227,12 +226,12 @@ else { if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0) { - strcpy(errorMsg, INCORRECT_HEADER); + errorMsg = INCORRECT_HEADER; return st_header_err; } else { - strcpy(errorMsg, UNKNOWN_ERROR); + errorMsg = UNKNOWN_ERROR; return st_unknown_err; } } @@ -249,7 +248,7 @@ ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0); if (ret <= 0) { - strcpy(errorMsg, SEND_LOGIN_ERROR); + errorMsg = SEND_LOGIN_ERROR; return st_send_fail; } @@ -259,12 +258,12 @@ return st_ok; int NETTRANSACT::RxLoginAnswer() { char buffer[sizeof(OK_LOGIN)+1]; -int ret;//, we; +int ret; ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0); if (ret <= 0) { - strcpy(errorMsg, RECV_LOGIN_ANSWER_ERROR); + errorMsg = RECV_LOGIN_ANSWER_ERROR; return st_recv_fail; } @@ -276,12 +275,12 @@ else { if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0) { - strcpy(errorMsg, INCORRECT_LOGIN); + errorMsg = INCORRECT_LOGIN; return st_login_err; } else { - strcpy(errorMsg, UNKNOWN_ERROR); + errorMsg = UNKNOWN_ERROR; return st_unknown_err; } } @@ -305,7 +304,7 @@ for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++) ret = send(outerSocket, ct, ENC_MSG_LEN, 0); if (ret <= 0) { - strcpy(errorMsg, SEND_LOGIN_ERROR); + errorMsg = SEND_LOGIN_ERROR; return st_send_fail; } } @@ -321,7 +320,7 @@ int ret; ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0); if (ret <= 0) { - strcpy(errorMsg, RECV_LOGIN_ANSWER_ERROR); + errorMsg = RECV_LOGIN_ANSWER_ERROR; return st_recv_fail; } @@ -333,12 +332,12 @@ else { if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0) { - strcpy(errorMsg, INCORRECT_LOGIN); + errorMsg = INCORRECT_LOGIN; return st_logins_err; } else { - strcpy(errorMsg, UNKNOWN_ERROR); + errorMsg = UNKNOWN_ERROR; return st_unknown_err; } } @@ -364,7 +363,7 @@ for (j = 0; j < n; j++) ret = send(outerSocket, ct, ENC_MSG_LEN, 0); if (ret <= 0) { - strcpy(errorMsg, SEND_DATA_ERROR); + errorMsg = SEND_DATA_ERROR; return st_send_fail; } } @@ -379,7 +378,7 @@ Encrypt(ct, textZ, &ctx); ret = send(outerSocket, ct, ENC_MSG_LEN, 0); if (ret <= 0) { - strcpy(errorMsg, SEND_DATA_ERROR); + errorMsg = SEND_DATA_ERROR; return st_send_fail; } @@ -429,7 +428,7 @@ while (1) if (ret <= 0) { close(outerSocket); - strcpy(errorMsg, RECV_DATA_ANSWER_ERROR); + errorMsg = RECV_DATA_ANSWER_ERROR; return st_recv_fail; } @@ -470,7 +469,7 @@ strncpy(password, p, ADM_PASSWD_LEN); //--------------------------------------------------------------------------- void NETTRANSACT::SetServer(const char * serverName) { -strncpy(server, serverName, SERVER_NAME_LEN); +server = serverName; } //--------------------------------------------------------------------------- void NETTRANSACT::SetServerPort(short unsigned p) @@ -484,7 +483,7 @@ RxCallBack = cb; dataRxCallBack = data; } //--------------------------------------------------------------------------- -char * NETTRANSACT::GetError() +const std::string & NETTRANSACT::GetError() const { return errorMsg; } diff --git a/stglibs/srvconf.lib/netunit.h b/stglibs/srvconf.lib/netunit.h index f5161503..007dc8c5 100644 --- a/stglibs/srvconf.lib/netunit.h +++ b/stglibs/srvconf.lib/netunit.h @@ -30,6 +30,7 @@ #include #include #include + #include #include @@ -80,7 +81,7 @@ class NETTRANSACT public: NETTRANSACT(); int Transact(const char * data); - char *GetError(); + const std::string & GetError() const; void SetRxCallback(void * data, RxCallback_t); @@ -111,7 +112,7 @@ private: void EnDecryptInit(const char * passwd, int passwdLen, BLOWFISH_CTX *ctx); void Decrypt(char * d, const char * s, BLOWFISH_CTX *ctx); - char server[SERVER_NAME_LEN]; + std::string server; short unsigned port; char login[ADM_LOGIN_LEN]; char password[ADM_PASSWD_LEN]; @@ -119,7 +120,7 @@ private: std::list answerList; RxCallback_t RxCallBack; void * dataRxCallBack; - char errorMsg[MAX_ERR_STR_LEN]; + std::string errorMsg; }; //--------------------------------------------------------------------------- #endif diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 8061d7aa..8074801e 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -24,8 +24,8 @@ $Author: faust $ */ -#include -#include +#include +#include #include "servconf.h" @@ -63,12 +63,9 @@ while (node != list1->end()) if (XML_Parse(sc->parser, ans, len, done) == XML_STATUS_ERROR) { - snprintf(sc->errorMsg, MAX_ERR_STR_LEN, "XML parse error at line %d: %s", - static_cast(XML_GetCurrentLineNumber(sc->parser)), - XML_ErrorString(XML_GetErrorCode(sc->parser))); - printf(sc->errorMsg, "XML parse error at line %d: %s", - XML_GetCurrentLineNumber(sc->parser), - XML_ErrorString(XML_GetErrorCode(sc->parser))); + strprintf(&sc->errorMsg, "XML parse error at line %d: %s", + static_cast(XML_GetCurrentLineNumber(sc->parser)), + XML_ErrorString(XML_GetErrorCode(sc->parser))); return st_xml_parse_error; } ++node; @@ -132,17 +129,17 @@ nt.SetRxCallback(this, AnsRecv); if ((ret = nt.Connect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Transact(request)) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Disconnect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } @@ -162,17 +159,17 @@ nt.SetRxCallback(this, AnsRecv); if ((ret = nt.Connect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Transact(request)) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Disconnect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } @@ -195,17 +192,17 @@ nt.SetRxCallback(this, AnsRecv); if ((ret = nt.Connect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Transact(request)) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Disconnect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } @@ -225,17 +222,17 @@ nt.SetRxCallback(this, AnsRecv); if ((ret = nt.Connect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Transact(request)) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Disconnect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } @@ -254,20 +251,17 @@ nt.SetRxCallback(this, AnsRecv); if ((ret = nt.Connect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); - printfd(__FILE__, "Error on connect: '%s'\n", errorMsg); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Transact(request)) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); - printfd(__FILE__, "Error on transact: '%s'\n", errorMsg); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Disconnect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); - printfd(__FILE__, "Error on disconnect: '%s'\n", errorMsg); + errorMsg = nt.GetError(); return ret; } @@ -288,17 +282,17 @@ nt.SetRxCallback(this, AnsRecv); if ((ret = nt.Connect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Transact(request)) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Disconnect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } @@ -319,17 +313,17 @@ nt.SetRxCallback(this, AnsRecv); if ((ret = nt.Connect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Transact(request)) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } if ((ret = nt.Disconnect()) != st_ok) { - strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + errorMsg = nt.GetError(); return ret; } @@ -383,7 +377,7 @@ RecvSendMessageCb = f; sendMessageDataCb = data; } //----------------------------------------------------------------------------- -char * SERVCONF::GetStrError() +const std::string & SERVCONF::GetStrError() const { return errorMsg; } diff --git a/stglibs/srvconf.lib/servconf.h b/stglibs/srvconf.lib/servconf.h index 874b6ab1..9edbe6dd 100644 --- a/stglibs/srvconf.lib/servconf.h +++ b/stglibs/srvconf.lib/servconf.h @@ -250,8 +250,8 @@ public: int GetServerInfo(); int CheckUser(const char * login, const char * password); - char * GetStrError(); - int GetError(); + const std::string & GetStrError() const; + int GetError(); int Start(const char *el, const char **attr); void End(const char *el); @@ -269,9 +269,9 @@ private: int parseDepth; USERDATA ud; - char errorMsg[MAX_ERR_STR_LEN]; - int error; - XML_Parser parser; + std::string errorMsg; + int error; + XML_Parser parser; RecvUserDataCb_t RecvUserDataCb; RecvUserDataCb_t RecvGetUserDataCb; -- 2.44.2