X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Finspsocket.cpp;h=d1dc1ce6bb783b45bd8f6c0ece5c5e6ee13d406d;hb=547ee1342e8b07bcdf46bc81343d1a1f7a2998e5;hp=1bf58c91cf80b50bea277bf90d60eef59f161722;hpb=e84bf9f3ec5a60078c32b272d3d7885c0708c544;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/inspsocket.cpp b/src/inspsocket.cpp index 1bf58c91c..d1dc1ce6b 100644 --- a/src/inspsocket.cpp +++ b/src/inspsocket.cpp @@ -14,16 +14,9 @@ * --------------------------------------------------- */ -#include -#include -#include -#include -#include -#include "inspircd_config.h" #include "socket.h" #include "configreader.h" #include "inspstring.h" -#include "helperfuncs.h" #include "socketengine.h" #include "inspircd.h" @@ -31,11 +24,16 @@ using irc::sockets::OpenTCPSocket; using irc::sockets::insp_inaddr; using irc::sockets::insp_sockaddr; +bool InspSocket::Readable() +{ + return ((this->state != I_CONNECTING) && (this->WaitingForWriteEvent == false)); +} + InspSocket::InspSocket(InspIRCd* SI) { this->state = I_DISCONNECTED; this->fd = -1; - this->ClosePending = false; + this->WaitingForWriteEvent = false; this->Instance = SI; } @@ -44,27 +42,25 @@ InspSocket::InspSocket(InspIRCd* SI, int newfd, const char* ip) this->fd = newfd; this->state = I_CONNECTED; strlcpy(this->IP,ip,MAXBUF); - this->ClosePending = false; + this->WaitingForWriteEvent = false; this->Instance = SI; if (this->fd > -1) - { - this->ClosePending = (!this->Instance->SE->AddFd(this->fd,true,X_ESTAB_MODULE)); - this->Instance->socket_ref[this->fd] = this; - } + this->Instance->SE->AddFd(this); } -InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool listening, unsigned long maxtime) : fd(-1) +InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool listening, unsigned long maxtime) { + this->fd = -1; this->Instance = SI; strlcpy(host,ipaddr.c_str(),MAXBUF); - this->ClosePending = false; - if (listening) { + this->WaitingForWriteEvent = false; + if (listening) + { if ((this->fd = OpenTCPSocket()) == ERROR) { this->fd = -1; this->state = I_ERROR; this->OnError(I_ERR_SOCKET); - this->ClosePending = true; this->Instance->Log(DEBUG,"OpenTCPSocket() error"); return; } @@ -85,14 +81,12 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool this->state = I_LISTENING; if (this->fd > -1) { - if (!this->Instance->SE->AddFd(this->fd,true,X_ESTAB_MODULE)) + if (!this->Instance->SE->AddFd(this)) { this->Close(); this->state = I_ERROR; this->OnError(I_ERR_NOMOREFDS); - this->ClosePending = true; } - this->Instance->socket_ref[this->fd] = this; } this->Instance->Log(DEBUG,"New socket now in I_LISTENING state"); return; @@ -111,14 +105,13 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool this->fd = -1; this->state = I_ERROR; this->OnError(I_ERR_RESOLVE); - this->ClosePending = true; return; } else { this->Instance->Log(DEBUG,"No need to resolve %s",this->host); strlcpy(this->IP,host,MAXBUF); - timeout_end = time(NULL) + maxtime; + timeout_val = maxtime; this->DoConnect(); } } @@ -136,15 +129,14 @@ void InspSocket::WantWrite() * * This behaviour may be fixed in a later version. */ + this->Instance->SE->DelFd(this); this->WaitingForWriteEvent = true; - this->Instance->SE->DelFd(this->fd); - if (!this->Instance->SE->AddFd(this->fd,false,X_ESTAB_MODULE)) + if (!this->Instance->SE->AddFd(this)) { this->Close(); this->fd = -1; this->state = I_ERROR; this->OnError(I_ERR_NOMOREFDS); - this->ClosePending = true; } } @@ -221,7 +213,6 @@ bool InspSocket::DoConnect() this->Instance->Log(DEBUG,"Cant socket()"); this->state = I_ERROR; this->OnError(I_ERR_SOCKET); - this->fd = -1; return false; } @@ -255,24 +246,22 @@ bool InspSocket::DoConnect() this->OnError(I_ERR_CONNECT); this->Close(); this->state = I_ERROR; - this->fd = -1; - this->ClosePending = true; return false; } + + this->Timeout = new SocketTimeout(this->GetFd(), this->Instance, this, timeout_val, this->Instance->Time()); + this->Instance->Timers->AddTimer(this->Timeout); } this->state = I_CONNECTING; if (this->fd > -1) { - if (!this->Instance->SE->AddFd(this->fd,false,X_ESTAB_MODULE)) + if (!this->Instance->SE->AddFd(this)) { this->OnError(I_ERR_NOMOREFDS); this->Close(); - this->fd = -1; this->state = I_ERROR; - this->ClosePending = true; return false; } - this->Instance->socket_ref[this->fd] = this; this->SetQueues(this->fd); } this->Instance->Log(DEBUG,"Returning true from InspSocket::DoConnect"); @@ -282,14 +271,11 @@ bool InspSocket::DoConnect() void InspSocket::Close() { - if (this->fd != -1) + if (this->fd > -1) { this->OnClose(); shutdown(this->fd,2); close(this->fd); - this->Instance->socket_ref[this->fd] = NULL; - this->ClosePending = true; - this->fd = -1; } } @@ -310,13 +296,17 @@ char* InspSocket::Read() } else { - if (errno == EAGAIN) + int err = errno; + if (err == EAGAIN) { return ""; } else { - this->Instance->Log(DEBUG,"EOF or error on socket: %s",strerror(errno)); + if (!n) + this->Instance->Log(DEBUG,"EOF or error on socket: EOF"); + else + this->Instance->Log(DEBUG,"EOF or error on socket: %s",strerror(err)); return NULL; } } @@ -325,7 +315,6 @@ char* InspSocket::Read() void InspSocket::MarkAsClosed() { this->Instance->Log(DEBUG,"Marked as closed"); - this->ClosePending = true; } // There are two possible outcomes to this function. @@ -334,14 +323,6 @@ void InspSocket::MarkAsClosed() // and should be aborted. int InspSocket::Write(const std::string &data) { - if (this->ClosePending) - return false; - - /*int result = write(this->fd,data.c_str(),data.length()); - if (result < 1) - return false; - return true;*/ - /* Try and append the data to the back of the queue, and send it on its way */ outbuffer.push_back(data); @@ -350,9 +331,6 @@ int InspSocket::Write(const std::string &data) bool InspSocket::FlushWriteBuffer() { - if (this->ClosePending) - return true; - if ((this->fd > -1) && (this->state == I_CONNECTED)) { if (outbuffer.size()) @@ -378,7 +356,6 @@ bool InspSocket::FlushWriteBuffer() this->Instance->Log(DEBUG,"Write error on socket: %s",strerror(errno)); this->OnError(I_ERR_WRITE); this->state = I_ERROR; - this->ClosePending = true; return true; } } @@ -386,60 +363,56 @@ bool InspSocket::FlushWriteBuffer() return (fd < 0); } -bool InspSocket::Timeout(time_t current) +void SocketTimeout::Tick(time_t now) { - if (!this->Instance->socket_ref[this->fd] || !this->Instance->SE->HasFd(this->fd)) - { - this->Instance->Log(DEBUG,"No FD or socket ref"); - return false; - } - - if (this->ClosePending) + if (ServerInstance->SE->GetRef(this->sfd) != this->sock) { - this->Instance->Log(DEBUG,"Close is pending"); - return true; + ServerInstance->Log(DEBUG,"No FD or socket ref"); + return; } - if ((this->state == I_CONNECTING) && (current > timeout_end)) + if (this->sock->state == I_CONNECTING) { - this->Instance->Log(DEBUG,"Timed out, current=%lu timeout_end=%lu"); + ServerInstance->Log(DEBUG,"Timed out, current=%lu",now); // for non-listening sockets, the timeout can occur // which causes termination of the connection after // the given number of seconds without a successful // connection. - this->OnTimeout(); - this->OnError(I_ERR_TIMEOUT); - timeout = true; - this->state = I_ERROR; - this->ClosePending = true; - return true; + this->sock->OnTimeout(); + this->sock->OnError(I_ERR_TIMEOUT); + this->sock->timeout = true; + this->sock->state = I_ERROR; + ServerInstance->SE->DelFd(this->sock); + this->sock->Close(); + delete this->sock; + return; } - return this->FlushWriteBuffer(); + this->sock->FlushWriteBuffer(); } bool InspSocket::Poll() { - if (!this->Instance->socket_ref[this->fd] || !this->Instance->SE->HasFd(this->fd)) + if (this->Instance->SE->GetRef(this->fd) != this) return false; int incoming = -1; bool n = true; - if ((fd < 0) || (fd > MAX_DESCRIPTORS) || (this->ClosePending)) + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) return false; switch (this->state) { case I_CONNECTING: this->Instance->Log(DEBUG,"State = I_CONNECTING"); - this->SetState(I_CONNECTED); /* Our socket was in write-state, so delete it and re-add it * in read-state. */ if (this->fd > -1) { - this->Instance->SE->DelFd(this->fd); - if (!this->Instance->SE->AddFd(this->fd,true,X_ESTAB_MODULE)) + this->Instance->SE->DelFd(this); + this->SetState(I_CONNECTED); + if (!this->Instance->SE->AddFd(this)) return false; } return this->OnConnected(); @@ -460,8 +433,9 @@ bool InspSocket::Poll() if (this->WaitingForWriteEvent) { /* Switch back to read events */ - this->Instance->SE->DelFd(this->fd); - if (!this->Instance->SE->AddFd(this->fd,true,X_ESTAB_MODULE)) + this->Instance->SE->DelFd(this); + this->WaitingForWriteEvent = false; + if (!this->Instance->SE->AddFd(this)) return false; /* Trigger the write event */ @@ -516,3 +490,14 @@ InspSocket::~InspSocket() { this->Close(); } + +void InspSocket::HandleEvent(EventType et) +{ + if (!this->Poll()) + { + this->Instance->SE->DelFd(this); + this->Close(); + delete this; + } +} +