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