diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/socket.cpp | 113 | ||||
-rw-r--r-- | src/socketengine.cpp | 33 |
2 files changed, 83 insertions, 63 deletions
diff --git a/src/socket.cpp b/src/socket.cpp index f1c795c60..917fe9323 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -61,6 +61,7 @@ ListenSocket::ListenSocket(InspIRCd* Instance, int port, char* addr) : ServerIns if (socketcount == 0) { /* All instances of ListenSocket share these, so reference count it */ + ServerInstance->Logs->Log("SOCKET", DEBUG,"Allocate sockaddr structures"); sock_us = new sockaddr[2]; client = new sockaddr[2]; raddr = new sockaddr[2]; @@ -87,67 +88,81 @@ ListenSocket::~ListenSocket() } } -void ListenSocket::HandleEvent(EventType, int) +void ListenSocket::HandleEvent(EventType e, int err) { - socklen_t uslen, length; // length of our port number - int incomingSockfd, in_port; + switch (e) + { + case EVENT_ERROR: + ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for listensocket GOT AN ERROR! well shit! '%s'", strerror(err)); + break; + case EVENT_WRITE: + ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** HandleEvent for listensocket got a WRITE event!!!"); + break; + case EVENT_READ: + { + ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket"); + 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 + if (this->family == AF_INET6) + { + uslen = sizeof(sockaddr_in6); + length = sizeof(sockaddr_in6); + } + else #endif - { - uslen = sizeof(sockaddr_in); - length = sizeof(sockaddr_in); - } + { + uslen = sizeof(sockaddr_in); + length = sizeof(sockaddr_in); + } - incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length); + ServerInstance->Logs->Log("SOCKET",DEBUG,"Call to accept with client=%08lx, length=%d", client, length); + incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length); + ServerInstance->Logs->Log("SOCKET",DEBUG,"Accept returned %d: %d", WSAGetLastError()); - if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen))) - { - char buf[MAXBUF]; - char target[MAXBUF]; + if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen))) + { + char buf[MAXBUF]; + char target[MAXBUF]; - *target = *buf = '\0'; + *target = *buf = '\0'; #ifdef IPV6 - if (this->family == AF_INET6) - { - in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port); - inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf)); - socklen_t raddrsz = sizeof(sockaddr_in6); - if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0) - inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target)); - else - ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno)); - } - else + if (this->family == AF_INET6) + { + in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port); + inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf)); + socklen_t raddrsz = sizeof(sockaddr_in6); + if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0) + inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target)); + else + ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno)); + } + else #endif - { - inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf)); - in_port = ntohs(((sockaddr_in*)sock_us)->sin_port); - socklen_t raddrsz = sizeof(sockaddr_in); - if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0) - inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target)); - else - ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno)); + { + inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf)); + in_port = ntohs(((sockaddr_in*)sock_us)->sin_port); + socklen_t raddrsz = sizeof(sockaddr_in); + if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0) + inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target)); + else + ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno)); + } + ServerInstance->SE->NonBlocking(incomingSockfd); + ServerInstance->stats->statsAccept++; + ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target); + } + else + { + ServerInstance->SE->Shutdown(incomingSockfd, 2); + ServerInstance->SE->Close(incomingSockfd); + ServerInstance->stats->statsRefused++; + } } - - ServerInstance->SE->NonBlocking(incomingSockfd); - - ServerInstance->stats->statsAccept++; - ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target); - } - else - { - ServerInstance->SE->Shutdown(incomingSockfd, 2); - ServerInstance->SE->Close(incomingSockfd); - ServerInstance->stats->statsRefused++; + break; } } diff --git a/src/socketengine.cpp b/src/socketengine.cpp index d0c181be4..dd80b7f94 100644 --- a/src/socketengine.cpp +++ b/src/socketengine.cpp @@ -121,17 +121,6 @@ bool SocketEngine::BoundsCheckFd(EventHandler* eh) return true; } -#ifdef WINDOWS - -int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen) { return -1; } -int SocketEngine::Close(int fd) { return -1; } -int SocketEngine::Close(EventHandler* fd) { return -1; } -int SocketEngine::Blocking(int fd) { return -1; } -int SocketEngine::NonBlocking(int fd) { return -1; } -int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen) { return -1; } -int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen) { return -1; } - -#else int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen) { @@ -140,24 +129,42 @@ int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen) int SocketEngine::Close(EventHandler* fd) { +#ifdef WINDOWS + return closesocket(fd->GetFd()); +#else return close(fd->GetFd()); +#endif } int SocketEngine::Close(int fd) { +#ifdef WINDOWS + return closesocket(fd); +#else return close(fd); +#endif } int SocketEngine::Blocking(int fd) { +#ifdef WINDOWS + unsigned long opt = 0; + return ioctlsocket(fd, FIONBIO, &opt); +#else int flags = fcntl(fd, F_GETFL, 0); return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK); +#endif } int SocketEngine::NonBlocking(int fd) { +#ifdef WINDOWS + unsigned long opt = 1; + return ioctlsocket(fd, FIONBIO, &opt); +#else int flags = fcntl(fd, F_GETFL, 0); return fcntl(fd, F_SETFL, flags | O_NONBLOCK); +#endif } int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen) @@ -167,11 +174,9 @@ int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namel int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen) { - return recvfrom(fd->GetFd(), buf, len, flags, from, fromlen); + return recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen); } -#endif - int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags) { return send(fd->GetFd(), (const char*)buf, len, flags); |