JavaBridgeTest.java 4.8 KB

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