Help with UDP ports calculation

For security reasons, I’d like to have only a strict amount of UDP ports opened on the server (with some small margin). In one of the topics I’ve read that mediasoup requires a number of WebRtcTransport multiplied by listenIps. It works perfectly fine when I use only one router for every connection. The confusion comes when I try with several routers and piping between them.

Let’s say I have 3 transports created on the client side: 1 receive and 2 send transports. And I have 3 routers on the server side to distribute the load between them. When I connect to the media server, the picture is the following:

lsof -i -n -P | grep UDP | grep mediasoup
mediasoup 4199 user   15u  IPv4 0x9677262ced6aa011      0t0  UDP 192.168.2.2:4079
mediasoup 4199 user   16u  IPv4 0x9677262ced6ab751      0t0  UDP 192.168.2.2:4073
mediasoup 4199 user   17u  IPv4 0x9677262cefdcfe99      0t0  UDP 192.168.2.2:4027
mediasoup 4199 user   18u  IPv4 0x9677262cefdd0469      0t0  UDP 192.168.2.2:4013
mediasoup 4199 user   19u  IPv4 0x9677262cefdcf2f9      0t0  UDP 192.168.2.2:4050
mediasoup 4200 user   15u  IPv4 0x9677262cefdd0751      0t0  UDP 192.168.2.2:4095
mediasoup 4201 user   15u  IPv4 0x9677262cefdcfbb1      0t0  UDP 192.168.2.2:4063

Which is fine, because 1 port is for receive transport, and 2 others are for separate send transports. Also, those 2 send transports get piped to 2 other routers (we don’t pipe to own router), so 7 in total.
However, when I connect another client, which uses 1 receive transport and 1 send transport, here’s the ports:

lsof -i -n -P | grep UDP | grep mediasoup
mediasoup 4199 user   15u  IPv4 0x9677262ced6aa011      0t0  UDP 192.168.2.2:4079
mediasoup 4199 user   16u  IPv4 0x9677262ced6ab751      0t0  UDP 192.168.2.2:4073
mediasoup 4199 user   17u  IPv4 0x9677262cefdcfe99      0t0  UDP 192.168.2.2:4027
mediasoup 4199 user   18u  IPv4 0x9677262cefdd0469      0t0  UDP 192.168.2.2:4013
mediasoup 4199 user   19u  IPv4 0x9677262cefdcf2f9      0t0  UDP 192.168.2.2:4050
mediasoup 4199 user   20u  IPv4 0x9677262cefd0a181      0t0  UDP 192.168.2.2:4064
mediasoup 4199 user   21u  IPv4 0x9677262cedfcc469      0t0  UDP 192.168.2.2:4052
mediasoup 4200 user   15u  IPv4 0x9677262cefdd0751      0t0  UDP 192.168.2.2:4095
mediasoup 4200 user   16u  IPv4 0x9677262cedfcb2f9      0t0  UDP 192.168.2.2:4077
mediasoup 4200 user   17u  IPv4 0x9677262cedfcb5e1      0t0  UDP 192.168.2.2:4001
mediasoup 4200 user   18u  IPv4 0x9677262cedfcca39      0t0  UDP 192.168.2.2:4003
mediasoup 4201 user   15u  IPv4 0x9677262cefdcfbb1      0t0  UDP 192.168.2.2:4063
mediasoup 4201 user   16u  IPv4 0x9677262cedfcb011      0t0  UDP 192.168.2.2:4024

13 in total. So 6 more ports. Why is it so? Shouldn’t it be 4 more (1 receive transport; 1 send transport, which get piped between 2 other routers)?

I don’t know how you are piping Routers but if you call router1.pipeToRouter(router2) many times, it will reuse the same transport between both routers so those are just two new ports in total. However if you call router2.pipeToRouter(router1) it will create a new transport.

BTW you can use 127.0.0.1 for pipe transports.

1 Like