]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Rename the spanningtree module header to server.
[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 int InspIRCd::BindPorts(FailedPortList &failed_ports)
27 {
28         int bound = 0;
29         std::vector<ListenSocket*> old_ports(ports.begin(), ports.end());
30
31         ConfigTagList tags = ServerInstance->Config->ConfTags("bind");
32         for(ConfigIter i = tags.first; i != tags.second; ++i)
33         {
34                 ConfigTag* tag = i->second;
35                 std::string porttag = tag->getString("port");
36                 std::string Addr = tag->getString("address");
37
38                 if (strncasecmp(Addr.c_str(), "::ffff:", 7) == 0)
39                         this->Logs->Log("SOCKET", LOG_DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
40
41                 irc::portparser portrange(porttag, false);
42                 int portno = -1;
43                 while (0 != (portno = portrange.GetToken()))
44                 {
45                         irc::sockets::sockaddrs bindspec;
46                         if (!irc::sockets::aptosa(Addr, portno, bindspec))
47                                 continue;
48
49                         bool skip = false;
50                         for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
51                         {
52                                 if ((**n).bind_sa == bindspec)
53                                 {
54                                         (*n)->bind_tag = tag; // Replace tag, we know addr and port match, but other info (type, ssl) may not
55                                         (*n)->ResetIOHookProvider();
56
57                                         skip = true;
58                                         old_ports.erase(n);
59                                         break;
60                                 }
61                         }
62                         if (!skip)
63                         {
64                                 ListenSocket* ll = new ListenSocket(tag, bindspec);
65
66                                 if (ll->GetFd() > -1)
67                                 {
68                                         bound++;
69                                         ports.push_back(ll);
70                                 }
71                                 else
72                                 {
73                                         failed_ports.push_back(std::make_pair(bindspec, errno));
74                                         delete ll;
75                                 }
76                         }
77                 }
78         }
79
80         std::vector<ListenSocket*>::iterator n = ports.begin();
81         for (std::vector<ListenSocket*>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
82         {
83                 while (n != ports.end() && *n != *o)
84                         n++;
85                 if (n == ports.end())
86                 {
87                         this->Logs->Log("SOCKET", LOG_DEFAULT, "Port bindings slipped out of vector, aborting close!");
88                         break;
89                 }
90
91                 this->Logs->Log("SOCKET", LOG_DEFAULT, "Port binding %s was removed from the config file, closing.",
92                         (**n).bind_sa.str().c_str());
93                 delete *n;
94
95                 // this keeps the iterator valid, pointing to the next element
96                 n = ports.erase(n);
97         }
98
99         return bound;
100 }
101
102 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa)
103 {
104         memset(&sa, 0, sizeof(sa));
105         if (addr.empty() || addr.c_str()[0] == '*')
106         {
107                 if (ServerInstance->Config->WildcardIPv6)
108                 {
109                         sa.in6.sin6_family = AF_INET6;
110                         sa.in6.sin6_port = htons(port);
111                 }
112                 else
113                 {
114                         sa.in4.sin_family = AF_INET;
115                         sa.in4.sin_port = htons(port);
116                 }
117                 return true;
118         }
119         else if (inet_pton(AF_INET, addr.c_str(), &sa.in4.sin_addr) > 0)
120         {
121                 sa.in4.sin_family = AF_INET;
122                 sa.in4.sin_port = htons(port);
123                 return true;
124         }
125         else if (inet_pton(AF_INET6, addr.c_str(), &sa.in6.sin6_addr) > 0)
126         {
127                 sa.in6.sin6_family = AF_INET6;
128                 sa.in6.sin6_port = htons(port);
129                 return true;
130         }
131         return false;
132 }
133
134 int irc::sockets::sockaddrs::port() const
135 {
136         if (sa.sa_family == AF_INET)
137                 return ntohs(in4.sin_port);
138         if (sa.sa_family == AF_INET6)
139                 return ntohs(in6.sin6_port);
140         return -1;
141 }
142
143 std::string irc::sockets::sockaddrs::addr() const
144 {
145         char addrv[INET6_ADDRSTRLEN+1];
146         if (sa.sa_family == AF_INET)
147         {
148                 if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, addrv, sizeof(addrv)))
149                         return "";
150                 return addrv;
151         }
152         else if (sa.sa_family == AF_INET6)
153         {
154                 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, addrv, sizeof(addrv)))
155                         return "";
156                 return addrv;
157         }
158         return "";
159 }
160
161 std::string irc::sockets::sockaddrs::str() const
162 {
163         if (sa.sa_family == AF_INET)
164         {
165                 char ipaddr[INET_ADDRSTRLEN];
166                 inet_ntop(AF_INET, (void*)&in4.sin_addr, ipaddr, sizeof(ipaddr));
167                 return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
168         }
169
170         if (sa.sa_family == AF_INET6)
171         {
172                 char ipaddr[INET6_ADDRSTRLEN];
173                 inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ipaddr, sizeof(ipaddr));
174                 return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
175         }
176
177         // This should never happen.
178         return "<unknown>";
179 }
180
181 socklen_t irc::sockets::sockaddrs::sa_size() const
182 {
183         if (sa.sa_family == AF_INET)
184                 return sizeof(in4);
185         if (sa.sa_family == AF_INET6)
186                 return sizeof(in6);
187         return 0;
188 }
189
190 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
191 {
192         if (sa.sa_family != other.sa.sa_family)
193                 return false;
194         if (sa.sa_family == AF_INET)
195                 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
196         if (sa.sa_family == AF_INET6)
197                 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
198         return !memcmp(this, &other, sizeof(*this));
199 }
200
201 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
202 {
203         const unsigned char* base;
204         unsigned char target_byte;
205         cidr.type = sa.sa.sa_family;
206
207         memset(cidr.bits, 0, sizeof(cidr.bits));
208
209         if (cidr.type == AF_INET)
210         {
211                 target_byte = sizeof(sa.in4.sin_addr);
212                 base = (unsigned char*)&sa.in4.sin_addr;
213                 if (range > 32)
214                         range = 32;
215         }
216         else if (cidr.type == AF_INET6)
217         {
218                 target_byte = sizeof(sa.in6.sin6_addr);
219                 base = (unsigned char*)&sa.in6.sin6_addr;
220                 if (range > 128)
221                         range = 128;
222         }
223         else
224         {
225                 cidr.length = 0;
226                 return;
227         }
228         cidr.length = range;
229         unsigned int border = range / 8;
230         unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
231         for(unsigned int i=0; i < target_byte; i++)
232         {
233                 if (i < border)
234                         cidr.bits[i] = base[i];
235                 else if (i == border)
236                         cidr.bits[i] = base[i] & bitmask;
237                 else
238                         return;
239         }
240 }
241
242 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, unsigned char range)
243 {
244         sa2cidr(*this, sa, range);
245 }
246
247 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
248 {
249         std::string::size_type bits_chars = mask.rfind('/');
250         irc::sockets::sockaddrs sa;
251
252         if (bits_chars == std::string::npos)
253         {
254                 irc::sockets::aptosa(mask, 0, sa);
255                 sa2cidr(*this, sa, 128);
256         }
257         else
258         {
259                 int range = ConvToInt(mask.substr(bits_chars + 1));
260                 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
261                 sa2cidr(*this, sa, range);
262         }
263 }
264
265 std::string irc::sockets::cidr_mask::str() const
266 {
267         irc::sockets::sockaddrs sa;
268         sa.sa.sa_family = type;
269         unsigned char* base;
270         size_t len;
271         if (type == AF_INET)
272         {
273                 base = (unsigned char*)&sa.in4.sin_addr;
274                 len = 4;
275         }
276         else if (type == AF_INET6)
277         {
278                 base = (unsigned char*)&sa.in6.sin6_addr;
279                 len = 16;
280         }
281         else
282                 return "";
283         memcpy(base, bits, len);
284         return sa.addr() + "/" + ConvToStr((int)length);
285 }
286
287 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
288 {
289         return type == other.type && length == other.length &&
290                 0 == memcmp(bits, other.bits, 16);
291 }
292
293 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
294 {
295         if (type != other.type)
296                 return type < other.type;
297         if (length != other.length)
298                 return length < other.length;
299         return memcmp(bits, other.bits, 16) < 0;
300 }
301
302 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
303 {
304         if (addr.sa.sa_family != type)
305                 return false;
306         irc::sockets::cidr_mask tmp(addr, length);
307         return tmp == *this;
308 }