1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include "socketengine.h"
17 using irc::sockets::sockaddrs;
19 /** This will bind a socket to a port. It works for UDP/TCP.
20 * It can only bind to IP addresses, if you wish to bind to hostnames
21 * you should first resolve them using class 'Resolver'.
23 bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
28 if ((*addr == '*' || *addr == '\0') && port == -1)
30 /* Port -1: Means UDP IPV4 port binding - Special case
33 memset(&servaddr, 0, sizeof(servaddr));
34 servaddr.in4.sin_family = AF_INET;
36 else if (!irc::sockets::aptosa(addr, port, servaddr))
39 ret = SE->Bind(sockfd, servaddr);
49 if (SE->Listen(sockfd, Config->MaxConn) == -1)
51 this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
56 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
57 SE->NonBlocking(sockfd);
63 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
69 int InspIRCd::BindPorts(FailedPortList &failed_ports)
72 std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
74 ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
75 for(ConfigIter i = tags.first; i != tags.second; ++i)
77 ConfigTag* tag = i->second;
78 std::string porttag = tag->getString("port");
79 std::string Addr = tag->getString("address");
81 if (strncasecmp(Addr.c_str(), "::ffff:", 7) == 0)
82 this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
84 irc::portparser portrange(porttag, false);
86 while (0 != (portno = portrange.GetToken()))
88 irc::sockets::sockaddrs bindspec;
89 if (!irc::sockets::aptosa(Addr, portno, bindspec))
91 std::string bind_readable = bindspec.str();
94 for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
96 if ((**n).bind_desc == bind_readable)
105 ListenSocket* ll = new ListenSocket(tag, bindspec);
107 if (ll->GetFd() > -1)
114 failed_ports.push_back(std::make_pair(bind_readable, strerror(errno)));
121 std::vector<ListenSocket*>::iterator n = ports.begin();
122 for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
124 while (n != ports.end() && *n != *o)
126 if (n == ports.end())
128 this->Logs->Log("SOCKET",DEFAULT,"Port bindings slipped out of vector, aborting close!");
132 this->Logs->Log("SOCKET",DEFAULT, "Port binding %s was removed from the config file, closing.",
133 (**n).bind_desc.c_str());
136 // this keeps the iterator valid, pointing to the next element
143 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
145 memset(&sa, 0, sizeof(sa));
146 if (addr.empty() || addr.c_str()[0] == '*')
148 if (ServerInstance->Config->WildcardIPv6)
150 sa.in6.sin6_family = AF_INET6;
151 sa.in6.sin6_port = htons(port);
155 sa.in4.sin_family = AF_INET;
156 sa.in4.sin_port = htons(port);
160 else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
162 sa.in4.sin_family = AF_INET;
163 sa.in4.sin_port = htons(port);
166 else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
168 sa.in6.sin6_family = AF_INET6;
169 sa.in6.sin6_port = htons(port);
175 int irc::sockets::sockaddrs::port() const
177 if (sa.sa_family == AF_INET)
178 return ntohs(in4.sin_port);
179 if (sa.sa_family == AF_INET6)
180 return ntohs(in6.sin6_port);
184 std::string irc::sockets::sockaddrs::addr() const
186 char addrv[INET6_ADDRSTRLEN+1];
187 if (sa.sa_family == AF_INET)
189 if (!inet_ntop(AF_INET, &in4.sin_addr, addrv, sizeof(addrv)))
193 else if (sa.sa_family == AF_INET6)
195 if (!inet_ntop(AF_INET6, &in6.sin6_addr, addrv, sizeof(addrv)))
202 bool irc::sockets::satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port)
206 return !addr.empty();
209 static const char all_zero[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
211 std::string irc::sockets::sockaddrs::str() const
214 if (sa.sa_family == AF_INET)
216 const uint8_t* bits = reinterpret_cast<const uint8_t*>(&in4.sin_addr);
217 sprintf(buffer, "%d.%d.%d.%d:%u", bits[0], bits[1], bits[2], bits[3], ntohs(in4.sin_port));
219 else if (sa.sa_family == AF_INET6)
222 if (!inet_ntop(AF_INET6, &in6.sin6_addr, buffer+1, MAXBUF - 10))
223 return "<unknown>"; // should never happen, buffer is large enough
224 int len = strlen(buffer);
225 // no need for snprintf, buffer has at least 9 chars left, max short len = 5
226 sprintf(buffer + len, "]:%u", ntohs(in6.sin6_port));
230 return std::string(buffer);
233 int irc::sockets::sockaddrs::sa_size() const
235 if (sa.sa_family == AF_INET)
237 if (sa.sa_family == AF_INET6)
242 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
244 if (sa.sa_family != other.sa.sa_family)
246 if (sa.sa_family == AF_INET)
247 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
248 if (sa.sa_family == AF_INET6)
249 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
250 return !memcmp(this, &other, sizeof(*this));
253 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, int range)
255 const unsigned char* base;
256 cidr.type = sa.sa.sa_family;
257 if (cidr.type == AF_INET)
259 base = (unsigned char*)&sa.in4.sin_addr;
263 else if (cidr.type == AF_INET6)
265 base = (unsigned char*)&sa.in6.sin6_addr;
271 base = (unsigned char*)"";
275 unsigned int border = range / 8;
276 unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
277 for(unsigned int i=0; i < 16; i++)
280 cidr.bits[i] = base[i];
281 else if (i == border)
282 cidr.bits[i] = base[i] & bitmask;
288 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, int range)
290 sa2cidr(*this, sa, range);
293 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
295 std::string::size_type bits_chars = mask.rfind('/');
296 irc::sockets::sockaddrs sa;
298 if (bits_chars == std::string::npos)
300 irc::sockets::aptosa(mask, 0, sa);
301 sa2cidr(*this, sa, 128);
305 int range = atoi(mask.substr(bits_chars + 1).c_str());
306 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
307 sa2cidr(*this, sa, range);
311 std::string irc::sockets::cidr_mask::str() const
313 irc::sockets::sockaddrs sa;
314 sa.sa.sa_family = type;
319 base = (unsigned char*)&sa.in4.sin_addr;
322 else if (type == AF_INET6)
324 base = (unsigned char*)&sa.in6.sin6_addr;
329 memcpy(base, bits, len);
330 return sa.addr() + "/" + ConvToStr((int)length);
333 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
335 return type == other.type && length == other.length &&
336 0 == memcmp(bits, other.bits, 16);
339 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
341 return type < other.type || length < other.length ||
342 memcmp(bits, other.bits, 16) < 0;
345 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
347 if (addr.sa.sa_family != type)
349 irc::sockets::cidr_mask tmp(addr, length);