diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/bin/mydbgen | 1 | ||||
-rw-r--r-- | src/dns.cpp | 15 | ||||
-rw-r--r-- | src/mail.cpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 40 | ||||
-rw-r--r-- | src/misc.cpp | 2 | ||||
-rw-r--r-- | src/modules.cpp | 1 | ||||
-rw-r--r-- | src/socketengines/socketengine_eventfd.cpp | 41 | ||||
-rw-r--r-- | src/socketengines/socketengine_pipe.cpp | 3 | ||||
-rw-r--r-- | src/socketengines/socketengine_win32.cpp | 7 | ||||
-rw-r--r-- | src/sockets.cpp | 324 | ||||
-rw-r--r-- | src/threadengine.cpp | 23 | ||||
-rw-r--r-- | src/threadengines/threadengine_pthread.cpp | 8 | ||||
-rw-r--r-- | src/threadengines/threadengine_win32.cpp | 6 | ||||
-rw-r--r-- | src/users.cpp | 1 |
14 files changed, 308 insertions, 168 deletions
diff --git a/src/bin/mydbgen b/src/bin/mydbgen index f5663ab61..7e3d1929b 100755 --- a/src/bin/mydbgen +++ b/src/bin/mydbgen @@ -147,7 +147,6 @@ if test "x$FAILED" = "x" ; then echo " username = \"$SQLUSER\"" echo " password = \"$SQLPASS\"" echo " port = \"$SQLPORT\"" - echo " updatedelay = \"60\"" echo "}" echo "" else diff --git a/src/dns.cpp b/src/dns.cpp index 30624aeca..9f99a5b49 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -12,7 +12,10 @@ DNSRequest::DNSRequest(const Anope::string &addr, QueryType qt, bool cache, Modu if (!DNSEngine) DNSEngine = new DNSManager(); if (!DNSEngine->sock) - DNSEngine->sock = new DNSSocket(Config->NameServer, DNSManager::DNSPort); + { + DNSEngine->sock = new DNSSocket(); + DNSEngine->sock->Connect(Config->NameServer, DNSManager::DNSPort); + } if (DNSEngine->packets.size() == 65535) throw SocketException("DNS queue full"); @@ -197,7 +200,7 @@ inline DNSRecord::DNSRecord() this->created = Anope::CurTime; } -DNSSocket::DNSSocket(const Anope::string &TargetHost, int Port) : ClientSocket(TargetHost, Port, "", false, SOCK_DGRAM) +DNSSocket::DNSSocket() : ConnectionSocket(false, SOCK_DGRAM) { } @@ -209,13 +212,13 @@ DNSSocket::~DNSSocket() int DNSSocket::SendTo(const unsigned char *buf, size_t len) const { - return sendto(this->GetSock(), buf, len, 0, &this->conaddrs.sa, this->conaddrs.size()); + return sendto(this->GetFD(), buf, len, 0, &this->conaddr.sa, this->conaddr.size()); } int DNSSocket::RecvFrom(char *buf, size_t len, sockaddrs &addrs) const { socklen_t x = sizeof(addrs); - return recvfrom(this->GetSock(), buf, len, 0, &addrs.sa, &x); + return recvfrom(this->GetFD(), buf, len, 0, &addrs.sa, &x); } bool DNSSocket::ProcessRead() @@ -229,9 +232,9 @@ bool DNSSocket::ProcessRead() if (length < 0) return false; - if (this->conaddrs != from_server) + if (this->conaddr != from_server) { - Log(LOG_DEBUG_2) << "Resolver: Received an answer from the wrong nameserver, Bad NAT or DNS forging attempt? '" << this->conaddrs.addr() << "' != '" << from_server.addr() << "'"; + Log(LOG_DEBUG_2) << "Resolver: Received an answer from the wrong nameserver, Bad NAT or DNS forging attempt? '" << this->conaddr.addr() << "' != '" << from_server.addr() << "'"; return true; } diff --git a/src/mail.cpp b/src/mail.cpp index 5cb64f6d3..66cf1b81e 100644 --- a/src/mail.cpp +++ b/src/mail.cpp @@ -13,7 +13,10 @@ void MailThread::Run() FILE *pipe = popen(Config->SendMailPath.c_str(), "w"); if (!pipe) + { + SetExitState(); return; + } fprintf(pipe, "From: %s\n", Config->SendFrom.c_str()); if (Config->DontQuoteAddresses) @@ -27,6 +30,7 @@ void MailThread::Run() pclose(pipe); Success = true; + SetExitState(); } bool Mail(User *u, NickRequest *nr, BotInfo *service, const Anope::string &subject, const Anope::string &message) diff --git a/src/main.cpp b/src/main.cpp index a1417b197..2f37b64d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -104,27 +104,23 @@ class UpdateTimer : public Timer } }; -Socket *UplinkSock = NULL; +ConnectionSocket *UplinkSock = NULL; -class UplinkSocket : public ClientSocket +UplinkSocket::UplinkSocket(bool ipv6) : ConnectionSocket(ipv6) { - public: - UplinkSocket(const Anope::string &nTargetHost, int nPort, const Anope::string &nBindHost = "", bool nIPv6 = false) : ClientSocket(nTargetHost, nPort, nBindHost, nIPv6) - { - UplinkSock = this; - } + UplinkSock = this; +} - ~UplinkSocket() - { - UplinkSock = NULL; - } +UplinkSocket::~UplinkSocket() +{ + UplinkSock = NULL; +} - bool Read(const Anope::string &buf) - { - process(buf); - return true; - } -}; +bool UplinkSocket::Read(const Anope::string &buf) +{ + process(buf); + return true; +} /*************************************************************************/ @@ -354,13 +350,14 @@ static bool Connect() if (MOD_RESULT != EVENT_CONTINUE) { if (MOD_RESULT == EVENT_STOP) - break; + continue; return true; } try { - new UplinkSocket(uplink_server->host, uplink_server->port, Config->LocalHost, uplink_server->ipv6); + new UplinkSocket(uplink_server->ipv6); + UplinkSock->Connect(uplink_server->host, uplink_server->port, Config->LocalHost); } catch (const SocketException &ex) { @@ -368,7 +365,7 @@ static bool Connect() continue; } - Log() << "Connected to Server " << servernum << " (" << uplink_server->host << ":" << uplink_server->port << ")"; + Log() << "Connected to server " << servernum << " (" << uplink_server->host << ":" << uplink_server->port << ")"; return true; } @@ -469,6 +466,9 @@ int main(int ac, char **av, char **envp) last_check = Anope::CurTime; } + /* Free up any finished threads */ + threadEngine.Process(); + /* Process any modes that need to be (un)set */ ModeManager::ProcessModes(); diff --git a/src/misc.cpp b/src/misc.cpp index a835c8e41..72e991249 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -1317,7 +1317,7 @@ void Anope::Unhex(const Anope::string &src, char *dest) const Anope::string Anope::LastError() { #ifndef _WIN32 - return LastError(); + return strerror(errno); #else char errbuf[513]; DWORD err = GetLastError(); diff --git a/src/modules.cpp b/src/modules.cpp index 127620333..a31a94786 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -332,7 +332,6 @@ void Module::SendMessage(BotInfo *from, User *to, const char *fmt, ...) Service::Service(Module *o, const Anope::string &n) : owner(o), name(n) { - ModuleManager::RegisterService(this); } Service::~Service() diff --git a/src/socketengines/socketengine_eventfd.cpp b/src/socketengines/socketengine_eventfd.cpp index a0efe5286..4e4016326 100644 --- a/src/socketengines/socketengine_eventfd.cpp +++ b/src/socketengines/socketengine_eventfd.cpp @@ -1,32 +1,47 @@ #include "services.h" #include <sys/eventfd.h> -int Pipe::RecvInternal(char *buf, size_t sz) const +class PipeIO : public SocketIO { - static eventfd_t dummy; - return !eventfd_read(this->Sock, &dummy); -} - -int Pipe::SendInternal(const Anope::string &) const -{ - return !eventfd_write(this->Sock, 1); -} - -Pipe::Pipe() : Socket() + public: + /** Receive something from the buffer + * @param s The socket + * @param buf The buf to read to + * @param sz How much to read + * @return Number of bytes received + */ + int Recv(Socket *s, char *buf, size_t sz) const + { + static eventfd_t dummy; + return !eventfd_read(s->GetFD(), &dummy); + } + + /** Write something to the socket + * @param s The socket + * @param buf What to write + * @return Number of bytes written + */ + int Send(Socket *s, const Anope::string &buf) const + { + return !eventfd_write(s->GetFD(), 1); + } +} pipeSocketIO; + +Pipe::Pipe() : BufferedSocket() { + this->IO = &pipeSocketIO; this->Sock = eventfd(0, EFD_NONBLOCK); if (this->Sock < 0) throw CoreException(Anope::string("Could not create pipe: ") + Anope::LastError()); this->IPv6 = false; - this->Type = SOCKTYPE_CLIENT; SocketEngine->AddSocket(this); } bool Pipe::ProcessRead() { - this->RecvInternal(NULL, 0); + this->IO->Recv(this, NULL, 0); return this->Read(""); } diff --git a/src/socketengines/socketengine_pipe.cpp b/src/socketengines/socketengine_pipe.cpp index b0be664c8..21a558d66 100644 --- a/src/socketengines/socketengine_pipe.cpp +++ b/src/socketengines/socketengine_pipe.cpp @@ -13,7 +13,7 @@ int Pipe::SendInternal(const Anope::string &) const return write(this->WritePipe, &dummy, 1); } -Pipe::Pipe() : Socket() +Pipe::Pipe() : BufferedSocket() { int fds[2]; if (pipe(fds)) @@ -26,7 +26,6 @@ Pipe::Pipe() : Socket() this->Sock = fds[0]; this->WritePipe = fds[1]; this->IPv6 = false; - this->Type = SOCKTYPE_CLIENT; SocketEngine->AddSocket(this); } diff --git a/src/socketengines/socketengine_win32.cpp b/src/socketengines/socketengine_win32.cpp index 1b1a4e50d..5d2c2d7cb 100644 --- a/src/socketengines/socketengine_win32.cpp +++ b/src/socketengines/socketengine_win32.cpp @@ -7,9 +7,9 @@ class LSocket : public ListenSocket public: LSocket(const Anope::string &host, int port) : ListenSocket(host, port) { } - bool OnAccept(Socket *s) + bool OnAccept(int fd, const sockaddrs &) { - newsocket = s; + newsocket = new Socket(fd, this->IPv6); return true; } }; @@ -26,7 +26,7 @@ int Pipe::SendInternal(const Anope::string &) const return write(this->WritePipe, &dummy, 1); } -Pipe::Pipe() : Socket() +Pipe::Pipe() : BufferedSocket() { LSocket lfs("127.0.0.1", 0); @@ -47,7 +47,6 @@ Pipe::Pipe() : Socket() this->Sock = cfd; this->WritePipe = newsocket->GetSock(); this->IPv6 = false; - this->Type = SOCKTYPE_CLIENT; SocketEngine->AddSocket(this); newsocket = NULL; diff --git a/src/sockets.cpp b/src/sockets.cpp index fe712357a..f22238fcc 100644 --- a/src/sockets.cpp +++ b/src/sockets.cpp @@ -4,6 +4,8 @@ SocketEngineBase *SocketEngine = NULL; int32 TotalRead = 0; int32 TotalWritten = 0; +SocketIO normalSocketIO; + /** Trims all the \r and \ns from the begining and end of a string * @param buffer The buffer to trim */ @@ -15,6 +17,20 @@ static void TrimBuf(std::string &buffer) buffer.erase(buffer.length() - 1); } +/** Construct the object, sets everything to 0 + */ +sockaddrs::sockaddrs() +{ + this->clear(); +} + +/** Memset the object to 0 + */ +void sockaddrs::clear() +{ + memset(this, 0, sizeof(*this)); +} + /** Get the size of the sockaddr we represent * @return The size */ @@ -75,13 +91,6 @@ Anope::string sockaddrs::addr() const return address; } -/** Construct the object, sets everything to 0 - */ -sockaddrs::sockaddrs() -{ - memset(this, 0, sizeof(*this)); -} - /** Check if this sockaddr has data in it */ bool sockaddrs::operator()() const @@ -184,25 +193,96 @@ SocketEngineBase::~SocketEngineBase() #endif } +/** Receive something from the buffer + * @param s The socket + * @param buf The buf to read to + * @param sz How much to read + * @return Number of bytes received + */ +int SocketIO::Recv(Socket *s, char *buf, size_t sz) const +{ + size_t i = recv(s->GetFD(), buf, sz, 0); + TotalRead += i; + return i; +} + +/** Write something to the socket + * @param s The socket + * @param buf What to write + * @return Number of bytes written + */ +int SocketIO::Send(Socket *s, const Anope::string &buf) const +{ + size_t i = send(s->GetFD(), buf.c_str(), buf.length(), 0); + TotalWritten += i; + return i; +} + +/** Accept a connection from a socket + * @param s The socket + */ +void SocketIO::Accept(ListenSocket *s) +{ + sockaddrs conaddr; + + socklen_t size = conaddr.size(); + int newsock = accept(s->GetFD(), &conaddr.sa, &size); + +#ifndef INVALID_SOCKET +# define INVALID_SOCKET 0 +#endif + + if (newsock > 0 && newsock != INVALID_SOCKET) + s->OnAccept(newsock, conaddr); + else + throw SocketException("Unable to accept connection: " + Anope::LastError()); +} + +/** Connect the socket + * @param s THe socket + * @param target IP to connect to + * @param port to connect to + * @param bindip IP to bind to, if any + */ +void SocketIO::Connect(ConnectionSocket *s, const Anope::string &target, int port, const Anope::string &bindip) +{ + s->bindaddr.clear(); + s->conaddr.clear(); + + if (!bindip.empty()) + { + s->bindaddr.pton(s->IsIPv6() ? AF_INET6 : AF_INET, bindip, 0); + if (bind(s->GetFD(), &s->bindaddr.sa, s->bindaddr.size()) == -1) + throw SocketException(Anope::string("Unable to bind to address: ") + Anope::LastError()); + } + + s->conaddr.pton(s->IsIPv6() ? AF_INET6 : AF_INET, target, port); + if (connect(s->GetFD(), &s->conaddr.sa, s->conaddr.size()) == -1 && errno != EINPROGRESS) + throw SocketException(Anope::string("Error connecting to server: ") + Anope::LastError()); +} + /** Empty constructor, used for things such as the pipe socket */ Socket::Socket() { + this->Type = SOCKTYPE_BASE; + this->IO = &normalSocketIO; } /** Constructor - * @param nsock The socket - * @param nIPv6 IPv6? + * @param sock The socket + * @param ipv6 IPv6? * @param type The socket type, defaults to SOCK_STREAM */ -Socket::Socket(int nsock, bool nIPv6, int type) +Socket::Socket(int sock, bool ipv6, int type) { - Type = SOCKTYPE_CLIENT; - IPv6 = nIPv6; - if (nsock == 0) - Sock = socket(IPv6 ? AF_INET6 : AF_INET, type, 0); + this->Type = SOCKTYPE_BASE; + this->IO = &normalSocketIO; + this->IPv6 = ipv6; + if (sock == 0) + this->Sock = socket(this->IPv6 ? AF_INET6 : AF_INET, type, 0); else - Sock = nsock; + this->Sock = sock; SocketEngine->AddSocket(this); } @@ -212,34 +292,24 @@ Socket::~Socket() { if (SocketEngine) SocketEngine->DelSocket(this); - CloseSocket(Sock); -} - -/** Really receive something from the buffer - * @param buf The buf to read to - * @param sz How much to read - * @return Number of bytes received - */ -int Socket::RecvInternal(char *buf, size_t sz) const -{ - return recv(GetSock(), buf, sz, 0); + CloseSocket(this->Sock); + this->IO->Destroy(); } -/** Really write something to the socket - * @param buf What to write - * @return Number of bytes written +/** Get the socket FD for this socket + * @return the fd */ -int Socket::SendInternal(const Anope::string &buf) const +int Socket::GetFD() const { - return send(GetSock(), buf.c_str(), buf.length(), 0); + return Sock; } -/** Get the socket FD for this socket - * @return the fd +/** Check if this socket is IPv6 + * @return true or false */ -int Socket::GetSock() const +bool Socket::IsIPv6() const { - return Sock; + return IPv6; } /** Mark a socket as blockig @@ -249,10 +319,10 @@ bool Socket::SetBlocking() { #ifdef _WIN32 unsigned long opt = 0; - return !ioctlsocket(this->GetSock(), FIONBIO, &opt); + return !ioctlsocket(this->GetFD(), FIONBIO, &opt); #else - int flags = fcntl(this->GetSock(), F_GETFL, 0); - return !fcntl(this->GetSock(), F_SETFL, flags & ~O_NONBLOCK); + int flags = fcntl(this->GetFD(), F_GETFL, 0); + return !fcntl(this->GetFD(), F_SETFL, flags & ~O_NONBLOCK); #endif } @@ -263,45 +333,67 @@ bool Socket::SetNonBlocking() { #ifdef _WIN32 unsigned long opt = 1; - return !ioctlsocket(this->GetSock(), FIONBIO, &opt); + return !ioctlsocket(this->GetFD(), FIONBIO, &opt); #else - int flags = fcntl(this->GetSock(), F_GETFL, 0); - return !fcntl(this->GetSock(), F_SETFL, flags | O_NONBLOCK); + int flags = fcntl(this->GetFD(), F_GETFL, 0); + return !fcntl(this->GetFD(), F_SETFL, flags | O_NONBLOCK); #endif } -/** Check if this socket is IPv6 - * @return true or false +/** Called when there is something to be received for this socket + * @return true on success, false to drop this socket */ -bool Socket::IsIPv6() const +bool Socket::ProcessRead() { - return IPv6; + return true; } -/** Get the length of the read buffer - * @return The length of the read buffer +/** Called when the socket is ready to be written to + * @return true on success, false to drop this socket */ -size_t Socket::ReadBufferLen() const +bool Socket::ProcessWrite() { - return RecvLen; + return true; } -/** Get the length of the write buffer - * @return The length of the write buffer +/** Called when there is an error for this socket + * @return true on success, false to drop this socket */ -size_t Socket::WriteBufferLen() const +void Socket::ProcessError() +{ +} + +/** Constructor for pipe socket + */ +BufferedSocket::BufferedSocket() : Socket() +{ + this->Type = SOCKTYPE_BUFFERED; +} + +/** Constructor + * @param fd FD to use + * @param ipv6 true for ipv6 + * @param type socket type, defaults to SOCK_STREAM + */ +BufferedSocket::BufferedSocket(int fd, bool ipv6, int type) : Socket(fd, ipv6, type) +{ + this->Type = SOCKTYPE_BUFFERED; +} + +/** Default destructor + */ +BufferedSocket::~BufferedSocket() { - return WriteBuffer.length(); } /** Called when there is something to be received for this socket * @return true on success, false to drop this socket */ -bool Socket::ProcessRead() +bool BufferedSocket::ProcessRead() { char tbuffer[NET_BUFSIZE] = ""; - RecvLen = RecvInternal(tbuffer, sizeof(tbuffer) - 1); + RecvLen = this->IO->Recv(this, tbuffer, sizeof(tbuffer) - 1); if (RecvLen <= 0) return false; @@ -341,13 +433,13 @@ bool Socket::ProcessRead() /** Called when the socket is ready to be written to * @return true on success, false to drop this socket */ -bool Socket::ProcessWrite() +bool BufferedSocket::ProcessWrite() { if (WriteBuffer.empty()) { return true; } - if (SendInternal(WriteBuffer) == -1) + if (this->IO->Send(this, WriteBuffer) == -1) { return false; } @@ -357,18 +449,11 @@ bool Socket::ProcessWrite() return true; } -/** Called when there is an error for this socket - * @return true on success, false to drop this socket - */ -void Socket::ProcessError() -{ -} - /** Called with a line received from the socket * @param buf The line * @return true to continue reading, false to drop the socket */ -bool Socket::Read(const Anope::string &buf) +bool BufferedSocket::Read(const Anope::string &buf) { return false; } @@ -376,7 +461,7 @@ bool Socket::Read(const Anope::string &buf) /** Write to the socket * @param message The message */ -void Socket::Write(const char *message, ...) +void BufferedSocket::Write(const char *message, ...) { va_list vi; char tbuffer[BUFSIZE]; @@ -395,58 +480,36 @@ void Socket::Write(const char *message, ...) /** Write to the socket * @param message The message */ -void Socket::Write(const Anope::string &message) +void BufferedSocket::Write(const Anope::string &message) { WriteBuffer.append(message.str() + "\r\n"); SocketEngine->MarkWritable(this); } -/** Constructor - * @param TargetHost The target host to connect to - * @param Port The target port to connect to - * @param BindHost The host to bind to for connecting - * @param nIPv6 true to use IPv6 - * @param type The socket type, defaults to SOCK_STREAM - */ -ClientSocket::ClientSocket(const Anope::string &TargetHost, int Port, const Anope::string &BindHost, bool nIPv6, int type) : Socket(0, nIPv6, type) -{ - this->SetNonBlocking(); - - if (!BindHost.empty()) - { - this->bindaddrs.pton(IPv6 ? AF_INET6 : AF_INET, BindHost, 0); - if (bind(Sock, &this->bindaddrs.sa, this->bindaddrs.size()) == -1) - throw SocketException(Anope::string("Unable to bind to address: ") + Anope::LastError()); - } - - this->conaddrs.pton(IPv6 ? AF_INET6 : AF_INET, TargetHost, Port); - if (connect(Sock, &this->conaddrs.sa, this->conaddrs.size()) == -1 && errno != EINPROGRESS) - throw SocketException(Anope::string("Error connecting to server: ") + Anope::LastError()); -} - -/** Default destructor +/** Get the length of the read buffer + * @return The length of the read buffer */ -ClientSocket::~ClientSocket() +size_t BufferedSocket::ReadBufferLen() const { + return RecvLen; } -/** Called with a line received from the socket - * @param buf The line - * @return true to continue reading, false to drop the socket +/** Get the length of the write buffer + * @return The length of the write buffer */ -bool ClientSocket::Read(const Anope::string &buf) +size_t BufferedSocket::WriteBufferLen() const { - return true; + return WriteBuffer.length(); } /** Constructor * @param bindip The IP to bind to * @param port The port to listen on + * @param ipv6 true for ipv6 */ -ListenSocket::ListenSocket(const Anope::string &bindip, int port) : Socket(0, (bindip.find(':') != Anope::string::npos ? true : false)) +ListenSocket::ListenSocket(const Anope::string &bindip, int port, bool ipv6) : Socket(0, ipv6) { - Type = SOCKTYPE_LISTEN; - + this->Type = SOCKTYPE_LISTEN; this->SetNonBlocking(); this->listenaddrs.pton(IPv6 ? AF_INET6 : AF_INET, bindip, port); @@ -468,24 +531,61 @@ ListenSocket::~ListenSocket() */ bool ListenSocket::ProcessRead() { - int newsock = accept(Sock, NULL, NULL); + try + { + this->IO->Accept(this); + } + catch (const SocketException &ex) + { + Log() << ex.GetReason(); + } + return true; +} -#ifndef INVALID_SOCKET -# define INVALID_SOCKET 0 -#endif +/** Called when a connection is accepted + * @param fd The FD for the new connection + * @param addr The sockaddr for where the connection came from + * @return The new socket + */ +ClientSocket *ListenSocket::OnAccept(int fd, const sockaddrs &addr) +{ + return new ClientSocket(this, fd, addr); +} - if (newsock > 0 && newsock != INVALID_SOCKET) - return this->OnAccept(new Socket(newsock, IPv6)); +/** Constructor + * @param ipv6 true to use IPv6 + * @param type The socket type, defaults to SOCK_STREAM + */ +ConnectionSocket::ConnectionSocket(bool ipv6, int type) : BufferedSocket(0, ipv6, type) +{ + this->Type = SOCKTYPE_CONNECTION; +} - return true; +/** Connect the socket + * @param TargetHost The target host to connect to + * @param Port The target port to connect to + * @param BindHost The host to bind to for connecting + */ +void ConnectionSocket::Connect(const Anope::string &TargetHost, int Port, const Anope::string &BindHost) +{ + try + { + this->IO->Connect(this, TargetHost, Port, BindHost); + } + catch (const SocketException &) + { + delete this; + throw; + } } -/** Called when a connection is accepted - * @param s The socket for the new connection - * @return true if the listen socket should remain alive +/** Constructor + * @param ls Listen socket this connection is from + * @param fd New FD for this socket + * @param addr Address the connection came from */ -bool ListenSocket::OnAccept(Socket *s) +ClientSocket::ClientSocket(ListenSocket *ls, int fd, const sockaddrs &addr) : BufferedSocket(fd, ls->IsIPv6()), LS(ls), clientaddr(addr) { - return true; + this->Type = SOCKTYPE_CLIENT; } diff --git a/src/threadengine.cpp b/src/threadengine.cpp index 36a73d324..0ac049c38 100644 --- a/src/threadengine.cpp +++ b/src/threadengine.cpp @@ -2,16 +2,39 @@ ThreadEngine threadEngine; +/** Check for finished threads + */ +void ThreadEngine::Process() +{ + for (unsigned i = this->threads.size(); i > 0; --i) + { + Thread *t = this->threads[i - 1]; + + if (t->GetExitState()) + { + t->Join(); + delete t; + } + } +} + /** Threads constructor */ Thread::Thread() : Exit(false) { + threadEngine.threads.push_back(this); } /** Threads destructor */ Thread::~Thread() { + std::vector<Thread *>::iterator it = std::find(threadEngine.threads.begin(), threadEngine.threads.end(), this); + + if (it != threadEngine.threads.end()) + { + threadEngine.threads.erase(it); + } } /** Sets the exit state as true informing the thread we want it to shut down diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp index fb0b29506..65d1533f3 100644 --- a/src/threadengines/threadengine_pthread.cpp +++ b/src/threadengines/threadengine_pthread.cpp @@ -10,9 +10,7 @@ static void *entry_point(void *parameter) { Thread *thread = static_cast<Thread *>(parameter); thread->Run(); - if (!thread->GetExitState()) - thread->Join(); - delete thread; + thread->SetExitState(); pthread_exit(0); } @@ -22,6 +20,8 @@ ThreadEngine::ThreadEngine() { if (pthread_attr_init(&threadengine_attr)) throw CoreException("ThreadEngine: Error calling pthread_attr_init"); + if (pthread_attr_setdetachstate(&threadengine_attr, PTHREAD_CREATE_JOINABLE)) + throw CoreException("ThreadEngine: Unable to mark threads as joinable"); } /** Threadengines destructor @@ -35,7 +35,7 @@ ThreadEngine::~ThreadEngine() */ void Thread::Join() { - SetExitState(); + this->SetExitState(); pthread_join(Handle, NULL); } diff --git a/src/threadengines/threadengine_win32.cpp b/src/threadengines/threadengine_win32.cpp index b67f01681..8fb103267 100644 --- a/src/threadengines/threadengine_win32.cpp +++ b/src/threadengines/threadengine_win32.cpp @@ -7,9 +7,7 @@ static DWORD WINAPI entry_point(void *parameter) { Thread *thread = static_cast<Thread *>(parameter); thread->Run(); - if (!thread->GetExitState()) - thread->Join(); - delete thread; + thread->SetExitState(); return 0; } @@ -29,7 +27,7 @@ ThreadEngine::~ThreadEngine() */ void Thread::Join() { - SetExitState(); + this->SetExitState(); WaitForSingleObject(Handle, INFINITE); } diff --git a/src/users.cpp b/src/users.cpp index d4b1e15f2..b89e6c271 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -741,6 +741,7 @@ User *do_nick(const Anope::string &source, const Anope::string &nick, const Anop if (!vhost.empty()) user->SetCloakedHost(vhost); user->SetVIdent(username); + user->SetModesInternal(modes.c_str()); if (!ip.empty()) { |