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 #include "socketengine.h"
27 using irc::sockets::sockaddrs;
29 /** This will bind a socket to a port. It works for UDP/TCP.
30 * It can only bind to IP addresses, if you wish to bind to hostnames
31 * you should first resolve them using class 'Resolver'.
33 bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
38 if ((*addr == '*' || *addr == '\0') && port == -1)
40 /* Port -1: Means UDP IPV4 port binding - Special case
43 memset(&servaddr, 0, sizeof(servaddr));
44 servaddr.in4.sin_family = AF_INET;
46 else if (!irc::sockets::aptosa(addr, port, servaddr))
49 ret = SE->Bind(sockfd, servaddr);
59 if (SE->Listen(sockfd, Config->MaxConn) == -1)
61 this->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR in listen(): %s",strerror(errno));
66 this->Logs->Log("SOCKET", LOG_DEBUG, "New socket binding for %d with listen: %s:%d", sockfd, addr, port);
67 SE->NonBlocking(sockfd);
73 this->Logs->Log("SOCKET", LOG_DEBUG, "New socket binding for %d without listen: %s:%d", sockfd, addr, port);
79 int InspIRCd::BindPorts(FailedPortList &failed_ports)
82 std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
84 ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
85 for(ConfigIter i = tags.first; i != tags.second; ++i)
87 ConfigTag* tag = i->second;
88 std::string porttag = tag->getString("port");
89 std::string Addr = tag->getString("address");
91 if (strncasecmp(Addr.c_str(), "::ffff:", 7) == 0)
92 this->Logs->Log("SOCKET", LOG_DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
94 irc::portparser portrange(porttag, false);
96 while (0 != (portno = portrange.GetToken()))
98 irc::sockets::sockaddrs bindspec;
99 if (!irc::sockets::aptosa(Addr, portno, bindspec))
101 std::string bind_readable = bindspec.str();
104 for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
106 if ((**n).bind_desc == bind_readable)
108 (*n)->bind_tag = tag; // Replace tag, we know addr and port match, but other info (type, ssl) may not
116 ListenSocket* ll = new ListenSocket(tag, bindspec);
118 if (ll->GetFd() > -1)
125 failed_ports.push_back(std::make_pair(bind_readable, strerror(errno)));
132 std::vector<ListenSocket*>::iterator n = ports.begin();
133 for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
135 while (n != ports.end() && *n != *o)
137 if (n == ports.end())
139 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
143 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
144 (**n).bind_desc.c_str());
147 // this keeps the iterator valid, pointing to the next element
154 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
156 memset(&sa, 0, sizeof(sa));
157 if (addr.empty() || addr.c_str()[0] == '*')
159 if (ServerInstance->Config->WildcardIPv6)
161 sa.in6.sin6_family = AF_INET6;
162 sa.in6.sin6_port = htons(port);
166 sa.in4.sin_family = AF_INET;
167 sa.in4.sin_port = htons(port);
171 else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
173 sa.in4.sin_family = AF_INET;
174 sa.in4.sin_port = htons(port);
177 else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
179 sa.in6.sin6_family = AF_INET6;
180 sa.in6.sin6_port = htons(port);
186 int irc::sockets::sockaddrs::port() const
188 if (sa.sa_family == AF_INET)
189 return ntohs(in4.sin_port);
190 if (sa.sa_family == AF_INET6)
191 return ntohs(in6.sin6_port);
195 std::string irc::sockets::sockaddrs::addr() const
197 char addrv[INET6_ADDRSTRLEN+1];
198 if (sa.sa_family == AF_INET)
200 if (!inet_ntop(AF_INET, &in4.sin_addr, addrv, sizeof(addrv)))
204 else if (sa.sa_family == AF_INET6)
206 if (!inet_ntop(AF_INET6, &in6.sin6_addr, addrv, sizeof(addrv)))
213 bool irc::sockets::satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port)
217 return !addr.empty();
220 static const char all_zero[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
222 std::string irc::sockets::sockaddrs::str() const
224 if (sa.sa_family == AF_INET)
226 char ipaddr[INET_ADDRSTRLEN];
227 inet_ntop(AF_INET, &in4.sin_addr, ipaddr, sizeof(ipaddr));
228 return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
231 if (sa.sa_family == AF_INET6)
233 char ipaddr[INET6_ADDRSTRLEN];
234 inet_ntop(AF_INET6, &in6.sin6_addr, ipaddr, sizeof(ipaddr));
235 return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
238 // This should never happen.
242 int irc::sockets::sockaddrs::sa_size() const
244 if (sa.sa_family == AF_INET)
246 if (sa.sa_family == AF_INET6)
251 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
253 if (sa.sa_family != other.sa.sa_family)
255 if (sa.sa_family == AF_INET)
256 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
257 if (sa.sa_family == AF_INET6)
258 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
259 return !memcmp(this, &other, sizeof(*this));
262 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, int range)
264 const unsigned char* base;
265 cidr.type = sa.sa.sa_family;
266 if (cidr.type == AF_INET)
268 base = (unsigned char*)&sa.in4.sin_addr;
272 else if (cidr.type == AF_INET6)
274 base = (unsigned char*)&sa.in6.sin6_addr;
280 base = (unsigned char*)"";
284 unsigned int border = range / 8;
285 unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
286 for(unsigned int i=0; i < 16; i++)
289 cidr.bits[i] = base[i];
290 else if (i == border)
291 cidr.bits[i] = base[i] & bitmask;
297 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, int range)
299 sa2cidr(*this, sa, range);
302 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
304 std::string::size_type bits_chars = mask.rfind('/');
305 irc::sockets::sockaddrs sa;
307 if (bits_chars == std::string::npos)
309 irc::sockets::aptosa(mask, 0, sa);
310 sa2cidr(*this, sa, 128);
314 int range = ConvToInt(mask.substr(bits_chars + 1));
315 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
316 sa2cidr(*this, sa, range);
320 std::string irc::sockets::cidr_mask::str() const
322 irc::sockets::sockaddrs sa;
323 sa.sa.sa_family = type;
328 base = (unsigned char*)&sa.in4.sin_addr;
331 else if (type == AF_INET6)
333 base = (unsigned char*)&sa.in6.sin6_addr;
338 memcpy(base, bits, len);
339 return sa.addr() + "/" + ConvToStr((int)length);
342 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
344 return type == other.type && length == other.length &&
345 0 == memcmp(bits, other.bits, 16);
348 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
350 if (type != other.type)
351 return type < other.type;
352 if (length != other.length)
353 return length < other.length;
354 return memcmp(bits, other.bits, 16) < 0;
357 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
359 if (addr.sa.sa_family != type)
361 irc::sockets::cidr_mask tmp(addr, length);