]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Fix some bugs in cidr_mask::str().
[user/henk/code/inspircd.git] / src / socket.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
9  *
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.
13  *
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
17  * details.
18  *
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/>.
21  */
22
23
24 #include "inspircd.h"
25
26 bool InspIRCd::BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std::vector<ListenSocket*>& old_ports)
27 {
28         for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
29         {
30                 if ((**n).bind_sa == sa)
31                 {
32                         // Replace tag, we know addr and port match, but other info (type, ssl) may not.
33                         ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Replacing listener on %s from old tag at %s with new tag from %s",
34                                 sa.str().c_str(), (*n)->bind_tag->getTagLocation().c_str(), tag->getTagLocation().c_str());
35                         (*n)->bind_tag = tag;
36                         (*n)->ResetIOHookProvider();
37
38                         old_ports.erase(n);
39                         return true;
40                 }
41         }
42         
43         ListenSocket* ll = new ListenSocket(tag, sa);
44         if (ll->GetFd() < 0)
45         {
46                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to listen on %s from tag at %s: %s",
47                         sa.str().c_str(), tag->getTagLocation().c_str(), strerror(errno));
48                 delete ll;
49                 return false;
50         }
51
52         ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Added a listener on %s from tag at %s", sa.str().c_str(), tag->getTagLocation().c_str());
53         ports.push_back(ll);
54         return true;
55 }
56
57 int InspIRCd::BindPorts(FailedPortList& failed_ports)
58 {
59         int bound = 0;
60         std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
61
62         ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
63         for (ConfigIter i = tags.first; i != tags.second; ++i)
64         {
65                 ConfigTag* tag = i->second;
66
67                 // Are we creating a TCP/IP listener?
68                 const std::string address = tag->getString("address");
69                 const std::string portlist = tag->getString("port");
70                 if (!address.empty() || !portlist.empty())
71                 {
72                         // InspIRCd supports IPv4 and IPv6 natively; no 4in6 required.
73                         if (strncasecmp(address.c_str(), "::ffff:", 7) == 0)
74                                 this->Logs->Log("SOCKET", LOG_DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
75
76                         // A TCP listener with no ports is not very useful.
77                         if (portlist.empty())
78                                 this->Logs->Log("SOCKET", LOG_DEFAULT, "TCP listener on %s at %s has no ports specified!",
79                                         address.empty() ? "*" : address.c_str(), tag->getTagLocation().c_str());
80
81                         irc::portparser portrange(portlist, false);
82                         for (int port; (port = portrange.GetToken()); )
83                         {
84                                 irc::sockets::sockaddrs bindspec;
85                                 if (!irc::sockets::aptosa(address, port, bindspec))
86                                         continue;
87
88                                 if (!BindPort(tag, bindspec, old_ports))
89                                         failed_ports.push_back(std::make_pair(bindspec, errno));
90                                 else
91                                         bound++;
92                         }
93                         continue;
94                 }
95
96 #ifndef _WIN32
97                 // Are we creating a UNIX listener?
98                 const std::string path = tag->getString("path");
99                 if (!path.empty())
100                 {
101                         // UNIX socket paths are length limited to less than PATH_MAX.
102                         irc::sockets::sockaddrs bindspec;
103                         if (path.length() > std::min(ServerInstance->Config->Limits.MaxHost, sizeof(bindspec.un.sun_path)))
104                         {
105                                 this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path that is too long!",
106                                         path.c_str(), tag->getTagLocation().c_str());
107                                 continue;
108                         }
109
110                         // Create the bindspec manually (aptosa doesn't work with AF_UNIX yet).
111                         memset(&bindspec, 0, sizeof(bindspec));
112                         bindspec.un.sun_family = AF_UNIX;
113                         memcpy(&bindspec.un.sun_path, path.c_str(), sizeof(bindspec.un.sun_path));
114
115                         if (!BindPort(tag, bindspec, old_ports))
116                                 failed_ports.push_back(std::make_pair(bindspec, errno));
117                         else
118                                 bound++;
119                 }
120 #endif
121         }
122
123         std::vector<ListenSocket*>::iterator n = ports.begin();
124         for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
125         {
126                 while (n != ports.end() && *n != *o)
127                         n++;
128                 if (n == ports.end())
129                 {
130                         this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
131                         break;
132                 }
133
134                 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
135                         (**n).bind_sa.str().c_str());
136                 delete *n;
137
138                 // this keeps the iterator valid, pointing to the next element
139                 n = ports.erase(n);
140         }
141
142         return bound;
143 }
144
145 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
146 {
147         memset(&sa, 0, sizeof(sa));
148         if (addr.empty() || addr.c_str()[0] == '*')
149         {
150                 if (ServerInstance->Config->WildcardIPv6)
151                 {
152                         sa.in6.sin6_family = AF_INET6;
153                         sa.in6.sin6_port = htons(port);
154                 }
155                 else
156                 {
157                         sa.in4.sin_family = AF_INET;
158                         sa.in4.sin_port = htons(port);
159                 }
160                 return true;
161         }
162         else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
163         {
164                 sa.in4.sin_family = AF_INET;
165                 sa.in4.sin_port = htons(port);
166                 return true;
167         }
168         else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
169         {
170                 sa.in6.sin6_family = AF_INET6;
171                 sa.in6.sin6_port = htons(port);
172                 return true;
173         }
174         return false;
175 }
176
177 int irc::sockets::sockaddrs::family() const
178 {
179         return sa.sa_family;
180 }
181
182 int irc::sockets::sockaddrs::port() const
183 {
184         switch (family())
185         {
186                 case AF_INET:
187                         return ntohs(in4.sin_port);
188
189                 case AF_INET6:
190                         return ntohs(in6.sin6_port);
191
192                 case AF_UNIX:
193                         return 0;
194         }
195
196         // If we have reached this point then we have encountered a bug.
197         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::port(): socket type %d is unknown!", family());
198         return 0;
199 }
200
201 std::string irc::sockets::sockaddrs::addr() const
202 {
203         switch (family())
204         {
205                 case AF_INET:
206                         char ip4addr[INET_ADDRSTRLEN];
207                         if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
208                                 return "0.0.0.0";
209                         return ip4addr;
210
211                 case AF_INET6:
212                         char ip6addr[INET6_ADDRSTRLEN];
213                         if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
214                                 return "0:0:0:0:0:0:0:0";
215                         return ip6addr;
216
217                 case AF_UNIX:
218                         return un.sun_path;
219         }
220
221         // If we have reached this point then we have encountered a bug.
222         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::addr(): socket type %d is unknown!", family());
223         return "<unknown>";
224 }
225
226 std::string irc::sockets::sockaddrs::str() const
227 {
228         switch (family())
229         {
230                 case AF_INET:
231                         char ip4addr[INET_ADDRSTRLEN];
232                         if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
233                                 strcpy(ip4addr, "0.0.0.0");
234                         return InspIRCd::Format("%s:%u", ip4addr, ntohs(in4.sin_port));
235
236                 case AF_INET6:
237                         char ip6addr[INET6_ADDRSTRLEN];
238                         if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
239                                 strcpy(ip6addr, "0:0:0:0:0:0:0:0");
240                         return InspIRCd::Format("[%s]:%u", ip6addr, ntohs(in6.sin6_port));
241
242                 case AF_UNIX:
243                         return un.sun_path;
244         }
245
246         // If we have reached this point then we have encountered a bug.
247         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::str(): socket type %d is unknown!", family());
248         return "<unknown>";
249 }
250
251 socklen_t irc::sockets::sockaddrs::sa_size() const
252 {
253         switch (family())
254         {
255                 case AF_INET:
256                         return sizeof(in4);
257
258                 case AF_INET6:
259                         return sizeof(in6);
260
261                 case AF_UNIX:
262                         return sizeof(un);
263         }
264
265         // If we have reached this point then we have encountered a bug.
266         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::sa_size(): socket type %d is unknown!", family());
267         return 0;
268 }
269
270 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
271 {
272         if (family() != other.family())
273                 return false;
274
275         switch (family())
276         {
277                 case AF_INET:
278                         return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
279
280                 case AF_INET6:
281                         return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
282
283                 case AF_UNIX:
284                         return !strcmp(un.sun_path, other.un.sun_path);
285         }
286
287         // If we have reached this point then we have encountered a bug.
288         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::operator==(): socket type %d is unknown!", family());
289         return !memcmp(this, &other, sizeof(*this));
290 }
291
292 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
293 {
294         const unsigned char* base;
295         unsigned char target_byte;
296
297         memset(cidr.bits, 0, sizeof(cidr.bits));
298
299         cidr.type = sa.family();
300         switch (cidr.type)
301         {
302                 case AF_UNIX:
303                         // XXX: UNIX sockets don't support CIDR. This fix is non-ideal but I can't
304                         // really think of another way to handle it.
305                         cidr.length = 0;
306                         return;
307
308                 case AF_INET:
309                         cidr.length = range > 32 ? 32 : range;
310                         target_byte = sizeof(sa.in4.sin_addr);
311                         base = (unsigned char*)&sa.in4.sin_addr;
312                         break;
313
314                 case AF_INET6:
315                         cidr.length = range > 128 ? 128 : range;
316                         target_byte = sizeof(sa.in6.sin6_addr);
317                         base = (unsigned char*)&sa.in6.sin6_addr;
318                         break;
319
320                 default:
321                         // If we have reached this point then we have encountered a bug.
322                         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: sa2cidr(): socket type %d is unknown!", cidr.type);
323                         cidr.length = 0;
324                         return;
325         }
326
327         unsigned int border = cidr.length / 8;
328         unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
329         for(unsigned int i=0; i < target_byte; i++)
330         {
331                 if (i < border)
332                         cidr.bits[i] = base[i];
333                 else if (i == border)
334                         cidr.bits[i] = base[i] & bitmask;
335                 else
336                         return;
337         }
338 }
339
340 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, unsigned char range)
341 {
342         sa2cidr(*this, sa, range);
343 }
344
345 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
346 {
347         std::string::size_type bits_chars = mask.rfind('/');
348         irc::sockets::sockaddrs sa;
349
350         if (bits_chars == std::string::npos)
351         {
352                 irc::sockets::aptosa(mask, 0, sa);
353                 sa2cidr(*this, sa, 128);
354         }
355         else
356         {
357                 int range = ConvToInt(mask.substr(bits_chars + 1));
358                 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
359                 sa2cidr(*this, sa, range);
360         }
361 }
362
363 std::string irc::sockets::cidr_mask::str() const
364 {
365         irc::sockets::sockaddrs sa;
366         sa.sa.sa_family = type;
367
368         unsigned char* base;
369         size_t len;
370         switch (type)
371         {
372                 case AF_INET:
373                         base = (unsigned char*)&sa.in4.sin_addr;
374                         len = 4;
375                         break;
376
377                 case AF_INET6:
378                         base = (unsigned char*)&sa.in6.sin6_addr;
379                         len = 16;
380                         break;
381
382                 case AF_UNIX:
383                         return sa.un.sun_path;
384
385                 default:
386                         // If we have reached this point then we have encountered a bug.
387                         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::cidr_mask::str(): socket type %d is unknown!", type);
388                         return "<unknown>";
389         }
390
391         memcpy(base, bits, len);
392         return sa.addr() + "/" + ConvToStr((int)length);
393 }
394
395 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
396 {
397         return type == other.type && length == other.length &&
398                 0 == memcmp(bits, other.bits, 16);
399 }
400
401 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
402 {
403         if (type != other.type)
404                 return type < other.type;
405         if (length != other.length)
406                 return length < other.length;
407         return memcmp(bits, other.bits, 16) < 0;
408 }
409
410 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
411 {
412         if (addr.family() != type)
413                 return false;
414         irc::sockets::cidr_mask tmp(addr, length);
415         return tmp == *this;
416 }