JavaBridgeTest.java 4.4 KB

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