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