Asif
Asif

Reputation: 5038

How to make sure a system is connected through LAN?

In my Swing application (with Web-Start) I have to manually enter the IP addresses of the machines whom I want to give access to the application, now at the time of enetring the IP addresses I want to check that the machine's IP address I've entered is connected to my machine (acting as a local server) via LAN only (through switch, not a case of router) or not. Because if the machine is not in LAN, it should not given the permission to access the application.

How can I achieve this?

Upvotes: 3

Views: 1701

Answers (2)

Vadym S. Khondar
Vadym S. Khondar

Reputation: 1438

As far as I understand your problem, you need to check whether particular IP-address entered in your app is on directly attached network for client host.

If this is the case then using plain ping won't work for you as ping will involve packet routing. So even hosts behind the routers would reply.

As a workaround, you could possibly add '-t 1' parameter to ping specifying TTL for ICMP packets so that they are not able to pass through router.

Or have a look on following sample if you want something like this implemented in java (you should adopt it for your needs):

public class IsAddressDirectlyConnected {

    private static class Network {
        int network;
        int mask;

        Network(int n, int m) {
            network = n;
            mask = m;
        }
    };

    // list of networks on interfaces of machine this code is being run on
    List<Network> mDirectlyAttachedNetworks = new ArrayList<Network>();

    private int addrBytesToInt(byte[] addr) {
        int addri = 0;
        for (int i = 0; i < addr.length; ++i)
            addri = (addri << 8) | ((int)addr[i] & 0xFF);        
        return addri;
    }

    private void collectLocalAddresses() {
        try {
            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

            while (nifs.hasMoreElements()) {
                NetworkInterface nif = nifs.nextElement();
                if (nif.isUp()) {
                    List<InterfaceAddress> ias = nif.getInterfaceAddresses();
                    for (InterfaceAddress ia : ias) {
                        InetAddress ina = ia.getAddress();
                        if (ina instanceof Inet4Address) {
                            int addri = addrBytesToInt(ina.getAddress());
                            int mask = -1 << (32 - ia.getNetworkPrefixLength());
                            addri &= mask;
                            mDirectlyAttachedNetworks.add(new Network(addri, mask));
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            System.err.println("Socket i/o error: " + ex.getLocalizedMessage());
        }
    }

    public boolean isDirectlyAttachedAndReachable(InetAddress address) {
        int checkedAddr = addrBytesToInt(address.getAddress());
        try {
            if (!address.isReachable(1000))
                return false;
        } catch (IOException ex) {
            System.err.println("Failed to check reachability: " + ex.getLocalizedMessage());
            return false;
        }

        for (Network n : mDirectlyAttachedNetworks) {
            if ((checkedAddr & n.mask) == n.network)
                return true;
        }
        return false;
    }

    public IsAddressDirectlyConnected() {
        collectLocalAddresses();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        IsAddressDirectlyConnected iadc = new IsAddressDirectlyConnected();

        if (args.length == 1) {
            try {
                boolean check = iadc.isDirectlyAttachedAndReachable(Inet4Address.getByName(args[0]));
                System.out.println("Given IP is " + (check ? "" : "not ") + "on directly attached network " + (check ? "and " : "or not ")  + "reachable from local host.");
            } catch (UnknownHostException ex) {
                System.err.println("Failed to parse address: " + ex.getLocalizedMessage());
            }
        } else System.out.println("Specify address to check.");
    }
}

Upvotes: 4

Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

Simply Ping to that address

Process p = Runtime.getRuntime().exec("ping " + your_ip_address);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

Upvotes: 3

Related Questions