From 3999c2eccf43c998e70d49023d8f35e3fa1632c8 Mon Sep 17 00:00:00 2001 From: danieldg Date: Wed, 2 Sep 2009 00:50:04 +0000 Subject: Rewrite BindPorts to index ports by their human-readable description git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11635 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/socket.cpp | 55 ++++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/socket.cpp b/src/socket.cpp index 277f61bcd..ccae48a6d 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -111,12 +111,7 @@ int InspIRCd::BindPorts(FailedPortList &failed_ports) { char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF], Desc[MAXBUF]; int bound = 0; - bool started_with_nothing = (ports.size() == 0); - std::vector > old_ports; - - /* XXX: Make a copy of the old ip/port pairs here */ - for (std::vector::iterator o = ports.begin(); o != ports.end(); ++o) - old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort())); + std::vector old_ports(ports.begin(), ports.end()); for (int count = 0; count < Config->ConfValueEnum("bind"); count++) { @@ -137,21 +132,18 @@ int InspIRCd::BindPorts(FailedPortList &failed_ports) if (*Addr == '*') *Addr = 0; + irc::sockets::sockaddrs bindspec; + irc::sockets::aptosa(Addr, portno, &bindspec); + std::string bind_readable = irc::sockets::satouser(&bindspec); + bool skip = false; - for (std::vector::iterator n = ports.begin(); n != ports.end(); ++n) + for (std::vector::iterator n = old_ports.begin(); n != old_ports.end(); ++n) { - if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno)) + if ((*n)->GetBindDesc() == bind_readable) { skip = true; - /* XXX: Here, erase from our copy of the list */ - for (std::vector >::iterator k = old_ports.begin(); k != old_ports.end(); ++k) - { - if ((k->first == Addr) && (k->second == portno)) - { - old_ports.erase(k); - break; - } - } + old_ports.erase(n); + break; } } if (!skip) @@ -165,29 +157,30 @@ int InspIRCd::BindPorts(FailedPortList &failed_ports) } else { - failed_ports.push_back(std::make_pair((*Addr ? Addr : "*") + std::string(":") + ConvToStr(portno), strerror(errno))); + failed_ports.push_back(std::make_pair(bind_readable, strerror(errno))); } } } } } - /* XXX: Here, anything left in our copy list, close as removed */ - if (!started_with_nothing) + std::vector::iterator n = ports.begin(); + for (std::vector::iterator o = old_ports.begin(); o != old_ports.end(); ++o) { - for (size_t k = 0; k < old_ports.size(); ++k) + while (n != ports.end() && *n != *o) + n++; + if (n == ports.end()) { - for (std::vector::iterator n = ports.begin(); n != ports.end(); ++n) - { - if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second)) - { - this->Logs->Log("SOCKET",DEFAULT,"Port binding %s:%d was removed from the config file, closing.", old_ports[k].first.c_str(), old_ports[k].second); - delete *n; - ports.erase(n); - break; - } - } + this->Logs->Log("SOCKET",ERROR,"Port bindings slipped out of vector, aborting close!"); + break; } + + this->Logs->Log("SOCKET",DEFAULT, "Port binding %s was removed from the config file, closing.", + (*n)->GetBindDesc().c_str()); + delete *n; + + // this keeps the iterator valid, pointing to the next element + n = ports.erase(n); } return bound; -- cgit v1.2.3