Search in sources :

Example 1 with UPnPWANConnectionListener

use of com.biglybt.net.upnp.services.UPnPWANConnectionListener in project BiglyBT by BiglySoftware.

the class UPnPSSWANConnectionImpl method getPortMappings.

@Override
public UPnPWANConnectionPortMapping[] getPortMappings() throws UPnPException {
    boolean ok = true;
    try {
        // UPnPStateVariable noe = service.getStateVariable("PortMappingNumberOfEntries");
        // System.out.println( "NOE = " + noe.getValue());
        // Integer.parseInt( noe.getValue());
        int entries = 0;
        // some routers (e.g. Gudy's) return 0 here whatever!
        // In this case take mindless approach
        // hmm, even for my router the state variable isn't accurate...
        UPnPAction act = service.getAction("GetGenericPortMappingEntry");
        if (act == null) {
            log("Action 'GetGenericPortMappingEntry' not supported, can't enumerate bindings");
            return (new UPnPWANConnectionPortMapping[0]);
        } else {
            List res = new ArrayList();
            // I've also seen some routers loop here rather than failing when the index gets too large (they
            // seem to keep returning the last entry) - check for a duplicate entry and exit if found
            portMapping prev_mapping = null;
            for (int i = 0; i < (entries == 0 ? 512 : entries); i++) {
                UPnPActionInvocation inv = act.getInvocation();
                inv.addArgument("NewPortMappingIndex", "" + i);
                try {
                    UPnPActionArgument[] outs = inv.invoke();
                    int port = 0;
                    boolean tcp = false;
                    String internal_host = null;
                    String description = "";
                    for (int j = 0; j < outs.length; j++) {
                        UPnPActionArgument out = outs[j];
                        String out_name = out.getName();
                        if (out_name.equalsIgnoreCase("NewExternalPort")) {
                            port = Integer.parseInt(out.getValue());
                        } else if (out_name.equalsIgnoreCase("NewProtocol")) {
                            tcp = out.getValue().equalsIgnoreCase("TCP");
                        } else if (out_name.equalsIgnoreCase("NewInternalClient")) {
                            internal_host = out.getValue();
                        } else if (out_name.equalsIgnoreCase("NewPortMappingDescription")) {
                            description = out.getValue();
                        }
                    }
                    if (prev_mapping != null) {
                        if (prev_mapping.getExternalPort() == port && prev_mapping.isTCP() == tcp) {
                            break;
                        }
                    }
                    prev_mapping = new portMapping(port, tcp, internal_host, description);
                    res.add(prev_mapping);
                } catch (UPnPException e) {
                    if (entries == 0) {
                        break;
                    }
                    ok = false;
                    throw (e);
                }
            }
            UPnPWANConnectionPortMapping[] res2 = new UPnPWANConnectionPortMapping[res.size()];
            res.toArray(res2);
            return (res2);
        }
    } finally {
        for (int i = 0; i < listeners.size(); i++) {
            UPnPWANConnectionListener listener = (UPnPWANConnectionListener) listeners.get(i);
            try {
                listener.mappingsReadResult(this, ok);
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) UPnPWANConnectionListener(com.biglybt.net.upnp.services.UPnPWANConnectionListener) UPnPWANConnectionPortMapping(com.biglybt.net.upnp.services.UPnPWANConnectionPortMapping) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with UPnPWANConnectionListener

use of com.biglybt.net.upnp.services.UPnPWANConnectionListener in project BiglyBT by BiglySoftware.

the class UPnPSSWANConnectionImpl method addPortMapping.

@Override
public void addPortMapping(// false -> UDP
boolean tcp, int port, String description) throws UPnPException {
    UPnPAction act = service.getAction("AddPortMapping");
    if (act == null) {
        log("Action 'AddPortMapping' not supported, binding not established");
    } else {
        UPnPActionInvocation add_inv = act.getInvocation();
        // "" = wildcard for hosts, 0 = wildcard for ports
        add_inv.addArgument("NewRemoteHost", "");
        add_inv.addArgument("NewExternalPort", "" + port);
        add_inv.addArgument("NewProtocol", tcp ? "TCP" : "UDP");
        add_inv.addArgument("NewInternalPort", "" + port);
        add_inv.addArgument("NewInternalClient", service.getDevice().getRootDevice().getLocalAddress().getHostAddress());
        add_inv.addArgument("NewEnabled", "1");
        add_inv.addArgument("NewPortMappingDescription", description);
        // 0 -> infinite (?)
        add_inv.addArgument("NewLeaseDuration", "0");
        boolean ok = false;
        try {
            add_inv.invoke();
            ok = true;
        } catch (UPnPException original_error) {
            try {
                log("Problem when adding port mapping - will try to see if an existing mapping is in the way");
                deletePortMapping(tcp, port);
            } catch (Throwable e) {
                throw (original_error);
            }
            add_inv.invoke();
            ok = true;
        } finally {
            ((UPnPRootDeviceImpl) service.getDevice().getRootDevice()).portMappingResult(ok);
            for (int i = 0; i < listeners.size(); i++) {
                UPnPWANConnectionListener listener = (UPnPWANConnectionListener) listeners.get(i);
                try {
                    listener.mappingResult(this, ok);
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
            }
        }
        try {
            class_mon.enter();
            Iterator it = mappings.iterator();
            while (it.hasNext()) {
                portMapping m = (portMapping) it.next();
                if (m.getExternalPort() == port && m.isTCP() == tcp) {
                    it.remove();
                }
            }
            mappings.add(new portMapping(port, tcp, "", description));
        } finally {
            class_mon.exit();
        }
    }
}
Also used : UPnPRootDeviceImpl(com.biglybt.net.upnp.impl.device.UPnPRootDeviceImpl) Iterator(java.util.Iterator) UPnPWANConnectionListener(com.biglybt.net.upnp.services.UPnPWANConnectionListener)

Aggregations

UPnPWANConnectionListener (com.biglybt.net.upnp.services.UPnPWANConnectionListener)2 UPnPRootDeviceImpl (com.biglybt.net.upnp.impl.device.UPnPRootDeviceImpl)1 UPnPWANConnectionPortMapping (com.biglybt.net.upnp.services.UPnPWANConnectionPortMapping)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1