+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
+#include <csignal>
#include <cerrno>
#include <cstring>
#include <cassert>
void * PROTO::Runner(void * data)
{
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
PROTO * protoPtr = static_cast<PROTO *>(data);
protoPtr->Run();
return NULL;
void PROTO::AddUser(const USER & user, bool connect)
{
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
users.push_back(std::make_pair(user.GetIP(), user));
users.back().second.InitNetwork();
bool PROTO::Connect(uint32_t ip)
{
std::list<std::pair<uint32_t, USER> >::iterator it;
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
it = std::find_if(users.begin(), users.end(), HasIP(ip));
if (it == users.end())
return false;
bool PROTO::Disconnect(uint32_t ip)
{
std::list<std::pair<uint32_t, USER> >::iterator it;
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
it = std::find_if(users.begin(), users.end(), HasIP(ip));
if (it == users.end())
return false;
{
while (running)
{
- int res = poll(&pollFds.front(), pollFds.size(), timeout);
+ int res;
+ {
+ STG_LOCKER lock(&mutex);
+ res = poll(&pollFds.front(), pollFds.size(), timeout);
+ }
if (res < 0)
break;
if (!running)
printfd(__FILE__, "PROTO::Run() - events: %d\n", res);
RecvPacket();
}
+ else
+ {
+ CheckTimeouts();
+ }
}
stopped = true;
}
+void PROTO::CheckTimeouts()
+{
+STG_LOCKER lock(&mutex);
+std::list<std::pair<uint32_t, USER> >::iterator it;
+for (it = users.begin(); it != users.end(); ++it)
+ {
+ int delta = difftime(time(NULL), it->second.GetPhaseChangeTime());
+ if ((it->second.GetPhase() == 3) &&
+ (delta > it->second.GetUserTimeout()))
+ {
+ printfd(__FILE__, "PROTO::CheckTimeouts() - user alive timeout (ip: %s, login: '%s', delta: %d > %d)\n", inet_ntostring(it->second.GetIP()).c_str(), it->second.GetLogin().c_str(), delta, it->second.GetUserTimeout());
+ it->second.SetPhase(1);
+ }
+ if ((it->second.GetPhase() == 2) &&
+ (delta > it->second.GetAliveTimeout()))
+ {
+ printfd(__FILE__, "PROTO::CheckTimeouts() - user connect timeout (ip: %s, login: '%s', delta: %d > %d)\n", inet_ntostring(it->second.GetIP()).c_str(), it->second.GetLogin().c_str(), delta, it->second.GetAliveTimeout());
+ it->second.SetPhase(1);
+ }
+ }
+}
+
bool PROTO::RecvPacket()
{
bool result = true;
std::vector<struct pollfd>::iterator it;
std::list<std::pair<uint32_t, USER> >::iterator userIt;
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
for (it = pollFds.begin(), userIt = users.begin(); it != pollFds.end() && userIt != users.end(); ++it, ++userIt)
{
if (it->revents)
{
it->revents = 0;
- printfd(__FILE__, "PROTO::RecvPacket() - pollfd: %d, socket: %d\n", it->fd, userIt->second.GetSocket());
assert(it->fd == userIt->second.GetSocket() && "File descriptors from poll fds and users must be syncked");
struct sockaddr_in addr;
socklen_t fromLen = sizeof(addr);
{
errorStr = "Unexpected CONN_SYN_ACK";
printfd(__FILE__, "PROTO::CONN_SYN_ACK_Proc() - wrong phase: %d\n", user->GetPhase());
+ return false;
}
user->SetPhase(3);
{
errorStr = "Unexpected ALIVE_SYN";
printfd(__FILE__, "PROTO::ALIVE_SYN_Proc() - wrong phase: %d\n", user->GetPhase());
+ return false;
}
user->SetPhase(3);
{
errorStr = "Unexpected DISCONN_SYN_ACK";
printfd(__FILE__, "PROTO::DISCONN_SYN_ACK_Proc() - wrong phase: %d\n", user->GetPhase());
+ return false;
}
if (user->GetRnd() + 1 != rnd)
{
errorStr = "Unexpected FIN";
printfd(__FILE__, "PROTO::FIN_Proc() - wrong phase: %d\n", user->GetPhase());
+ return false;
}
user->SetPhase(1);
return true;
}
-bool PROTO::ERR_Proc(const void * buffer, USER * user)
+bool PROTO::ERR_Proc(const void * /*buffer*/, USER * user)
{
-const ERR_8 * packet = static_cast<const ERR_8 *>(buffer);
-const char * ptr = static_cast<const char *>(buffer);
-
//uint32_t len = packet->len;
#ifdef ARCH_BE
hdr.protoVer[1] = 8; // IA_PROTO_VER
unsigned char buffer[2048];
-memset(buffer, 0, sizeof(buffer));
-memcpy(buffer, packet, length);
memcpy(buffer, &hdr, sizeof(hdr));
+memcpy(buffer + sizeof(hdr), packet, length);
size_t offset = sizeof(HDR_8);
for (size_t i = 0; i < IA_LOGIN_LEN / 8; i++)