X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/07bd133ff9a290ff7475c94c1578db3d0d91b853..3cc6c36fcf3f0c6449debeb56e53c2ede03efc26:/projects/sgauthstress/proto.cpp diff --git a/projects/sgauthstress/proto.cpp b/projects/sgauthstress/proto.cpp index b89681f6..d4624594 100644 --- a/projects/sgauthstress/proto.cpp +++ b/projects/sgauthstress/proto.cpp @@ -5,12 +5,21 @@ #include #include #include +#include #include "stg/common.h" #include "stg/ia_packets.h" #include "proto.h" +class HasIP : public std::unary_function, bool> { + public: + explicit HasIP(uint32_t i) : ip(i) {} + bool operator()(const std::pair & value) { return value.first == ip; } + private: + uint32_t ip; +}; + PROTO::PROTO(const std::string & server, uint16_t port, uint16_t localPort, @@ -111,38 +120,45 @@ if (pthread_join(tid, NULL)) return true; } -void PROTO::AddUser(const USER & user) +void PROTO::AddUser(const USER & user, bool connect) { - users.push_back(std::make_pair(user.GetIP(), user)); - struct pollfd pfd; - pfd.fd = user.GetSocket(); - pfd.events = POLLIN; - pfd.revents = 0; - pollFds.push_back(pfd); +users.push_back(std::make_pair(user.GetIP(), user)); +struct pollfd pfd; +pfd.fd = user.GetSocket(); +pfd.events = POLLIN; +pfd.revents = 0; +pollFds.push_back(pfd); + +users.back().second.InitNetwork(); + +if (connect) + { + RealConnect(&users.back().second); + } } bool PROTO::Connect(uint32_t ip) { -/*std::vector >::const_iterator it; -it = users.find(ip); +std::vector >::iterator it; +it = std::find_if(users.begin(), users.end(), HasIP(ip)); if (it == users.end()) - return false;*/ + return false; // Do something -return true; +return RealConnect(&it->second); } bool PROTO::Disconnect(uint32_t ip) { -/*std::vector >::const_iterator it; -it = users.find(ip); +std::vector >::iterator it; +it = std::find_if(users.begin(), users.end(), HasIP(ip)); if (it == users.end()) - return false;*/ + return false; // Do something -return true; +return RealDisconnect(&it->second); } void PROTO::Run() @@ -235,6 +251,8 @@ user->SetAliveTimeout(aliveTimeout); user->SetUserTimeout(userTimeout); user->SetRnd(rnd); +printfd(__FILE__, "PROTO::CONN_SYN_ACK_Proc() - user '%s' successfully logged in from IP %s\n", user->GetLogin().c_str(), inet_ntostring(user->GetIP()).c_str()); + return true; } @@ -468,7 +486,7 @@ if (res < 0) errorStr = "Failed to send packet: '"; errorStr += strerror(errno); errorStr += "'"; - printfd(__FILE__, "PROTO::SendPacket() - %s\n", errorStr.c_str()); + printfd(__FILE__, "PROTO::SendPacket() - %s, fd: %d\n", errorStr.c_str(), user->GetSocket()); return false; } @@ -481,3 +499,28 @@ if (res < sizeof(buffer)) return true; } + +bool PROTO::RealConnect(USER * user) +{ +if (user->GetPhase() != 1 && + user->GetPhase() != 5) + { + errorStr = "Unexpected connect"; + printfd(__FILE__, "PROTO::RealConnect() - wrong phase: %d\n", user->GetPhase()); + } +user->SetPhase(2); + +return Send_CONN_SYN(user); +} + +bool PROTO::RealDisconnect(USER * user) +{ +if (user->GetPhase() != 3) + { + errorStr = "Unexpected disconnect"; + printfd(__FILE__, "PROTO::RealDisconnect() - wrong phase: %d\n", user->GetPhase()); + } +user->SetPhase(4); + +return Send_DISCONN_SYN(user); +}