cxong / cdogs-sdl

Classic overhead run-and-gun game
https://cxong.github.io/cdogs-sdl/
GNU General Public License v2.0
868 stars 115 forks source link

Feature request: Stable network ports #840

Closed reinerh closed 3 months ago

reinerh commented 4 months ago

I was just playing cdogs-sdl with a friend over the Internet, which is quite fun! It was a bit annoying that besides the main listening port (34219), it opens additional random ports that need to be reachable. So to achieve a connection, I had to lookup the open ports and re-configure my router (firewall/NAT).

It would be nice if cdogs-sdl would choose the open ports from a predictable range (even better if only one port would be sufficient), so that the router needs to be configured only once.

cxong commented 4 months ago

The solution C-Dogs SDL uses is described here https://cxong.github.io/2016/01/how-to-write-a-lan-server where the listen port 34219 is only used for server discovery, and the game connection itself is on a random free port. This is to avoid having that port conflict with some other program, and also allow multiple servers on the same machine

I don't know what's the canonical way of solving this, would you need to just add a firewall exception for cdogs-sdl?

reinerh commented 4 months ago

Choosing random ports works fine on a LAN, where there are no firewall or other restrictions. But for accepting connections from WAN/internet, the router (and a maybe firewall on the router) need to be configured to forward the ports to a machine inside the local network. Each time a different random port is chosen, the port needs to be figured out and the router needs to be re-configured with it.

A simple solution could be to pick random ports from a small but known range (e.g. 34220-34300). Such a fixed range can be easily configured in most routers.

Or maybe allow for configuration of the randomly chosen ports. Then the user can decide for themselves what ports to use.

would you need to just add a firewall exception for cdogs-sdl

On the machine itself it would be easy to allow every port of a program like cdogs-sdl, but the router (which is a different device) does not know the ports that cdogs-sdl is using, so it can't automatically forward them. (There is UPnP for automated asking a router to forward ports to a machine, but using it is discouraged as it has a bunch of security problems.)

cxong commented 4 months ago

I think it would be best to add a cmd line arg to specify the port to serve; since there is no standard way to specify a range for selecting a random port, we'd have to repeatedly bind() which could be very slow.