views:

45

answers:

2

I am trying to write a program in Java to know whether a machine is in Global/Private network or not. Below is my code snippet. I thought if a machine detects only loopback address(127.0.0.1 or ::1) then it can be assumed that machine is not in network. But in one of the System where multicast is enabled, I am getting following IP Addresses other than loop back address.

fe80::20c:29ff:fe90:8041
ff01::1,
ff02::1
ff02::1:ff90:8041
fe80::ffff:ffff:fffd
::1
fe80::1
127.0.0.1
224.0.0.1

Code snippet:

private static ArrayList< InetAddress > getInetAddresses( ) throws IPAddressException
    {

        ArrayList< InetAddress > arrayIPAddress = new ArrayList< InetAddress >( );

        try
        {
            Enumeration< NetworkInterface > networkInterfaces = NetworkInterface.getNetworkInterfaces( );

            if ( networkInterfaces == null )
            {
                throw new IPAddressException( "NetworkInterface Not found" );
            }

            while ( networkInterfaces.hasMoreElements( ) )
            {
                NetworkInterface card = ( NetworkInterface ) networkInterfaces.nextElement( );

                Enumeration< InetAddress > addresses = card.getInetAddresses( );

                if ( addresses == null )
                {
                    continue;
                }

                while ( addresses.hasMoreElements( ) )
                {
                    InetAddress inetAddress = ( InetAddress ) addresses.nextElement( );
                    arrayIPAddress.add( inetAddress );

                }
            }
        }
        catch ( SocketException obj )
        {
            throw new IPAddressException( "NetworkInterface Not found" );
        }

        return arrayIPAddress;
    }

IPConfig report:

C:\Documents and Settings\Administrator>ipconfig /all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : vm13autopassdl1
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Unknown
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   DNS Suffix Search List. . . . . . : ind.hp.com
                                       india.hp.com

Ethernet adapter Local Area Connection 2:

   Media State . . . . . . . . . . . : Media disconnected
   Description . . . . . . . . . . . : Intel(R) PRO/1000 MT Network Connection #
2
   Physical Address. . . . . . . . . : 00-0C-29-90-80-41

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
   Physical Address. . . . . . . . . : FF-FF-FF-FF-FF-FF-FF-FF
   DHCP Enabled. . . . . . . . . . . : No
   IP Address. . . . . . . . . . . . : fe80::ffff:ffff:fffd%4
   Default Gateway . . . . . . . . . :
   NetBIOS over Tcpip. . . . . . . . : Disabled

Is there any other way to detect whethre machine is not in network?

A: 

I would try to see if you can connect to a well-known site on the Internet (e.g., google.com, microsoft.com) using a well-known port (e.g., 80). If you can, then you're on the Internet; if you can't, then you might as well not be. Note that corporate firewall rules could still interfere with whatever you're trying to do -- even if you're on the Internet.

Steve Emmerson
A: 

In addition to the loopback addresses 127.0.0.1 and ::1, you should also ignore:

  • IPv6 Link-local addresses - these start with fe8, fe9, fea or feb.
  • IPv4 Multicast addresses - these start with 224 through to 239.
  • IPv6 Multicast addresses - these start with ff.
caf
Thank you for the response.
Tripati
Thanks. But it is possible that ,a machine can have only linklocal address while it is in thenetwork.so filtering Link local address in this case will not work . Do you have any other solution to this ?
Tripati
I think you need to clarify exactly what you mean by "is in the network".
caf
In the network means , there should be way to ping or sending packets to the machine.
Tripati
A machine can be in Private or Global network. Generally Site Local Addresses are used for Private networking and Global addresses are used for Global networking. Is it possible that a machine has configured to only multicast or LinkLocal address can be pinged/(packets can be sent) by any other machine outside? Could you please also confirm that if a machine doesn't detect any SiteLocal or Global IP addresses then it is not in any network.This answer will solve my problem?
Tripati
If it has a link-local address it can only be contacted by hosts on the same subnet. Of course there may not *be* any other hosts on the same subnet...
caf
What I a trying to do here is that, my Product should behave differently if product is running in a machine where there is no network .To achieve this how to find whether the machine is out of network or not.It seems my way of doing is not correct .Is ther any other way to find it.
Tripati
@Tripati: The problem you have is the (very common) edge case of a network with only one computer on it.
caf