From 347dcb9ef44e19e2e2613ee7e8c7d5fbb0aca4ca Mon Sep 17 00:00:00 2001 From: danieldg Date: Wed, 2 Sep 2009 00:49:15 +0000 Subject: Remove duplicated address conversion from InspIRCd::BindSocket, fix FD leak in listensocket git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11632 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/listensocket.cpp | 35 +++++++++++++++++++++++++---------- src/socket.cpp | 37 ++++++++++--------------------------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/listensocket.cpp b/src/listensocket.cpp index df252f18b..823cb9eca 100644 --- a/src/listensocket.cpp +++ b/src/listensocket.cpp @@ -26,24 +26,39 @@ ListenSocketBase::ListenSocketBase(InspIRCd* Instance, int port, const std::stri irc::sockets::sockaddrs bind_to; // canonicalize address if it is defined - if (irc::sockets::aptosa(addr.c_str(), port, &bind_to)) - { - irc::sockets::satoap(&bind_to, bind_addr, bind_port); - bind_desc = irc::sockets::satouser(&bind_to); - } - else + if (!irc::sockets::aptosa(addr.c_str(), port, &bind_to)) { + // malformed address bind_addr = addr; bind_port = port; bind_desc = addr + ":" + ConvToStr(port); + this->fd = -1; + } + else + { + irc::sockets::satoap(&bind_to, bind_addr, bind_port); + bind_desc = irc::sockets::satouser(&bind_to); + + this->fd = irc::sockets::OpenTCPSocket(bind_addr.c_str()); } - this->SetFd(irc::sockets::OpenTCPSocket(bind_addr.c_str())); - if (this->GetFd() > -1) + if (this->fd > -1) { - if (!Instance->BindSocket(this->fd,port,bind_addr.c_str())) + int rv = Instance->SE->Bind(this->fd, &bind_to.sa, sizeof(bind_to)); + if (rv >= 0) + rv = Instance->SE->Listen(this->fd, Instance->Config->MaxConn); + + if (rv < 0) + { + Instance->SE->Shutdown(this, 2); + Instance->SE->Close(this); this->fd = -1; - Instance->SE->AddFd(this); + } + else + { + Instance->SE->NonBlocking(this->fd); + Instance->SE->AddFd(this); + } } } diff --git a/src/socket.cpp b/src/socket.cpp index e32bda901..277f61bcd 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -27,37 +27,20 @@ bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten) sockaddrs servaddr; int ret; - if (*addr == '*') - addr = ""; + if (*addr == '*' || *addr == '\0') + addr = NULL; - if (*addr) - { - irc::sockets::aptosa(addr, port, &servaddr); - } - else + if (port == -1 && !addr) { + /* Port -1: Means UDP IPV4 port binding - Special case + * used by DNS engine. + */ memset(&servaddr, 0, sizeof(servaddr)); - if (port == -1) - { - /* Port -1: Means UDP IPV4 port binding - Special case - * used by DNS engine. - */ - servaddr.in4.sin_family = AF_INET; - } - else - { - /* No address */ -#ifdef IPV6 - /* Default to ipv6 bind to all */ - servaddr.in6.sin6_family = AF_INET6; - servaddr.in6.sin6_port = htons(port); -#else - /* Bind ipv4 to all */ - servaddr.in4.sin_family = AF_INET; - servaddr.in4.sin_port = htons(port); -#endif - } + servaddr.in4.sin_family = AF_INET; } + else if (!irc::sockets::aptosa(addr, port, &servaddr)) + return false; + ret = SE->Bind(sockfd, &servaddr.sa, sa_size(servaddr)); if (ret < 0) -- cgit v1.2.3