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 int InspIRCd::BindPorts(FailedPortList &failed_ports)
29 std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
31 ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
32 for(ConfigIter i = tags.first; i != tags.second; ++i)
34 ConfigTag* tag = i->second;
35 std::string porttag = tag->getString("port");
36 std::string Addr = tag->getString("address");
38 if (strncasecmp(Addr.c_str(), "::ffff:", 7) == 0)
39 this->Logs->Log("SOCKET", LOG_DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
41 irc::portparser portrange(porttag, false);
43 while (0 != (portno = portrange.GetToken()))
45 irc::sockets::sockaddrs bindspec;
46 if (!irc::sockets::aptosa(Addr, portno, bindspec))
50 for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
52 if ((**n).bind_sa == bindspec)
54 (*n)->bind_tag = tag; // Replace tag, we know addr and port match, but other info (type, ssl) may not
55 (*n)->ResetIOHookProvider();
64 ListenSocket* ll = new ListenSocket(tag, bindspec);
73 failed_ports.push_back(std::make_pair(bindspec, errno));
80 std::vector<ListenSocket*>::iterator n = ports.begin();
81 for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
83 while (n != ports.end() && *n != *o)
87 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
91 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
92 (**n).bind_sa.str().c_str());
95 // this keeps the iterator valid, pointing to the next element
102 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
104 memset(&sa, 0, sizeof(sa));
105 if (addr.empty() || addr.c_str()[0] == '*')
107 if (ServerInstance->Config->WildcardIPv6)
109 sa.in6.sin6_family = AF_INET6;
110 sa.in6.sin6_port = htons(port);
114 sa.in4.sin_family = AF_INET;
115 sa.in4.sin_port = htons(port);
119 else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
121 sa.in4.sin_family = AF_INET;
122 sa.in4.sin_port = htons(port);
125 else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
127 sa.in6.sin6_family = AF_INET6;
128 sa.in6.sin6_port = htons(port);
134 int irc::sockets::sockaddrs::family() const
139 int irc::sockets::sockaddrs::port() const
141 if (family() == AF_INET)
142 return ntohs(in4.sin_port);
143 if (family() == AF_INET6)
144 return ntohs(in6.sin6_port);
148 std::string irc::sockets::sockaddrs::addr() const
150 char addrv[INET6_ADDRSTRLEN+1];
151 if (family() == AF_INET)
153 if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, addrv, sizeof(addrv)))
157 else if (family() == AF_INET6)
159 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, addrv, sizeof(addrv)))
166 std::string irc::sockets::sockaddrs::str() const
168 if (family() == AF_INET)
170 char ipaddr[INET_ADDRSTRLEN];
171 inet_ntop(AF_INET, (void*)&in4.sin_addr, ipaddr, sizeof(ipaddr));
172 return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
175 if (family() == AF_INET6)
177 char ipaddr[INET6_ADDRSTRLEN];
178 inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ipaddr, sizeof(ipaddr));
179 return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
182 // This should never happen.
186 socklen_t irc::sockets::sockaddrs::sa_size() const
188 if (family() == AF_INET)
190 if (family() == AF_INET6)
195 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
197 if (family() != other.family())
199 if (family() == AF_INET)
200 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
201 if (family() == AF_INET6)
202 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
203 return !memcmp(this, &other, sizeof(*this));
206 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
208 const unsigned char* base;
209 unsigned char target_byte;
210 cidr.type = sa.family();
212 memset(cidr.bits, 0, sizeof(cidr.bits));
214 if (cidr.type == AF_INET)
216 target_byte = sizeof(sa.in4.sin_addr);
217 base = (unsigned char*)&sa.in4.sin_addr;
221 else if (cidr.type == AF_INET6)
223 target_byte = sizeof(sa.in6.sin6_addr);
224 base = (unsigned char*)&sa.in6.sin6_addr;
234 unsigned int border = range / 8;
235 unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
236 for(unsigned int i=0; i < target_byte; i++)
239 cidr.bits[i] = base[i];
240 else if (i == border)
241 cidr.bits[i] = base[i] & bitmask;
247 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, unsigned char range)
249 sa2cidr(*this, sa, range);
252 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
254 std::string::size_type bits_chars = mask.rfind('/');
255 irc::sockets::sockaddrs sa;
257 if (bits_chars == std::string::npos)
259 irc::sockets::aptosa(mask, 0, sa);
260 sa2cidr(*this, sa, 128);
264 int range = ConvToInt(mask.substr(bits_chars + 1));
265 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
266 sa2cidr(*this, sa, range);
270 std::string irc::sockets::cidr_mask::str() const
272 irc::sockets::sockaddrs sa;
273 sa.sa.sa_family = type;
278 base = (unsigned char*)&sa.in4.sin_addr;
281 else if (type == AF_INET6)
283 base = (unsigned char*)&sa.in6.sin6_addr;
288 memcpy(base, bits, len);
289 return sa.addr() + "/" + ConvToStr((int)length);
292 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
294 return type == other.type && length == other.length &&
295 0 == memcmp(bits, other.bits, 16);
298 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
300 if (type != other.type)
301 return type < other.type;
302 if (length != other.length)
303 return length < other.length;
304 return memcmp(bits, other.bits, 16) < 0;
307 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
309 if (addr.family() != type)
311 irc::sockets::cidr_mask tmp(addr, length);