-
Notifications
You must be signed in to change notification settings - Fork 22
Binding a socket to a local interface when it is not specifically specified. #36
Description
[Paatric]:
- the concept of an isolated "default local interface" (used in a few
different places) doesn't really align with networking.. generally
when a local interface isn't specified for a socket the one it is
assigned is derived from looking up the remote address in the routing
table and taking the address of the interface with the most preferred
route to the remote address.. This is equally true of TCP and UDP.think about a case where you've got 3 interfaces defined on your
machine - 192.168.16.1 which is a natted address used to connect to
the internet, 130.215.21.5 which is an address assigned to you while
you're connected to your university's VPN, and 127.0.0.1 (localhost).Without additional context - none of those qualify as the default
local interface. What generally happens is that when you ask to
connect to
8.8.8.8 your local address is assigned to be 192.168.16.1 because your
Internet route will be used for 8.8.8.8.. but if you ask to connect to
130.215.21.1 your local address is assigned to be 130.215.21.5.. and
if you want to connect to 127.0.0.1 your local address is also 127.0.0.1.
So the remote address and the routing table matter - there really
isn't a default local address outside of that context.so in general whenever you want a local interface (and you did not
explicitly provide one) it can only be determined after your provide
the remote address and a system call is made to consult the routing
table.you specifically asked about
#24 .. I'm not concerned
about blocking IO here.. the address lookup will require a system call
but its just another kernel service with a quick response.. no
different than gettimeofday() or something really. To me the issue is
really just that the concept of assigning a local address is
nonsensical until you have assigned the remote one.[Ke-Fong]: When you bind with INADDR_ANY, that is indeed the behavior:
Use the network interface according to routing.
We're a bit higher level from this, having a specified default network
interface makes sense and would be needed.
Suppose my smartphone has 3G, wifi, and bluetooth, they can all act as
network interfaces.
Yet, I may don't want to use 3G because it's usually expensive and use
wifi instead.
[Claes] So this means that for TCP a better description on the process for allocating a local address is needed in the case when a specific local address is not defined by the web application but I don't think that the API itself has to change?
For the case when a local interface needs to be stated by the web application I refer to the latest comments in #24, i.e. that the phase 2 SysApps API, Network Interface API, should be used to list the available networks so that a specific interface can be selected by the application.
However, your description above on how the routing table is used to set the local address based on the remote address to connect to works for TCP. What applies then to UDP? With the constructor's options argument we can set an optional "default remote address" for subsequent send() calls. So if this option is used I assume that the routing table can be used in the same manner as for TCP. However, which is the procedure if we don't set this option? Does that mean that the local address can't be determined until we issue a send() call?
Regarding setting the localAddress attribute I assume that Patrick means that for TCP the localAddress attribute immediately could be set to the actual value during the execution of the constructor, not to null, even if it is not explicitly specified in the constructor. Correct?