]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Add the family() member to the sockaddrs union.
[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::family() const
135 {
136         return sa.sa_family;
137 }
138
139 int irc::sockets::sockaddrs::port() const
140 {
141         if (family() == AF_INET)
142                 return ntohs(in4.sin_port);
143         if (family() == AF_INET6)
144                 return ntohs(in6.sin6_port);
145         return -1;
146 }
147
148 std::string irc::sockets::sockaddrs::addr() const
149 {
150         char addrv[INET6_ADDRSTRLEN+1];
151         if (family() == AF_INET)
152         {
153                 if (!inet_ntop(AF_INET, (void*)&in4.sin_addr, addrv, sizeof(addrv)))
154                         return "";
155                 return addrv;
156         }
157         else if (family() == AF_INET6)
158         {
159                 if (!inet_ntop(AF_INET6, (void*)&in6.sin6_addr, addrv, sizeof(addrv)))
160                         return "";
161                 return addrv;
162         }
163         return "";
164 }
165
166 std::string irc::sockets::sockaddrs::str() const
167 {
168         if (family() == AF_INET)
169         {
170                 char ipaddr[INET_ADDRSTRLEN];
171                 inet_ntop(AF_INET, (void*)&in4.sin_addr, ipaddr, sizeof(ipaddr));
172                 return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
173         }
174
175         if (family() == AF_INET6)
176         {
177                 char ipaddr[INET6_ADDRSTRLEN];
178                 inet_ntop(AF_INET6, (void*)&in6.sin6_addr, ipaddr, sizeof(ipaddr));
179                 return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
180         }
181
182         // This should never happen.
183         return "<unknown>";
184 }
185
186 socklen_t irc::sockets::sockaddrs::sa_size() const
187 {
188         if (family() == AF_INET)
189                 return sizeof(in4);
190         if (family() == AF_INET6)
191                 return sizeof(in6);
192         return 0;
193 }
194
195 bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
196 {
197         if (family() != other.family())
198                 return false;
199         if (family() ==  AF_INET)
200                 return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);
201         if (family() ==  AF_INET6)
202                 return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);
203         return !memcmp(this, &other, sizeof(*this));
204 }
205
206 static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
207 {
208         const unsigned char* base;
209         unsigned char target_byte;
210         cidr.type = sa.family();
211
212         memset(cidr.bits, 0, sizeof(cidr.bits));
213
214         if (cidr.type == AF_INET)
215         {
216                 target_byte = sizeof(sa.in4.sin_addr);
217                 base = (unsigned char*)&sa.in4.sin_addr;
218                 if (range > 32)
219                         range = 32;
220         }
221         else if (cidr.type == AF_INET6)
222         {
223                 target_byte = sizeof(sa.in6.sin6_addr);
224                 base = (unsigned char*)&sa.in6.sin6_addr;
225                 if (range > 128)
226                         range = 128;
227         }
228         else
229         {
230                 cidr.length = 0;
231                 return;
232         }
233         cidr.length = range;
234         unsigned int border = range / 8;
235         unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
236         for(unsigned int i=0; i < target_byte; i++)
237         {
238                 if (i < border)
239                         cidr.bits[i] = base[i];
240                 else if (i == border)
241                         cidr.bits[i] = base[i] & bitmask;
242                 else
243                         return;
244         }
245 }
246
247 irc::sockets::cidr_mask::cidr_mask(const irc::sockets::sockaddrs& sa, unsigned char range)
248 {
249         sa2cidr(*this, sa, range);
250 }
251
252 irc::sockets::cidr_mask::cidr_mask(const std::string& mask)
253 {
254         std::string::size_type bits_chars = mask.rfind('/');
255         irc::sockets::sockaddrs sa;
256
257         if (bits_chars == std::string::npos)
258         {
259                 irc::sockets::aptosa(mask, 0, sa);
260                 sa2cidr(*this, sa, 128);
261         }
262         else
263         {
264                 int range = ConvToInt(mask.substr(bits_chars + 1));
265                 irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa);
266                 sa2cidr(*this, sa, range);
267         }
268 }
269
270 std::string irc::sockets::cidr_mask::str() const
271 {
272         irc::sockets::sockaddrs sa;
273         sa.sa.sa_family = type;
274         unsigned char* base;
275         size_t len;
276         if (type == AF_INET)
277         {
278                 base = (unsigned char*)&sa.in4.sin_addr;
279                 len = 4;
280         }
281         else if (type == AF_INET6)
282         {
283                 base = (unsigned char*)&sa.in6.sin6_addr;
284                 len = 16;
285         }
286         else
287                 return "";
288         memcpy(base, bits, len);
289         return sa.addr() + "/" + ConvToStr((int)length);
290 }
291
292 bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const
293 {
294         return type == other.type && length == other.length &&
295                 0 == memcmp(bits, other.bits, 16);
296 }
297
298 bool irc::sockets::cidr_mask::operator<(const cidr_mask& other) const
299 {
300         if (type != other.type)
301                 return type < other.type;
302         if (length != other.length)
303                 return length < other.length;
304         return memcmp(bits, other.bits, 16) < 0;
305 }
306
307 bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
308 {
309         if (addr.family() != type)
310                 return false;
311         irc::sockets::cidr_mask tmp(addr, length);
312         return tmp == *this;
313 }