aboutsummaryrefslogtreecommitdiff
path: root/external/miniupnpc/java
diff options
context:
space:
mode:
authorAntonio Juarez <antonio.maria.juarez@live.com>2014-04-09 13:14:35 +0100
committerAntonio Juarez <antonio.maria.juarez@live.com>2014-04-09 13:14:35 +0100
commit9682a15400495ae490543e7e5af242ff5c4c9fa0 (patch)
tree63ba7287d8f679742a244413291d4334fc26b6e6 /external/miniupnpc/java
parentImprovements in JSON RPC (diff)
downloadmonero-9682a15400495ae490543e7e5af242ff5c4c9fa0.tar.xz
Port mapping with UPnP
Diffstat (limited to 'external/miniupnpc/java')
-rwxr-xr-xexternal/miniupnpc/java/JavaBridgeTest.java97
-rwxr-xr-xexternal/miniupnpc/java/testjava.bat8
-rwxr-xr-xexternal/miniupnpc/java/testjava.sh8
3 files changed, 113 insertions, 0 deletions
diff --git a/external/miniupnpc/java/JavaBridgeTest.java b/external/miniupnpc/java/JavaBridgeTest.java
new file mode 100755
index 000000000..a7fa56d8f
--- /dev/null
+++ b/external/miniupnpc/java/JavaBridgeTest.java
@@ -0,0 +1,97 @@
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+
+import fr.free.miniupnp.*;
+
+/**
+ *
+ * @author syuu
+ */
+public class JavaBridgeTest {
+ public static void main(String[] args) {
+ int UPNP_DELAY = 2000;
+ MiniupnpcLibrary miniupnpc = MiniupnpcLibrary.INSTANCE;
+ UPNPDev devlist = null;
+ UPNPUrls urls = new UPNPUrls();
+ IGDdatas data = new IGDdatas();
+ 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;
+ int i;
+
+ if(args.length < 2) {
+ System.err.println("Usage : java [...] JavaBridgeTest port protocol");
+ System.out.println(" port is numeric, protocol is TCP or UDP");
+ return;
+ }
+
+ devlist = miniupnpc.upnpDiscover(UPNP_DELAY, (String) null, (String) null, 0, 0, IntBuffer.allocate(1));
+ if (devlist != null) {
+ System.out.println("List of UPNP devices found on the network :");
+ for (UPNPDev device = devlist; device != null; device = device.pNext) {
+ System.out.println("desc: " + device.descURL.getString(0) + " st: " + device.st.getString(0));
+ }
+ if ((i = miniupnpc.UPNP_GetValidIGD(devlist, urls, data, lanaddr, 16)) != 0) {
+ switch (i) {
+ case 1:
+ System.out.println("Found valid IGD : " + urls.controlURL.getString(0));
+ break;
+ case 2:
+ System.out.println("Found a (not connected?) IGD : " + urls.controlURL.getString(0));
+ System.out.println("Trying to continue anyway");
+ break;
+ case 3:
+ System.out.println("UPnP device found. Is it an IGD ? : " + urls.controlURL.getString(0));
+ System.out.println("Trying to continue anyway");
+ break;
+ default:
+ System.out.println("Found device (igd ?) : " + urls.controlURL.getString(0));
+ System.out.println("Trying to continue anyway");
+
+ }
+ System.out.println("Local LAN ip address : " + new String(lanaddr.array()));
+ ByteBuffer externalAddress = ByteBuffer.allocate(16);
+ miniupnpc.UPNP_GetExternalIPAddress(urls.controlURL.getString(0),
+ new String(data.first.servicetype), externalAddress);
+ System.out.println("ExternalIPAddress = " + new String(externalAddress.array()));
+ ret = miniupnpc.UPNP_AddPortMapping(
+ urls.controlURL.getString(0), // controlURL
+ new String(data.first.servicetype), // servicetype
+ args[0], // external Port
+ args[0], // internal Port
+ new String(lanaddr.array()), // internal client
+ "added via miniupnpc/JAVA !", // description
+ args[1], // protocol UDP or TCP
+ null, // remote host (useless)
+ "0"); // leaseDuration
+ if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS)
+ System.out.println("AddPortMapping() failed with code " + ret);
+ ret = miniupnpc.UPNP_GetSpecificPortMappingEntry(
+ urls.controlURL.getString(0), new String(data.first.servicetype),
+ args[0], args[1], null, intClient, intPort,
+ desc, enabled, leaseDuration);
+ if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS)
+ System.out.println("GetSpecificPortMappingEntry() failed with code " + ret);
+ System.out.println("InternalIP:Port = " +
+ new String(intClient.array()) + ":" + new String(intPort.array()) +
+ " (" + new String(desc.array()) + ")");
+ ret = miniupnpc.UPNP_DeletePortMapping(
+ urls.controlURL.getString(0),
+ new String(data.first.servicetype),
+ args[0], args[1], null);
+ if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS)
+ System.out.println("DelPortMapping() failed with code " + ret);
+ miniupnpc.FreeUPNPUrls(urls);
+ } else {
+ System.out.println("No valid UPNP Internet Gateway Device found.");
+ }
+ miniupnpc.freeUPNPDevlist(devlist);
+ } else {
+ System.out.println("No IGD UPnP Device found on the network !\n");
+ }
+ }
+}
diff --git a/external/miniupnpc/java/testjava.bat b/external/miniupnpc/java/testjava.bat
new file mode 100755
index 000000000..b836da149
--- /dev/null
+++ b/external/miniupnpc/java/testjava.bat
@@ -0,0 +1,8 @@
+@echo off
+set JAVA=java
+set JAVAC=javac
+REM notice the semicolon for Windows. Write once, run ... oh nevermind
+set CP=miniupnpc_win32.jar;.
+
+%JAVAC% -cp "%CP%" JavaBridgeTest.java || exit 1
+%JAVA% -cp "%CP%" JavaBridgeTest 12345 UDP || exit 1
diff --git a/external/miniupnpc/java/testjava.sh b/external/miniupnpc/java/testjava.sh
new file mode 100755
index 000000000..9880523a1
--- /dev/null
+++ b/external/miniupnpc/java/testjava.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+JAVA=java
+JAVAC=javac
+CP=$(for i in *.jar; do echo -n $i:; done).
+
+$JAVAC -cp $CP JavaBridgeTest.java || exit 1
+$JAVA -cp $CP JavaBridgeTest 12345 UDP || exit 1