-#include "wildcard.h"
-
-using namespace std;
-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 sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr) : ServerInstance(Instance)
-{
- this->SetFd(sockfd);
- Instance->Log(DEBUG,"Binding to port %s:%d",addr,port);
- if (!Instance->BindSocket(this->fd,client,server,port,addr))
- {
- Instance->Log(DEBUG,"Binding failed!");
- this->fd = -1;
- }
-}
-
-void ListenSocket::HandleEvent(EventType et)
-{
- insp_sockaddr sock_us; // our port number
- socklen_t uslen; // length of our port number
- insp_sockaddr client;
- socklen_t length;
- int incomingSockfd, in_port;
-
- ServerInstance->Log(DEBUG,"Handle ListenSocket event");
-
- uslen = sizeof(sock_us);
- length = sizeof(client);
- incomingSockfd = accept (this->GetFd(),(struct sockaddr*)&client, &length);
-
- if ((incomingSockfd > -1) && (!getsockname(incomingSockfd, (sockaddr*)&sock_us, &uslen)))
- {
-#ifdef IPV6
- in_port = ntohs(sock_us.sin6_port);
-#else
- in_port = ntohs(sock_us.sin_port);
-#endif
- ServerInstance->Log(DEBUG,"Accepted socket %d",incomingSockfd);
- NonBlocking(incomingSockfd);
- if (ServerInstance->Config->GetIOHook(in_port))
- {
- try
- {
-#ifdef IPV6
- ServerInstance->Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, insp_ntoa(client.sin6_addr), in_port);
-#else
- ServerInstance->Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, insp_ntoa(client.sin_addr), in_port);
-#endif
- }
- catch (ModuleException& modexcept)
- {
- ServerInstance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
- }
- }
- ServerInstance->stats->statsAccept++;
-#ifdef IPV6
- ServerInstance->Log(DEBUG,"Add ipv6 client");
- userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, client.sin6_addr);
-#else
- ServerInstance->Log(DEBUG,"Add ipv4 client");
- userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, client.sin_addr);
-#endif
- ServerInstance->Log(DEBUG,"Adding client on port %d fd=%d",in_port,incomingSockfd);
- }
- else
- {
- ServerInstance->Log(DEBUG,"Accept failed on fd %d: %s",incomingSockfd,strerror(errno));
- shutdown(incomingSockfd,2);
- close(incomingSockfd);
- ServerInstance->stats->statsRefused++;
- }
-}
-
-/* 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 */
-
- /* First compare the whole bytes, if they dont match, return false */
- if (memcmp(address, mask, divisor))
- return false;
-
- /* Now if there are any remainder bits, we compare them with logic AND */
- if (modulus)
- if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
- /* If they dont match, return false */
- return false;
-
- /* The address matches the mask, to mask_bits bits of mask */
- return true;
-}