Example #1
0
  private synchronized void removeUpnpMappings(final Collection<UpnpMapping> toRemove) {
    if (toRemove.size() == 0) {
      return;
    }
    log.info("Deleting mappings {}", toRemove);
    UPNPDev devlist =
        MiniupnpcLibrary.INSTANCE.upnpDiscover(
            UPNP_DELAY, (String) null, (String) null, 0, 0, IntBuffer.allocate(1));
    if (devlist == null) {
      log.debug("No devices?");
      // no devices, so no way to remove mapping
      return;
    }

    final UPNPUrls urls = new UPNPUrls();
    final IGDdatas data = new IGDdatas();

    ByteBuffer lanaddr = ByteBuffer.allocate(16);
    int ret = MiniupnpcLibrary.INSTANCE.UPNP_GetValidIGD(devlist, urls, data, lanaddr, 16);
    if (ret == 0) {
      log.debug("No valid IGD?");
      devlist.setAutoRead(false);
      MiniupnpcLibrary.INSTANCE.freeUPNPDevlist(devlist);
      return;
    }
    try {
      logIGDResponse(ret, urls);
      for (UpnpMapping mapping : toRemove) {
        ret =
            MiniupnpcLibrary.INSTANCE.UPNP_DeletePortMapping(
                urls.controlURL.getString(0),
                zeroTerminatedString(data.first.servicetype),
                "" + mapping.externalPort,
                mapping.prot.toString(),
                null);
        if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS)
          log.debug("DeletePortMapping() failed with code " + ret);
      }
    } finally {
      MiniupnpcLibrary.INSTANCE.FreeUPNPUrls(urls);
      devlist.setAutoRead(false);
      MiniupnpcLibrary.INSTANCE.freeUPNPDevlist(devlist);
    }
  }
Example #2
0
  protected synchronized void addMapping(
      final PortMappingProtocol prot,
      final int externalPortRequested,
      int localPort,
      final PortMapListener portMapListener) {

    ByteBuffer lanaddr = ByteBuffer.allocate(16);
    ByteBuffer intClient = ByteBuffer.allocate(16);
    ByteBuffer intPort = ByteBuffer.allocate(6);
    ByteBuffer desc = ByteBuffer.allocate(80);
    ByteBuffer enabled = ByteBuffer.allocate(4);
    ByteBuffer leaseDuration = ByteBuffer.allocate(16);
    int ret;

    final UPNPUrls urls = new UPNPUrls();
    final IGDdatas data = new IGDdatas();

    UPNPDev devlist =
        MiniupnpcLibrary.INSTANCE.upnpDiscover(
            UPNP_DELAY, (String) null, (String) null, 0, 0, IntBuffer.allocate(1));
    if (devlist == null) {
      MiniupnpcLibrary.INSTANCE.FreeUPNPUrls(urls);
      portMapListener.onPortMapError();
      return;
    }
    ret = MiniupnpcLibrary.INSTANCE.UPNP_GetValidIGD(devlist, urls, data, lanaddr, 16);
    if (ret == 0) {
      log.debug("No valid UPNP Internet Gateway Device found.");
      portMapListener.onPortMapError();
      MiniupnpcLibrary.INSTANCE.FreeUPNPUrls(urls);
      devlist.setAutoRead(false);
      MiniupnpcLibrary.INSTANCE.freeUPNPDevlist(devlist);
      return;
    }
    try {

      logIGDResponse(ret, urls);

      log.debug("Local LAN ip address : " + zeroTerminatedString(lanaddr.array()));
      ByteBuffer externalAddress = ByteBuffer.allocate(16);
      MiniupnpcLibrary.INSTANCE.UPNP_GetExternalIPAddress(
          urls.controlURL.getString(0),
          zeroTerminatedString(data.first.servicetype),
          externalAddress);
      publicIp = zeroTerminatedString(externalAddress.array());
      log.debug("ExternalIPAddress = " + publicIp);

      ret =
          MiniupnpcLibrary.INSTANCE.UPNP_AddPortMapping(
              urls.controlURL.getString(0), // controlURL
              zeroTerminatedString(data.first.servicetype), // servicetype
              "" + externalPortRequested, // external Port
              "" + localPort, // internal Port
              zeroTerminatedString(lanaddr.array()), // internal client
              "added via MiniupnpcLibrary.INSTANCE/JAVA !", // description
              prot.toString(), // protocol UDP or TCP
              null, // remote host (useless)
              "0"); // leaseDuration

      if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS) {
        portMapListener.onPortMapError();
        return;
      }

      // get the local port (but didn't we request one?)
      ret =
          MiniupnpcLibrary.INSTANCE.UPNP_GetSpecificPortMappingEntry(
              urls.controlURL.getString(0),
              zeroTerminatedString(data.first.servicetype),
              "" + externalPortRequested,
              prot.toString(),
              intClient,
              intPort,
              desc,
              enabled,
              leaseDuration);

      log.debug(
          "InternalIP:Port = "
              + zeroTerminatedString(intClient.array())
              + ":"
              + zeroTerminatedString(intPort.array())
              + " ("
              + zeroTerminatedString(desc.array())
              + ")");

      model.getInstanceStats().setUsingUPnP(true);

      final UpnpMapping mapping = new UpnpMapping();
      mapping.prot = prot;
      mapping.internalPort = localPort;
      mapping.externalPort = externalPortRequested;
      mappings.add(mapping);
      log.debug("Added mapping. Mappings now: {}", mappings);
    } finally {
      MiniupnpcLibrary.INSTANCE.FreeUPNPUrls(urls);
      devlist.setAutoRead(false);
      MiniupnpcLibrary.INSTANCE.freeUPNPDevlist(devlist);
    }
    portMapListener.onPortMap(externalPortRequested);
  }