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