-#include "wildcard.h"
-
-using namespace irc::sockets;
-
-/* Used when comparing CIDR masks for the modulus bits left over.
- * A lot of ircd's seem to do this:
- * ((-1) << (8 - (mask % 8)))
- * But imho, it sucks in comparison to a nice neat lookup table.
- */
-const char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits - never actually used */
- 0x80, /* 10000000 - 1 bits */
- 0xC0, /* 11000000 - 2 bits */
- 0xE0, /* 11100000 - 3 bits */
- 0xF0, /* 11110000 - 4 bits */
- 0xF8, /* 11111000 - 5 bits */
- 0xFC, /* 11111100 - 6 bits */
- 0xFE /* 11111110 - 7 bits */
-};
-
-
-ListenSocket::ListenSocket(InspIRCd* Instance, int port, char* addr) : ServerInstance(Instance), desc("plaintext"), bind_addr(addr), bind_port(port)
-{
- this->SetFd(OpenTCPSocket(addr));
- if (this->GetFd() > -1)
- {
- if (!Instance->BindSocket(this->fd,port,addr))
- this->fd = -1;
-#ifdef IPV6
- if ((!*addr) || (strchr(addr,':')))
- this->family = AF_INET6;
- else
- this->family = AF_INET;
-#else
- this->family = AF_INET;
-#endif
- Instance->SE->AddFd(this);
- }
-}
-
-ListenSocket::~ListenSocket()
-{
- if (this->GetFd() > -1)
- {
- ServerInstance->SE->DelFd(this);
- ServerInstance->Log(DEBUG,"Shut down listener on fd %d", this->fd);
- if (shutdown(this->fd, 2) || close(this->fd))
- ServerInstance->Log(DEBUG,"Failed to cancel listener: %s", strerror(errno));
- this->fd = -1;
- }
-}
-
-void ListenSocket::HandleEvent(EventType et, int errornum)
-{
- sockaddr* sock_us = new sockaddr[2]; // our port number
- sockaddr* client = new sockaddr[2];
- socklen_t uslen, length; // length of our port number
- int incomingSockfd, in_port;
-
-#ifdef IPV6
- if (this->family == AF_INET6)
- {
- uslen = sizeof(sockaddr_in6);
- length = sizeof(sockaddr_in6);
- }
- else
- {
- uslen = sizeof(sockaddr_in);
- length = sizeof(sockaddr_in);
- }
-#else
- uslen = sizeof(sockaddr_in);
- length = sizeof(sockaddr_in);
-#endif
- incomingSockfd = accept (this->GetFd(), (sockaddr*)client, &length);
-
- if ((incomingSockfd > -1) && (!getsockname(incomingSockfd, sock_us, &uslen)))
- {
- char buf[MAXBUF];
-#ifdef IPV6
- if (this->family == AF_INET6)
- {
- inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
- in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port);
- }
- else
- {
- inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
- in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
- }
-#else
- inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
- in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
-#endif
- NonBlocking(incomingSockfd);
- if (ServerInstance->Config->GetIOHook(in_port))
- {
- try
- {
- ServerInstance->Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, buf, in_port);
- }
- catch (CoreException& modexcept)
- {
- ServerInstance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
- }
- }
- ServerInstance->stats->statsAccept++;
- userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, this->family, client);
- }
- else
- {
- shutdown(incomingSockfd,2);
- close(incomingSockfd);
- ServerInstance->stats->statsRefused++;
- }
- delete[] client;
- delete[] sock_us;
-}
-
-/* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
-bool irc::sockets::MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
-{
- unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */
- unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */