]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/listensocket.cpp
Fix MySQL crash on module unload with empty query queue
[user/henk/code/inspircd.git] / src / listensocket.cpp
index 676898647847ff91cf5bcd9b06c1798d5d2b436a..0c6fd3b5c451bd516bee9699da552c3d5f87a8f8 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
  * See: http://wiki.inspircd.org/Credits
  *
  * This program is free but copyrighted software; see
  * ---------------------------------------------------
  */
 
-/* $Core */
-
 #include "inspircd.h"
 #include "socket.h"
 #include "socketengine.h"
 
-ListenSocket::ListenSocket(ConfigTag* tag, const std::string& addr, int port)
+ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to)
        : bind_tag(tag)
 {
-       irc::sockets::sockaddrs bind_to;
-
-       // canonicalize address if it is defined
-       if (!irc::sockets::aptosa(addr, port, bind_to))
-       {
-               fd = -1;
-               return;
-       }
        irc::sockets::satoap(bind_to, bind_addr, bind_port);
        bind_desc = irc::sockets::satouser(bind_to);
 
        fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);
 
-       if (this->fd > -1)
-       {
-               ServerInstance->SE->SetReuse(fd);
-               int rv = ServerInstance->SE->Bind(this->fd, &bind_to.sa, sizeof(bind_to));
-               if (rv >= 0)
-                       rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);
+       if (this->fd == -1)
+               return;
 
-               if (rv < 0)
-               {
-                       ServerInstance->SE->Shutdown(this, 2);
-                       ServerInstance->SE->Close(this);
-                       this->fd = -1;
-               }
-               else
-               {
-                       ServerInstance->SE->NonBlocking(this->fd);
-                       ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
-               }
+       ServerInstance->SE->SetReuse(fd);
+       int rv = ServerInstance->SE->Bind(this->fd, bind_to);
+       if (rv >= 0)
+               rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);
+
+       if (rv < 0)
+       {
+               int errstore = errno;
+               ServerInstance->SE->Shutdown(this, 2);
+               ServerInstance->SE->Close(this);
+               this->fd = -1;
+               errno = errstore;
+       }
+       else
+       {
+               ServerInstance->SE->NonBlocking(this->fd);
+               ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
        }
 }
 
@@ -75,7 +67,7 @@ void ListenSocket::AcceptInternal()
        socklen_t length = sizeof(client);
        int incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
 
-       ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket %s nfd=%d", bind_desc.c_str(), incomingSockfd);
+       ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensocket %s nfd=%d", bind_desc.c_str(), incomingSockfd);
        if (incomingSockfd < 0)
        {
                ServerInstance->stats->statsRefused++;
@@ -84,7 +76,10 @@ void ListenSocket::AcceptInternal()
 
        socklen_t sz = sizeof(server);
        if (getsockname(incomingSockfd, &server.sa, &sz))
+       {
                ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
+               irc::sockets::aptosa(bind_addr, bind_port, server);
+       }
 
        /*
         * XXX -