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