@Override public void run() { while (!mStopped) { try { for (Target t : System.getTargets()) { if (t.getAddress() == null) continue; DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket( NETBIOS_REQUEST, NETBIOS_REQUEST.length, t.getAddress(), NETBIOS_UDP_PORT); socket.setSoTimeout(200); socket.send(packet); socket.close(); } sleep(5000); } catch (Exception e) { if (!mStopped) System.errorLogging(e); } } }
@Override public void run() { Logger.debug("ArpReader started ..."); mNetBiosMap.clear(); mStopped = false; String iface = ""; ArrayList<Target> foundTargets = new ArrayList<Target>(); try { iface = System.getNetwork().getInterface().getDisplayName(); } catch (Exception e) { System.errorLogging(e); } while (!mStopped) { try { BufferedReader reader = new BufferedReader(new FileReader(ARP_TABLE_FILE)); String line = null, name = null; Matcher matcher = null; Endpoint endpoint = null; Target target = null; Network network = System.getNetwork(); foundTargets.clear(); while ((line = reader.readLine()) != null) { if ((matcher = ARP_TABLE_PARSER.matcher(line)) != null && matcher.find()) { String address = matcher.group(1), // hwtype = matcher.group( 2 ), flags = matcher.group(3), hwaddr = matcher.group(4), // mask = matcher.group( 5 ), device = matcher.group(6); if (device.equals(iface) && !hwaddr.equals("00:00:00:00:00:00") && flags.contains("2")) { endpoint = new Endpoint(address, hwaddr); target = new Target(endpoint); foundTargets.add(target); // rescanning the gateway could cause an issue when the gateway itself has multiple // interfaces ( LAN, WAN ... ) if (!endpoint.getAddress().equals(network.getGatewayAddress()) && !endpoint.getAddress().equals(network.getLocalAddress())) { synchronized (mNetBiosMap) { name = mNetBiosMap.get(address); } if (name == null) { try { mExecutor.execute(new NBResolver(address)); } catch (RejectedExecutionException e) { // ignore since this is happening because the executor was shut down. } catch (OutOfMemoryError m) { // wait until the thread queue gets freed break; } if (!target.isRouter()) { // attempt DNS resolution name = endpoint.getAddress().getHostName(); if (!name.equals(address)) { Logger.debug(address + " was DNS resolved to " + name); synchronized (mNetBiosMap) { mNetBiosMap.put(address, name); } } else name = null; } } if (!System.hasTarget(target)) sendNewEndpointNotification(endpoint, name); else if (name != null) { target = System.getTargetByAddress(address); if (target != null && !target.hasAlias()) { target.setAlias(name); sendEndpointUpdateNotification(); } } } } } } reader.close(); boolean update = false; boolean found; int i; Target[] currentTargets = System.getTargets().toArray(new Target[System.getTargets().size()]); for (Target t : currentTargets) { endpoint = t.getEndpoint(); if (endpoint == null) continue; for (found = false, i = 0; i < foundTargets.size() && !found; i++) { if (endpoint.equals(foundTargets.get(i).getEndpoint())) found = true; } if (t.isConnected() != found && !t.isRouter() && !t.getAddress().equals(network.getLocalAddress())) { t.setConneced(found); update = true; } } if (update) sendEndpointUpdateNotification(); Thread.sleep(500); } catch (Exception e) { System.errorLogging(e); String msg = e.getMessage(); if (msg != null && msg.contains("EMFILE")) { try { Shell.exec( "lsof | grep " + android.os.Process.myPid(), new Shell.OutputReceiver() { @Override public void onStart(String command) {} @Override public void onNewLine(String line) { Logger.debug(line); } @Override public void onEnd(int exitCode) {} }); } catch (Exception e1) { System.errorLogging(e1); } } } } }