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/>.
25 #include <netinet/tcp.h>
28 ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to)
31 bind_addr = bind_to.addr();
32 bind_port = bind_to.port();
33 bind_desc = bind_to.str();
35 fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);
41 /* This OS supports IPv6 sockets that can also listen for IPv4
42 * connections. If our address is "*" or empty, enable both v4 and v6 to
43 * allow for simpler configuration on dual-stack hosts. Otherwise, if it
44 * is "::" or an IPv6 address, disable support so that an IPv4 bind will
45 * work on the port (by us or another application).
47 if (bind_to.sa.sa_family == AF_INET6)
49 std::string addr = tag->getString("address");
50 /* This must be >= sizeof(DWORD) on Windows */
51 const int enable = (addr.empty() || addr == "*") ? 0 : 1;
52 /* This must be before bind() */
53 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&enable), sizeof(enable));
54 // errors ignored intentionally
58 if (tag->getBool("free"))
61 #if defined IP_FREEBIND // Linux 2.4+
62 setsockopt(fd, SOL_IP, IP_FREEBIND, &enable, sizeof(enable));
63 #elif defined IP_BINDANY // FreeBSD
64 setsockopt(fd, IPPROTO_IP, IP_BINDANY, &enable, sizeof(enable));
65 #elif defined SO_BINDANY // NetBSD/OpenBSD
66 setsockopt(fd, SOL_SOCKET, SO_BINDANY, &enable, sizeof(enable));
72 SocketEngine::SetReuse(fd);
73 int rv = SocketEngine::Bind(this->fd, bind_to);
75 rv = SocketEngine::Listen(this->fd, ServerInstance->Config->MaxConn);
77 // Default defer to on for TLS listeners because in TLS the client always speaks first
78 int timeout = tag->getDuration("defer", (tag->getString("ssl").empty() ? 0 : 3));
81 #if defined TCP_DEFER_ACCEPT
82 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, sizeof(timeout));
83 #elif defined SO_ACCEPTFILTER
84 struct accept_filter_arg afa;
85 memset(&afa, 0, sizeof(afa));
86 strcpy(afa.af_name, "dataready");
87 setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
94 SocketEngine::Shutdown(this, 2);
95 SocketEngine::Close(this->GetFd());
101 SocketEngine::NonBlocking(this->fd);
102 SocketEngine::AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
104 this->ResetIOHookProvider();
108 ListenSocket::~ListenSocket()
110 if (this->GetFd() > -1)
112 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Shut down listener on fd %d", this->fd);
113 SocketEngine::Shutdown(this, 2);
114 if (SocketEngine::Close(this) != 0)
115 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Failed to cancel listener: %s", strerror(errno));
119 void ListenSocket::OnEventHandlerRead()
121 irc::sockets::sockaddrs client;
122 irc::sockets::sockaddrs server;
124 socklen_t length = sizeof(client);
125 int incomingSockfd = SocketEngine::Accept(this, &client.sa, &length);
127 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Accepting connection on socket %s fd %d", bind_desc.c_str(), incomingSockfd);
128 if (incomingSockfd < 0)
130 ServerInstance->stats.Refused++;
134 socklen_t sz = sizeof(server);
135 if (getsockname(incomingSockfd, &server.sa, &sz))
137 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Can't get peername: %s", strerror(errno));
138 irc::sockets::aptosa(bind_addr, bind_port, server);
141 if (client.sa.sa_family == AF_INET6)
144 * This case is the be all and end all patch to catch and nuke 4in6
145 * instead of special-casing shit all over the place and wreaking merry
146 * havoc with crap, instead, we just recreate sockaddr and strip ::ffff: prefix
149 * This is, of course, much improved over the older way of handling this
150 * (pretend it doesn't exist + hack around it -- yes, both were done!)
152 * Big, big thanks to danieldg for his work on this.
155 static const unsigned char prefix4in6[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xFF,0xFF };
156 if (!memcmp(prefix4in6, &client.in6.sin6_addr, 12))
158 // recreate as a sockaddr_in using the IPv4 IP
159 uint16_t sport = client.in6.sin6_port;
160 client.in4.sin_family = AF_INET;
161 client.in4.sin_port = sport;
162 memcpy(&client.in4.sin_addr.s_addr, client.in6.sin6_addr.s6_addr + 12, sizeof(uint32_t));
164 sport = server.in6.sin6_port;
165 server.in4.sin_family = AF_INET;
166 server.in4.sin_port = sport;
167 memcpy(&server.in4.sin_addr.s_addr, server.in6.sin6_addr.s6_addr + 12, sizeof(uint32_t));
171 SocketEngine::NonBlocking(incomingSockfd);
174 FIRST_MOD_RESULT(OnAcceptConnection, res, (incomingSockfd, this, &client, &server));
175 if (res == MOD_RES_PASSTHRU)
177 std::string type = bind_tag->getString("type", "clients");
178 if (type == "clients")
180 ServerInstance->Users->AddUser(incomingSockfd, this, &client, &server);
184 if (res == MOD_RES_ALLOW)
186 ServerInstance->stats.Accept++;
190 ServerInstance->stats.Refused++;
191 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Refusing connection on %s - %s",
192 bind_desc.c_str(), res == MOD_RES_DENY ? "Connection refused by module" : "Module for this port not found");
193 SocketEngine::Close(incomingSockfd);
197 void ListenSocket::ResetIOHookProvider()
199 iohookprovs[0].SetProvider(bind_tag->getString("hook"));
201 // Check that all non-last hooks support being in the middle
202 for (IOHookProvList::iterator i = iohookprovs.begin(); i != iohookprovs.end()-1; ++i)
204 IOHookProvRef& curr = *i;
205 // Ignore if cannot be in the middle
206 if ((curr) && (!curr->IsMiddle()))
207 curr.SetProvider(std::string());
210 std::string provname = bind_tag->getString("ssl");
211 if (!provname.empty())
212 provname.insert(0, "ssl/");
214 // SSL should be the last
215 iohookprovs.back().SetProvider(provname);