summaryrefslogtreecommitdiff
path: root/src/network/core/tcp_listen.h
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-04-27 11:51:00 +0200
committerPatric Stout <github@truebrain.nl>2021-07-16 19:50:29 +0200
commit8adade26ed0354e5357803cf19ea9839c2eb785c (patch)
tree08c73e20a16ea19ee1545de8df6c4b1095c08707 /src/network/core/tcp_listen.h
parent55eed246b842d372cd32784c1afcc904aef67f65 (diff)
downloadopenttd-8adade26ed0354e5357803cf19ea9839c2eb785c.tar.xz
Feature: allow the use of STUN to connect client and server together
This method doesn't require port-forwarding to be used, and works for most common NAT routers in home setups. But, for sure it doesn't work for all setups, and not everyone will be able to use this.
Diffstat (limited to 'src/network/core/tcp_listen.h')
-rw-r--r--src/network/core/tcp_listen.h72
1 files changed, 37 insertions, 35 deletions
diff --git a/src/network/core/tcp_listen.h b/src/network/core/tcp_listen.h
index 03945e230..0c7b11df1 100644
--- a/src/network/core/tcp_listen.h
+++ b/src/network/core/tcp_listen.h
@@ -30,6 +30,42 @@ class TCPListenHandler {
static SocketList sockets;
public:
+ static bool ValidateClient(SOCKET s, NetworkAddress &address)
+ {
+ /* Check if the client is banned. */
+ for (const auto &entry : _network_ban_list) {
+ if (address.IsInNetmask(entry)) {
+ Packet p(Tban_packet);
+ p.PrepareToSend();
+
+ Debug(net, 2, "[{}] Banned ip tried to join ({}), refused", Tsocket::GetName(), entry);
+
+ if (p.TransferOut<int>(send, s, 0) < 0) {
+ Debug(net, 0, "[{}] send failed: {}", Tsocket::GetName(), NetworkError::GetLast().AsString());
+ }
+ closesocket(s);
+ return false;
+ }
+ }
+
+ /* Can we handle a new client? */
+ if (!Tsocket::AllowConnection()) {
+ /* No more clients allowed?
+ * Send to the client that we are full! */
+ Packet p(Tfull_packet);
+ p.PrepareToSend();
+
+ if (p.TransferOut<int>(send, s, 0) < 0) {
+ Debug(net, 0, "[{}] send failed: {}", Tsocket::GetName(), NetworkError::GetLast().AsString());
+ }
+ closesocket(s);
+
+ return false;
+ }
+
+ return true;
+ }
+
/**
* Accepts clients from the sockets.
* @param ls Socket to accept clients from.
@@ -53,41 +89,7 @@ public:
SetNoDelay(s); // XXX error handling?
- /* Check if the client is banned */
- bool banned = false;
- for (const auto &entry : _network_ban_list) {
- banned = address.IsInNetmask(entry);
- if (banned) {
- Packet p(Tban_packet);
- p.PrepareToSend();
-
- Debug(net, 2, "[{}] Banned ip tried to join ({}), refused", Tsocket::GetName(), entry);
-
- if (p.TransferOut<int>(send, s, 0) < 0) {
- Debug(net, 0, "[{}] send failed: {}", Tsocket::GetName(), NetworkError::GetLast().AsString());
- }
- closesocket(s);
- break;
- }
- }
- /* If this client is banned, continue with next client */
- if (banned) continue;
-
- /* Can we handle a new client? */
- if (!Tsocket::AllowConnection()) {
- /* no more clients allowed?
- * Send to the client that we are full! */
- Packet p(Tfull_packet);
- p.PrepareToSend();
-
- if (p.TransferOut<int>(send, s, 0) < 0) {
- Debug(net, 0, "[{}] send failed: {}", Tsocket::GetName(), NetworkError::GetLast().AsString());
- }
- closesocket(s);
-
- continue;
- }
-
+ if (!Tsocket::ValidateClient(s, address)) continue;
Tsocket::AcceptConnection(s, address);
}
}