user1004995
user1004995

Reputation: 61

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?

Upvotes: 6

Views: 3807

Answers (3)

Reno
Reno

Reputation: 33792

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

Upvotes: 2

johannes
johannes

Reputation: 7272

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.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718826

Is this possible in Java?

Not in pure Java.

There is no service that directly maps MAC addresses to IP addresses. The network stack of your host computer's operating system uses the ARP protocol and (on Linux/UNIX) the ARP daemon to map IP addresses to MAC addresses. AFAIK, it is not possible to use the ARP protocol from Java.

The best you can do (on Linux) is to run "arpd -l" as an external command. This will dump the daemon's ARP mappings in a well-defined format that your Java app can read and parse. If your server is active on the local network, its MAC address will appear along with the corresponding IP address. But if it is not on the local network then its ARP broadcasts won't reach your machine so there won't be an entry in the daemon's tables.


It would be better to assign your server a static IP address ... and a DNS address. Another possibility would be to have the server register itself using a Dynamic DNS service.

Upvotes: 0

Related Questions