-ListenSocket::ListenSocket(InspIRCd* Instance, int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr) : ServerInstance(Instance), desc("plaintext")
-{
- this->SetFd(sockfd);
- Instance->Log(DEBUG,"CRAP");
- if (!Instance->BindSocket(this->fd,client,server,port,addr))
- this->fd = -1;
-}
-
-ListenSocket::~ListenSocket()
-{
- if (this->GetFd() > -1)
- {
- shutdown(this->fd, 2);
- close(this->fd);
- this->fd = -1;
- }
-}
-
-void ListenSocket::HandleEvent(EventType et, int errornum)
-{
- 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;
- 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
- 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 (CoreException& modexcept)
- {
- ServerInstance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
- }
- }
- ServerInstance->stats->statsAccept++;
-#ifdef IPV6
- userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, client.sin6_addr);
-#else
- userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, client.sin_addr);
-#endif
- }
- else
- {
- 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;
-}
-
-/* Match CIDR, but dont attempt to match() against leading *!*@ sections */
-bool irc::sockets::MatchCIDR(const char* address, const char* cidr_mask)
-{
- return MatchCIDR(address, cidr_mask, false);
-}
-
-/* Match CIDR strings, e.g. 127.0.0.1 to 127.0.0.0/8 or 3ffe:1:5:6::8 to 3ffe:1::0/32
- * If you have a lot of hosts to match, youre probably better off building your mask once
- * and then using the lower level MatchCIDRBits directly.
- *
- * This will also attempt to match any leading usernames or nicknames on the mask, using
- * match(), when match_with_username is true.