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, (void*)&in4.sin_addr, addrv, sizeof(addrv)))
153 else if (sa.sa_family == AF_INET6)
155 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, addrv, sizeof(addrv)))
162 std::string irc::sockets::sockaddrs::str() const
164 if (sa.sa_family == AF_INET)
166 char ipaddr[INET_ADDRSTRLEN];
167 inet_ntop(AF_INET, (void*)&in4.sin_addr, ipaddr, sizeof(ipaddr));
168 return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
171 if (sa.sa_family == AF_INET6)
173 char ipaddr[INET6_ADDRSTRLEN];
174 inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ipaddr, sizeof(ipaddr));
175 return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
178 // This should never happen.
182 int irc::sockets::sockaddrs::sa_size() const
184 if (sa.sa_family == AF_INET)
186 if (sa.sa_family == AF_INET6)
191 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
193 if (sa.sa_family != other.sa.sa_family)
195 if (sa.sa_family == AF_INET)
196 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
197 if (sa.sa_family == AF_INET6)
198 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
199 return !memcmp(this, &other, sizeof(*this));
202 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, int range)
204 const unsigned char* base;
205 unsigned char target_byte;
206 cidr.type = sa.sa.sa_family;
208 memset(cidr.bits, 0, sizeof(cidr.bits));
210 if (cidr.type == AF_INET)
212 target_byte = sizeof(sa.in4.sin_addr);
213 base = (unsigned char*)&sa.in4.sin_addr;
217 else if (cidr.type == AF_INET6)
219 target_byte = sizeof(sa.in6.sin6_addr);
220 base = (unsigned char*)&sa.in6.sin6_addr;
230 unsigned int border = range / 8;
231 unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
232 for(unsigned int i=0; i < target_byte; i++)
235 cidr.bits[i] = base[i];
236 else if (i == border)
237 cidr.bits[i] = base[i] & bitmask;
243 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, int range)
245 sa2cidr(*this, sa, range);
248 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
250 std::string::size_type bits_chars = mask.rfind('/');
251 irc::sockets::sockaddrs sa;
253 if (bits_chars == std::string::npos)
255 irc::sockets::aptosa(mask, 0, sa);
256 sa2cidr(*this, sa, 128);
260 int range = ConvToInt(mask.substr(bits_chars + 1));
261 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
262 sa2cidr(*this, sa, range);
266 std::string irc::sockets::cidr_mask::str() const
268 irc::sockets::sockaddrs sa;
269 sa.sa.sa_family = type;
274 base = (unsigned char*)&sa.in4.sin_addr;
277 else if (type == AF_INET6)
279 base = (unsigned char*)&sa.in6.sin6_addr;
284 memcpy(base, bits, len);
285 return sa.addr() + "/" + ConvToStr((int)length);
288 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
290 return type == other.type && length == other.length &&
291 0 == memcmp(bits, other.bits, 16);
294 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
296 if (type != other.type)
297 return type < other.type;
298 if (length != other.length)
299 return length < other.length;
300 return memcmp(bits, other.bits, 16) < 0;
303 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
305 if (addr.sa.sa_family != type)
307 irc::sockets::cidr_mask tmp(addr, length);