]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listensocket.cpp
Merge pull request #92 from Robby-/insp20-headers
[user/henk/code/inspircd.git] / src / listensocket.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "socket.h"
23 #include "socketengine.h"
24
25 ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to)
26         : bind_tag(tag)
27 {
28         irc::sockets::satoap(bind_to, bind_addr, bind_port);
29         bind_desc = irc::sockets::satouser(bind_to);
30
31         fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);
32
33         if (this->fd == -1)
34                 return;
35
36         ServerInstance->SE->SetReuse(fd);
37         int rv = ServerInstance->SE->Bind(this->fd, bind_to);
38         if (rv >= 0)
39                 rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);
40
41 #ifdef IPV6_V6ONLY
42         /* This OS supports IPv6 sockets that can also listen for IPv4
43          * connections. If our address is "*" or empty, enable both v4 and v6 to
44          * allow for simpler configuration on dual-stack hosts. Otherwise, if it
45          * is "::" or an IPv6 address, disable support so that an IPv4 bind will
46          * work on the port (by us or another application).
47          */
48         if (bind_to.sa.sa_family == AF_INET6)
49         {
50                 std::string addr = tag->getString("address");
51                 const char enable = (addr.empty() || addr == "*") ? 0 : 1;
52                 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable));
53                 // errors ignored intentionally
54         }
55 #endif
56
57         if (rv < 0)
58         {
59                 int errstore = errno;
60                 ServerInstance->SE->Shutdown(this, 2);
61                 ServerInstance->SE->Close(this);
62                 this->fd = -1;
63                 errno = errstore;
64         }
65         else
66         {
67                 ServerInstance->SE->NonBlocking(this->fd);
68                 ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
69         }
70 }
71
72 ListenSocket::~ListenSocket()
73 {
74         if (this->GetFd() > -1)
75         {
76                 ServerInstance->SE->DelFd(this);
77                 ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
78                 if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
79                         ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
80                 this->fd = -1;
81         }
82 }
83
84 /* Just seperated into another func for tidiness really.. */
85 void ListenSocket::AcceptInternal()
86 {
87         irc::sockets::sockaddrs client;
88         irc::sockets::sockaddrs server;
89
90         socklen_t length = sizeof(client);
91         int incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
92
93         ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensocket %s nfd=%d", bind_desc.c_str(), incomingSockfd);
94         if (incomingSockfd < 0)
95         {
96                 ServerInstance->stats->statsRefused++;
97                 return;
98         }
99
100         socklen_t sz = sizeof(server);
101         if (getsockname(incomingSockfd, &server.sa, &sz))
102         {
103                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
104                 irc::sockets::aptosa(bind_addr, bind_port, server);
105         }
106
107         /*
108          * XXX -
109          * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
110          * its a pretty big but for the moment valid assumption:
111          * file descriptors are handed out starting at 0, and are recycled as theyre freed.
112          * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
113          * irc server at once (or the irc server otherwise initiating this many connections, files etc)
114          * which for the time being is a physical impossibility (even the largest networks dont have more
115          * than about 10,000 users on ONE server!)
116          */
117         if (incomingSockfd >= ServerInstance->SE->GetMaxFds())
118         {
119                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Server is full");
120                 ServerInstance->SE->Shutdown(incomingSockfd, 2);
121                 ServerInstance->SE->Close(incomingSockfd);
122                 ServerInstance->stats->statsRefused++;
123                 return;
124         }
125
126         if (client.sa.sa_family == AF_INET6)
127         {
128                 /*
129                  * This case is the be all and end all patch to catch and nuke 4in6
130                  * instead of special-casing shit all over the place and wreaking merry
131                  * havoc with crap, instead, we just recreate sockaddr and strip ::ffff: prefix
132                  * if it's a 4in6 IP.
133                  *
134                  * This is, of course, much improved over the older way of handling this
135                  * (pretend it doesn't exist + hack around it -- yes, both were done!)
136                  *
137                  * Big, big thanks to danieldg for his work on this.
138                  * -- w00t
139                  */
140                 static const unsigned char prefix4in6[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xFF,0xFF };
141                 if (!memcmp(prefix4in6, &client.in6.sin6_addr, 12))
142                 {
143                         // recreate as a sockaddr_in using the IPv4 IP
144                         uint16_t sport = client.in6.sin6_port;
145                         uint32_t addr = *reinterpret_cast<uint32_t*>(client.in6.sin6_addr.s6_addr + 12);
146                         client.in4.sin_family = AF_INET;
147                         client.in4.sin_port = sport;
148                         client.in4.sin_addr.s_addr = addr;
149
150                         sport = server.in6.sin6_port;
151                         addr = *reinterpret_cast<uint32_t*>(server.in6.sin6_addr.s6_addr + 12);
152                         server.in4.sin_family = AF_INET;
153                         server.in4.sin_port = sport;
154                         server.in4.sin_addr.s_addr = addr;
155                 }
156         }
157
158         ServerInstance->SE->NonBlocking(incomingSockfd);
159
160         ModResult res;
161         FIRST_MOD_RESULT(OnAcceptConnection, res, (incomingSockfd, this, &client, &server));
162         if (res == MOD_RES_PASSTHRU)
163         {
164                 std::string type = bind_tag->getString("type", "clients");
165                 if (type == "clients")
166                 {
167                         ServerInstance->Users->AddUser(incomingSockfd, this, &client, &server);
168                         res = MOD_RES_ALLOW;
169                 }
170         }
171         if (res == MOD_RES_ALLOW)
172         {
173                 ServerInstance->stats->statsAccept++;
174         }
175         else
176         {
177                 ServerInstance->stats->statsRefused++;
178                 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Refusing connection on %s - %s",
179                         bind_desc.c_str(), res == MOD_RES_DENY ? "Connection refused by module" : "Module for this port not found");
180                 ServerInstance->SE->Close(incomingSockfd);
181         }
182 }
183
184 void ListenSocket::HandleEvent(EventType e, int err)
185 {
186         switch (e)
187         {
188                 case EVENT_ERROR:
189                         ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
190                         break;
191                 case EVENT_WRITE:
192                         ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
193                         break;
194                 case EVENT_READ:
195                         this->AcceptInternal();
196                         break;
197         }
198 }