diff options
author | danieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7> | 2009-10-21 23:46:33 +0000 |
---|---|---|
committer | danieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7> | 2009-10-21 23:46:33 +0000 |
commit | d221de88276b9e33a108281a9cd0a58875032fc6 (patch) | |
tree | 3032609bd5ef7f4a6a4bad5c0b2a4100432ea2df | |
parent | dcbb0ae938711cd49df73dc2ff6cd6289aeefb44 (diff) |
Kill ListenSocketBase, use OnAcceptConnection for all new connections
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11950 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | conf/modules.conf.example | 12 | ||||
-rw-r--r-- | include/inspircd.h | 2 | ||||
-rw-r--r-- | include/modules.h | 13 | ||||
-rw-r--r-- | include/socket.h | 52 | ||||
-rw-r--r-- | include/usermanager.h | 2 | ||||
-rw-r--r-- | src/inspsocket.cpp | 1 | ||||
-rw-r--r-- | src/listensocket.cpp | 52 | ||||
-rw-r--r-- | src/modules.cpp | 3 | ||||
-rw-r--r-- | src/modules/extra/m_ssl_gnutls.cpp | 12 | ||||
-rw-r--r-- | src/modules/extra/m_ssl_openssl.cpp | 12 | ||||
-rw-r--r-- | src/modules/m_httpd.cpp | 111 | ||||
-rw-r--r-- | src/modules/m_spanningtree/main.cpp | 6 | ||||
-rw-r--r-- | src/modules/m_spanningtree/main.h | 1 | ||||
-rw-r--r-- | src/modules/m_spanningtree/treesocket.h | 2 | ||||
-rw-r--r-- | src/modules/m_spanningtree/treesocket1.cpp | 2 | ||||
-rw-r--r-- | src/modules/m_spanningtree/utils.cpp | 61 | ||||
-rw-r--r-- | src/modules/m_spanningtree/utils.h | 18 | ||||
-rw-r--r-- | src/socket.cpp | 64 | ||||
-rw-r--r-- | src/stats.cpp | 6 | ||||
-rw-r--r-- | src/usermanager.cpp | 2 |
20 files changed, 140 insertions, 294 deletions
diff --git a/conf/modules.conf.example b/conf/modules.conf.example index 4dca4f3b9..e9a306e3f 100644 --- a/conf/modules.conf.example +++ b/conf/modules.conf.example @@ -834,15 +834,9 @@ # #-#-#-#-#-#-#-#-#-#-#-#- HTTPD CONFIGURATION -#-#-#-#-#-#-#-#-#-#-# # -# Optional - If you choose to use the m_httpd.so module, then you must -# specify the port number and other details of your http server: -# -#<http ip="192.168.1.10" host="brainwave" port="32006" -# index="/home/brain/inspircd/http/index.html"> -# -# You may have as many of these tags as you wish, each with a different -# IP, port, host or index file. Each one will act as an independent -# HTTP server. +# If you choose to use the m_httpd.so module, then you will need to add +# a <bind> tag with type "httpd", and load at least one of the other +# m_httpd_* modules to provide pages to display. # #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# diff --git a/include/inspircd.h b/include/inspircd.h index ab815dd10..4198864ca 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -458,7 +458,7 @@ class CoreExport InspIRCd /** List of the open ports */ - std::vector<ListenSocketBase *> ports; + std::vector<ListenSocket*> ports; /** Set to the current signal recieved */ diff --git a/include/modules.h b/include/modules.h index a066d104a..08f1a2dc0 100644 --- a/include/modules.h +++ b/include/modules.h @@ -303,7 +303,7 @@ enum Implementation I_OnSendSnotice, I_OnUserPreJoin, I_OnUserPreKick, I_OnUserKick, I_OnOper, I_OnInfo, I_OnWhois, I_OnUserPreInvite, I_OnUserInvite, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreNick, I_OnUserMessage, I_OnUserNotice, I_OnMode, I_OnGetServerDescription, I_OnSyncUser, - I_OnSyncChannel, I_OnDecodeMetaData, I_OnWallops, + I_OnSyncChannel, I_OnDecodeMetaData, I_OnWallops, I_OnAcceptConnection, I_OnChangeHost, I_OnChangeName, I_OnAddLine, I_OnDelLine, I_OnExpireLine, I_OnUserPostNick, I_OnPreMode, I_On005Numeric, I_OnKill, I_OnRemoteKill, I_OnLoadModule, I_OnUnloadModule, I_OnBackgroundTimer, I_OnPreCommand, I_OnCheckReady, I_OnCheckInvite, @@ -1149,7 +1149,16 @@ class CoreExport Module : public classbase * @param user The item to possibly install the I/O hook on * @param via The port that <user> connected on */ - virtual void OnHookIO(StreamSocket*, ListenSocketBase* via); + virtual void OnHookIO(StreamSocket*, ListenSocket* via); + + /** Called when a port accepts a connection + * Return MOD_RES_ACCEPT if you have used the file descriptor. + * @param fd The file descriptor returned from accept() + * @param from The local port the user connected to + * @param client The client IP address and port + * @param server The server IP address and port + */ + virtual ModResult OnAcceptConnection(int fd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server); /** Called immediately after any connection is accepted. This is intended for raw socket * processing (e.g. modules which wrap the tcp connection within another library) and provides diff --git a/include/socket.h b/include/socket.h index a42696379..a9b0a472e 100644 --- a/include/socket.h +++ b/include/socket.h @@ -124,68 +124,32 @@ namespace irc } } +struct ConfigTag; /** This class handles incoming connections on client ports. * It will create a new User for every valid connection * and assign it a file descriptor. */ -class CoreExport ListenSocketBase : public EventHandler +class CoreExport ListenSocket : public EventHandler { - protected: - /** Raw address socket is bound to */ + public: + const reference<ConfigTag> bind_tag; std::string bind_addr; - /** Human-readable address/port socket is bound to */ + int bind_port; + /** Human-readable bind description */ std::string bind_desc; - - /** The client address if the most recently connected client. - * Should only be used when accepting a new client. - */ - static irc::sockets::sockaddrs client; - /** The server address used by the most recently connected client. - * This may differ from the bind address by having a nonzero address, - * if the port is wildcard bound, or being IPv4 on a 6to4 IPv6 port. - * The address family will always match that of "client" - */ - static irc::sockets::sockaddrs server; - - public: - /** Socket type (client/server) */ - const std::string type; - /** Socket hook (plain/gnutls/openssl/zip) */ - const std::string hook; - /** Port socket is bound to */ - const int bind_port; /** Create a new listening socket */ - ListenSocketBase(int port, const std::string &addr, const std::string &type, const std::string &hook); + ListenSocket(ConfigTag* tag, const std::string& addr, int port); /** Handle an I/O event */ void HandleEvent(EventType et, int errornum = 0); /** Close the socket */ - ~ListenSocketBase(); - - /** Get IP address socket is bound to - */ - const std::string &GetIP() { return bind_addr; } - - const std::string &GetBindDesc() { return bind_desc; } + ~ListenSocket(); /** Handles sockets internals crap of a connection, convenience wrapper really */ void AcceptInternal(); - - /** Called when a new connection has successfully been accepted on this listener. - * @param fd The file descriptor of the new connection - */ - virtual void OnAcceptReady(int fd) = 0; -}; - -class CoreExport ClientListenSocket : public ListenSocketBase -{ - virtual void OnAcceptReady(int fd); - public: - ClientListenSocket(int port, const std::string &addr, const std::string &Type, const std::string &Hook) - : ListenSocketBase(port, addr, Type, Hook) { } }; #endif diff --git a/include/usermanager.h b/include/usermanager.h index 885394f76..97277f3fb 100644 --- a/include/usermanager.h +++ b/include/usermanager.h @@ -74,7 +74,7 @@ class CoreExport UserManager * @param server The server IP address and port used by the user * @return This function has no return value, but a call to AddClient may remove the user. */ - void AddUser(int socket, ClientListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server); + void AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server); /** Disconnect a user gracefully * @param user The user to remove diff --git a/src/inspsocket.cpp b/src/inspsocket.cpp index fc8bc142f..a37cb9ea0 100644 --- a/src/inspsocket.cpp +++ b/src/inspsocket.cpp @@ -395,6 +395,7 @@ void StreamSocket::DoWrite() else if (errno == EINTR) { // restart interrupted syscall + errno = 0; } else { diff --git a/src/listensocket.cpp b/src/listensocket.cpp index e0a18a043..8b7053d8f 100644 --- a/src/listensocket.cpp +++ b/src/listensocket.cpp @@ -17,30 +17,21 @@ #include "socket.h" #include "socketengine.h" -/* Private static member data must be declared in this manner */ -irc::sockets::sockaddrs ListenSocketBase::client; -irc::sockets::sockaddrs ListenSocketBase::server; - -ListenSocketBase::ListenSocketBase(int port, const std::string &addr, const std::string &Type, const std::string &Hook) - : type(Type), hook(Hook), bind_port(port) +ListenSocket::ListenSocket(ConfigTag* tag, const std::string& addr, int port) + : bind_tag(tag) { irc::sockets::sockaddrs bind_to; // canonicalize address if it is defined if (!irc::sockets::aptosa(addr, port, &bind_to)) { - // malformed address - bind_addr = addr; - bind_desc = addr + ":" + ConvToStr(port); - this->fd = -1; + fd = -1; + return; } - else - { - irc::sockets::satoap(&bind_to, bind_addr, port); - bind_desc = irc::sockets::satouser(&bind_to); + irc::sockets::satoap(&bind_to, bind_addr, bind_port); + bind_desc = irc::sockets::satouser(&bind_to); - this->fd = irc::sockets::OpenTCPSocket(bind_addr); - } + fd = irc::sockets::OpenTCPSocket(bind_addr); if (this->fd > -1) { @@ -62,7 +53,7 @@ ListenSocketBase::ListenSocketBase(int port, const std::string &addr, const std: } } -ListenSocketBase::~ListenSocketBase() +ListenSocket::~ListenSocket() { if (this->GetFd() > -1) { @@ -75,8 +66,11 @@ ListenSocketBase::~ListenSocketBase() } /* Just seperated into another func for tidiness really.. */ -void ListenSocketBase::AcceptInternal() +void ListenSocket::AcceptInternal() { + irc::sockets::sockaddrs client; + irc::sockets::sockaddrs server; + ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket"); int incomingSockfd; @@ -148,10 +142,23 @@ void ListenSocketBase::AcceptInternal() ServerInstance->SE->NonBlocking(incomingSockfd); ServerInstance->stats->statsAccept++; - this->OnAcceptReady(incomingSockfd); + + ModResult res; + FIRST_MOD_RESULT(OnAcceptConnection, res, (incomingSockfd, this, &client, &server)); + if (res == MOD_RES_PASSTHRU) + { + std::string type = bind_tag->getString("type", "clients"); + if (type == "clients") + { + ServerInstance->Users->AddUser(incomingSockfd, this, &client, &server); + res = MOD_RES_ALLOW; + } + } + if (res != MOD_RES_ALLOW) + ServerInstance->SE->Close(incomingSockfd); } -void ListenSocketBase::HandleEvent(EventType e, int err) +void ListenSocket::HandleEvent(EventType e, int err) { switch (e) { @@ -166,8 +173,3 @@ void ListenSocketBase::HandleEvent(EventType e, int err) break; } } - -void ClientListenSocket::OnAcceptReady(int nfd) -{ - ServerInstance->Users->AddUser(nfd, this, &client, &server); -} diff --git a/src/modules.cpp b/src/modules.cpp index f70c45bf2..a1c53a1e8 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -149,7 +149,8 @@ void Module::OnText(User*, void*, int, const std::string&, char, CUList&) { } void Module::OnRunTestSuite() { } void Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { } ModResult Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; } -void Module::OnHookIO(StreamSocket*, ListenSocketBase*) { } +void Module::OnHookIO(StreamSocket*, ListenSocket*) { } +ModResult Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; } void Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { } ModResult Module::OnChannelRestrictionApply(User*, Channel*, const char*) { return MOD_RES_PASSTHRU; } diff --git a/src/modules/extra/m_ssl_gnutls.cpp b/src/modules/extra/m_ssl_gnutls.cpp index 29e439ed3..25b47c2fe 100644 --- a/src/modules/extra/m_ssl_gnutls.cpp +++ b/src/modules/extra/m_ssl_gnutls.cpp @@ -173,14 +173,14 @@ class ModuleSSLGnuTLS : public Module for (size_t i = 0; i < ServerInstance->ports.size(); i++) { - ListenSocketBase* port = ServerInstance->ports[i]; - if (port->hook != "gnutls") + ListenSocket* port = ServerInstance->ports[i]; + if (port->bind_tag->getString("ssl") != "gnutls") continue; - const std::string& portid = port->GetBindDesc(); + const std::string& portid = port->bind_desc; ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %s", portid.c_str()); - if (port->type == "clients" && port->GetIP() != "127.0.0.1") + if (port->bind_tag->getString("type", "clients") == "clients" && port->bind_addr != "127.0.0.1") sslports.append(portid).append(";"); } @@ -321,9 +321,9 @@ class ModuleSSLGnuTLS : public Module output.append(" STARTTLS"); } - void OnHookIO(StreamSocket* user, ListenSocketBase* lsb) + void OnHookIO(StreamSocket* user, ListenSocket* lsb) { - if (!user->GetIOHook() && lsb->hook == "gnutls") + if (!user->GetIOHook() && lsb->bind_tag->getString("ssl") == "gnutls") { /* Hook the user with our module */ user->AddIOHook(this); diff --git a/src/modules/extra/m_ssl_openssl.cpp b/src/modules/extra/m_ssl_openssl.cpp index 0c01e4d08..25d1cd9dd 100644 --- a/src/modules/extra/m_ssl_openssl.cpp +++ b/src/modules/extra/m_ssl_openssl.cpp @@ -131,9 +131,9 @@ class ModuleSSLOpenSSL : public Module ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation)); } - void OnHookIO(StreamSocket* user, ListenSocketBase* lsb) + void OnHookIO(StreamSocket* user, ListenSocket* lsb) { - if (!user->GetIOHook() && lsb->hook == "openssl") + if (!user->GetIOHook() && lsb->bind_tag->getString("ssl") == "openssl") { /* Hook the user with our module */ user->AddIOHook(this); @@ -148,13 +148,13 @@ class ModuleSSLOpenSSL : public Module for (size_t i = 0; i < ServerInstance->ports.size(); i++) { - ListenSocketBase* port = ServerInstance->ports[i]; - if (port->hook != "openssl") + ListenSocket* port = ServerInstance->ports[i]; + if (port->bind_tag->getString("ssl") != "openssl") continue; - std::string portid = port->GetBindDesc(); + std::string portid = port->bind_desc; ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "m_ssl_openssl.so: Enabling SSL for port %s", portid.c_str()); - if (port->type == "clients" && port->GetIP() != "127.0.0.1") + if (port->bind_tag->getString("type", "clients") == "clients" && port->bind_addr != "127.0.0.1") sslports.append(portid).append(";"); } diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index 76e89666a..7fc342e04 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -35,7 +35,6 @@ enum HttpState */ class HttpServerSocket : public BufferedSocket { - FileReader* index; HttpState InternalState; std::string ip; @@ -49,15 +48,14 @@ class HttpServerSocket : public BufferedSocket public: - HttpServerSocket(int newfd, const char* IP, FileReader* ind) - : BufferedSocket(newfd), index(ind), ip(IP), postsize(0) + HttpServerSocket(int newfd, const std::string& IP, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) + : BufferedSocket(newfd), ip(IP), postsize(0) { InternalState = HTTP_SERVE_WAIT_REQUEST; - } - FileReader* GetIndex() - { - return index; + FOREACH_MOD(I_OnHookIO, OnHookIO(this, via)); + if (GetIOHook()) + GetIOHook()->OnStreamSocketAccept(this, client, server); } virtual void OnError(BufferedSocketError) @@ -300,25 +298,16 @@ class HttpServerSocket : public BufferedSocket { InternalState = HTTP_SERVE_SEND_DATA; - if ((request_type == "GET") && (uri == "/")) - { - HTTPHeaders empty; - SendHeaders(index->ContentSize(), 200, empty); - WriteData(index->Contents()); - } - else + claimed = false; + HTTPRequest acl((Module*)HttpModule, "httpd_acl", request_type, uri, &headers, this, ip, postdata); + acl.Send(); + if (!claimed) { - claimed = false; - HTTPRequest acl((Module*)HttpModule, "httpd_acl", request_type, uri, &headers, this, ip, postdata); - acl.Send(); + HTTPRequest url((Module*)HttpModule, "httpd_url", request_type, uri, &headers, this, ip, postdata); + url.Send(); if (!claimed) { - HTTPRequest url((Module*)HttpModule, "httpd_url", request_type, uri, &headers, this, ip, postdata); - url.Send(); - if (!claimed) - { - SendHTTPError(404); - } + SendHTTPError(404); } } } @@ -330,71 +319,12 @@ class HttpServerSocket : public BufferedSocket } }; -/** Spawn HTTP sockets from a listener - */ -class HttpListener : public ListenSocketBase -{ - FileReader* index; - - public: - HttpListener(FileReader *idx, int port, const std::string &addr) - : ListenSocketBase(port, addr, "httpd", "plaintext") - { - this->index = idx; - } - - ~HttpListener() - { - delete index; - } - - virtual void OnAcceptReady(int nfd) - { - int port; - std::string incomingip; - irc::sockets::satoap(&client, incomingip, port); - new HttpServerSocket(nfd, incomingip.c_str(), index); - } -}; - class ModuleHttpServer : public Module { std::vector<HttpServerSocket *> httpsocks; - std::vector<HttpListener *> httplisteners; public: - void ReadConfig() - { - int port; - std::string host; - std::string bindip; - std::string indexfile; - FileReader* index; - HttpListener *http; - ConfigReader c; - - httpsocks.clear(); // XXX this will BREAK if this module is made rehashable - httplisteners.clear(); - - for (int i = 0; i < c.Enumerate("http"); i++) - { - host = c.ReadValue("http", "host", i); - bindip = c.ReadValue("http", "ip", i); - port = c.ReadInteger("http", "port", i, true); - indexfile = c.ReadValue("http", "index", i); - index = new FileReader(indexfile); - if (!index->Exists()) - { - delete index; - throw ModuleException("Can't read index file: "+indexfile); - } - http = new HttpListener(index, port, bindip); - httplisteners.push_back(http); - } - } - ModuleHttpServer() { - ReadConfig(); HttpModule = this; } @@ -407,15 +337,20 @@ class ModuleHttpServer : public Module resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers); } + ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) + { + if (from->bind_tag->getString("type") != "httpd"); + return MOD_RES_PASSTHRU; + int port; + std::string incomingip; + irc::sockets::satoap(client, incomingip, port); + new HttpServerSocket(nfd, incomingip, from, client, server); + return MOD_RES_ALLOW; + } + virtual ~ModuleHttpServer() { - for (size_t i = 0; i < httplisteners.size(); i++) - { - httplisteners[i]->cull(); - delete httplisteners[i]; - } - for (size_t i = 0; i < httpsocks.size(); i++) { httpsocks[i]->cull(); diff --git a/src/modules/m_spanningtree/main.cpp b/src/modules/m_spanningtree/main.cpp index a0acd9b08..624049357 100644 --- a/src/modules/m_spanningtree/main.cpp +++ b/src/modules/m_spanningtree/main.cpp @@ -49,7 +49,7 @@ ModuleSpanningTree::ModuleSpanningTree() I_OnChangeHost, I_OnChangeName, I_OnChangeIdent, I_OnUserPart, I_OnUnloadModule, I_OnUserQuit, I_OnUserPostNick, I_OnUserKick, I_OnRemoteKill, I_OnRehash, I_OnPreRehash, I_OnOper, I_OnAddLine, I_OnDelLine, I_OnMode, I_OnLoadModule, I_OnStats, - I_OnSetAway, I_OnPostCommand, I_OnUserConnect + I_OnSetAway, I_OnPostCommand, I_OnUserConnect, I_OnAcceptConnection }; ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation)); @@ -768,7 +768,7 @@ void ModuleSpanningTree::OnPreRehash(User* user, const std::string ¶meter) void ModuleSpanningTree::OnRehash(User* user) { // Re-read config stuff - Utils->ReadConfiguration(true); + Utils->ReadConfiguration(); } void ModuleSpanningTree::OnLoadModule(Module* mod) @@ -795,7 +795,7 @@ void ModuleSpanningTree::RedoConfig(Module* mod) if (mod->ModuleSourceFile == "m_sha256.so" || IsBufferSocketModule) { - Utils->ReadConfiguration(true); + Utils->ReadConfiguration(); } } diff --git a/src/modules/m_spanningtree/main.h b/src/modules/m_spanningtree/main.h index b9d5debb3..02513fb2e 100644 --- a/src/modules/m_spanningtree/main.h +++ b/src/modules/m_spanningtree/main.h @@ -190,6 +190,7 @@ class ModuleSpanningTree : public Module void ProtoSendMetaData(void* opaque, Extensible* target, const std::string &extname, const std::string &extdata); void OnLoadModule(Module* mod); void OnUnloadModule(Module* mod); + ModResult OnAcceptConnection(int newsock, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server); CullResult cull(); ~ModuleSpanningTree(); Version GetVersion(); diff --git a/src/modules/m_spanningtree/treesocket.h b/src/modules/m_spanningtree/treesocket.h index b5f97c30f..8359ebf37 100644 --- a/src/modules/m_spanningtree/treesocket.h +++ b/src/modules/m_spanningtree/treesocket.h @@ -103,7 +103,7 @@ class TreeSocket : public BufferedSocket * we must associate it with a socket without creating a new * connection. This constructor is used for this purpose. */ - TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocketBase* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server); + TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server); /** Get link state */ diff --git a/src/modules/m_spanningtree/treesocket1.cpp b/src/modules/m_spanningtree/treesocket1.cpp index 4a0af45b8..962b63984 100644 --- a/src/modules/m_spanningtree/treesocket1.cpp +++ b/src/modules/m_spanningtree/treesocket1.cpp @@ -69,7 +69,7 @@ found: * we must associate it with a socket without creating a new * connection. This constructor is used for this purpose. */ -TreeSocket::TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocketBase* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) +TreeSocket::TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) : BufferedSocket(newfd), Utils(Util) { int dummy; diff --git a/src/modules/m_spanningtree/utils.cpp b/src/modules/m_spanningtree/utils.cpp index 10a888833..11c96bf7c 100644 --- a/src/modules/m_spanningtree/utils.cpp +++ b/src/modules/m_spanningtree/utils.cpp @@ -24,12 +24,15 @@ #include "resolvers.h" /* Create server sockets off a listener. */ -void ServerSocketListener::OnAcceptReady(int newsock) +ModResult ModuleSpanningTree::OnAcceptConnection(int newsock, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) { + if (from->bind_tag->getString("type") != "servers") + return MOD_RES_PASSTHRU; + bool found = false; int port; std::string incomingip; - irc::sockets::satoap(&client, incomingip, port); + irc::sockets::satoap(client, incomingip, port); found = (std::find(Utils->ValidIPs.begin(), Utils->ValidIPs.end(), incomingip) != Utils->ValidIPs.end()); if (!found) @@ -46,14 +49,14 @@ void ServerSocketListener::OnAcceptReady(int newsock) if (!found) { ServerInstance->SNO->WriteToSnoMask('l', "Server connection from %s denied (no link blocks with that IP address)", incomingip.c_str()); - ServerInstance->SE->Close(newsock); - return; + return MOD_RES_DENY; } } /* we don't need to do anything with the pointer, creating it stores it in the necessary places */ - new TreeSocket(Utils, newsock, this, &client, &server); + new TreeSocket(Utils, newsock, from, client, server); + return MOD_RES_ALLOW; } /** Yay for fast searches! @@ -146,17 +149,11 @@ SpanningTreeUtilities::SpanningTreeUtilities(ModuleSpanningTree* C) : Creator(C) this->TreeRoot = new TreeServer(this, ServerInstance->Config->ServerName, ServerInstance->Config->ServerDesc, ServerInstance->Config->GetSID()); ServerUser = new FakeUser(TreeRoot->GetID()); - this->ReadConfiguration(true); + this->ReadConfiguration(); } CullResult SpanningTreeUtilities::cull() { - for (unsigned int i = 0; i < ServerInstance->ports.size(); i++) - { - if (ServerInstance->ports[i]->type == "servers") - ServerInstance->ports[i]->cull(); - } - while (TreeRoot->ChildCount()) { TreeServer* child_server = TreeRoot->GetChild(0); @@ -176,12 +173,6 @@ CullResult SpanningTreeUtilities::cull() SpanningTreeUtilities::~SpanningTreeUtilities() { - for (unsigned int i = 0; i < ServerInstance->ports.size(); i++) - { - if (ServerInstance->ports[i]->type == "servers") - delete ServerInstance->ports[i]; - } - delete TreeRoot; } @@ -362,42 +353,10 @@ void SpanningTreeUtilities::RefreshIPCache() } } -void SpanningTreeUtilities::ReadConfiguration(bool rebind) +void SpanningTreeUtilities::ReadConfiguration() { ConfigReader Conf; - if (rebind) - { - ConfigTagList tags = ServerInstance->Config->ConfTags("bind"); - for(ConfigIter i = tags.first; i != tags.second; ++i) - { - ConfigTag* tag = i->second; - std::string Type = tag->getString("type"); - std::string IP = tag->getString("address"); - std::string Port = tag->getString("port"); - std::string ssl = tag->getString("ssl"); - if (Type == "servers") - { - irc::portparser portrange(Port, false); - int portno = -1; - - if (IP == "*") - IP.clear(); - - while ((portno = portrange.GetToken())) - { - ServerSocketListener *listener = new ServerSocketListener(this, portno, IP, ssl); - if (listener->GetFd() == -1) - { - delete listener; - continue; - } - - ServerInstance->ports.push_back(listener); - } - } - } - } FlatLinks = Conf.ReadFlag("security","flatlinks",0); HideULines = Conf.ReadFlag("security","hideulines",0); AnnounceTSChange = Conf.ReadFlag("options","announcets",0); diff --git a/src/modules/m_spanningtree/utils.h b/src/modules/m_spanningtree/utils.h index 2fc7304af..507cb3c7d 100644 --- a/src/modules/m_spanningtree/utils.h +++ b/src/modules/m_spanningtree/utils.h @@ -37,22 +37,6 @@ class SpanningTreeUtilities; #endif #endif -/* - * Initialises server connections - */ -class ServerSocketListener : public ListenSocketBase -{ - SpanningTreeUtilities *Utils; - - public: - ServerSocketListener(SpanningTreeUtilities *u, int port, const std::string& addr, const std::string& Hook) - : ListenSocketBase(port, addr, "servers", Hook), Utils(u) - { - } - - virtual void OnAcceptReady(int nfd); -}; - typedef std::map<TreeServer*,TreeServer*> TreeServerList; /** Contains helper functions and variables for this module, @@ -167,7 +151,7 @@ class SpanningTreeUtilities : public classbase /** Read the spanningtree module's tags from the config file */ - void ReadConfiguration(bool rebind); + void ReadConfiguration(); /** Add a server to the server list for GetListOfServersForChannel */ diff --git a/src/socket.cpp b/src/socket.cpp index 1666e3e8b..167d90b4f 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -102,11 +102,10 @@ int irc::sockets::OpenTCPSocket(const std::string& addr, int socktype) } } -// XXX: it would be VERY nice to genericize this so all listen stuff (server/client) could use the one function. -- w00t int InspIRCd::BindPorts(FailedPortList &failed_ports) { int bound = 0; - std::vector<ListenSocketBase*> old_ports(ports.begin(), ports.end()); + std::vector<ListenSocket*> old_ports(ports.begin(), ports.end()); ConfigTagList tags = ServerInstance->Config->ConfTags("bind"); for(ConfigIter i = tags.first; i != tags.second; ++i) @@ -114,52 +113,47 @@ int InspIRCd::BindPorts(FailedPortList &failed_ports) ConfigTag* tag = i->second; std::string porttag = tag->getString("port"); std::string Addr = tag->getString("address"); - std::string Type = tag->getString("type"); - std::string Desc = tag->getString("ssl"); if (strncmp(Addr.c_str(), "::ffff:", 7) == 0) this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead."); - if (Type.empty() || Type == "clients") + irc::portparser portrange(porttag, false); + int portno = -1; + while (0 != (portno = portrange.GetToken())) { - irc::portparser portrange(porttag, false); - int portno = -1; - while (0 != (portno = portrange.GetToken())) - { - irc::sockets::sockaddrs bindspec; - irc::sockets::aptosa(Addr, portno, &bindspec); - std::string bind_readable = irc::sockets::satouser(&bindspec); + irc::sockets::sockaddrs bindspec; + irc::sockets::aptosa(Addr, portno, &bindspec); + std::string bind_readable = irc::sockets::satouser(&bindspec); - bool skip = false; - for (std::vector<ListenSocketBase *>::iterator n = old_ports.begin(); n != old_ports.end(); ++n) + bool skip = false; + for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n) + { + if ((**n).bind_desc == bind_readable) + { + skip = true; + old_ports.erase(n); + break; + } + } + if (!skip) + { + ListenSocket *ll = new ListenSocket(tag, Addr, portno); + if (ll->GetFd() > -1) { - if ((*n)->GetBindDesc() == bind_readable) - { - skip = true; - old_ports.erase(n); - break; - } + bound++; + ports.push_back(ll); } - if (!skip) + else { - ClientListenSocket *ll = new ClientListenSocket(portno, Addr, "clients", Desc.empty() ? "plaintext" : Desc); - if (ll->GetFd() > -1) - { - bound++; - ports.push_back(ll); - } - else - { - failed_ports.push_back(std::make_pair(bind_readable, strerror(errno))); - delete ll; - } + failed_ports.push_back(std::make_pair(bind_readable, strerror(errno))); + delete ll; } } } } - std::vector<ListenSocketBase *>::iterator n = ports.begin(); - for (std::vector<ListenSocketBase *>::iterator o = old_ports.begin(); o != old_ports.end(); ++o) + std::vector<ListenSocket*>::iterator n = ports.begin(); + for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o) { while (n != ports.end() && *n != *o) n++; @@ -170,7 +164,7 @@ int InspIRCd::BindPorts(FailedPortList &failed_ports) } this->Logs->Log("SOCKET",DEFAULT, "Port binding %s was removed from the config file, closing.", - (*n)->GetBindDesc().c_str()); + (**n).bind_desc.c_str()); delete *n; // this keeps the iterator valid, pointing to the next element diff --git a/src/stats.cpp b/src/stats.cpp index a469600af..decdfe36e 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -46,12 +46,14 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results) { for (size_t i = 0; i < this->ports.size(); i++) { - std::string ip = this->ports[i]->GetIP(); + std::string ip = this->ports[i]->bind_addr; if (ip.empty()) ip.assign("*"); + std::string type = ports[i]->bind_tag->getString("type", "clients"); + std::string hook = ports[i]->bind_tag->getString("ssl", "plaintext"); results.push_back(sn+" 249 "+user->nick+" :"+ ip + ":"+ConvToStr(ports[i]->bind_port)+ - " (" + ports[i]->type + ", " + ports[i]->hook + ")"); + " (" + type + ", " + hook + ")"); } } break; diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 2d824c6b2..bc058a462 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -18,7 +18,7 @@ #include "bancache.h" /* add a client connection to the sockets list */ -void UserManager::AddUser(int socket, ClientListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) +void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) { /* NOTE: Calling this one parameter constructor for User automatically * allocates a new UUID and places it in the hash_map. |