2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 bool InspIRCd::BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std::vector<ListenSocket*>& old_ports)
28 for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
30 if ((**n).bind_sa == sa)
32 // Replace tag, we know addr and port match, but other info (type, ssl) may not.
33 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Replacing listener on %s from old tag at %s with new tag from %s",
34 sa.str().c_str(), (*n)->bind_tag->getTagLocation().c_str(), tag->getTagLocation().c_str());
36 (*n)->ResetIOHookProvider();
43 ListenSocket* ll = new ListenSocket(tag, sa);
46 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to listen on %s from tag at %s: %s",
47 sa.str().c_str(), tag->getTagLocation().c_str(), strerror(errno));
52 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Added a listener on %s from tag at %s", sa.str().c_str(), tag->getTagLocation().c_str());
57 int InspIRCd::BindPorts(FailedPortList& failed_ports)
60 std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
62 ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
63 for (ConfigIter i = tags.first; i != tags.second; ++i)
65 ConfigTag* tag = i->second;
67 // Are we creating a TCP/IP listener?
68 const std::string address = tag->getString("address");
69 const std::string portlist = tag->getString("port");
70 if (!address.empty() || !portlist.empty())
72 // InspIRCd supports IPv4 and IPv6 natively; no 4in6 required.
73 if (strncasecmp(address.c_str(), "::ffff:", 7) == 0)
74 this->Logs->Log("SOCKET", LOG_DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
76 // A TCP listener with no ports is not very useful.
78 this->Logs->Log("SOCKET", LOG_DEFAULT, "TCP listener on %s at %s has no ports specified!",
79 address.empty() ? "*" : address.c_str(), tag->getTagLocation().c_str());
81 irc::portparser portrange(portlist, false);
82 for (int port; (port = portrange.GetToken()); )
84 irc::sockets::sockaddrs bindspec;
85 if (!irc::sockets::aptosa(address, port, bindspec))
88 if (!BindPort(tag, bindspec, old_ports))
89 failed_ports.push_back(std::make_pair(bindspec, errno));
97 // Are we creating a UNIX listener?
98 const std::string path = tag->getString("path");
101 // UNIX socket paths are length limited to less than PATH_MAX.
102 irc::sockets::sockaddrs bindspec;
103 if (path.length() > std::min(ServerInstance->Config->Limits.MaxHost, sizeof(bindspec.un.sun_path) - 1))
105 this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path that is too long!",
106 path.c_str(), tag->getTagLocation().c_str());
110 // Check for characters which are problematic in the IRC message format.
111 if (path.find_first_of("\n\r\t!@: ") != std::string::npos)
113 this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path containing invalid characters!",
114 path.c_str(), tag->getTagLocation().c_str());
118 irc::sockets::untosa(path, bindspec);
119 if (!BindPort(tag, bindspec, old_ports))
120 failed_ports.push_back(std::make_pair(bindspec, errno));
127 std::vector<ListenSocket*>::iterator n = ports.begin();
128 for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
130 while (n != ports.end() && *n != *o)
132 if (n == ports.end())
134 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
138 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
139 (**n).bind_sa.str().c_str());
142 // this keeps the iterator valid, pointing to the next element
149 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
151 memset(&sa, 0, sizeof(sa));
152 if (addr.empty() || addr.c_str()[0] == '*')
154 if (ServerInstance->Config->WildcardIPv6)
156 sa.in6.sin6_family = AF_INET6;
157 sa.in6.sin6_port = htons(port);
161 sa.in4.sin_family = AF_INET;
162 sa.in4.sin_port = htons(port);
166 else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
168 sa.in4.sin_family = AF_INET;
169 sa.in4.sin_port = htons(port);
172 else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
174 sa.in6.sin6_family = AF_INET6;
175 sa.in6.sin6_port = htons(port);
181 bool irc::sockets::untosa(const std::string& path, irc::sockets::sockaddrs& sa)
183 memset(&sa, 0, sizeof(sa));
184 if (path.length() >= sizeof(sa.un.sun_path))
187 sa.un.sun_family = AF_UNIX;
188 memcpy(&sa.un.sun_path, path.c_str(), path.length() + 1);
192 int irc::sockets::sockaddrs::family() const
197 int irc::sockets::sockaddrs::port() const
202 return ntohs(in4.sin_port);
205 return ntohs(in6.sin6_port);
211 // If we have reached this point then we have encountered a bug.
212 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::port(): socket type %d is unknown!", family());
216 std::string irc::sockets::sockaddrs::addr() const
221 char ip4addr[INET_ADDRSTRLEN];
222 if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
227 char ip6addr[INET6_ADDRSTRLEN];
228 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
229 return "0:0:0:0:0:0:0:0";
236 // If we have reached this point then we have encountered a bug.
237 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::addr(): socket type %d is unknown!", family());
241 std::string irc::sockets::sockaddrs::str() const
246 char ip4addr[INET_ADDRSTRLEN];
247 if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
248 strcpy(ip4addr, "0.0.0.0");
249 return InspIRCd::Format("%s:%u", ip4addr, ntohs(in4.sin_port));
252 char ip6addr[INET6_ADDRSTRLEN];
253 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
254 strcpy(ip6addr, "0:0:0:0:0:0:0:0");
255 return InspIRCd::Format("[%s]:%u", ip6addr, ntohs(in6.sin6_port));
261 // If we have reached this point then we have encountered a bug.
262 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::str(): socket type %d is unknown!", family());
266 socklen_t irc::sockets::sockaddrs::sa_size() const
280 // If we have reached this point then we have encountered a bug.
281 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::sa_size(): socket type %d is unknown!", family());
285 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
287 if (family() != other.family())
293 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
296 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
299 return !strcmp(un.sun_path, other.un.sun_path);
302 // If we have reached this point then we have encountered a bug.
303 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::operator==(): socket type %d is unknown!", family());
304 return !memcmp(this, &other, sizeof(*this));
307 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
309 const unsigned char* base;
310 unsigned char target_byte;
312 memset(cidr.bits, 0, sizeof(cidr.bits));
314 cidr.type = sa.family();
318 // XXX: UNIX sockets don't support CIDR. This fix is non-ideal but I can't
319 // really think of another way to handle it.
324 cidr.length = range > 32 ? 32 : range;
325 target_byte = sizeof(sa.in4.sin_addr);
326 base = (unsigned char*)&sa.in4.sin_addr;
330 cidr.length = range > 128 ? 128 : range;
331 target_byte = sizeof(sa.in6.sin6_addr);
332 base = (unsigned char*)&sa.in6.sin6_addr;
336 // If we have reached this point then we have encountered a bug.
337 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: sa2cidr(): socket type %d is unknown!", cidr.type);
342 unsigned int border = cidr.length / 8;
343 unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
344 for(unsigned int i=0; i < target_byte; i++)
347 cidr.bits[i] = base[i];
348 else if (i == border)
349 cidr.bits[i] = base[i] & bitmask;
355 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, unsigned char range)
357 sa2cidr(*this, sa, range);
360 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
362 std::string::size_type bits_chars = mask.rfind('/');
363 irc::sockets::sockaddrs sa;
365 if (bits_chars == std::string::npos)
367 irc::sockets::aptosa(mask, 0, sa);
368 sa2cidr(*this, sa, 128);
372 unsigned char range = ConvToNum<unsigned char>(mask.substr(bits_chars + 1));
373 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
374 sa2cidr(*this, sa, range);
378 std::string irc::sockets::cidr_mask::str() const
380 irc::sockets::sockaddrs sa;
381 sa.sa.sa_family = type;
388 base = (unsigned char*)&sa.in4.sin_addr;
393 base = (unsigned char*)&sa.in6.sin6_addr;
398 return sa.un.sun_path;
401 // If we have reached this point then we have encountered a bug.
402 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::cidr_mask::str(): socket type %d is unknown!", type);
406 memcpy(base, bits, len);
407 return sa.addr() + "/" + ConvToStr((int)length);
410 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
412 return type == other.type && length == other.length &&
413 0 == memcmp(bits, other.bits, 16);
416 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
418 if (type != other.type)
419 return type < other.type;
420 if (length != other.length)
421 return length < other.length;
422 return memcmp(bits, other.bits, 16) < 0;
425 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
427 if (addr.family() != type)
429 irc::sockets::cidr_mask tmp(addr, length);