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