Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android arp table - developing questions

Tags:

java

android

I've got two problems with the android app I'm writing.

I'm reading out the local arp table from /proc/net/arp and save ip and corresponding mac address in a hash map. See my function. It's working properly.

    /**
     * Extract and save ip and corresponding MAC address from arp table in HashMap
     */
    public Map<String, String> createArpMap() throws IOException {      
        checkMapARP.clear();
        BufferedReader localBufferdReader = new BufferedReader(new FileReader(new File("/proc/net/arp")));
        String line = "";       

        while ((line = localBufferdReader.readLine()) != null) {            
            String[] ipmac = line.split("[ ]+");
            if (!ipmac[0].matches("IP")) {
                String ip = ipmac[0];
                String mac = ipmac[3];
                if (!checkMapARP.containsKey(ip)) {
                    checkMapARP.put(ip, mac);               
                }                   
            }
        }
        return Collections.unmodifiableMap(checkMapARP);
    }
  1. First problem:

    I'm also using a broadcast receiver. When my app receives the State WifiManager.NETWORK_STATE_CHANGED_ACTION i check if the connection to the gateway is established. If true i call my function to read the arp table. But in this stage the system has not yet builded up the arp table. Sometimes when i receive the connection state the arp table is sill empty.

    Anyone got an idea to solve this?

  2. Second problem:

    I want to save the ip and mac address of the gateway in a persistent way. Right now i'm using Shared Preferences for this. Maybe it's better to write to an internal storage?

    Any tips?

like image 578
IcedEarth Avatar asked Nov 04 '22 09:11

IcedEarth


1 Answers

For the first problem you could start a new thread that runs that method after sleeping for a set amount of time or until it has some entries (Make a Runnable with a mailbox to get the Map) - unless you need to use the map directly, then I think the only way is to wait for the entries. For example (if you need to use the map directly):

public Map<String, String> createArpMap() throws IOException {      
    checkMapARP.clear();
    BufferedReader localBufferdReader = new BufferedReader(new FileReader(new File("/proc/net/arp")));
    String line = "";       
    while ((line = localBufferdReader.readLine()) == null) {
        localBufferdReader.close();
        Thread.sleep(1000);
        localBufferdReader = new BufferedReader(new FileReader(new File("/proc/net/arp")));
    }
    do {            
        String[] ipmac = line.split("[ ]+");
        if (!ipmac[0].matches("IP")) {
            String ip = ipmac[0];
            String mac = ipmac[3];
            if (!checkMapARP.containsKey(ip)) {
                checkMapARP.put(ip, mac);               
            }                   
        }
    } while ((line = localBufferdReader.readLine()) != null);
    return Collections.unmodifiableMap(checkMapARP);
}
like image 198
ddmps Avatar answered Nov 11 '22 11:11

ddmps