Skip to content

Commit b298ba2

Browse files
committed
Add custom IP bind option
1 parent f0141e4 commit b298ba2

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

include/Settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct Settings {
7979
General_Map,
8080
General_AuthKey,
8181
General_Private,
82+
General_IP,
8283
General_Port,
8384
General_MaxCars,
8485
General_LogChat,

src/Settings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Settings::Settings() {
2828
{ General_Map, std::string("/levels/gridmap_v2/info.json") },
2929
{ General_AuthKey, std::string("") },
3030
{ General_Private, true },
31+
{ General_IP, "::"},
3132
{ General_Port, 30814 },
3233
{ General_MaxCars, 1 },
3334
{ General_LogChat, true },
@@ -47,6 +48,7 @@ Settings::Settings() {
4748
{ { "General", "Map" }, { General_Map, READ_WRITE } },
4849
{ { "General", "AuthKey" }, { General_AuthKey, NO_ACCESS } },
4950
{ { "General", "Private" }, { General_Private, READ_ONLY } },
51+
{ { "General", "IP" }, { General_IP, READ_ONLY } },
5052
{ { "General", "Port" }, { General_Port, READ_ONLY } },
5153
{ { "General", "MaxCars" }, { General_MaxCars, READ_WRITE } },
5254
{ { "General", "LogChat" }, { General_LogChat, READ_ONLY } },

src/TConfig.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static constexpr std::string_view StrDebug = "Debug";
3434
static constexpr std::string_view EnvStrDebug = "BEAMMP_DEBUG";
3535
static constexpr std::string_view StrPrivate = "Private";
3636
static constexpr std::string_view EnvStrPrivate = "BEAMMP_PRIVATE";
37+
static constexpr std::string_view StrIP = "IP";
38+
static constexpr std::string_view EnvStrIP = "BEAMMP_IP";
3739
static constexpr std::string_view StrPort = "Port";
3840
static constexpr std::string_view EnvStrPort = "BEAMMP_PORT";
3941
static constexpr std::string_view StrMaxCars = "MaxCars";
@@ -138,6 +140,8 @@ void TConfig::FlushToFile() {
138140
data["General"][StrInformationPacket.data()] = Application::Settings.getAsBool(Settings::Key::General_InformationPacket);
139141
data["General"][StrAllowGuests.data()] = Application::Settings.getAsBool(Settings::Key::General_AllowGuests);
140142
SetComment(data["General"][StrAllowGuests.data()].comments(), " Whether to allow guests");
143+
data["General"][StrIP.data()] = Application::Settings.getAsString(Settings::Key::General_IP);
144+
SetComment(data["General"][StrIP.data()].comments(), " The IP address to bind the server to, this is NOT related to your public IP.");
141145
data["General"][StrPort.data()] = Application::Settings.getAsInt(Settings::Key::General_Port);
142146
data["General"][StrName.data()] = Application::Settings.getAsString(Settings::Key::General_Name);
143147
SetComment(data["General"][StrTags.data()].comments(), " Add custom identifying tags to your server to make it easier to find. Format should be TagA,TagB,TagC. Note the comma seperation.");
@@ -254,6 +258,7 @@ void TConfig::ParseFromFile(std::string_view name) {
254258
} else {
255259
TryReadValue(data, "General", StrPort, EnvStrPort, Settings::Key::General_Port);
256260
}
261+
TryReadValue(data, "General", StrIP, EnvStrIP, Settings::Key::General_IP);
257262
TryReadValue(data, "General", StrMaxCars, EnvStrMaxCars, Settings::Key::General_MaxCars);
258263
TryReadValue(data, "General", StrMaxPlayers, EnvStrMaxPlayers, Settings::Key::General_MaxPlayers);
259264
TryReadValue(data, "General", StrMap, EnvStrMap, Settings::Key::General_Map);

src/TNetwork.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,17 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
8585

8686
void TNetwork::UDPServerMain() {
8787
RegisterThread("UDPServer");
88-
// listen on all ipv6 addresses
89-
ip::udp::endpoint UdpListenEndpoint(ip::make_address("::"), Application::Settings.getAsInt(Settings::Key::General_Port));
88+
9089
boost::system::error_code ec;
90+
auto address = ip::make_address(Application::Settings.getAsString(Settings::Key::General_IP));
91+
92+
if (ec) {
93+
beammp_errorf("Failed to parse IP: {}", ec.message());
94+
Application::GracefullyShutdown();
95+
}
96+
97+
ip::udp::endpoint UdpListenEndpoint(address, Application::Settings.getAsInt(Settings::Key::General_Port));
98+
9199
mUDPSock.open(UdpListenEndpoint.protocol(), ec);
92100
if (ec) {
93101
beammp_error("open() failed: " + ec.message());
@@ -107,7 +115,7 @@ void TNetwork::UDPServerMain() {
107115
Application::GracefullyShutdown();
108116
}
109117
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Good);
110-
beammp_info(("Vehicle data network online on port ") + std::to_string(Application::Settings.getAsInt(Settings::Key::General_Port)) + (" with a Max of ")
118+
beammp_info(("Vehicle data network online on port ") + std::to_string(UdpListenEndpoint.port()) + (" with a Max of ")
111119
+ std::to_string(Application::Settings.getAsInt(Settings::Key::General_MaxPlayers)) + (" Clients"));
112120
while (!Application::IsShuttingDown()) {
113121
try {
@@ -170,12 +178,17 @@ void TNetwork::UDPServerMain() {
170178
void TNetwork::TCPServerMain() {
171179
RegisterThread("TCPServer");
172180

173-
// listen on all ipv6 addresses
174-
auto port = uint16_t(Application::Settings.getAsInt(Settings::Key::General_Port));
175-
ip::tcp::endpoint ListenEp(ip::make_address("::"), port);
176-
beammp_infof("Listening on 0.0.0.0:{0} and [::]:{0}", port);
177-
ip::tcp::socket Listener(mServer.IoCtx());
178181
boost::system::error_code ec;
182+
auto address = ip::make_address(Application::Settings.getAsString(Settings::Key::General_IP), ec);
183+
if (ec) {
184+
beammp_errorf("Failed to parse IP: {}", ec.message());
185+
return;
186+
}
187+
188+
ip::tcp::endpoint ListenEp(address,
189+
uint16_t(Application::Settings.getAsInt(Settings::Key::General_Port)));
190+
191+
ip::tcp::socket Listener(mServer.IoCtx());
179192
Listener.open(ListenEp.protocol(), ec);
180193
if (ec) {
181194
beammp_errorf("Failed to open socket: {}", ec.message());
@@ -209,6 +222,7 @@ void TNetwork::TCPServerMain() {
209222
Application::GracefullyShutdown();
210223
}
211224
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Good);
225+
beammp_infof("Listening on {0} port {1}", ListenEp.address().to_string(), (int)ListenEp.port());
212226
beammp_info("Vehicle event network online");
213227
do {
214228
try {

0 commit comments

Comments
 (0)