From fcdc68fd8e777fa4c852acf7e471de914e4d7ee7 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 5 Mar 2013 13:35:19 +0000 Subject: IPv6 support: fixed IP string getting on accept() git-svn-id: http://mc-server.googlecode.com/svn/trunk@1256 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/ListenThread.cpp | 2 +- source/OSSupport/Socket.cpp | 29 ++++++++++++++++------------- source/OSSupport/Socket.h | 7 +++++-- source/OSSupport/SocketThreads.cpp | 2 +- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/source/ListenThread.cpp b/source/ListenThread.cpp index eaf60c4de..3013f8cbf 100644 --- a/source/ListenThread.cpp +++ b/source/ListenThread.cpp @@ -214,7 +214,7 @@ void cListenThread::Execute(void) { if (FD_ISSET(itr->GetSocket(), &fdRead)) { - cSocket Client = itr->Accept(); + cSocket Client = (m_Family == cSocket::IPv4) ? itr->AcceptIPv4() : itr->AcceptIPv6(); m_Callback.OnConnectionAccepted(Client); } } // for itr - m_Sockets[] diff --git a/source/OSSupport/Socket.cpp b/source/OSSupport/Socket.cpp index 2412852d5..efc20284f 100644 --- a/source/OSSupport/Socket.cpp +++ b/source/OSSupport/Socket.cpp @@ -265,37 +265,40 @@ bool cSocket::Listen(int a_Backlog) -cSocket cSocket::Accept() +cSocket cSocket::AcceptIPv4(void) { sockaddr_in from; - socklen_t fromlen=sizeof(from); + socklen_t fromlen = sizeof(from); - cSocket SClient = accept(m_Socket, (sockaddr*)&from, &fromlen); + cSocket SClient = accept(m_Socket, (sockaddr *)&from, &fromlen); if (SClient.IsValid() && (from.sin_addr.s_addr != 0)) // Get IP in string form { SClient.m_IPString = inet_ntoa(from.sin_addr); } - return SClient; } - -/* -int cSocket::Connect(SockAddr_In & a_Address) + +cSocket cSocket::AcceptIPv6(void) { - sockaddr_in local; + sockaddr_in6 from; + socklen_t fromlen = sizeof(from); - local.sin_family = a_Address.Family; - local.sin_addr.s_addr = a_Address.Address; - local.sin_port = htons((u_short)a_Address.Port); + cSocket SClient = accept(m_Socket, (sockaddr *)&from, &fromlen); - return connect(m_Socket, (sockaddr *)&local, sizeof(local)); + // Get IP in string form: + if (SClient.IsValid()) + { + char buffer[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, &(from.sin6_addr), buffer, sizeof(buffer)); + SClient.m_IPString.assign(buffer); + } + return SClient; } -*/ diff --git a/source/OSSupport/Socket.h b/source/OSSupport/Socket.h index 7c051d229..ddaf7d8c0 100644 --- a/source/OSSupport/Socket.h +++ b/source/OSSupport/Socket.h @@ -77,8 +77,11 @@ public: /// Sets the socket to listen for incoming connections. Returns true if successful. bool Listen(int a_Backlog = DEFAULT_BACKLOG); - /// Accepts an incoming connection. Blocks if none available. - cSocket Accept(); + /// Accepts an IPv4 incoming connection. Blocks if none available. + cSocket AcceptIPv4(void); + + /// Accepts an IPv6 incoming connection. Blocks if none available. + cSocket AcceptIPv6(void); /// Connects to a localhost socket on the specified port using IPv4; returns true if successful. bool ConnectToLocalhostIPv4(unsigned short a_Port); diff --git a/source/OSSupport/SocketThreads.cpp b/source/OSSupport/SocketThreads.cpp index b008cf2d2..3e505616c 100644 --- a/source/OSSupport/SocketThreads.cpp +++ b/source/OSSupport/SocketThreads.cpp @@ -461,7 +461,7 @@ bool cSocketThreads::cSocketThread::Start(void) } // Finish connecting the control socket by accepting connection from the thread's socket - cSocket tmp = m_ControlSocket2.Accept(); + cSocket tmp = m_ControlSocket2.AcceptIPv4(); if (!tmp.IsValid()) { LOGERROR("Cannot link Control sockets for a cSocketThread (\"%s\"); continuing, but server may be unreachable from now on.", cSocket::GetLastErrorString().c_str()); -- cgit v1.2.3