]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/listensocket.cpp
Use a set to mark client SSL ports rather than going by textual IP/port pairs
[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                 irc::sockets::satoap(&bind_to, bind_addr, bind_port);
32                 bind_desc = irc::sockets::satouser(&bind_to);
33         }
34         else
35         {
36                 bind_addr = addr;
37                 bind_port = port;
38                 bind_desc = addr + ":" + ConvToStr(port);
39         }
40
41         this->SetFd(irc::sockets::OpenTCPSocket(bind_addr.c_str()));
42         if (this->GetFd() > -1)
43         {
44                 if (!Instance->BindSocket(this->fd,port,bind_addr.c_str()))
45                         this->fd = -1;
46                 Instance->SE->AddFd(this);
47         }
48 }
49
50 ListenSocketBase::~ListenSocketBase()
51 {
52         if (this->GetFd() > -1)
53         {
54                 ServerInstance->SE->DelFd(this);
55                 ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
56                 if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
57                         ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
58                 this->fd = -1;
59         }
60 }
61
62 /* Just seperated into another func for tidiness really.. */
63 void ListenSocketBase::AcceptInternal()
64 {
65         ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
66         int incomingSockfd;
67
68         socklen_t length = sizeof(client);
69         incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
70
71         if (incomingSockfd < 0)
72         {
73                 ServerInstance->SE->Shutdown(incomingSockfd, 2);
74                 ServerInstance->SE->Close(incomingSockfd);
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         ServerInstance->stats->statsAccept++;
136         this->OnAcceptReady(incomingSockfd);
137 }
138
139 void ListenSocketBase::HandleEvent(EventType e, int err)
140 {
141         switch (e)
142         {
143                 case EVENT_ERROR:
144                         ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
145                         break;
146                 case EVENT_WRITE:
147                         ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
148                         break;
149                 case EVENT_READ:
150                         this->AcceptInternal();
151                         break;
152         }
153 }
154
155 void ClientListenSocket::OnAcceptReady(int nfd)
156 {
157         ServerInstance->Users->AddUser(ServerInstance, nfd, this, &client, &server);
158 }