Re: Primary network interface?
On Tue, 19 Oct 2010, Andreas Leitgeb wrote:
Robert Tomsick <robert+usenet@tomsick.net> wrote:
Spud wrote:
java.net.NetworkInterface can return a big list of all the network
connections that a system has, both physical and virtual.
Is there any way to determine which one is the primary interface?
I guess it depends on what you consider the "primary interface".
One usecase for "primary interface" is the following:
A server publishes its own address "A" into some registry "B" (e.g.
Corba's .ior file). Clients will query "B" to find "A".
For this, server "A" first of all needed to know, which of its own
lotsa addresses/interfaces will actually work for those clients.
That isn't necessarily the same as a 'primary' interface. If i run a
CosTrading server on my LAN, i want my firewall to register itself with
its inward-facing interface, not its outward-facing one.
Even if we're talking about interfaces going 'outward', there might be
more than one. Think about a machine with two network cards, or a single
card with two addresses.
Maybe the safest way is, to make that address configurable, besides
guesstimating a default. That would allow specifying an entirely
different address which e.g. points to a load-balancer, or special
firewall-back-passage.
Sounds consummately sensible.
Nonetheless, i would have a fallback to a best guess. I would make my
guess like this:
DatagramSocket ds = new DatagramSocket();
InetAddress remote = InetAddress.getByName("a.root-servers.net");
ds.connect(remote, 53);
InetAddress addr = ds.getLocalAddress();
NetworkInterface guess = NetworkInterface.getByInetAddress(addr);
I would, if i was doing this on my own time, actually try all the root
nameservers from a to m inclusive, in a random order, until i got a result
(which means an address which is not 0.0.0.0, and an interface which is
not null).
The idea here is that you are very unlikely to be in a special
relationship with any of the root nameservers, and thus that they function
as the routing analogues of the mathematical 'point at infinity',
addresses to which your 'outward' interface always points.
I should add that i've only tried this on linux (Fedora 13) using wifi
over a NATted home network. It might fail abysmally on other platforms or
network setups.
tom
--
Jim-Jammity Jesus Krispy Kreme Christ on a twat-rocket!