diff options
author | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-09-07 23:08:32 +0000 |
---|---|---|
committer | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-09-07 23:08:32 +0000 |
commit | 77b7c4300472ff1ba5c643b75ead4be75625f96f (patch) | |
tree | d8f48d938b5c8164db01032ec26a24939a43d0b1 | |
parent | 66b1ab1f4de1af96d84298107e64d93c8f4223e3 (diff) |
Tidy up some of the internals a bit, making things a bit more extensible and future proof.
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@10462 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | include/socket.h | 15 | ||||
-rw-r--r-- | src/listensocket.cpp | 132 |
2 files changed, 85 insertions, 62 deletions
diff --git a/include/socket.h b/include/socket.h index 105984f74..556ee1a90 100644 --- a/include/socket.h +++ b/include/socket.h @@ -202,7 +202,22 @@ class CoreExport ListenSocket : public EventHandler { return bind_addr; } + + /** Handles sockets internals crap of a connection, convenience wrapper really + */ + void AcceptInternal(); + + /** Called when a new connection has successfully been accepted on this listener. + * @param ipconnectedto The IP address the connection arrived on + * @param fd The file descriptor of the new connection + */ + virtual void OnAcceptReady(const std::string &ipconnectedto, int fd); }; +//class CoreExport ListenSocketClient : public ListenSocket +//{ +// +//} + #endif diff --git a/src/listensocket.cpp b/src/listensocket.cpp index 936dd85e9..58934e543 100644 --- a/src/listensocket.cpp +++ b/src/listensocket.cpp @@ -70,77 +70,85 @@ ListenSocket::~ListenSocket() } } -void ListenSocket::HandleEvent(EventType e, int err) +/* Just seperated into another func for tidiness really.. */ +void ListenSocket::AcceptInternal() { - switch (e) - { - case EVENT_ERROR: - ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err)); - break; - case EVENT_WRITE: - ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() 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; + ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket"); + socklen_t uslen, length; // length of our port number + int incomingSockfd; #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); + incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length); - if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen))) - { - char buf[MAXBUF]; - char target[MAXBUF]; + if (incomingSockfd < 0 || + ServerInstance->SE->GetSockName(this, sock_us, &uslen) == -1) + { + ServerInstance->SE->Shutdown(incomingSockfd, 2); + ServerInstance->SE->Close(incomingSockfd); + ServerInstance->stats->statsRefused++; + return; + } - *target = *buf = '\0'; + static char buf[MAXBUF]; + static char target[MAXBUF]; + + *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 (getsockname(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) + { + inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf)); + socklen_t raddrsz = sizeof(sockaddr_in6); + if (getsockname(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 (getsockname(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++; - } - } - break; + { + inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf)); + socklen_t raddrsz = sizeof(sockaddr_in); + if (getsockname(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++; + this->OnAcceptReady(target, incomingSockfd); +} + +void ListenSocket::HandleEvent(EventType e, int err) +{ + switch (e) + { + case EVENT_ERROR: + ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err)); + break; + case EVENT_WRITE: + ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!"); + break; + case EVENT_READ: + this->AcceptInternal(); + break; } } + +void ListenSocket::OnAcceptReady(const std::string &ipconnectedto, int nfd) +{ + ServerInstance->Users->AddUser(ServerInstance, nfd, bind_port, false, this->family, client, ipconnectedto); +} |