]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listensocket.cpp
Fix m_ident deleting sockets that are still in use
[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         if (rv < 0)
35         {
36                 int errstore = errno;
37                 ServerInstance->SE->Shutdown(this, 2);
38                 ServerInstance->SE->Close(this);
39                 this->fd = -1;
40                 errno = errstore;
41         }
42         else
43         {
44                 ServerInstance->SE->NonBlocking(this->fd);
45                 ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
46         }
47 }
48
49 ListenSocket::~ListenSocket()
50 {
51         if (this->GetFd() > -1)
52         {
53                 ServerInstance->SE->DelFd(this);
54                 ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
55                 if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
56                         ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
57                 this->fd = -1;
58         }
59 }
60
61 /* Just seperated into another func for tidiness really.. */
62 void ListenSocket::AcceptInternal()
63 {
64         irc::sockets::sockaddrs client;
65         irc::sockets::sockaddrs server;
66
67         socklen_t length = sizeof(client);
68         int incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
69
70         ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensocket %s nfd=%d", bind_desc.c_str(), incomingSockfd);
71         if (incomingSockfd < 0)
72         {
73                 ServerInstance->stats->statsRefused++;
74                 return;
75         }
76
77         socklen_t sz = sizeof(server);
78         if (getsockname(incomingSockfd, &server.sa, &sz))
79         {
80                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
81                 irc::sockets::aptosa(bind_addr, bind_port, server);
82         }
83
84         /*
85          * XXX -
86          * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
87          * its a pretty big but for the moment valid assumption:
88          * file descriptors are handed out starting at 0, and are recycled as theyre freed.
89          * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
90          * irc server at once (or the irc server otherwise initiating this many connections, files etc)
91          * which for the time being is a physical impossibility (even the largest networks dont have more
92          * than about 10,000 users on ONE server!)
93          */
94         if (incomingSockfd >= ServerInstance->SE->GetMaxFds())
95         {
96                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Server is full");
97                 ServerInstance->SE->Shutdown(incomingSockfd, 2);
98                 ServerInstance->SE->Close(incomingSockfd);
99                 ServerInstance->stats->statsRefused++;
100                 return;
101         }
102
103         if (client.sa.sa_family == AF_INET6)
104         {
105                 /*
106                  * This case is the be all and end all patch to catch and nuke 4in6
107                  * instead of special-casing shit all over the place and wreaking merry
108                  * havoc with crap, instead, we just recreate sockaddr and strip ::ffff: prefix
109                  * if it's a 4in6 IP.
110                  *
111                  * This is, of course, much improved over the older way of handling this
112                  * (pretend it doesn't exist + hack around it -- yes, both were done!)
113                  *
114                  * Big, big thanks to danieldg for his work on this.
115                  * -- w00t
116                  */
117                 static const unsigned char prefix4in6[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xFF,0xFF };
118                 if (!memcmp(prefix4in6, &client.in6.sin6_addr, 12))
119                 {
120                         // recreate as a sockaddr_in using the IPv4 IP
121                         uint16_t sport = client.in6.sin6_port;
122                         uint32_t addr = *reinterpret_cast<uint32_t*>(client.in6.sin6_addr.s6_addr + 12);
123                         client.in4.sin_family = AF_INET;
124                         client.in4.sin_port = sport;
125                         client.in4.sin_addr.s_addr = addr;
126
127                         sport = server.in6.sin6_port;
128                         addr = *reinterpret_cast<uint32_t*>(server.in6.sin6_addr.s6_addr + 12);
129                         server.in4.sin_family = AF_INET;
130                         server.in4.sin_port = sport;
131                         server.in4.sin_addr.s_addr = addr;
132                 }
133         }
134
135         ServerInstance->SE->NonBlocking(incomingSockfd);
136
137         ModResult res;
138         FIRST_MOD_RESULT(OnAcceptConnection, res, (incomingSockfd, this, &client, &server));
139         if (res == MOD_RES_PASSTHRU)
140         {
141                 std::string type = bind_tag->getString("type", "clients");
142                 if (type == "clients")
143                 {
144                         ServerInstance->Users->AddUser(incomingSockfd, this, &client, &server);
145                         res = MOD_RES_ALLOW;
146                 }
147         }
148         if (res == MOD_RES_ALLOW)
149         {
150                 ServerInstance->stats->statsAccept++;
151         }
152         else
153         {
154                 ServerInstance->stats->statsRefused++;
155                 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Refusing connection on %s - %s",
156                         bind_desc.c_str(), res == MOD_RES_DENY ? "Connection refused by module" : "Module for this port not found");
157                 ServerInstance->SE->Close(incomingSockfd);
158         }
159 }
160
161 void ListenSocket::HandleEvent(EventType e, int err)
162 {
163         switch (e)
164         {
165                 case EVENT_ERROR:
166                         ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
167                         break;
168                 case EVENT_WRITE:
169                         ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
170                         break;
171                 case EVENT_READ:
172                         this->AcceptInternal();
173                         break;
174         }
175 }