Search in sources :

Example 1 with UPnPWANConnectionPortMapping

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

the class UPnPSSWANConnectionImpl method checkMappings.

protected void checkMappings() throws UPnPException {
    if (!recheck_mappings) {
        return;
    }
    List mappings_copy;
    try {
        class_mon.enter();
        mappings_copy = new ArrayList(mappings);
    } finally {
        class_mon.exit();
    }
    UPnPWANConnectionPortMapping[] current = getPortMappings();
    Iterator it = mappings_copy.iterator();
    while (it.hasNext()) {
        portMapping mapping = (portMapping) it.next();
        for (int j = 0; j < current.length; j++) {
            UPnPWANConnectionPortMapping c = current[j];
            if (c.getExternalPort() == mapping.getExternalPort() && c.isTCP() == mapping.isTCP()) {
                it.remove();
                break;
            }
        }
    }
    boolean log = false;
    if (mappings_copy.size() > 0) {
        if (!last_mapping_check_failed) {
            last_mapping_check_failed = true;
            log = true;
        }
    } else {
        last_mapping_check_failed = false;
    }
    it = mappings_copy.iterator();
    while (it.hasNext()) {
        portMapping mapping = (portMapping) it.next();
        try {
            if (log) {
                log("Re-establishing mapping " + mapping.getString());
            }
            addPortMapping(mapping.isTCP(), mapping.getExternalPort(), mapping.getDescription());
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
    }
}
Also used : UPnPWANConnectionPortMapping(com.biglybt.net.upnp.services.UPnPWANConnectionPortMapping) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with UPnPWANConnectionPortMapping

use of com.biglybt.net.upnp.services.UPnPWANConnectionPortMapping 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 3 with UPnPWANConnectionPortMapping

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

the class UPnPPlugin method addService.

protected void addService(UPnPWANConnection wan_service) throws UPnPException {
    wan_service.addListener(this);
    mapping_manager.serviceFound(wan_service);
    log.log("    Found " + (!wan_service.getGenericService().getServiceType().contains("PPP") ? "WANIPConnection" : "WANPPPConnection"));
    UPnPWANConnectionPortMapping[] ports;
    String usn = wan_service.getGenericService().getDevice().getRootDevice().getUSN();
    if (getDeviceStats(usn, STATS_READ_OK) == 0 && getDeviceStats(usn, STATS_READ_BAD) > 2) {
        ports = new UPnPWANConnectionPortMapping[0];
        wan_service.periodicallyRecheckMappings(false);
        log.log("    Not reading port mappings from device due to previous failures");
    } else {
        ports = wan_service.getPortMappings();
    }
    for (int j = 0; j < ports.length; j++) {
        log.log("      mapping [" + j + "] " + ports[j].getExternalPort() + "/" + (ports[j].isTCP() ? "TCP" : "UDP") + " [" + ports[j].getDescription() + "] -> " + ports[j].getInternalHost());
    }
    try {
        this_mon.enter();
        services.add(new UPnPPluginService(wan_service, ports, desc_prefix_param, alert_success_param, grab_ports_param, alert_other_port_param, release_mappings_param));
        if (services.size() > 1) {
            // check this isn't a single device with multiple services
            String new_usn = wan_service.getGenericService().getDevice().getRootDevice().getUSN();
            boolean multiple_found = false;
            for (int i = 0; i < services.size() - 1; i++) {
                UPnPPluginService service = (UPnPPluginService) services.get(i);
                String existing_usn = service.getService().getGenericService().getDevice().getRootDevice().getUSN();
                if (!new_usn.equals(existing_usn)) {
                    multiple_found = true;
                    break;
                }
            }
            if (multiple_found) {
                PluginConfig pc = plugin_interface.getPluginconfig();
                if (!pc.getPluginBooleanParameter("upnp.device.multipledevices.warned", false)) {
                    pc.setPluginParameter("upnp.device.multipledevices.warned", true);
                    String text = MessageText.getString("upnp.alert.multipledevice.warning");
                    log.logAlertRepeatable(LoggerChannel.LT_WARNING, text);
                }
            }
        }
        checkState();
    } finally {
        this_mon.exit();
    }
}
Also used : PluginConfig(com.biglybt.pif.PluginConfig) UPnPWANConnectionPortMapping(com.biglybt.net.upnp.services.UPnPWANConnectionPortMapping)

Aggregations

UPnPWANConnectionPortMapping (com.biglybt.net.upnp.services.UPnPWANConnectionPortMapping)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 UPnPWANConnectionListener (com.biglybt.net.upnp.services.UPnPWANConnectionListener)1 PluginConfig (com.biglybt.pif.PluginConfig)1 Iterator (java.util.Iterator)1