2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
8 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2009-2011 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
12 * Copyright (C) 2007 John Brooks <special@inspircd.org>
13 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
14 * Copyright (C) 2006 Craig Edwards <brain@inspircd.org>
16 * This file is part of InspIRCd. InspIRCd is free software: you can
17 * redistribute it and/or modify it under the terms of the GNU General Public
18 * License as published by the Free Software Foundation, version 2.
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 bool InspIRCd::BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std::vector<ListenSocket*>& old_ports)
34 for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
36 if ((**n).bind_sa == sa)
38 // Replace tag, we know addr and port match, but other info (type, ssl) may not.
39 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Replacing listener on %s from old tag at %s with new tag from %s",
40 sa.str().c_str(), (*n)->bind_tag->getTagLocation().c_str(), tag->getTagLocation().c_str());
42 (*n)->ResetIOHookProvider();
49 ListenSocket* ll = new ListenSocket(tag, sa);
52 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to listen on %s from tag at %s: %s",
53 sa.str().c_str(), tag->getTagLocation().c_str(), strerror(errno));
58 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Added a listener on %s from tag at %s", sa.str().c_str(), tag->getTagLocation().c_str());
63 size_t InspIRCd::BindPorts(FailedPortList& failed_ports)
66 std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
68 ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
69 for (ConfigIter i = tags.first; i != tags.second; ++i)
71 ConfigTag* tag = i->second;
73 // Are we creating a TCP/IP listener?
74 const std::string address = tag->getString("address");
75 const std::string portlist = tag->getString("port");
76 if (!address.empty() || !portlist.empty())
78 // InspIRCd supports IPv4 and IPv6 natively; no 4in6 required.
79 if (strncasecmp(address.c_str(), "::ffff:", 7) == 0)
80 this->Logs->Log("SOCKET", LOG_DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
82 // A TCP listener with no ports is not very useful.
84 this->Logs->Log("SOCKET", LOG_DEFAULT, "TCP listener on %s at %s has no ports specified!",
85 address.empty() ? "*" : address.c_str(), tag->getTagLocation().c_str());
87 irc::portparser portrange(portlist, false);
88 for (int port; (port = portrange.GetToken()); )
90 irc::sockets::sockaddrs bindspec;
91 if (!irc::sockets::aptosa(address, port, bindspec))
94 if (!BindPort(tag, bindspec, old_ports))
95 failed_ports.push_back(FailedPort(errno, bindspec, tag));
103 // Are we creating a UNIX listener?
104 const std::string path = tag->getString("path");
107 // Expand the path relative to the config directory.
108 const std::string fullpath = ServerInstance->Config->Paths.PrependData(path);
110 // UNIX socket paths are length limited to less than PATH_MAX.
111 irc::sockets::sockaddrs bindspec;
112 if (fullpath.length() > std::min(ServerInstance->Config->Limits.MaxHost, sizeof(bindspec.un.sun_path) - 1))
114 this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path that is too long!",
115 fullpath.c_str(), tag->getTagLocation().c_str());
119 // Check for characters which are problematic in the IRC message format.
120 if (fullpath.find_first_of("\n\r\t!@: ") != std::string::npos)
122 this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path containing invalid characters!",
123 fullpath.c_str(), tag->getTagLocation().c_str());
127 irc::sockets::untosa(fullpath, bindspec);
128 if (!BindPort(tag, bindspec, old_ports))
129 failed_ports.push_back(FailedPort(errno, bindspec, tag));
136 std::vector<ListenSocket*>::iterator n = ports.begin();
137 for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
139 while (n != ports.end() && *n != *o)
141 if (n == ports.end())
143 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
147 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
148 (**n).bind_sa.str().c_str());
151 // this keeps the iterator valid, pointing to the next element
158 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
160 memset(&sa, 0, sizeof(sa));
161 if (addr.empty() || addr.c_str()[0] == '*')
163 if (ServerInstance->Config->WildcardIPv6)
165 sa.in6.sin6_family = AF_INET6;
166 sa.in6.sin6_port = htons(port);
170 sa.in4.sin_family = AF_INET;
171 sa.in4.sin_port = htons(port);
175 else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
177 sa.in4.sin_family = AF_INET;
178 sa.in4.sin_port = htons(port);
181 else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
183 sa.in6.sin6_family = AF_INET6;
184 sa.in6.sin6_port = htons(port);
190 bool irc::sockets::untosa(const std::string& path, irc::sockets::sockaddrs& sa)
192 memset(&sa, 0, sizeof(sa));
193 if (path.length() >= sizeof(sa.un.sun_path))
196 sa.un.sun_family = AF_UNIX;
197 memcpy(&sa.un.sun_path, path.c_str(), path.length() + 1);
201 bool irc::sockets::isunix(const std::string& file)
205 if (stat(file.c_str(), &sb) == 0 && S_ISSOCK(sb.st_mode))
212 int irc::sockets::sockaddrs::family() const
217 int irc::sockets::sockaddrs::port() const
222 return ntohs(in4.sin_port);
225 return ntohs(in6.sin6_port);
231 // If we have reached this point then we have encountered a bug.
232 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::port(): socket type %d is unknown!", family());
236 std::string irc::sockets::sockaddrs::addr() const
241 char ip4addr[INET_ADDRSTRLEN];
242 if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
247 char ip6addr[INET6_ADDRSTRLEN];
248 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
249 return "0:0:0:0:0:0:0:0";
256 // If we have reached this point then we have encountered a bug.
257 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::addr(): socket type %d is unknown!", family());
261 std::string irc::sockets::sockaddrs::str() const
266 char ip4addr[INET_ADDRSTRLEN];
267 if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
268 strcpy(ip4addr, "0.0.0.0");
269 return InspIRCd::Format("%s:%u", ip4addr, ntohs(in4.sin_port));
272 char ip6addr[INET6_ADDRSTRLEN];
273 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
274 strcpy(ip6addr, "0:0:0:0:0:0:0:0");
275 return InspIRCd::Format("[%s]:%u", ip6addr, ntohs(in6.sin6_port));
281 // If we have reached this point then we have encountered a bug.
282 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::str(): socket type %d is unknown!", family());
286 socklen_t irc::sockets::sockaddrs::sa_size() const
300 // If we have reached this point then we have encountered a bug.
301 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::sa_size(): socket type %d is unknown!", family());
305 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
307 if (family() != other.family())
313 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
316 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
319 return !strcmp(un.sun_path, other.un.sun_path);
322 // If we have reached this point then we have encountered a bug.
323 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::operator==(): socket type %d is unknown!", family());
324 return !memcmp(this, &other, sizeof(*this));
327 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
329 const unsigned char* base;
330 unsigned char target_byte;
332 memset(cidr.bits, 0, sizeof(cidr.bits));
334 cidr.type = sa.family();
338 // XXX: UNIX sockets don't support CIDR. This fix is non-ideal but I can't
339 // really think of another way to handle it.
344 cidr.length = range > 32 ? 32 : range;
345 target_byte = sizeof(sa.in4.sin_addr);
346 base = (unsigned char*)&sa.in4.sin_addr;
350 cidr.length = range > 128 ? 128 : range;
351 target_byte = sizeof(sa.in6.sin6_addr);
352 base = (unsigned char*)&sa.in6.sin6_addr;
356 // If we have reached this point then we have encountered a bug.
357 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: sa2cidr(): socket type %d is unknown!", cidr.type);
362 unsigned int border = cidr.length / 8;
363 unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
364 for(unsigned int i=0; i < target_byte; i++)
367 cidr.bits[i] = base[i];
368 else if (i == border)
369 cidr.bits[i] = base[i] & bitmask;
375 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, unsigned char range)
377 sa2cidr(*this, sa, range);
380 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
382 std::string::size_type bits_chars = mask.rfind('/');
383 irc::sockets::sockaddrs sa;
385 if (bits_chars == std::string::npos)
387 irc::sockets::aptosa(mask, 0, sa);
388 sa2cidr(*this, sa, 128);
392 unsigned char range = ConvToNum<unsigned char>(mask.substr(bits_chars + 1));
393 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
394 sa2cidr(*this, sa, range);
398 std::string irc::sockets::cidr_mask::str() const
400 irc::sockets::sockaddrs sa;
401 sa.sa.sa_family = type;
408 base = (unsigned char*)&sa.in4.sin_addr;
413 base = (unsigned char*)&sa.in6.sin6_addr;
418 return sa.un.sun_path;
421 // If we have reached this point then we have encountered a bug.
422 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::cidr_mask::str(): socket type %d is unknown!", type);
426 memcpy(base, bits, len);
427 return sa.addr() + "/" + ConvToStr((int)length);
430 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
432 return type == other.type && length == other.length &&
433 0 == memcmp(bits, other.bits, 16);
436 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
438 if (type != other.type)
439 return type < other.type;
440 if (length != other.length)
441 return length < other.length;
442 return memcmp(bits, other.bits, 16) < 0;
445 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
447 if (addr.family() != type)
449 irc::sockets::cidr_mask tmp(addr, length);