1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "socketengine.h"
20 /* Private static member data must be declared in this manner */
21 irc::sockets::sockaddrs ListenSocketBase::client;
22 irc::sockets::sockaddrs ListenSocketBase::server;
24 ListenSocketBase::ListenSocketBase(int port, const std::string &addr, const std::string &Type, const std::string &Hook)
25 : type(Type), hook(Hook), bind_port(port)
27 irc::sockets::sockaddrs bind_to;
29 // canonicalize address if it is defined
30 if (!irc::sockets::aptosa(addr, port, &bind_to))
34 bind_desc = addr + ":" + ConvToStr(port);
39 irc::sockets::satoap(&bind_to, bind_addr, port);
40 bind_desc = irc::sockets::satouser(&bind_to);
42 this->fd = irc::sockets::OpenTCPSocket(bind_addr);
47 int rv = ServerInstance->SE->Bind(this->fd, &bind_to.sa, sizeof(bind_to));
49 rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);
53 ServerInstance->SE->Shutdown(this, 2);
54 ServerInstance->SE->Close(this);
59 ServerInstance->SE->NonBlocking(this->fd);
60 ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
65 ListenSocketBase::~ListenSocketBase()
67 if (this->GetFd() > -1)
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));
77 /* Just seperated into another func for tidiness really.. */
78 void ListenSocketBase::AcceptInternal()
80 ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
83 socklen_t length = sizeof(client);
84 incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
86 if (incomingSockfd < 0)
88 ServerInstance->SE->Shutdown(incomingSockfd, 2);
89 ServerInstance->SE->Close(incomingSockfd);
90 ServerInstance->stats->statsRefused++;
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));
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!)
108 if (incomingSockfd >= ServerInstance->SE->GetMaxFds())
110 ServerInstance->Logs->Log("SOCKET", DEBUG, "Server is full");
111 ServerInstance->SE->Shutdown(incomingSockfd, 2);
112 ServerInstance->SE->Close(incomingSockfd);
113 ServerInstance->stats->statsRefused++;
117 if (client.sa.sa_family == AF_INET6)
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
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!)
128 * Big, big thanks to danieldg for his work on this.
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))
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;
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;
149 ServerInstance->SE->NonBlocking(incomingSockfd);
150 ServerInstance->stats->statsAccept++;
151 this->OnAcceptReady(incomingSockfd);
154 void ListenSocketBase::HandleEvent(EventType e, int err)
159 ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
162 ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
165 this->AcceptInternal();
170 void ClientListenSocket::OnAcceptReady(int nfd)
172 ServerInstance->Users->AddUser(nfd, this, &client, &server);