From 85fe75d1cbaf372f0a98558adcd5a612a0e19602 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Wed, 5 Feb 2014 21:24:46 +0100 Subject: Add the option to bind to a specific address --- src/connection.cpp | 27 ++++++++++++++++++--------- src/connection.h | 9 ++++----- src/defaultsettings.cpp | 1 + src/game.cpp | 26 +++++++++++++++++++++++--- src/main.cpp | 17 ++++++++++++++++- src/server.cpp | 10 ++++++---- src/server.h | 2 +- src/socket.cpp | 27 +++++++++++++++++++-------- src/socket.h | 2 +- src/test.cpp | 9 ++++++--- 10 files changed, 95 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/connection.cpp b/src/connection.cpp index 92f9f8ec2..f8c68ed2e 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -1597,8 +1597,8 @@ void ConnectionSendThread::processNonReliableCommand(ConnectionCommand &c) return; case CONNCMD_SERVE: LOG(dout_con<getDesc()<<" UDP processing CONNCMD_SERVE port=" - <getDesc()<<" UDP processing CONNCMD_CONNECT"<getDesc()<<"UDP serving at port "<getDesc() + <<"UDP serving at port " << bind_address.serializeString() <m_udpSocket.Bind(port); + m_connection->m_udpSocket.Bind(bind_address); m_connection->SetPeerID(PEER_ID_SERVER); } catch(SocketException &e){ @@ -1658,7 +1659,14 @@ void ConnectionSendThread::connect(Address address) e.peerAdded(peer->id, peer->address); m_connection->putEvent(e); - m_connection->m_udpSocket.Bind(0); + Address bind_addr; + + if (address.isIPv6()) + bind_addr.setAddress((IPv6AddressBytes*) NULL); + else + bind_addr.setAddress(0,0,0,0); + + m_connection->m_udpSocket.Bind(bind_addr); // Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT m_connection->SetPeerID(PEER_ID_INEXISTENT); @@ -1716,7 +1724,8 @@ void ConnectionSendThread::send(u16 peer_id, u8 channelnum, assert(channelnum < CHANNEL_COUNT); PeerHelper peer = m_connection->getPeerNoEx(peer_id); - if(!peer) { + if(!peer) + { LOG(dout_con<getDesc()<<" peer: peer_id="<>>NOT<<< found on sending packet" << ", channel " << (channelnum % 0xFF) @@ -2766,10 +2775,10 @@ void Connection::putCommand(ConnectionCommand &c) } } -void Connection::Serve(unsigned short port) +void Connection::Serve(Address bind_addr) { ConnectionCommand c; - c.serve(port); + c.serve(bind_addr); putCommand(c); } diff --git a/src/connection.h b/src/connection.h index c9474032d..9d646f499 100644 --- a/src/connection.h +++ b/src/connection.h @@ -406,7 +406,6 @@ enum ConnectionCommandType{ struct ConnectionCommand { enum ConnectionCommandType type; - u16 port; Address address; u16 peer_id; u8 channelnum; @@ -416,10 +415,10 @@ struct ConnectionCommand ConnectionCommand(): type(CONNCMD_NONE), peer_id(PEER_ID_INEXISTENT), reliable(false), raw(false) {} - void serve(u16 port_) + void serve(Address address_) { type = CONNCMD_SERVE; - port = port_; + address = address_; } void connect(Address address_) { @@ -912,7 +911,7 @@ private: void processReliableCommand (ConnectionCommand &c); void processNonReliableCommand (ConnectionCommand &c); - void serve (u16 port); + void serve (Address bind_address); void connect (Address address); void disconnect (); void disconnect_peer(u16 peer_id); @@ -996,7 +995,7 @@ public: void putCommand(ConnectionCommand &c); void SetTimeoutMs(int timeout){ m_bc_receive_timeout = timeout; } - void Serve(unsigned short port); + void Serve(Address bind_addr); void Connect(Address address); bool Connected(); void Disconnect(); diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index bac5e0e2d..9d1aadad9 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -175,6 +175,7 @@ void set_default_settings(Settings *settings) settings->setDefault("workaround_window_size","5"); settings->setDefault("max_packets_per_iteration","1024"); settings->setDefault("port", "30000"); + settings->setDefault("bind_address",""); settings->setDefault("default_game", "minetest"); settings->setDefault("motd", ""); settings->setDefault("max_users", "15"); diff --git a/src/game.cpp b/src/game.cpp index 56519d30a..b540e3314 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -909,8 +909,8 @@ bool nodePlacementPrediction(Client &client, // Dont place node when player would be inside new node // NOTE: This is to be eventually implemented by a mod as client-side Lua - if (!nodedef->get(n).walkable || - (client.checkPrivilege("noclip") && g_settings->getBool("noclip")) || + if (!nodedef->get(n).walkable || + (client.checkPrivilege("noclip") && g_settings->getBool("noclip")) || (nodedef->get(n).walkable && neighbourpos != player->getStandingNodePos() + v3s16(0,1,0) && neighbourpos != player->getStandingNodePos() + v3s16(0,2,0))) { @@ -1029,7 +1029,27 @@ void the_game( infostream<<"Creating server"<start(port); + + std::string bind_str = g_settings->get("bind_address"); + Address bind_addr(0,0,0,0, port); + + if (bind_str != "") + { + try { + bind_addr.Resolve(bind_str.c_str()); + address = bind_str; + } catch (ResolveError &e) { + infostream << "Resolving bind address \"" << bind_str + << "\" failed: " << e.what() + << " -- Listening on all addresses." << std::endl; + + if (g_settings->getBool("ipv6_server")) { + bind_addr.setAddress((IPv6AddressBytes*) NULL); + } + } + } + + server->start(bind_addr); } do{ // Client scope (breakable do-while(0)) diff --git a/src/main.cpp b/src/main.cpp index 8186e26c8..b5de4b93f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1024,6 +1024,21 @@ int main(int argc, char *argv[]) if(port == 0) port = 30000; + // Bind address + std::string bind_str = g_settings->get("bind_address"); + Address bind_addr(0,0,0,0, port); + try { + bind_addr.Resolve(bind_str.c_str()); + } catch (ResolveError &e) { + infostream << "Resolving bind address \"" << bind_str + << "\" failed: " << e.what() + << " -- Listening on all addresses." << std::endl; + + if (g_settings->getBool("ipv6_server")) { + bind_addr.setAddress((IPv6AddressBytes*) NULL); + } + } + // World directory std::string commanded_world = ""; if(cmd_args.exists("world")) @@ -1270,7 +1285,7 @@ int main(int argc, char *argv[]) return 0; } - server.start(port); + server.start(bind_addr); // Run server dedicated_server_loop(server, kill); diff --git a/src/server.cpp b/src/server.cpp index 00db9128d..151bcada1 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -452,17 +452,18 @@ Server::~Server() } } -void Server::start(unsigned short port) +void Server::start(Address bind_addr) { DSTACK(__FUNCTION_NAME); - infostream<<"Starting server on port "<Stop(); // Initialize connection m_con.SetTimeoutMs(30); - m_con.Serve(port); + m_con.Serve(bind_addr); // Start thread m_thread->Start(); @@ -477,7 +478,8 @@ void Server::start(unsigned short port) <<" \\/ \\/ \\/ \\/ \\/ "<