2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "socketengine.h"
25 ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to)
28 irc::sockets::satoap(bind_to, bind_addr, bind_port);
29 bind_desc = irc::sockets::satouser(bind_to);
31 fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);
37 /* This OS supports IPv6 sockets that can also listen for IPv4
38 * connections. If our address is "*" or empty, enable both v4 and v6 to
39 * allow for simpler configuration on dual-stack hosts. Otherwise, if it
40 * is "::" or an IPv6 address, disable support so that an IPv4 bind will
41 * work on the port (by us or another application).
43 if (bind_to.sa.sa_family == AF_INET6)
45 std::string addr = tag->getString("address");
46 /* This must be >= sizeof(DWORD) on Windows */
47 const int enable = (addr.empty() || addr == "*") ? 0 : 1;
48 /* This must be before bind() */
49 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&enable), sizeof(enable));
50 // errors ignored intentionally
54 ServerInstance->SE->SetReuse(fd);
55 int rv = ServerInstance->SE->Bind(this->fd, bind_to);
57 rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);
62 ServerInstance->SE->Shutdown(this, 2);
63 ServerInstance->SE->Close(this);
69 ServerInstance->SE->NonBlocking(this->fd);
70 ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
74 ListenSocket::~ListenSocket()
76 if (this->GetFd() > -1)
78 ServerInstance->SE->DelFd(this);
79 ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
80 ServerInstance->SE->Shutdown(this, 2);
81 if (ServerInstance->SE->Close(this) != 0)
82 ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
87 /* Just seperated into another func for tidiness really.. */
88 void ListenSocket::AcceptInternal()
90 irc::sockets::sockaddrs client;
91 irc::sockets::sockaddrs server;
93 socklen_t length = sizeof(client);
94 int incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
96 ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensocket %s nfd=%d", bind_desc.c_str(), incomingSockfd);
97 if (incomingSockfd < 0)
99 ServerInstance->stats->statsRefused++;
103 socklen_t sz = sizeof(server);
104 if (getsockname(incomingSockfd, &server.sa, &sz))
106 ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
107 irc::sockets::aptosa(bind_addr, bind_port, server);
112 * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
113 * its a pretty big but for the moment valid assumption:
114 * file descriptors are handed out starting at 0, and are recycled as theyre freed.
115 * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
116 * irc server at once (or the irc server otherwise initiating this many connections, files etc)
117 * which for the time being is a physical impossibility (even the largest networks dont have more
118 * than about 10,000 users on ONE server!)
120 if (incomingSockfd >= ServerInstance->SE->GetMaxFds())
122 ServerInstance->Logs->Log("SOCKET", DEBUG, "Server is full");
123 ServerInstance->SE->Shutdown(incomingSockfd, 2);
124 ServerInstance->SE->Close(incomingSockfd);
125 ServerInstance->stats->statsRefused++;
129 if (client.sa.sa_family == AF_INET6)
132 * This case is the be all and end all patch to catch and nuke 4in6
133 * instead of special-casing shit all over the place and wreaking merry
134 * havoc with crap, instead, we just recreate sockaddr and strip ::ffff: prefix
137 * This is, of course, much improved over the older way of handling this
138 * (pretend it doesn't exist + hack around it -- yes, both were done!)
140 * Big, big thanks to danieldg for his work on this.
143 static const unsigned char prefix4in6[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xFF,0xFF };
144 if (!memcmp(prefix4in6, &client.in6.sin6_addr, 12))
146 // recreate as a sockaddr_in using the IPv4 IP
147 uint16_t sport = client.in6.sin6_port;
148 client.in4.sin_family = AF_INET;
149 client.in4.sin_port = sport;
150 memcpy(&client.in4.sin_addr.s_addr, client.in6.sin6_addr.s6_addr + 12, sizeof(uint32_t));
152 sport = server.in6.sin6_port;
153 server.in4.sin_family = AF_INET;
154 server.in4.sin_port = sport;
155 memcpy(&server.in4.sin_addr.s_addr, server.in6.sin6_addr.s6_addr + 12, sizeof(uint32_t));
159 ServerInstance->SE->NonBlocking(incomingSockfd);
162 FIRST_MOD_RESULT(OnAcceptConnection, res, (incomingSockfd, this, &client, &server));
163 if (res == MOD_RES_PASSTHRU)
165 std::string type = bind_tag->getString("type", "clients");
166 if (type == "clients")
168 ServerInstance->Users->AddUser(incomingSockfd, this, &client, &server);
172 if (res == MOD_RES_ALLOW)
174 ServerInstance->stats->statsAccept++;
178 ServerInstance->stats->statsRefused++;
179 ServerInstance->Logs->Log("SOCKET",DEFAULT,"Refusing connection on %s - %s",
180 bind_desc.c_str(), res == MOD_RES_DENY ? "Connection refused by module" : "Module for this port not found");
181 ServerInstance->SE->Close(incomingSockfd);
185 void ListenSocket::HandleEvent(EventType e, int err)
190 ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
193 ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
196 this->AcceptInternal();