]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
736e094865d35cdb36d6a299eeb4d4f674e91492
[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 size_t InspIRCd::BindPorts(FailedPortList& failed_ports)
58 {
59         size_t 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                         // Expand the path relative to the config directory.
102                         const std::string fullpath = ServerInstance->Config->Paths.PrependData(path);
103
104                         // UNIX socket paths are length limited to less than PATH_MAX.
105                         irc::sockets::sockaddrs bindspec;
106                         if (fullpath.length() > std::min(ServerInstance->Config->Limits.MaxHost, sizeof(bindspec.un.sun_path) - 1))
107                         {
108                                 this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path that is too long!",
109                                         fullpath.c_str(), tag->getTagLocation().c_str());
110                                 continue;
111                         }
112
113                         // Check for characters which are problematic in the IRC message format.
114                         if (fullpath.find_first_of("\n\r\t!@: ") != std::string::npos)
115                         {
116                                 this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path containing invalid characters!",
117                                         fullpath.c_str(), tag->getTagLocation().c_str());
118                                 continue;
119                         }
120
121                         irc::sockets::untosa(fullpath, bindspec);
122                         if (!BindPort(tag, bindspec, old_ports))
123                                 failed_ports.push_back(std::make_pair(bindspec, errno));
124                         else
125                                 bound++;
126                 }
127 #endif
128         }
129
130         std::vector<ListenSocket*>::iterator n = ports.begin();
131         for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
132         {
133                 while (n != ports.end() && *n != *o)
134                         n++;
135                 if (n == ports.end())
136                 {
137                         this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
138                         break;
139                 }
140
141                 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
142                         (**n).bind_sa.str().c_str());
143                 delete *n;
144
145                 // this keeps the iterator valid, pointing to the next element
146                 n = ports.erase(n);
147         }
148
149         return bound;
150 }
151
152 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
153 {
154         memset(&sa, 0, sizeof(sa));
155         if (addr.empty() || addr.c_str()[0] == '*')
156         {
157                 if (ServerInstance->Config->WildcardIPv6)
158                 {
159                         sa.in6.sin6_family = AF_INET6;
160                         sa.in6.sin6_port = htons(port);
161                 }
162                 else
163                 {
164                         sa.in4.sin_family = AF_INET;
165                         sa.in4.sin_port = htons(port);
166                 }
167                 return true;
168         }
169         else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
170         {
171                 sa.in4.sin_family = AF_INET;
172                 sa.in4.sin_port = htons(port);
173                 return true;
174         }
175         else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
176         {
177                 sa.in6.sin6_family = AF_INET6;
178                 sa.in6.sin6_port = htons(port);
179                 return true;
180         }
181         return false;
182 }
183
184 bool irc::sockets::untosa(const std::string& path, irc::sockets::sockaddrs& sa)
185 {
186         memset(&sa, 0, sizeof(sa));
187         if (path.length() >= sizeof(sa.un.sun_path))
188                 return false;
189
190         sa.un.sun_family = AF_UNIX;
191         memcpy(&sa.un.sun_path, path.c_str(), path.length() + 1);
192         return true;
193 }
194
195 bool irc::sockets::isunix(const std::string& file)
196 {
197 #ifndef _WIN32
198         struct stat sb;
199         if (stat(file.c_str(), &sb) == 0 && S_ISSOCK(sb.st_mode))
200                 return true;
201 #endif
202         return false;
203 }
204
205
206 int irc::sockets::sockaddrs::family() const
207 {
208         return sa.sa_family;
209 }
210
211 int irc::sockets::sockaddrs::port() const
212 {
213         switch (family())
214         {
215                 case AF_INET:
216                         return ntohs(in4.sin_port);
217
218                 case AF_INET6:
219                         return ntohs(in6.sin6_port);
220
221                 case AF_UNIX:
222                         return 0;
223         }
224
225         // If we have reached this point then we have encountered a bug.
226         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::port(): socket type %d is unknown!", family());
227         return 0;
228 }
229
230 std::string irc::sockets::sockaddrs::addr() const
231 {
232         switch (family())
233         {
234                 case AF_INET:
235                         char ip4addr[INET_ADDRSTRLEN];
236                         if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
237                                 return "0.0.0.0";
238                         return ip4addr;
239
240                 case AF_INET6:
241                         char ip6addr[INET6_ADDRSTRLEN];
242                         if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
243                                 return "0:0:0:0:0:0:0:0";
244                         return ip6addr;
245
246                 case AF_UNIX:
247                         return un.sun_path;
248         }
249
250         // If we have reached this point then we have encountered a bug.
251         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::addr(): socket type %d is unknown!", family());
252         return "<unknown>";
253 }
254
255 std::string irc::sockets::sockaddrs::str() const
256 {
257         switch (family())
258         {
259                 case AF_INET:
260                         char ip4addr[INET_ADDRSTRLEN];
261                         if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, ip4addr, sizeof(ip4addr)))
262                                 strcpy(ip4addr, "0.0.0.0");
263                         return InspIRCd::Format("%s:%u", ip4addr, ntohs(in4.sin_port));
264
265                 case AF_INET6:
266                         char ip6addr[INET6_ADDRSTRLEN];
267                         if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ip6addr, sizeof(ip6addr)))
268                                 strcpy(ip6addr, "0:0:0:0:0:0:0:0");
269                         return InspIRCd::Format("[%s]:%u", ip6addr, ntohs(in6.sin6_port));
270
271                 case AF_UNIX:
272                         return un.sun_path;
273         }
274
275         // If we have reached this point then we have encountered a bug.
276         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::str(): socket type %d is unknown!", family());
277         return "<unknown>";
278 }
279
280 socklen_t irc::sockets::sockaddrs::sa_size() const
281 {
282         switch (family())
283         {
284                 case AF_INET:
285                         return sizeof(in4);
286
287                 case AF_INET6:
288                         return sizeof(in6);
289
290                 case AF_UNIX:
291                         return sizeof(un);
292         }
293
294         // If we have reached this point then we have encountered a bug.
295         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::sa_size(): socket type %d is unknown!", family());
296         return 0;
297 }
298
299 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
300 {
301         if (family() != other.family())
302                 return false;
303
304         switch (family())
305         {
306                 case AF_INET:
307                         return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
308
309                 case AF_INET6:
310                         return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
311
312                 case AF_UNIX:
313                         return !strcmp(un.sun_path, other.un.sun_path);
314         }
315
316         // If we have reached this point then we have encountered a bug.
317         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::operator==(): socket type %d is unknown!", family());
318         return !memcmp(this, &other, sizeof(*this));
319 }
320
321 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
322 {
323         const unsigned char* base;
324         unsigned char target_byte;
325
326         memset(cidr.bits, 0, sizeof(cidr.bits));
327
328         cidr.type = sa.family();
329         switch (cidr.type)
330         {
331                 case AF_UNIX:
332                         // XXX: UNIX sockets don't support CIDR. This fix is non-ideal but I can't
333                         // really think of another way to handle it.
334                         cidr.length = 0;
335                         return;
336
337                 case AF_INET:
338                         cidr.length = range > 32 ? 32 : range;
339                         target_byte = sizeof(sa.in4.sin_addr);
340                         base = (unsigned char*)&sa.in4.sin_addr;
341                         break;
342
343                 case AF_INET6:
344                         cidr.length = range > 128 ? 128 : range;
345                         target_byte = sizeof(sa.in6.sin6_addr);
346                         base = (unsigned char*)&sa.in6.sin6_addr;
347                         break;
348
349                 default:
350                         // If we have reached this point then we have encountered a bug.
351                         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: sa2cidr(): socket type %d is unknown!", cidr.type);
352                         cidr.length = 0;
353                         return;
354         }
355
356         unsigned int border = cidr.length / 8;
357         unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
358         for(unsigned int i=0; i < target_byte; i++)
359         {
360                 if (i < border)
361                         cidr.bits[i] = base[i];
362                 else if (i == border)
363                         cidr.bits[i] = base[i] & bitmask;
364                 else
365                         return;
366         }
367 }
368
369 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, unsigned char range)
370 {
371         sa2cidr(*this, sa, range);
372 }
373
374 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
375 {
376         std::string::size_type bits_chars = mask.rfind('/');
377         irc::sockets::sockaddrs sa;
378
379         if (bits_chars == std::string::npos)
380         {
381                 irc::sockets::aptosa(mask, 0, sa);
382                 sa2cidr(*this, sa, 128);
383         }
384         else
385         {
386                 unsigned char range = ConvToNum<unsigned char>(mask.substr(bits_chars + 1));
387                 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
388                 sa2cidr(*this, sa, range);
389         }
390 }
391
392 std::string irc::sockets::cidr_mask::str() const
393 {
394         irc::sockets::sockaddrs sa;
395         sa.sa.sa_family = type;
396
397         unsigned char* base;
398         size_t len;
399         switch (type)
400         {
401                 case AF_INET:
402                         base = (unsigned char*)&sa.in4.sin_addr;
403                         len = 4;
404                         break;
405
406                 case AF_INET6:
407                         base = (unsigned char*)&sa.in6.sin6_addr;
408                         len = 16;
409                         break;
410
411                 case AF_UNIX:
412                         return sa.un.sun_path;
413
414                 default:
415                         // If we have reached this point then we have encountered a bug.
416                         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::cidr_mask::str(): socket type %d is unknown!", type);
417                         return "<unknown>";
418         }
419
420         memcpy(base, bits, len);
421         return sa.addr() + "/" + ConvToStr((int)length);
422 }
423
424 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
425 {
426         return type == other.type && length == other.length &&
427                 0 == memcmp(bits, other.bits, 16);
428 }
429
430 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
431 {
432         if (type != other.type)
433                 return type < other.type;
434         if (length != other.length)
435                 return length < other.length;
436         return memcmp(bits, other.bits, 16) < 0;
437 }
438
439 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
440 {
441         if (addr.family() != type)
442                 return false;
443         irc::sockets::cidr_mask tmp(addr, length);
444         return tmp == *this;
445 }