X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fsocket.cpp;h=09885eca16d3fd209f1e68f0eaebbbccb8701c1b;hb=3593e33ce374caee9aec0575b49ec9b0001a8ec6;hp=b28414d4a7960073cc605f3e1d05d12f147ac7c9;hpb=5adcfa35c2c6df8c356841c11835f25e3422364c;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/socket.cpp b/src/socket.cpp index b28414d4a..09885eca1 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -11,9 +11,9 @@ * --------------------------------------------------- */ +/* $Core: libIRCDsocket */ + #include "inspircd.h" -#include -#include "configreader.h" #include "socket.h" #include "socketengine.h" #include "wildcard.h" @@ -59,13 +59,13 @@ ListenSocket::~ListenSocket() { ServerInstance->SE->DelFd(this); ServerInstance->Log(DEBUG,"Shut down listener on fd %d", this->fd); - if (shutdown(this->fd, 2) || close(this->fd)) + if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this)) ServerInstance->Log(DEBUG,"Failed to cancel listener: %s", strerror(errno)); this->fd = -1; } } -void ListenSocket::HandleEvent(EventType et, int errornum) +void ListenSocket::HandleEvent(EventType, int) { sockaddr* sock_us = new sockaddr[2]; // our port number sockaddr* client = new sockaddr[2]; @@ -85,11 +85,9 @@ void ListenSocket::HandleEvent(EventType et, int errornum) length = sizeof(sockaddr_in); } - void* m_acceptEvent = NULL; - GetExt("windows_acceptevent", m_acceptEvent); - incomingSockfd = _accept (this->GetFd(), (sockaddr*)client, &length); + incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length); - if ((incomingSockfd > -1) && (!_getsockname(incomingSockfd, sock_us, &uslen))) + if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen))) { char buf[MAXBUF]; #ifdef IPV6 @@ -105,7 +103,8 @@ void ListenSocket::HandleEvent(EventType et, int errornum) in_port = ntohs(((sockaddr_in*)sock_us)->sin_port); } - NonBlocking(incomingSockfd); + ServerInstance->SE->NonBlocking(incomingSockfd); + if (ServerInstance->Config->GetIOHook(in_port)) { try @@ -118,12 +117,12 @@ void ListenSocket::HandleEvent(EventType et, int errornum) } } ServerInstance->stats->statsAccept++; - userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, this->family, client); + User::AddClient(ServerInstance, incomingSockfd, in_port, false, this->family, client); } else { - shutdown(incomingSockfd,2); - close(incomingSockfd); + ServerInstance->SE->Shutdown(incomingSockfd, 2); + ServerInstance->SE->Close(incomingSockfd); ServerInstance->stats->statsRefused++; } delete[] client; @@ -133,19 +132,19 @@ void ListenSocket::HandleEvent(EventType et, int errornum) /* 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; + 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 */ - /* 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; } @@ -297,28 +296,6 @@ bool irc::sockets::MatchCIDR(const char* address, const char* cidr_mask, bool ma return MatchCIDRBits(addr_raw, mask_raw, bits); } -void irc::sockets::Blocking(int s) -{ -#ifndef WIN32 - int flags = fcntl(s, F_GETFL, 0); - fcntl(s, F_SETFL, flags ^ O_NONBLOCK); -#else - unsigned long opt = 0; - ioctlsocket(s, FIONBIO, &opt); -#endif -} - -void irc::sockets::NonBlocking(int s) -{ -#ifndef WIN32 - int flags = fcntl(s, F_GETFL, 0); - fcntl(s, F_SETFL, flags | O_NONBLOCK); -#else - unsigned long opt = 1; - ioctlsocket(s, FIONBIO, &opt); -#endif -} - /** This will bind a socket to a port. It works for UDP/TCP. * It can only bind to IP addresses, if you wish to bind to hostnames * you should first resolve them using class 'Resolver'. @@ -413,7 +390,7 @@ bool InspIRCd::BindSocket(int sockfd, int port, char* addr, bool dolisten) ((sockaddr_in*)server)->sin_port = htons(port); size = sizeof(sockaddr_in); #endif - ret = bind(sockfd, server, size); + ret = SE->Bind(sockfd, server, size); delete[] server; if (ret < 0) @@ -424,7 +401,7 @@ bool InspIRCd::BindSocket(int sockfd, int port, char* addr, bool dolisten) { if (dolisten) { - if (listen(sockfd, Config->MaxConn) == -1) + if (SE->Listen(sockfd, Config->MaxConn) == -1) { this->Log(DEFAULT,"ERROR in listen(): %s",strerror(errno)); return false; @@ -432,7 +409,7 @@ bool InspIRCd::BindSocket(int sockfd, int port, char* addr, bool dolisten) else { this->Log(DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port); - NonBlocking(sockfd); + SE->NonBlocking(sockfd); return true; } } @@ -449,7 +426,8 @@ int irc::sockets::OpenTCPSocket(char* addr, int socktype) { int sockfd; int on = 1; - struct linger linger = { 0 }; + addr = addr; + struct linger linger = { 0, 0 }; #ifdef IPV6 if (strchr(addr,':') || (!*addr)) sockfd = socket (PF_INET6, socktype, 0); @@ -473,7 +451,7 @@ int irc::sockets::OpenTCPSocket(char* addr, int socktype) } } -int InspIRCd::BindPorts(bool bail, int &ports_found, FailedPortList &failed_ports) +int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports) { char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF]; int bound = 0; @@ -489,7 +467,10 @@ int InspIRCd::BindPorts(bool bail, int &ports_found, FailedPortList &failed_port Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF); Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF); Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF); - + + if (strncmp(Addr, "::ffff:", 7) == 0) + this->Log(DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead."); + if ((!*Type) || (!strcmp(Type,"clients"))) { irc::portparser portrange(configToken, false);