Exemplo n.º 1
0
  protected void logNoRepeat(String usn, String msg) {
    synchronized (log_no_repeat_map) {
      String last = (String) log_no_repeat_map.get(usn);

      if (last != null && last.equals(msg)) {

        return;
      }

      log_no_repeat_map.put(usn, msg);
    }

    log.log(msg);
  }
Exemplo n.º 2
0
  public void rootDeviceFound(UPnPRootDevice device) {
    incrementDeviceStats(device.getUSN(), "found");

    checkDeviceStats(device);

    try {
      int interesting = processDevice(device.getDevice());

      if (interesting > 0) {

        try {
          this_mon.enter();

          root_info_map.put(device.getLocation(), device.getInfo());

          Iterator<String> it = root_info_map.values().iterator();

          String all_info = "";

          List reported_info = new ArrayList();

          while (it.hasNext()) {

            String info = (String) it.next();

            if (info != null && !reported_info.contains(info)) {

              reported_info.add(info);

              all_info += (all_info.length() == 0 ? "" : ",") + info;
            }
          }

          if (all_info.length() > 0) {

            plugin_interface.getPluginconfig().setPluginParameter("plugin.info", all_info);
          }

        } finally {

          this_mon.exit();
        }
      }
    } catch (Throwable e) {

      log.log("Root device processing fails", e);
    }
  }
Exemplo n.º 3
0
  protected void addMapping(UPnPMapping mapping) {
    try {
      this_mon.enter();

      mappings.add(mapping);

      log.log("Mapping request: " + mapping.getString() + ", enabled = " + mapping.isEnabled());

      mapping.addListener(this);

      checkState();

    } finally {

      this_mon.exit();
    }
  }
Exemplo n.º 4
0
  public void mappingDestroyed(UPnPMapping mapping) {
    try {
      this_mon.enter();

      mappings.remove(mapping);

      log.log("Mapping request removed: " + mapping.getString());

      for (int j = 0; j < services.size(); j++) {

        UPnPPluginService service = (UPnPPluginService) services.get(j);

        service.removeMapping(log, mapping, false);
      }
    } finally {

      this_mon.exit();
    }
  }
Exemplo n.º 5
0
  protected void updateIgnoreList() {
    try {
      String param = "";

      if (ignore_bad_devices.getValue()) {

        PluginConfig pc = plugin_interface.getPluginconfig();

        Map ignored = pc.getPluginMapParameter("upnp.device.ignorelist", new HashMap());

        Iterator it = ignored.entrySet().iterator();

        while (it.hasNext()) {

          Map.Entry entry = (Map.Entry) it.next();

          Map value = (Map) entry.getValue();

          param += "\n    " + entry.getKey() + ": " + new String((byte[]) value.get("Location"));
        }

        if (ignored.size() > 0) {

          log.log("Devices currently being ignored: " + param);
        }
      }

      String text =
          plugin_interface
              .getUtilities()
              .getLocaleUtilities()
              .getLocalisedMessageText("upnp.ignorebaddevices.info", new String[] {param});

      ignored_devices_list.setLabelText(text);

    } catch (Throwable e) {

      Debug.printStackTrace(e);
    }
  }
Exemplo n.º 6
0
  protected void removeService(UPnPWANConnection wan_service, boolean replaced) {
    try {
      this_mon.enter();

      String name =
          wan_service.getGenericService().getServiceType().indexOf("PPP") == -1
              ? "WANIPConnection"
              : "WANPPPConnection";

      String text =
          MessageText.getString(
              "upnp.alert.lostdevice",
              new String[] {
                name,
                wan_service.getGenericService().getDevice().getRootDevice().getLocation().getHost()
              });

      log.log(text);

      if ((!replaced) && alert_device_probs_param.getValue()) {

        log.logAlertRepeatable(LoggerChannel.LT_WARNING, text);
      }

      for (int i = 0; i < services.size(); i++) {

        UPnPPluginService ps = (UPnPPluginService) services.get(i);

        if (ps.getService() == wan_service) {

          services.remove(i);

          break;
        }
      }
    } finally {

      this_mon.exit();
    }
  }
Exemplo n.º 7
0
  protected void setNATPMPEnableState() {
    boolean enabled = natpmp_enable_param.getValue() && upnp_enable_param.getValue();

    try {
      if (enabled) {

        if (nat_pmp_upnp == null) {

          nat_pmp_upnp =
              NatPMPUPnPFactory.create(
                  upnp,
                  NatPMPDeviceFactory.getSingleton(
                      new NATPMPDeviceAdapter() {
                        public String getRouterAddress() {
                          return (nat_pmp_router.getValue());
                        }

                        public void log(String str) {
                          log.log("NAT-PMP: " + str);
                        }
                      }));

          nat_pmp_upnp.addListener(this);
        }

        nat_pmp_upnp.setEnabled(true);
      } else {

        if (nat_pmp_upnp != null) {

          nat_pmp_upnp.setEnabled(false);
        }
      }
    } catch (Throwable e) {

      log.log("Failed to initialise NAT-PMP subsystem", e);
    }
  }
Exemplo n.º 8
0
  protected void startUp() {
    if (upnp != null) {

      // already started up, must have been re-enabled

      refreshMappings();

      return;
    }

    final LoggerChannel core_log = plugin_interface.getLogger().getChannel("UPnP Core");

    try {
      upnp =
          UPnPFactory.getSingleton(
              new UPnPAdapter() {
                Set exception_traces = new HashSet();

                public SimpleXMLParserDocument parseXML(String data)
                    throws SimpleXMLParserDocumentException {
                  return (plugin_interface
                      .getUtilities()
                      .getSimpleXMLParserDocumentFactory()
                      .create(data));
                }

                public ResourceDownloaderFactory getResourceDownloaderFactory() {
                  return (plugin_interface.getUtilities().getResourceDownloaderFactory());
                }

                public UTTimer createTimer(String name) {
                  return (plugin_interface.getUtilities().createTimer(name, true));
                }

                public void createThread(String name, Runnable runnable) {
                  plugin_interface.getUtilities().createThread(name, runnable);
                }

                public Comparator getAlphanumericComparator() {
                  return (plugin_interface
                      .getUtilities()
                      .getFormatters()
                      .getAlphanumericComparator(true));
                }

                public void trace(String str) {
                  core_log.log(str);
                  if (trace_to_log.getValue()) {
                    upnp_log_listener.log(str);
                  }
                }

                public void log(Throwable e) {
                  String nested = Debug.getNestedExceptionMessage(e);

                  if (!exception_traces.contains(nested)) {

                    exception_traces.add(nested);

                    if (exception_traces.size() > 128) {

                      exception_traces.clear();
                    }

                    core_log.log(e);

                  } else {

                    core_log.log(nested);
                  }
                }

                public void log(String str) {
                  log.log(str);
                }

                public String getTraceDir() {
                  return (plugin_interface.getUtilities().getAzureusUserDir());
                }
              },
              getSelectedInterfaces());

      upnp.addRootDeviceListener(this);

      upnp_log_listener =
          new UPnPLogListener() {
            public void log(String str) {
              log.log(str);
            }

            public void logAlert(String str, boolean error, int type) {
              boolean logged = false;

              if (alert_device_probs_param.getValue()) {

                if (type == UPnPLogListener.TYPE_ALWAYS) {

                  log.logAlertRepeatable(
                      error ? LoggerChannel.LT_ERROR : LoggerChannel.LT_WARNING, str);

                  logged = true;

                } else {

                  boolean do_it = false;

                  if (type == UPnPLogListener.TYPE_ONCE_EVER) {

                    byte[] fp =
                        plugin_interface
                            .getUtilities()
                            .getSecurityManager()
                            .calculateSHA1(str.getBytes());

                    String key =
                        "upnp.alert.fp."
                            + plugin_interface
                                .getUtilities()
                                .getFormatters()
                                .encodeBytesToString(fp);

                    PluginConfig pc = plugin_interface.getPluginconfig();

                    if (!pc.getPluginBooleanParameter(key, false)) {

                      pc.setPluginParameter(key, true);

                      do_it = true;
                    }
                  } else {

                    do_it = true;
                  }

                  if (do_it) {

                    log.logAlert(error ? LoggerChannel.LT_ERROR : LoggerChannel.LT_WARNING, str);

                    logged = true;
                  }
                }
              }

              if (!logged) {

                log.log(str);
              }
            }
          };

      upnp.addLogListener(upnp_log_listener);

      mapping_manager.addListener(
          new UPnPMappingManagerListener() {
            public void mappingAdded(UPnPMapping mapping) {
              addMapping(mapping);
            }
          });

      UPnPMapping[] upnp_mappings = mapping_manager.getMappings();

      for (int i = 0; i < upnp_mappings.length; i++) {

        addMapping(upnp_mappings[i]);
      }

      setNATPMPEnableState();

    } catch (Throwable e) {

      log.log(e);
    }
  }
Exemplo n.º 9
0
  protected void addService(UPnPWANConnection wan_service) throws UPnPException {

    wan_service.addListener(this);

    mapping_manager.serviceFound(wan_service);

    try {
      this_mon.enter();

      log.log(
          "    Found "
              + (wan_service.getGenericService().getServiceType().indexOf("PPP") == -1
                  ? "WANIPConnection"
                  : "WANPPPConnection"));

      UPnPWANConnectionPortMapping[] ports;

      String usn = wan_service.getGenericService().getDevice().getRootDevice().getUSN();

      if (getDeviceStats(usn, STATS_READ_OK) == 0 && getDeviceStats(usn, STATS_READ_BAD) > 2) {

        ports = new UPnPWANConnectionPortMapping[0];

        wan_service.periodicallyRecheckMappings(false);

        log.log("    Not reading port mappings from device due to previous failures");

      } else {

        ports = wan_service.getPortMappings();
      }

      for (int j = 0; j < ports.length; j++) {

        log.log(
            "      mapping ["
                + j
                + "] "
                + ports[j].getExternalPort()
                + "/"
                + (ports[j].isTCP() ? "TCP" : "UDP")
                + " ["
                + ports[j].getDescription()
                + "] -> "
                + ports[j].getInternalHost());
      }

      services.add(
          new UPnPPluginService(
              wan_service,
              ports,
              alert_success_param,
              grab_ports_param,
              alert_other_port_param,
              release_mappings_param));

      if (services.size() > 1) {

        // check this isn't a single device with multiple services

        String new_usn = wan_service.getGenericService().getDevice().getRootDevice().getUSN();

        boolean multiple_found = false;

        for (int i = 0; i < services.size() - 1; i++) {

          UPnPPluginService service = (UPnPPluginService) services.get(i);

          String existing_usn =
              service.getService().getGenericService().getDevice().getRootDevice().getUSN();

          if (!new_usn.equals(existing_usn)) {

            multiple_found = true;

            break;
          }
        }

        if (multiple_found) {

          PluginConfig pc = plugin_interface.getPluginconfig();

          if (!pc.getPluginBooleanParameter("upnp.device.multipledevices.warned", false)) {

            pc.setPluginParameter("upnp.device.multipledevices.warned", true);

            String text = MessageText.getString("upnp.alert.multipledevice.warning");

            log.logAlertRepeatable(LoggerChannel.LT_WARNING, text);
          }
        }
      }

      checkState();

    } finally {

      this_mon.exit();
    }
  }