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