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