if ((!*addr) || (strchr(addr,':')))
this->family = AF_INET6;
else
- this->family = AF_INET;
-#else
- this->family = AF_INET;
#endif
+ this->family = AF_INET;
Instance->SE->AddFd(this);
}
}
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;
- int clients;
#ifdef IPV6
if (this->family == AF_INET6)
length = sizeof(sockaddr_in6);
}
else
+#endif
{
uslen = sizeof(sockaddr_in);
length = sizeof(sockaddr_in);
}
-#else
- uslen = sizeof(sockaddr_in);
- length = sizeof(sockaddr_in);
-#endif
- /*
- * This loop may make you wonder 'why' - simple reason. If we just sit here accept()ing until the
- * metaphorical cows come home, then we could very well end up with unresponsiveness in a ddos style
- * situation, which is not desirable (to put it mildly!). This will mean we'll stop accepting and get
- * on with our lives after accepting enough clients to sink a metaphorical battleship. :) -- w00t
- */
- for (clients = 0; clients < ServerInstance->Config->MaxConn; clients++)
+ void* m_acceptEvent = NULL;
+ GetExt("windows_acceptevent", m_acceptEvent);
+ incomingSockfd = _accept (this->GetFd(), (sockaddr*)client, &length);
+
+ if ((incomingSockfd > -1) && (!_getsockname(incomingSockfd, sock_us, &uslen)))
{
- sockaddr* sock_us = new sockaddr[2]; // our port number
- sockaddr* client = new sockaddr[2];
-
- incomingSockfd = _accept (this->GetFd(), (sockaddr*)client, &length);
+ 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
+#endif
+ {
+ inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
+ in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
+ }
- if ((incomingSockfd > -1) && (!_getsockname(incomingSockfd, sock_us, &uslen)))
+ NonBlocking(incomingSockfd);
+ if (ServerInstance->Config->GetIOHook(in_port))
{
- 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
+ try
{
- inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
- in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
+ ServerInstance->Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, buf, in_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))
+ catch (CoreException& modexcept)
{
- 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->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
- {
- /*
- * bail, bail, bail! if we get here, accept failed, meaning something is hardcore wrong.
- * cut our losses and don't try soak up any more clients during this loop iteration. -- w00t
- */
- shutdown(incomingSockfd,2);
- close(incomingSockfd);
- ServerInstance->stats->statsRefused++;
- return;
}
-
- delete[] client;
- delete[] sock_us;
+ 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 */
+ unsigned int divisor = mask_bits / 8; /* Number of whole bytes in the mask */
+ unsigned int modulus = 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 */
+ /* First (this is faster) compare the odd bits with logic ops */
if (modulus)
if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
/* If they dont match, return false */
return false;
+ /* Secondly (this is slower) compare the whole bytes */
+ if (memcmp(address, mask, divisor))
+ return false;
+
/* The address matches the mask, to mask_bits bits of mask */
return true;
}
}
}
-/* XXX: Probably belongs in class InspIRCd */
int InspIRCd::BindPorts(bool bail, int &ports_found, FailedPortList &failed_ports)
{
char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];