-/* +------------------------------------+
- * | Inspire Internet Relay Chat Daemon |
- * +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
*
- * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- * E-mail:
- * <brain@chatspike.net>
- * <Craig@chatspike.net>
- *
- * Written by Craig Edwards, Craig McLure, and others.
- * This program is free but copyrighted software; see
- * the file COPYING for details.
+ * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
+ * Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
+ * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
*
- * ---------------------------------------------------
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <string>
-#include "configreader.h"
-#include "socket.h"
+
#include "inspircd.h"
-#include "inspstring.h"
-#include "helperfuncs.h"
-#include "socketengine.h"
-#include "message.h"
-
-extern InspIRCd* ServerInstance;
-extern ServerConfig* Config;
-extern time_t TIME;
-extern int openSockfd[MAX_DESCRIPTORS];
-
-/** This will bind a socket to a port. It works for UDP/TCP.
- * If a hostname is given to bind to, the function will first
- * attempt to resolve the hostname, then bind to the IP the
- * hostname resolves to. This is a blocking lookup blocking for
- * a maximum of one second before it times out, using the DNS
- * server specified in the configuration file.
- */
-bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
-{
- memset(&server,0,sizeof(server));
- insp_inaddr addy;
- bool resolved = false;
- char resolved_addr[128];
- if (*addr == '*')
- *addr = 0;
+int InspIRCd::BindPorts(FailedPortList &failed_ports)
+{
+ int bound = 0;
+ std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
- if (*addr && !inet_aton(addr,&addy))
+ ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
+ for(ConfigIter i = tags.first; i != tags.second; ++i)
{
- /* If they gave a hostname, bind to the IP it resolves to */
- if (CleanAndResolve(resolved_addr, addr, true))
+ ConfigTag* tag = i->second;
+ std::string porttag = tag->getString("port");
+ std::string Addr = tag->getString("address");
+
+ if (strncasecmp(Addr.c_str(), "::ffff:", 7) == 0)
+ this->Logs->Log("SOCKET", LOG_DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
+
+ irc::portparser portrange(porttag, false);
+ int portno = -1;
+ while (0 != (portno = portrange.GetToken()))
{
- inet_aton(resolved_addr,&addy);
- log(DEFAULT,"Resolved binding '%s' -> '%s'",addr,resolved_addr);
- server.sin_addr = addy;
- resolved = true;
+ irc::sockets::sockaddrs bindspec;
+ if (!irc::sockets::aptosa(Addr, portno, bindspec))
+ continue;
+ std::string bind_readable = bindspec.str();
+
+ bool skip = false;
+ for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
+ {
+ if ((**n).bind_desc == bind_readable)
+ {
+ (*n)->bind_tag = tag; // Replace tag, we know addr and port match, but other info (type, ssl) may not
+ (*n)->ResetIOHookProvider();
+
+ skip = true;
+ old_ports.erase(n);
+ break;
+ }
+ }
+ if (!skip)
+ {
+ ListenSocket* ll = new ListenSocket(tag, bindspec);
+
+ if (ll->GetFd() > -1)
+ {
+ bound++;
+ ports.push_back(ll);
+ }
+ else
+ {
+ failed_ports.push_back(std::make_pair(bind_readable, strerror(errno)));
+ delete ll;
+ }
+ }
}
- else
+ }
+
+ std::vector<ListenSocket*>::iterator n = ports.begin();
+ for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
+ {
+ while (n != ports.end() && *n != *o)
+ n++;
+ if (n == ports.end())
{
- log(DEFAULT,"WARNING: Could not resolve '%s' to an IP for binding to on port %d",addr,port);
- return false;
+ this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
+ break;
}
+
+ this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
+ (**n).bind_desc.c_str());
+ delete *n;
+
+ // this keeps the iterator valid, pointing to the next element
+ n = ports.erase(n);
}
- server.sin_family = AF_INET;
- if (!resolved)
+
+ return bound;
+}
+
+bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
+{
+ memset(&sa, 0, sizeof(sa));
+ if (addr.empty() || addr.c_str()[0] == '*')
{
- if (!*addr)
+ if (ServerInstance->Config->WildcardIPv6)
{
- server.sin_addr.s_addr = htonl(INADDR_ANY);
+ sa.in6.sin6_family = AF_INET6;
+ sa.in6.sin6_port = htons(port);
}
else
{
- server.sin_addr = addy;
+ sa.in4.sin_family = AF_INET;
+ sa.in4.sin_port = htons(port);
}
+ return true;
}
- server.sin_port = htons(port);
- if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
+ else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
{
- return false;
+ sa.in4.sin_family = AF_INET;
+ sa.in4.sin_port = htons(port);
+ return true;
}
- else
+ else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
{
- log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
- if (listen(sockfd, Config->MaxConn) == -1)
- {
- log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
- return false;
- }
- else
- {
- NonBlocking(sockfd);
- return true;
- }
+ sa.in6.sin6_family = AF_INET6;
+ sa.in6.sin6_port = htons(port);
+ return true;
}
+ return false;
}
+int irc::sockets::sockaddrs::port() const
+{
+ if (sa.sa_family == AF_INET)
+ return ntohs(in4.sin_port);
+ if (sa.sa_family == AF_INET6)
+ return ntohs(in6.sin6_port);
+ return -1;
+}
-// Open a TCP Socket
-int OpenTCPSocket()
+std::string irc::sockets::sockaddrs::addr() const
{
- int sockfd;
- int on = 1;
- struct linger linger = { 0 };
-
- if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
+ char addrv[INET6_ADDRSTRLEN+1];
+ if (sa.sa_family == AF_INET)
{
- log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
- return (ERROR);
+ if (!inet_ntop(AF_INET, &in4.sin_addr, addrv, sizeof(addrv)))
+ return "";
+ return addrv;
}
- else
+ else if (sa.sa_family == AF_INET6)
{
- setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
- /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
- linger.l_onoff = 1;
- linger.l_linger = 1;
- setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
- return (sockfd);
+ if (!inet_ntop(AF_INET6, &in6.sin6_addr, addrv, sizeof(addrv)))
+ return "";
+ return addrv;
}
+ return "";
+}
+
+bool irc::sockets::satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port)
+{
+ port = sa.port();
+ addr = sa.addr();
+ return !addr.empty();
}
-bool HasPort(int port, char* addr)
+std::string irc::sockets::sockaddrs::str() const
{
- for (int count = 0; count < ServerInstance->stats->BoundPortCount; count++)
+ if (sa.sa_family == AF_INET)
{
- if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
- {
- return true;
- }
+ char ipaddr[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &in4.sin_addr, ipaddr, sizeof(ipaddr));
+ return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
}
- return false;
+
+ if (sa.sa_family == AF_INET6)
+ {
+ char ipaddr[INET6_ADDRSTRLEN];
+ inet_ntop(AF_INET6, &in6.sin6_addr, ipaddr, sizeof(ipaddr));
+ return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
+ }
+
+ // This should never happen.
+ return "<unknown>";
}
-int BindPorts(bool bail)
+int irc::sockets::sockaddrs::sa_size() const
{
- char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
- insp_sockaddr client, server;
- int clientportcount = 0;
- int BoundPortCount = 0;
+ if (sa.sa_family == AF_INET)
+ return sizeof(in4);
+ if (sa.sa_family == AF_INET6)
+ return sizeof(in6);
+ return 0;
+}
- if (!bail)
- {
- int InitialPortCount = ServerInstance->stats->BoundPortCount;
- log(DEBUG,"Initial port count: %d",InitialPortCount);
+bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
+{
+ if (sa.sa_family != other.sa.sa_family)
+ return false;
+ if (sa.sa_family == AF_INET)
+ return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
+ if (sa.sa_family == AF_INET6)
+ return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
+ return !memcmp(this, &other, sizeof(*this));
+}
- for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
- {
- Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
- Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
- Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
+static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, int range)
+{
+ const unsigned char* base;
+ unsigned char target_byte;
+ cidr.type = sa.sa.sa_family;
- if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
- {
- // modules handle server bind types now
- Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
- if (*Addr == '*')
- *Addr = 0;
-
- strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
- clientportcount++;
- log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
- }
- }
- int PortCount = clientportcount;
- if (PortCount)
- {
- for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
- {
- if ((openSockfd[count] = OpenTCPSocket()) == ERROR)
- {
- log(DEBUG,"Bad fd %d binding port [%s:%d]",openSockfd[count],Config->addrs[count],Config->ports[count]);
- return ERROR;
- }
- if (!BindSocket(openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
- {
- log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
- }
- else
- {
- /* Associate the new open port with a slot in the socket engine */
- if (openSockfd[count] > -1)
- {
- ServerInstance->SE->AddFd(openSockfd[count],true,X_LISTEN);
- BoundPortCount++;
- }
- }
- }
- return InitialPortCount + BoundPortCount;
- }
- else
- {
- log(DEBUG,"There is nothing new to bind!");
- }
- return InitialPortCount;
- }
+ memset(cidr.bits, 0, sizeof(cidr.bits));
- for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
+ if (cidr.type == AF_INET)
{
- Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
- Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
- Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
-
- if ((!*Type) || (!strcmp(Type,"clients")))
- {
- // modules handle server bind types now
- Config->ports[clientportcount] = atoi(configToken);
-
- // If the client put bind "*", this is an unrealism.
- // We don't actually support this as documented, but
- // i got fed up of people trying it, so now it converts
- // it to an empty string meaning the same 'bind to all'.
- if (*Addr == '*')
- *Addr = 0;
-
- strlcpy(Config->addrs[clientportcount],Addr,256);
- clientportcount++;
- log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
- }
+ target_byte = sizeof(sa.in4.sin_addr);
+ base = (unsigned char*)&sa.in4.sin_addr;
+ if (range > 32)
+ range = 32;
+ }
+ else if (cidr.type == AF_INET6)
+ {
+ target_byte = sizeof(sa.in6.sin6_addr);
+ base = (unsigned char*)&sa.in6.sin6_addr;
+ if (range > 128)
+ range = 128;
}
+ else
+ {
+ cidr.length = 0;
+ return;
+ }
+ cidr.length = range;
+ unsigned int border = range / 8;
+ unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
+ for(unsigned int i=0; i < target_byte; i++)
+ {
+ if (i < border)
+ cidr.bits[i] = base[i];
+ else if (i == border)
+ cidr.bits[i] = base[i] & bitmask;
+ else
+ return;
+ }
+}
- int PortCount = clientportcount;
+irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, int range)
+{
+ sa2cidr(*this, sa, range);
+}
- for (int count = 0; count < PortCount; count++)
- {
- if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
- {
- log(DEBUG,"Bad fd %d binding port [%s:%d]",openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
- return ERROR;
- }
+irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
+{
+ std::string::size_type bits_chars = mask.rfind('/');
+ irc::sockets::sockaddrs sa;
- if (!BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
- {
- log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
- }
- else
- {
- /* well we at least bound to one socket so we'll continue */
- BoundPortCount++;
- }
+ if (bits_chars == std::string::npos)
+ {
+ irc::sockets::aptosa(mask, 0, sa);
+ sa2cidr(*this, sa, 128);
+ }
+ else
+ {
+ int range = ConvToInt(mask.substr(bits_chars + 1));
+ irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
+ sa2cidr(*this, sa, range);
}
+}
- /* if we didn't bind to anything then abort */
- if (!BoundPortCount)
+std::string irc::sockets::cidr_mask::str() const
+{
+ irc::sockets::sockaddrs sa;
+ sa.sa.sa_family = type;
+ unsigned char* base;
+ int len;
+ if (type == AF_INET)
+ {
+ base = (unsigned char*)&sa.in4.sin_addr;
+ len = 4;
+ }
+ else if (type == AF_INET6)
{
- log(DEFAULT,"No ports bound, bailing!");
- printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
- return ERROR;
+ base = (unsigned char*)&sa.in6.sin6_addr;
+ len = 16;
}
+ else
+ return "";
+ memcpy(base, bits, len);
+ return sa.addr() + "/" + ConvToStr((int)length);
+}
+
+bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
+{
+ return type == other.type && length == other.length &&
+ 0 == memcmp(bits, other.bits, 16);
+}
- return BoundPortCount;
+bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
+{
+ if (type != other.type)
+ return type < other.type;
+ if (length != other.length)
+ return length < other.length;
+ return memcmp(bits, other.bits, 16) < 0;
+}
+
+bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
+{
+ if (addr.sa.sa_family != type)
+ return false;
+ irc::sockets::cidr_mask tmp(addr, length);
+ return tmp == *this;
}