]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Tidy up some of the internals a bit, making things a bit more extensible and future...
authorw00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7>
Sun, 7 Sep 2008 23:08:32 +0000 (23:08 +0000)
committerw00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7>
Sun, 7 Sep 2008 23:08:32 +0000 (23:08 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@10462 e03df62e-2008-0410-955e-edbf42e46eb7

include/socket.h
src/listensocket.cpp

index 105984f7483f4209413407925e585455e0510989..556ee1a9045a4495b868b88d22ac9426dd44cdcd 100644 (file)
@@ -202,7 +202,22 @@ class CoreExport ListenSocket : public EventHandler
        {
                return bind_addr;
        }
+
+       /** 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 ipconnectedto The IP address the connection arrived on
+        * @param fd The file descriptor of the new connection
+        */
+       virtual void OnAcceptReady(const std::string &ipconnectedto, int fd);
 };
 
+//class CoreExport ListenSocketClient : public ListenSocket
+//{
+//
+//}
+
 #endif
 
index 936dd85e9e1f0911616974f1418a9df8d8ade2de..58934e54312d756267a0369b355da5d0afb27bb0 100644 (file)
@@ -70,77 +70,85 @@ ListenSocket::~ListenSocket()
        }
 }
 
-void ListenSocket::HandleEvent(EventType e, int err)
+/* Just seperated into another func for tidiness really.. */
+void ListenSocket::AcceptInternal()
 {
-       switch (e)
-       {
-               case EVENT_ERROR:
-                       ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
-               break;
-               case EVENT_WRITE:
-                       ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
-               break;
-               case EVENT_READ:
-               {
-                       ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
-                       socklen_t uslen, length;                // length of our port number
-                       int incomingSockfd, in_port;
+       ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
+       socklen_t uslen, length;                // length of our port number
+       int incomingSockfd;
 
 #ifdef IPV6
-                       if (this->family == AF_INET6)
-                       {
-                               uslen = sizeof(sockaddr_in6);
-                               length = sizeof(sockaddr_in6);
-                       }
-                       else
+       if (this->family == AF_INET6)
+       {
+               uslen = sizeof(sockaddr_in6);
+               length = sizeof(sockaddr_in6);
+       }
+       else
 #endif
-                       {
-                               uslen = sizeof(sockaddr_in);
-                               length = sizeof(sockaddr_in);
-                       }
+       {
+               uslen = sizeof(sockaddr_in);
+               length = sizeof(sockaddr_in);
+       }
 
-                       incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);
+       incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);
 
-                       if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen)))
-                       {
-                               char buf[MAXBUF];
-                               char target[MAXBUF];    
+       if (incomingSockfd < 0 ||
+                 ServerInstance->SE->GetSockName(this, sock_us, &uslen) == -1)
+       {
+               ServerInstance->SE->Shutdown(incomingSockfd, 2);
+               ServerInstance->SE->Close(incomingSockfd);
+               ServerInstance->stats->statsRefused++;
+               return;
+       }
 
-                               *target = *buf = '\0';
+       static char buf[MAXBUF];
+       static char target[MAXBUF];     
+
+       *target = *buf = '\0';
 
 #ifdef IPV6
-                               if (this->family == AF_INET6)
-                               {
-                                       in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port);
-                                       inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
-                                       socklen_t raddrsz = sizeof(sockaddr_in6);
-                                       if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
-                                               inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
-                                       else
-                                               ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
-                               }
-                               else
+       if (this->family == AF_INET6)
+       {
+               inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
+               socklen_t raddrsz = sizeof(sockaddr_in6);
+               if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
+                       inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
+               else
+                       ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
+       }
+       else
 #endif
-                               {
-                                       inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
-                                       in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
-                                       socklen_t raddrsz = sizeof(sockaddr_in);
-                                       if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
-                                               inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
-                                       else
-                                               ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
-                               }
-                               ServerInstance->SE->NonBlocking(incomingSockfd);
-                               ServerInstance->stats->statsAccept++;
-                               ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target);   
-                       }
-                       else
-                       {
-                               ServerInstance->SE->Shutdown(incomingSockfd, 2);
-                               ServerInstance->SE->Close(incomingSockfd);
-                               ServerInstance->stats->statsRefused++;
-                       }
-               }
-               break;
+       {
+               inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
+               socklen_t raddrsz = sizeof(sockaddr_in);
+               if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
+                       inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
+               else
+                       ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
+       }
+
+       ServerInstance->SE->NonBlocking(incomingSockfd);
+       ServerInstance->stats->statsAccept++;
+       this->OnAcceptReady(target, incomingSockfd);
+}
+
+void ListenSocket::HandleEvent(EventType e, int err)
+{
+       switch (e)
+       {
+               case EVENT_ERROR:
+                       ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
+                       break;
+               case EVENT_WRITE:
+                       ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
+                       break;
+               case EVENT_READ:
+                       this->AcceptInternal();
+                       break;
        }
 }
+
+void ListenSocket::OnAcceptReady(const std::string &ipconnectedto, int nfd)
+{
+               ServerInstance->Users->AddUser(ServerInstance, nfd, bind_port, false, this->family, client, ipconnectedto);
+}