Annotation of embedaddon/miniupnpc/java/JavaBridgeTest.java, revision 1.1
1.1 ! misho 1: import java.nio.ByteBuffer;
! 2: import fr.free.miniupnp.*;
! 3:
! 4: /**
! 5: *
! 6: * @author syuu
! 7: */
! 8: public class JavaBridgeTest {
! 9: public static void main(String[] args) {
! 10: int UPNP_DELAY = 2000;
! 11: MiniupnpcLibrary miniupnpc = MiniupnpcLibrary.INSTANCE;
! 12: UPNPDev devlist = null;
! 13: UPNPUrls urls = new UPNPUrls();
! 14: IGDdatas data = new IGDdatas();
! 15: ByteBuffer lanaddr = ByteBuffer.allocate(16);
! 16: ByteBuffer intClient = ByteBuffer.allocate(16);
! 17: ByteBuffer intPort = ByteBuffer.allocate(6);
! 18: ByteBuffer desc = ByteBuffer.allocate(80);
! 19: ByteBuffer enabled = ByteBuffer.allocate(4);
! 20: ByteBuffer leaseDuration = ByteBuffer.allocate(16);
! 21: int ret;
! 22: int i;
! 23:
! 24: if(args.length < 2) {
! 25: System.err.println("Usage : java [...] JavaBridgeTest port protocol");
! 26: System.out.println(" port is numeric, protocol is TCP or UDP");
! 27: return;
! 28: }
! 29:
! 30: devlist = miniupnpc.upnpDiscover(UPNP_DELAY, (String) null, (String) null, 0, null);
! 31: if (devlist != null) {
! 32: System.out.println("List of UPNP devices found on the network :");
! 33: for (UPNPDev device = devlist; device != null; device = device.pNext) {
! 34: System.out.println("desc: " + device.descURL.getString(0) + " st: " + device.st.getString(0));
! 35: }
! 36: if ((i = miniupnpc.UPNP_GetValidIGD(devlist, urls, data, lanaddr, 16)) != 0) {
! 37: switch (i) {
! 38: case 1:
! 39: System.out.println("Found valid IGD : " + urls.controlURL.getString(0));
! 40: break;
! 41: case 2:
! 42: System.out.println("Found a (not connected?) IGD : " + urls.controlURL.getString(0));
! 43: System.out.println("Trying to continue anyway");
! 44: break;
! 45: case 3:
! 46: System.out.println("UPnP device found. Is it an IGD ? : " + urls.controlURL.getString(0));
! 47: System.out.println("Trying to continue anyway");
! 48: break;
! 49: default:
! 50: System.out.println("Found device (igd ?) : " + urls.controlURL.getString(0));
! 51: System.out.println("Trying to continue anyway");
! 52:
! 53: }
! 54: System.out.println("Local LAN ip address : " + new String(lanaddr.array()));
! 55: ByteBuffer externalAddress = ByteBuffer.allocate(16);
! 56: miniupnpc.UPNP_GetExternalIPAddress(urls.controlURL.getString(0),
! 57: new String(data.first.servicetype), externalAddress);
! 58: System.out.println("ExternalIPAddress = " + new String(externalAddress.array()));
! 59: ret = miniupnpc.UPNP_AddPortMapping(
! 60: urls.controlURL.getString(0), // controlURL
! 61: new String(data.first.servicetype), // servicetype
! 62: args[0], // external Port
! 63: args[0], // internal Port
! 64: new String(lanaddr.array()), // internal client
! 65: "added via miniupnpc/JAVA !", // description
! 66: args[1], // protocol UDP or TCP
! 67: null, // remote host (useless)
! 68: "0"); // leaseDuration
! 69: if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS)
! 70: System.out.println("AddPortMapping() failed with code " + ret);
! 71: ret = miniupnpc.UPNP_GetSpecificPortMappingEntry(
! 72: urls.controlURL.getString(0), new String(data.first.servicetype),
! 73: args[0], args[1], intClient, intPort,
! 74: desc, enabled, leaseDuration);
! 75: if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS)
! 76: System.out.println("GetSpecificPortMappingEntry() failed with code " + ret);
! 77: System.out.println("InternalIP:Port = " +
! 78: new String(intClient.array()) + ":" + new String(intPort.array()) +
! 79: " (" + new String(desc.array()) + ")");
! 80: ret = miniupnpc.UPNP_DeletePortMapping(
! 81: urls.controlURL.getString(0),
! 82: new String(data.first.servicetype),
! 83: args[0], args[1], null);
! 84: if (ret != MiniupnpcLibrary.UPNPCOMMAND_SUCCESS)
! 85: System.out.println("DelPortMapping() failed with code " + ret);
! 86: miniupnpc.FreeUPNPUrls(urls);
! 87: } else {
! 88: System.out.println("No valid UPNP Internet Gateway Device found.");
! 89: }
! 90: miniupnpc.freeUPNPDevlist(devlist);
! 91: } else {
! 92: System.out.println("No IGD UPnP Device found on the network !\n");
! 93: }
! 94: }
! 95: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>