Discussion:
[libtorrent] Question about session::listen_port() / interfaces and bound ports
Angel Leon
2016-05-31 00:48:05 UTC
Permalink
I'm trying to add the new `x.pe=` parameters to magnet uris, in there I'd
like to put all the interfaces on which my libtorrent client might be
listening on (may them be IPv4 or IPv6)

In session I see the `listen_port()` method, documented as follows:

*// ``listen_port()`` returns the port we ended up listening on. If the*
*// port specified in settings_pack::listen_interfaces failed, libtorrent*
*// will try to bind to the next port, and so on. If it fails*
*// settings_pack::max_retry_port_bind times, it will bind to port 0*
*// (meaning the OS picks the port). *
*// The only way to know which port it ended up binding to is to ask for it
by calling ``listen_port()``*.

However, when I look at portmap alerts, I see that it attempts to port map
on 3 different interfaces (2 are IPv6, one is IPv4, one of the IPv6 fails,
this is on android)
but at the end, what seems to be a successful portmap response, has a port
number different than the one being returned by session::listen_port.

So if I use the port from session, I'm probably not sending the right port
on the magnet, it perhaps might work if I send the magnet to a peer on the
local network, but someone outside will never be able to take advantage of
those x.pe= parameters to try and establish a connection to my mapped port,
because I have the port of the wrong interface.

So my question is the following, is there a way to find out which are my
listening tcp::endpoint's/udp::endpoint's once the session has started?

I wish I had something like this:

void session::get_tcp_endpoints(std::vector<tcp::endpoint>& endpoints);
void session::get_udp_endpoints(std::vector<udp::endpoint>& endpoints);

and then I could iterate through those and find which ports they're using,
or does that not make much sense in the way sockets are created?
or perhaps endpoints are different for each torrent handle and I shouldn't
be looking at session?
Arvid Norberg
2016-05-31 14:43:47 UTC
Permalink
Post by Angel Leon
I'm trying to add the new `x.pe=` parameters to magnet uris, in there I'd
like to put all the interfaces on which my libtorrent client might be
listening on (may them be IPv4 or IPv6)
*// ``listen_port()`` returns the port we ended up listening on. If the*
*// port specified in settings_pack::listen_interfaces failed, libtorrent*
*// will try to bind to the next port, and so on. If it fails*
*// settings_pack::max_retry_port_bind times, it will bind to port 0*
*// (meaning the OS picks the port). *
*// The only way to know which port it ended up binding to is to ask for it
by calling ``listen_port()``*.
the listen_port() API is inadequate and it needs to be replaced by
something more useful. It's a leftover from the simpler times when
libtorrent listened ona single port.

if you look at the implementation of listen_port() [1] it will return the
SOCKS5 listen port if there is one, otherwise the external TCP port for the
first interface in the list. The external port is supposed to have been
updated by the portmap alert.

However, when I look at portmap alerts, I see that it attempts to port map
Post by Angel Leon
on 3 different interfaces (2 are IPv6, one is IPv4, one of the IPv6 fails,
this is on android)
but at the end, what seems to be a successful portmap response, has a port
number different than the one being returned by session::listen_port.
it's possible that the successful port map is not the first interface.
Post by Angel Leon
So if I use the port from session, I'm probably not sending the right port
on the magnet, it perhaps might work if I send the magnet to a peer on the
local network, but someone outside will never be able to take advantage of
those x.pe= parameters to try and establish a connection to my mapped port,
because I have the port of the wrong interface.
So my question is the following, is there a way to find out which are my
listening tcp::endpoint's/udp::endpoint's once the session has started?
only via the alerts.
Post by Angel Leon
void session::get_tcp_endpoints(std::vector<tcp::endpoint>& endpoints);
void session::get_udp_endpoints(std::vector<udp::endpoint>& endpoints);
I'm envisioning something that exposes more of the m_listen_sockstes list.

and then I could iterate through those and find which ports they're using,
Post by Angel Leon
or does that not make much sense in the way sockets are created?
or perhaps endpoints are different for each torrent handle and I shouldn't
be looking at session?
They are all shared across the whole session.
--
Arvid Norberg

[1]:
https://github.com/arvidn/libtorrent/blob/master/src/session_impl.cpp#L5297
Angel Leon
2016-05-31 15:32:18 UTC
Permalink
Thank you so much, beautiful answer addressed all my questions.
Post by Angel Leon
Post by Angel Leon
I'm trying to add the new `x.pe=` parameters to magnet uris, in there
I'd
Post by Angel Leon
like to put all the interfaces on which my libtorrent client might be
listening on (may them be IPv4 or IPv6)
*// ``listen_port()`` returns the port we ended up listening on. If the*
*// port specified in settings_pack::listen_interfaces failed,
libtorrent*
Post by Angel Leon
*// will try to bind to the next port, and so on. If it fails*
*// settings_pack::max_retry_port_bind times, it will bind to port 0*
*// (meaning the OS picks the port). *
*// The only way to know which port it ended up binding to is to ask for
it
Post by Angel Leon
by calling ``listen_port()``*.
the listen_port() API is inadequate and it needs to be replaced by
something more useful. It's a leftover from the simpler times when
libtorrent listened ona single port.
if you look at the implementation of listen_port() [1] it will return the
SOCKS5 listen port if there is one, otherwise the external TCP port for the
first interface in the list. The external port is supposed to have been
updated by the portmap alert.
However, when I look at portmap alerts, I see that it attempts to port map
Post by Angel Leon
on 3 different interfaces (2 are IPv6, one is IPv4, one of the IPv6
fails,
Post by Angel Leon
this is on android)
but at the end, what seems to be a successful portmap response, has a
port
Post by Angel Leon
number different than the one being returned by session::listen_port.
it's possible that the successful port map is not the first interface.
Post by Angel Leon
So if I use the port from session, I'm probably not sending the right
port
Post by Angel Leon
on the magnet, it perhaps might work if I send the magnet to a peer on
the
Post by Angel Leon
local network, but someone outside will never be able to take advantage
of
Post by Angel Leon
those x.pe= parameters to try and establish a connection to my mapped port,
because I have the port of the wrong interface.
So my question is the following, is there a way to find out which are my
listening tcp::endpoint's/udp::endpoint's once the session has started?
only via the alerts.
Post by Angel Leon
void session::get_tcp_endpoints(std::vector<tcp::endpoint>& endpoints);
void session::get_udp_endpoints(std::vector<udp::endpoint>& endpoints);
I'm envisioning something that exposes more of the m_listen_sockstes list.
and then I could iterate through those and find which ports they're using,
Post by Angel Leon
or does that not make much sense in the way sockets are created?
or perhaps endpoints are different for each torrent handle and I
shouldn't
Post by Angel Leon
be looking at session?
They are all shared across the whole session.
--
Arvid Norberg
https://github.com/arvidn/libtorrent/blob/master/src/session_impl.cpp#L5297
------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and
traffic
patterns at an interface-level. Reveals which users, apps, and protocols
are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Libtorrent-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/libtorrent-discuss
Loading...