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))
48 std::string bind_readable = bindspec.str();
51 for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
53 if ((**n).bind_desc == bind_readable)
55 (*n)->bind_tag = tag; // Replace tag, we know addr and port match, but other info (type, ssl) may not
56 (*n)->ResetIOHookProvider();
65 ListenSocket* ll = new ListenSocket(tag, bindspec);
74 failed_ports.push_back(std::make_pair(bind_readable, strerror(errno)));
81 std::vector<ListenSocket*>::iterator n = ports.begin();
82 for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
84 while (n != ports.end() && *n != *o)
88 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
92 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
93 (**n).bind_desc.c_str());
96 // this keeps the iterator valid, pointing to the next element
103 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
105 memset(&sa, 0, sizeof(sa));
106 if (addr.empty() || addr.c_str()[0] == '*')
108 if (ServerInstance->Config->WildcardIPv6)
110 sa.in6.sin6_family = AF_INET6;
111 sa.in6.sin6_port = htons(port);
115 sa.in4.sin_family = AF_INET;
116 sa.in4.sin_port = htons(port);
120 else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
122 sa.in4.sin_family = AF_INET;
123 sa.in4.sin_port = htons(port);
126 else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
128 sa.in6.sin6_family = AF_INET6;
129 sa.in6.sin6_port = htons(port);
135 int irc::sockets::sockaddrs::port() const
137 if (sa.sa_family == AF_INET)
138 return ntohs(in4.sin_port);
139 if (sa.sa_family == AF_INET6)
140 return ntohs(in6.sin6_port);
144 std::string irc::sockets::sockaddrs::addr() const
146 char addrv[INET6_ADDRSTRLEN+1];
147 if (sa.sa_family == AF_INET)
149 if (!inet_ntop(AF_INET, &in4.sin_addr, addrv, sizeof(addrv)))
153 else if (sa.sa_family == AF_INET6)
155 if (!inet_ntop(AF_INET6, &in6.sin6_addr, addrv, sizeof(addrv)))
162 bool irc::sockets::satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port)
166 return !addr.empty();
169 std::string irc::sockets::sockaddrs::str() const
171 if (sa.sa_family == AF_INET)
173 char ipaddr[INET_ADDRSTRLEN];
174 inet_ntop(AF_INET, &in4.sin_addr, ipaddr, sizeof(ipaddr));
175 return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
178 if (sa.sa_family == AF_INET6)
180 char ipaddr[INET6_ADDRSTRLEN];
181 inet_ntop(AF_INET6, &in6.sin6_addr, ipaddr, sizeof(ipaddr));
182 return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
185 // This should never happen.
189 int irc::sockets::sockaddrs::sa_size() const
191 if (sa.sa_family == AF_INET)
193 if (sa.sa_family == AF_INET6)
198 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
200 if (sa.sa_family != other.sa.sa_family)
202 if (sa.sa_family == AF_INET)
203 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
204 if (sa.sa_family == AF_INET6)
205 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
206 return !memcmp(this, &other, sizeof(*this));
209 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, int range)
211 const unsigned char* base;
212 unsigned char target_byte;
213 cidr.type = sa.sa.sa_family;
215 memset(cidr.bits, 0, sizeof(cidr.bits));
217 if (cidr.type == AF_INET)
219 target_byte = sizeof(sa.in4.sin_addr);
220 base = (unsigned char*)&sa.in4.sin_addr;
224 else if (cidr.type == AF_INET6)
226 target_byte = sizeof(sa.in6.sin6_addr);
227 base = (unsigned char*)&sa.in6.sin6_addr;
237 unsigned int border = range / 8;
238 unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
239 for(unsigned int i=0; i < target_byte; i++)
242 cidr.bits[i] = base[i];
243 else if (i == border)
244 cidr.bits[i] = base[i] & bitmask;
250 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, int range)
252 sa2cidr(*this, sa, range);
255 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
257 std::string::size_type bits_chars = mask.rfind('/');
258 irc::sockets::sockaddrs sa;
260 if (bits_chars == std::string::npos)
262 irc::sockets::aptosa(mask, 0, sa);
263 sa2cidr(*this, sa, 128);
267 int range = ConvToInt(mask.substr(bits_chars + 1));
268 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
269 sa2cidr(*this, sa, range);
273 std::string irc::sockets::cidr_mask::str() const
275 irc::sockets::sockaddrs sa;
276 sa.sa.sa_family = type;
281 base = (unsigned char*)&sa.in4.sin_addr;
284 else if (type == AF_INET6)
286 base = (unsigned char*)&sa.in6.sin6_addr;
291 memcpy(base, bits, len);
292 return sa.addr() + "/" + ConvToStr((int)length);
295 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
297 return type == other.type && length == other.length &&
298 0 == memcmp(bits, other.bits, 16);
301 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
303 if (type != other.type)
304 return type < other.type;
305 if (length != other.length)
306 return length < other.length;
307 return memcmp(bits, other.bits, 16) < 0;
310 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
312 if (addr.sa.sa_family != type)
314 irc::sockets::cidr_mask tmp(addr, length);