From eb68b7ce0157fd55b7d50cca956986d0fdb60bd1 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Mon, 4 Mar 2013 18:40:48 +0000 Subject: Starting an IPv6 support / cSocket rewrite git-svn-id: http://mc-server.googlecode.com/svn/trunk@1250 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/OSSupport/Socket.cpp | 24 ++++++++++++++++++++---- source/OSSupport/Socket.h | 14 +++++++++++++- source/OSSupport/SocketThreads.cpp | 6 +----- source/Server.cpp | 7 +------ 4 files changed, 35 insertions(+), 16 deletions(-) (limited to 'source') diff --git a/source/OSSupport/Socket.cpp b/source/OSSupport/Socket.cpp index cad120fbe..e0cecc0e3 100644 --- a/source/OSSupport/Socket.cpp +++ b/source/OSSupport/Socket.cpp @@ -207,14 +207,30 @@ unsigned long cSocket::INTERNET_ADDRESS_LOCALHOST(void) -int cSocket::Bind(SockAddr_In& a_Address) +int cSocket::BindToAny(unsigned short a_Port) { sockaddr_in local; memset(&local, 0, sizeof(local)); - local.sin_family = a_Address.Family; - local.sin_addr.s_addr = a_Address.Address; - local.sin_port = htons((u_short)a_Address.Port); + local.sin_family = AF_INET; + local.sin_addr.s_addr = 0; + local.sin_port = htons((u_short)a_Port); + + return bind(m_Socket, (sockaddr*)&local, sizeof(local)); +} + + + + + +int cSocket::BindToLocalhost(unsigned short a_Port) +{ + sockaddr_in local; + memset(&local, 0, sizeof(local)); + + local.sin_family = AF_INET;; + local.sin_addr.s_addr = INTERNET_ADDRESS_LOCALHOST(); + local.sin_port = htons((u_short)a_Port); return bind(m_Socket, (sockaddr*)&local, sizeof(local)); } diff --git a/source/OSSupport/Socket.h b/source/OSSupport/Socket.h index 43a80dca5..71ec99fad 100644 --- a/source/OSSupport/Socket.h +++ b/source/OSSupport/Socket.h @@ -62,8 +62,20 @@ public: static const short ADDRESS_FAMILY_INTERNET = 2; static const unsigned long INTERNET_ADDRESS_ANY = 0; static unsigned long INTERNET_ADDRESS_LOCALHOST(void); // 127.0.0.1 represented in network byteorder; must be a function due to GCC :( + static const unsigned short ANY_PORT = 0; // When given to Bind() functions, they will find a free port - int Bind( SockAddr_In& a_Address ); + /// Binds to the specified port on "any" interface (0.0.0.0) + int BindToAny(unsigned short a_Port); + + /* + // TODO: + /// Binds to the specified port + int BindToAny6(unsigned short a_Port); + */ + + /// Binds to the specified port on localhost interface (127.0.0.1) through IPv4 + int BindToLocalhost(unsigned short a_Port); + int Listen( int a_Backlog ); cSocket Accept(); int Connect(SockAddr_In & a_Address); // Returns 0 on success, !0 on failure diff --git a/source/OSSupport/SocketThreads.cpp b/source/OSSupport/SocketThreads.cpp index 5058f3313..943293f5c 100644 --- a/source/OSSupport/SocketThreads.cpp +++ b/source/OSSupport/SocketThreads.cpp @@ -433,11 +433,7 @@ bool cSocketThreads::cSocketThread::Start(void) LOGERROR("Cannot create a Control socket for a cSocketThread (\"%s\"); continuing, but server may be unreachable from now on.", cSocket::GetLastErrorString().c_str()); return false; } - cSocket::SockAddr_In Addr; - Addr.Family = cSocket::ADDRESS_FAMILY_INTERNET; - Addr.Address = cSocket::INTERNET_ADDRESS_LOCALHOST(); - Addr.Port = 0; // Any free port is okay - if (m_ControlSocket2.Bind(Addr) != 0) + if (m_ControlSocket2.BindToLocalhost(cSocket::ANY_PORT) != 0) { LOGERROR("Cannot bind a Control socket for a cSocketThread (\"%s\"); continuing, but server may be unreachable from now on.", cSocket::GetLastErrorString().c_str()); m_ControlSocket2.CloseSocket(); diff --git a/source/Server.cpp b/source/Server.cpp index 7a41352ab..1e830874e 100644 --- a/source/Server.cpp +++ b/source/Server.cpp @@ -188,12 +188,7 @@ bool cServer::InitServer(cIniFile & a_SettingsIni) int Port = a_SettingsIni.GetValueSetI("Server", "Port", 25565); - cSocket::SockAddr_In local; - local.Family = cSocket::ADDRESS_FAMILY_INTERNET; - local.Address = cSocket::INTERNET_ADDRESS_ANY; - local.Port = (unsigned short)Port; - - if( m_pState->SListenClient.Bind( local ) != 0 ) + if (m_pState->SListenClient.BindToAny(Port) != 0) { LOGERROR("bind fail (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); return false; -- cgit v1.2.3