Пример #1
0
    @Override
    public void run() {
      byte[] buffer = new byte[128];
      DatagramPacket packet = new DatagramPacket(buffer, buffer.length, mAddress, NETBIOS_UDP_PORT),
          query =
              new DatagramPacket(
                  NETBIOS_REQUEST, NETBIOS_REQUEST.length, mAddress, NETBIOS_UDP_PORT);
      String name, address = mAddress.getHostAddress();
      Target target;

      for (int i = 0; i < MAX_RETRIES; i++) {
        try {
          mSocket.send(query);
          mSocket.receive(packet);

          byte[] data = packet.getData();

          if (data != null && data.length >= 74) {
            String response = new String(data, "ASCII");

            // i know this is horrible, but i really need only the netbios name
            name = response.substring(57, 73).trim();

            Logger.debug(address + " was resolved to " + name);

            // update netbios cache
            mArpReader.addNetBiosName(address, name);

            // existing target
            target = System.getTargetByAddress(address);
            if (target != null) {
              target.setAlias(name);
              sendEndpointUpdateNotification();
            }

            break;
          }
        } catch (SocketTimeoutException ste) {
          // swallow timeout error
        } catch (IOException e) {
          System.errorLogging(e);
        } finally {
          try {
            // send again a query
            mSocket.send(query);
          } catch (Exception e) {
            // swallow error
          }
        }
      }

      mSocket.close();
    }
Пример #2
0
  @Override
  public void run() {
    Logger.debug("Network monitor started ...");

    mRunning = true;

    try {
      mProber.start();
      mArpReader.start();
      mTargetProber.start();

      mProber.join();
      mArpReader.join();
      mTargetProber.join();

      Logger.debug("Network monitor stopped.");

      mRunning = false;
    } catch (Exception e) {
      System.errorLogging(e);
    }
  }
Пример #3
0
 /* keep grabbing commands, acquiring locks, until everything is executed */
 public void run() {
   try {
     while (true) {
       while (mSleep) Thread.sleep(200);
       String next = grabCommand();
       if (next != null) {
         processCommand(next);
         Thread.sleep(50);
       } else Thread.sleep(200);
     }
   } catch (InterruptedException e) {
     Logger.warning("interrupted");
   } catch (TimeoutException e) {
     Logger.error("Session timed out: " + e.getMessage());
   } catch (RPCClient.MSFException e) {
     System.errorLogging(e);
   } catch (IOException e) {
     System.errorLogging(e);
   } finally {
     stopSession();
   }
 }
Пример #4
0
    @Override
    public void run() {
      Logger.debug("UdpProber started ...");

      mStopped = false;

      int i, nhosts = 0;
      IP4Address current = null;

      try {
        mNetwork = System.getNetwork();
        nhosts = mNetwork.getNumberOfAddresses();
      } catch (Exception e) {
        System.errorLogging(e);
      }

      while (!mStopped && mNetwork != null && nhosts > 0) {
        try {
          for (i = 1, current = IP4Address.next(mNetwork.getStartAddress());
              current != null && i <= nhosts;
              current = IP4Address.next(current), i++) {
            // rescanning the gateway could cause an issue when the gateway itself has multiple
            // interfaces ( LAN, WAN ... )
            if (!current.equals(mNetwork.getGatewayAddress())
                && !current.equals(mNetwork.getLocalAddress())) {
              InetAddress address = current.toInetAddress();

              try {
                mExecutor.execute(new SingleProber(address));
              } catch (RejectedExecutionException e) {
                // ignore since this is happening because the executor was shut down.
                break;
              } catch (OutOfMemoryError m) {
                // wait until the thread queue gets freed
                break;
              }
            }
          }

          Thread.sleep(1000);
        } catch (Exception e) {
        }
      }
    }
Пример #5
0
    @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);
            }
          }
        }
      }
    }