Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - find host by MAC address

I hava a Java/Android app that needs to send some data to server on a local network. the problem is that ip address of server is dynamic so the only way to find it is lookup by it's MAC address. Is this possible in Java? Can you see any other options?

like image 874
user1004995 Avatar asked Nov 26 '11 09:11

user1004995


2 Answers

Finding a Host by MAC Address can only work in your local network. Mac Addresses are one layer under IP Adresses. There exists no routing based on Mac Adresses to other networks.

Broadcast/Multicast

If you are looking for a solution which only works in local network, listening on and sending to broadcast or multicast may be an option to you. If you send a packet to a broadcast address, every host in the local network receives this packet and can answer if it is the server you are looking for. Multicast differ concepually in, that only that hosts who want to receive packets which are addressed to a specific mulitcast address receive this packets. If you are using Multicast you need to pick an address for your application, every client sends packets to this address, every to be found server listens on this address. There exists even some network (for example some university networks), where multicast packets are routed to a limited set of other local networks.

DNS or other static Server

You could use a second server with a static ip address which you use to find your dynamic serevr. Your dynamic server would tell your static server his ip address whenever it changes. You client would ask the static server for the address of the dynamic server. This patterns does work on the entire internet, nomatter where your dynamic server and client are.

This static server could be your DNS server or an dns server of some dyndns provider. But this is not limited to DNS. DNS is designed for finding out how to reach services/servers, but this could be done over any protocol. For example if you prefer webservices this could be done over some http/web application developed by you.

like image 152
johannes Avatar answered Oct 14 '22 01:10

johannes


Just a little warning: this code is untested,

Try digging into the arp cache, like so:

public static String getIpFromArpCache(String macaddr) {
    if (ip == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4 && macaddr.equals(splitted[3])) {
                // Basic sanity check
                String ip = splitted[0];
                if (validateIp(ip)) {
                    return ip;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

private static final String PATTERN = 
    "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

public static boolean validateIp(final String ip){          

      Pattern pattern = Pattern.compile(PATTERN);
      Matcher matcher = pattern.matcher(ip);
      return matcher.matches();             
}

Modified routine from here

like image 29
Reno Avatar answered Oct 14 '22 01:10

Reno