rakshasa / rtorrent

rTorrent BitTorrent client
https://github.com/rakshasa/rtorrent/wiki
GNU General Public License v2.0
4.17k stars 415 forks source link

set port range for outgoing connections #626

Open sss123next opened 7 years ago

sss123next commented 7 years ago

it would be good to have possibility to set port range for outgoing connections it will simplify firewall/traffic shaping setup

chros73 commented 7 years ago

Isn't having a single port better?

sss123next commented 7 years ago

single is ok too.

chros73 commented 7 years ago
# Port range to use for listening. (port_range)
network.port_range.set = 62820-62820

# Set whether the client should try to connect to UDP trackers (It can cause various problems if it's enabled, if you experience any with this option enabled then disable it.)
trackers.use_udp.set = 1

# Enable DHT support for trackerless torrents or when all trackers are down. May be set to "disable" (completely disable DHT), "off" (do not start DHT),
# "auto" (start and stop DHT as needed), or "on" (start DHT immediately). The default is "off". For DHT to work, a session directory must be defined.
dht.mode.set = auto

# UDP port to use for DHT
dht.port.set = 62882
sss123next commented 7 years ago

this is listen port, i am talking about outgoing port.

chros73 commented 7 years ago

Oh .... Sorry, my bad ...

rakshasa commented 7 years ago

There is currently no posix or l/unix compliant ways of selecting the source port range.

We can however select random source ports in a range and try binding them until something succeeds, and the new feature-bind code should make that easy to add. However it is not a priority so you'd have to do it yourself.

cxtal commented 7 months ago

There is currently no posix or l/unix compliant ways of selecting the source port range.

Any TCP / UDP connection explicitly needs a source port number to bind to as well as a destination port. This does not depend on Unix nor POSIX, but a property of network / sockets programming and how connections work. In order to establish a connection, both a source and destination port is needed on both client and server.

  memset(&server_addr, '\0', sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  // you set it here
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = inet_addr(ip);

The tricky part is selecting a random port, but that's because passing 0 to server_adr.sin_port, which is the short hand way of making the runtime select a random port, is not supported on all platforms.

Irrespective of that, you can ensure that port is randomly chosen between two configurable values and then passed to server_adr.sin_port.