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"
26 #include <netinet/tcp.h>
29 ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to)
31 , iohookprov(NULL, std::string())
33 irc::sockets::satoap(bind_to, bind_addr, bind_port);
34 bind_desc = bind_to.str();
36 fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);
42 /* This OS supports IPv6 sockets that can also listen for IPv4
43 * connections. If our address is "*" or empty, enable both v4 and v6 to
44 * allow for simpler configuration on dual-stack hosts. Otherwise, if it
45 * is "::" or an IPv6 address, disable support so that an IPv4 bind will
46 * work on the port (by us or another application).
48 if (bind_to.sa.sa_family == AF_INET6)
50 std::string addr = tag->getString("address");
51 /* This must be >= sizeof(DWORD) on Windows */
52 const int enable = (addr.empty() || addr == "*") ? 0 : 1;
53 /* This must be before bind() */
54 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&enable), sizeof(enable));
55 // errors ignored intentionally
59 SocketEngine::SetReuse(fd);
60 int rv = SocketEngine::Bind(this->fd, bind_to);
62 rv = SocketEngine::Listen(this->fd, ServerInstance->Config->MaxConn);
64 int timeout = tag->getInt("defer", 0);
67 #if defined TCP_DEFER_ACCEPT
68 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, sizeof(timeout));
69 #elif defined SO_ACCEPTFILTER
70 struct accept_filter_arg afa;
71 memset(&afa, 0, sizeof(afa));
72 strcpy(afa.af_name, "dataready");
73 setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
80 SocketEngine::Shutdown(this, 2);
81 SocketEngine::Close(this->GetFd());
87 SocketEngine::NonBlocking(this->fd);
88 SocketEngine::AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
90 this->ResetIOHookProvider();
94 ListenSocket::~ListenSocket()
96 if (this->GetFd() > -1)
98 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Shut down listener on fd %d", this->fd);
99 SocketEngine::Shutdown(this, 2);
100 if (SocketEngine::Close(this) != 0)
101 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Failed to cancel listener: %s", strerror(errno));
105 /* Just seperated into another func for tidiness really.. */
106 void ListenSocket::AcceptInternal()
108 irc::sockets::sockaddrs client;
109 irc::sockets::sockaddrs server;
111 socklen_t length = sizeof(client);
112 int incomingSockfd = SocketEngine::Accept(this, &client.sa, &length);
114 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "HandleEvent for Listensocket %s nfd=%d", bind_desc.c_str(), incomingSockfd);
115 if (incomingSockfd < 0)
117 ServerInstance->stats->statsRefused++;
121 socklen_t sz = sizeof(server);
122 if (getsockname(incomingSockfd, &server.sa, &sz))
124 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Can't get peername: %s", strerror(errno));
125 irc::sockets::aptosa(bind_addr, bind_port, server);
128 if (client.sa.sa_family == AF_INET6)
131 * This case is the be all and end all patch to catch and nuke 4in6
132 * instead of special-casing shit all over the place and wreaking merry
133 * havoc with crap, instead, we just recreate sockaddr and strip ::ffff: prefix
136 * This is, of course, much improved over the older way of handling this
137 * (pretend it doesn't exist + hack around it -- yes, both were done!)
139 * Big, big thanks to danieldg for his work on this.
142 static const unsigned char prefix4in6[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xFF,0xFF };
143 if (!memcmp(prefix4in6, &client.in6.sin6_addr, 12))
145 // recreate as a sockaddr_in using the IPv4 IP
146 uint16_t sport = client.in6.sin6_port;
147 client.in4.sin_family = AF_INET;
148 client.in4.sin_port = sport;
149 memcpy(&client.in4.sin_addr.s_addr, client.in6.sin6_addr.s6_addr + 12, sizeof(uint32_t));
151 sport = server.in6.sin6_port;
152 server.in4.sin_family = AF_INET;
153 server.in4.sin_port = sport;
154 memcpy(&server.in4.sin_addr.s_addr, server.in6.sin6_addr.s6_addr + 12, sizeof(uint32_t));
158 SocketEngine::NonBlocking(incomingSockfd);
161 FIRST_MOD_RESULT(OnAcceptConnection, res, (incomingSockfd, this, &client, &server));
162 if (res == MOD_RES_PASSTHRU)
164 std::string type = bind_tag->getString("type", "clients");
165 if (type == "clients")
167 ServerInstance->Users->AddUser(incomingSockfd, this, &client, &server);
171 if (res == MOD_RES_ALLOW)
173 ServerInstance->stats->statsAccept++;
177 ServerInstance->stats->statsRefused++;
178 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Refusing connection on %s - %s",
179 bind_desc.c_str(), res == MOD_RES_DENY ? "Connection refused by module" : "Module for this port not found");
180 SocketEngine::Close(incomingSockfd);
184 void ListenSocket::HandleEvent(EventType e, int err)
189 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
192 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
195 this->AcceptInternal();
200 bool ListenSocket::ResetIOHookProvider()
202 std::string provname = bind_tag->getString("ssl");
203 if (!provname.empty())
204 provname.insert(0, "ssl/");
206 // Set the new provider name, dynref handles the rest
207 iohookprov.SetProvider(provname);
209 // Return true if no provider was set, or one was set and it was also found
210 return (provname.empty() || iohookprov);