]> git.meshlink.io Git - meshlink/commitdiff
Never automatically try to bind to ports >= 32768. master
authorGuus Sliepen <guus@meshlink.io>
Tue, 8 Nov 2022 21:13:42 +0000 (22:13 +0100)
committerGuus Sliepen <guus@meshlink.io>
Tue, 8 Nov 2022 21:17:13 +0000 (22:17 +0100)
RFC 6335 says operating systems should use ports 49152-65535 for
ephemeral ports. However, Linux and several other operating systems use
32768-65535 by default. Make sure we don't bind in that range, as a
single outgoing TCP connection that uses the same port for the local end
as MeshLink wants to use for the listening port can cause problems.

src/meshlink.c

index f425b4b2f2bad692fd3030673dfe63b4c4056056..a1a2ec407c87477a9e77e204b700b5f944afdb52 100644 (file)
@@ -651,7 +651,7 @@ static bool try_bind(meshlink_handle_t *mesh, int port) {
 
 int check_port(meshlink_handle_t *mesh) {
        for(int i = 0; i < 1000; i++) {
-               int port = 0x1000 + prng(mesh, 0x8000);
+               int port = 0x1000 + prng(mesh, 0x7000);
 
                if(try_bind(mesh, port)) {
                        free(mesh->myport);