From 1a95ce0b2b6f19780d274748594964a57e6ede53 Mon Sep 17 00:00:00 2001 From: brain Date: Tue, 13 Dec 2005 21:33:57 +0000 Subject: Optimized accept() stuff to eliminate loop, idea while talking to w00t git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@2381 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/dnsqueue.cpp | 20 +++++++++++++++----- src/inspircd.cpp | 57 +++++++++++++++++++++++++++++--------------------------- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/dnsqueue.cpp b/src/dnsqueue.cpp index 2d123a419..78be4d8f1 100644 --- a/src/dnsqueue.cpp +++ b/src/dnsqueue.cpp @@ -98,9 +98,11 @@ public: strlcpy(u,nick.c_str(),NICKMAX); /* ASSOCIATE WITH DNS LOOKUP LIST */ - dnslist[resolver1.GetFD()] = this; - - return true; + if (resolver1.GetFD() != -1) + { + dnslist[resolver1.GetFD()] = this; + return true; + } } return false; } @@ -157,7 +159,11 @@ public: { usr = Find(u); if ((usr) && (usr->dns_done)) + { + if (resolver1.GetFD() != -1) + dnslist[resolver1.GetFD()] = NULL; return true; + } if (resolver1.GetFD() != -1) { dnslist[resolver1.GetFD()] = NULL; @@ -173,7 +179,7 @@ public: if (hostname != "") { resolver2.ForwardLookup(hostname); - if (resolver2.GetFD()) + if (resolver2.GetFD() != -1) dnslist[resolver2.GetFD()] = this; } } @@ -258,6 +264,10 @@ void dns_poll(int fdcheck) */ return; } - log(DEBUG,"DNS: Received an event for an invalid descriptor!"); + /* This FD doesnt belong here, lets be rid of it, + * just to be safe so we dont get any more events + * about it. + */ + SE->DelFd(fdcheck); } diff --git a/src/inspircd.cpp b/src/inspircd.cpp index 4d68da04f..a2b0dea75 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -2326,11 +2326,14 @@ int InspIRCd(char** argv, int argc) bool expire_run = false; std::vector activefds; int incomingSockfd; + int in_port; userrec* cu = NULL; InspSocket* s = NULL; InspSocket* s_del = NULL; char target[MAXBUF]; unsigned int numberactive; + sockaddr_in sock_us; // our port number + socklen_t uslen; // length of our port number /* Beta 7 moved all this stuff out of the main function * into smaller sub-functions, much tidier -- Brain @@ -2483,36 +2486,36 @@ int InspIRCd(char** argv, int argc) case X_LISTEN: /* It's a listener */ - for (int count = 0; count < boundPortCount; count++) + uslen = sizeof(sock_us); + themlen = sizeof(sock_them); + length = sizeof (client); + incomingSockfd = accept (activefds[activefd], (struct sockaddr *) &client, &length); + if (!getsockname(incomingSockfd,(sockaddr*)&sock_us,&uslen)) { - if (activefds[activefd] == openSockfd[count]) + in_port = ntohs(sock_us.sin_port); + log(DEBUG,"Accepted socket %d",incomingSockfd); + strlcpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF); + /* Years and years ago, we used to resolve here + * using gethostbyaddr(). That is sucky and we + * don't do that any more... + */ + if (incomingSockfd >= 0) { - length = sizeof (client); - incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length); - log(DEBUG,"Accepted socket %d",incomingSockfd); - strlcpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF); - /* Years and years ago, we used to resolve here - * using gethostbyaddr(). That is sucky and we - * don't do that any more... - */ - if (incomingSockfd >= 0) - { - FOREACH_MOD OnRawSocketAccept(incomingSockfd, target, ports[count]); - statsAccept++; - AddClient(incomingSockfd, target, ports[count], false, inet_ntoa (client.sin_addr)); - log(DEBUG,"Adding client on port %lu fd=%lu",(unsigned long)ports[count],(unsigned long)incomingSockfd); - } - else - { - WriteOpers("*** WARNING: accept() failed on port %lu (%s)",(unsigned long)ports[count],target); - log(DEBUG,"accept failed: %lu",(unsigned long)ports[count]); - statsRefused++; - } - /* We've found out what port it belongs on, - * no need to iterate the rest - */ - break; + FOREACH_MOD OnRawSocketAccept(incomingSockfd, target, in_port); + statsAccept++; + AddClient(incomingSockfd, target, in_port, false, inet_ntoa (client.sin_addr)); + log(DEBUG,"Adding client on port %lu fd=%lu",(unsigned long)in_port,(unsigned long)incomingSockfd); } + else + { + WriteOpers("*** WARNING: accept() failed on port %lu (%s)",(unsigned long)in_port,target); + log(DEBUG,"accept failed: %lu",(unsigned long)in_port); + statsRefused++; + } + } + else + { + log(DEBUG,"Couldnt look up the port number for fd %lu(?!)",incomingSockfd); } break; -- cgit v1.2.3