UPnP: Improve diagnostics

try PPP 2nd
other cleanups
This commit is contained in:
zzz
2018-02-27 13:05:03 +00:00
parent 27042f9930
commit 7035db2bcd

View File

@@ -198,6 +198,9 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
name = "???"; name = "???";
boolean isIGD = ROUTER_DEVICE.equals(dev.getDeviceType()) && dev.isRootDevice(); boolean isIGD = ROUTER_DEVICE.equals(dev.getDeviceType()) && dev.isRootDevice();
name += isIGD ? " IGD" : (" " + dev.getDeviceType()); name += isIGD ? " IGD" : (" " + dev.getDeviceType());
String ip = getIP(dev);
if (ip != null)
name += ' ' + ip;
synchronized (lock) { synchronized (lock) {
if(isDisabled) { if(isDisabled) {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
@@ -289,13 +292,13 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
if (!current2.getDeviceType().equals(WANCON_DEVICE)) if (!current2.getDeviceType().equals(WANCON_DEVICE))
continue; continue;
_service = current2.getService(WAN_PPP_CONNECTION); _service = current2.getService(WAN_IP_CONNECTION);
if(_service == null) { if(_service == null) {
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info(_router.getFriendlyName()+ " doesn't seems to be using PPP; we won't be able to extract bandwidth-related informations out of it."); _log.info(_router.getFriendlyName()+ " does not support WAN_IP_CONNECTION");
_service = current2.getService(WAN_IP_CONNECTION); _service = current2.getService(WAN_PPP_CONNECTION);
if(_service == null) if(_service == null)
_log.error(_router.getFriendlyName()+ " doesn't export WAN_IP_CONNECTION either: we won't be able to use it!"); _log.error(_router.getFriendlyName()+ " doesn't export WAN_PPP_CONNECTION either; we won't be able to use it!");
} }
_serviceLacksAPM = false; _serviceLacksAPM = false;
@@ -563,6 +566,34 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
} }
} }
/**
* @since 0.9.34
*/
private String toLong(String action, String arg, Service serv) {
String rv = toString(action, arg, serv);
if (rv != null && rv.length() > 0) {
try {
long l = Long.parseLong(rv);
rv = DataHelper.formatSize2Decimal(l);
} catch (NumberFormatException nfe) {}
}
return rv;
}
/**
* @since 0.9.34
*/
private String toTime(String action, String arg, Service serv) {
String rv = toString(action, arg, serv);
if (rv != null && rv.length() > 0) {
try {
long l = Long.parseLong(rv);
rv = DataHelper.formatDuration2(l * 1000);
} catch (NumberFormatException nfe) {}
}
return rv;
}
// TODO: extend it! RTFM // TODO: extend it! RTFM
private void listSubServices(Device dev, StringBuilder sb) { private void listSubServices(Device dev, StringBuilder sb) {
ServiceList sl = dev.getServiceList(); ServiceList sl = dev.getServiceList();
@@ -575,69 +606,62 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
sb.append("<li>").append(_t("Service")).append(": "); sb.append("<li>").append(_t("Service")).append(": ");
// NOTE: Group all toString() of common actions together // NOTE: Group all toString() of common actions together
// to avoid excess fetches, since toString() caches. // to avoid excess fetches, since toString() caches.
if("urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1".equals(serv.getServiceType())){ String type = serv.getServiceType();
if("urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1".equals(type)){
sb.append(_t("WAN Common Interface Configuration")); sb.append(_t("WAN Common Interface Configuration"));
sb.append("<ul><li>").append(_t("Status")).append(": ") sb.append("<ul><li>").append(_t("Status")).append(": ")
.append(toString("GetCommonLinkProperties", "NewPhysicalLinkStatus", serv)); .append(toString("GetCommonLinkProperties", "NewPhysicalLinkStatus", serv));
sb.append("<li>").append(_t("Type")).append(": ") sb.append("<li>").append(_t("Type")).append(": ")
.append(toString("GetCommonLinkProperties", "NewWANAccessType", serv)); .append(toString("GetCommonLinkProperties", "NewWANAccessType", serv));
sb.append("<li>").append(_t("Upstream")).append(": ") sb.append("<li>").append(_t("Upstream")).append(": ")
.append(toString("GetCommonLinkProperties", "NewLayer1UpstreamMaxBitRate", serv)); .append(toLong("GetCommonLinkProperties", "NewLayer1UpstreamMaxBitRate", serv)).append("bps");
sb.append("<li>").append(_t("Downstream")).append(": ") sb.append("<li>").append(_t("Downstream")).append(": ")
.append(toString("GetCommonLinkProperties", "NewLayer1DownstreamMaxBitRate", serv)) .append(toLong("GetCommonLinkProperties", "NewLayer1DownstreamMaxBitRate", serv)).append("bps");
.append("</li>"); if (_context.getBooleanProperty(PROP_ADVANCED)) {
}else if("urn:schemas-upnp-org:service:WANPPPConnection:1".equals(serv.getServiceType())){ // don't bother translating
sb.append("<li>").append("Sent: ")
.append(toLong("GetTotalBytesSent", "NewTotalBytesSent", serv)).append('B');
sb.append("<li>").append("Received: ")
.append(toLong("GetTotalBytesReceived", "NewTotalBytesReceived", serv)).append('B');
sb.append("<li>").append("Sent packets: ")
.append(toLong("GetTotalPacketsSent", "NewTotalPacketsSent", serv));
sb.append("<li>").append("Received packets: ")
.append(toLong("GetTotalPacketsReceived", "NewTotalPacketsReceived", serv));
}
}else if(WAN_PPP_CONNECTION.equals(type)){
sb.append(_t("WAN PPP Connection")); sb.append(_t("WAN PPP Connection"));
sb.append("<ul><li>").append(_t("Status")).append(": ") sb.append("<ul><li>").append(_t("Status")).append(": ")
.append(toString("GetStatusInfo", "NewConnectionStatus", serv)); .append(toString("GetStatusInfo", "NewConnectionStatus", serv));
String up = toString("GetStatusInfo", "NewUptime", serv); sb.append("<li>").append(_t("Uptime")).append(": ")
if (up != null) { .append(toTime("GetStatusInfo", "NewUptime", serv));
try {
long uptime = Long.parseLong(up);
uptime *= 1000;
sb.append("<li>").append(_t("Uptime")).append(": ")
.append(DataHelper.formatDuration2(uptime));
} catch (NumberFormatException nfe) {}
}
sb.append("<li>").append(_t("Type")).append(": ") sb.append("<li>").append(_t("Type")).append(": ")
.append(toString("GetConnectionTypeInfo", "NewConnectionType", serv)); .append(toString("GetConnectionTypeInfo", "NewConnectionType", serv));
sb.append("<li>").append(_t("Upstream")).append(": ") sb.append("<li>").append(_t("Upstream")).append(": ")
.append(toString("GetLinkLayerMaxBitRates", "NewUpstreamMaxBitRate", serv)); .append(toLong("GetLinkLayerMaxBitRates", "NewUpstreamMaxBitRate", serv)).append("bps");
sb.append("<li>").append(_t("Downstream")).append(": ") sb.append("<li>").append(_t("Downstream")).append(": ")
.append(toString("GetLinkLayerMaxBitRates", "NewDownstreamMaxBitRate", serv) + "<br>"); .append(toLong("GetLinkLayerMaxBitRates", "NewDownstreamMaxBitRate", serv)).append("bps");
sb.append("<li>").append(_t("External IP")).append(": ") sb.append("<li>").append(_t("External IP")).append(": ")
.append(toString("GetExternalIPAddress", "NewExternalIPAddress", serv)) .append(toString("GetExternalIPAddress", "NewExternalIPAddress", serv));
.append("</li>"); }else if("urn:schemas-upnp-org:service:Layer3Forwarding:1".equals(type)){
}else if("urn:schemas-upnp-org:service:Layer3Forwarding:1".equals(serv.getServiceType())){
sb.append(_t("Layer 3 Forwarding")); sb.append(_t("Layer 3 Forwarding"));
sb.append("<ul><li>").append(_t("Default Connection Service")).append(": ") sb.append("<ul><li>").append(_t("Default Connection Service")).append(": ")
.append(toString("GetDefaultConnectionService", "NewDefaultConnectionService", serv)) .append(toString("GetDefaultConnectionService", "NewDefaultConnectionService", serv));
.append("</li>"); }else if(WAN_IP_CONNECTION.equals(type)){
}else if(WAN_IP_CONNECTION.equals(serv.getServiceType())){
sb.append(_t("WAN IP Connection")); sb.append(_t("WAN IP Connection"));
sb.append("<ul><li>").append(_t("Status")).append(": ") sb.append("<ul><li>").append(_t("Status")).append(": ")
.append(toString("GetStatusInfo", "NewConnectionStatus", serv)); .append(toString("GetStatusInfo", "NewConnectionStatus", serv));
String up = toString("GetStatusInfo", "NewUptime", serv); sb.append("<li>").append(_t("Uptime")).append(": ")
if (up != null) { .append(toTime("GetStatusInfo", "NewUptime", serv));
try {
long uptime = Long.parseLong(up);
uptime *= 1000;
sb.append("<li>").append(_t("Uptime")).append(": ")
.append(DataHelper.formatDuration2(uptime));
} catch (NumberFormatException nfe) {}
}
sb.append("<li>").append(_t("Type")).append(": ") sb.append("<li>").append(_t("Type")).append(": ")
.append(toString("GetConnectionTypeInfo", "NewConnectionType", serv)); .append(toString("GetConnectionTypeInfo", "NewConnectionType", serv));
sb.append("<li>").append(_t("External IP")).append(": ") sb.append("<li>").append(_t("External IP")).append(": ")
.append(toString("GetExternalIPAddress", "NewExternalIPAddress", serv)) .append(toString("GetExternalIPAddress", "NewExternalIPAddress", serv));
.append("</li>"); }else if("urn:schemas-upnp-org:service:WANEthernetLinkConfig:1".equals(type)){
}else if("urn:schemas-upnp-org:service:WANEthernetLinkConfig:1".equals(serv.getServiceType())){
sb.append(_t("WAN Ethernet Link Configuration")); sb.append(_t("WAN Ethernet Link Configuration"));
sb.append("<ul><li>").append(_t("Status")).append(": ") sb.append("<ul><li>").append(_t("Status")).append(": ")
.append(toString("GetEthernetLinkStatus", "NewEthernetLinkStatus", serv)) .append(toString("GetEthernetLinkStatus", "NewEthernetLinkStatus", serv));
.append("</li>");
} else { } else {
sb.append(DataHelper.escapeHTML(serv.getServiceType())).append("<ul>"); sb.append(DataHelper.escapeHTML(type)).append("<ul>");
} }
if (_context.getBooleanProperty(PROP_ADVANCED)) { if (_context.getBooleanProperty(PROP_ADVANCED)) {
sb.append("<li>Actions"); sb.append("<li>Actions");
@@ -657,8 +681,10 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
else else
sb.append("<li>").append(_t("Subdevice")).append(": "); sb.append("<li>").append(_t("Subdevice")).append(": ");
sb.append(DataHelper.escapeHTML(dev.getFriendlyName())); sb.append(DataHelper.escapeHTML(dev.getFriendlyName()));
if (prefix == null) String ip = getIP(dev);
sb.append("</p>"); if (ip != null)
sb.append(' ').append(ip);
sb.append("</p>");
listSubServices(dev, sb); listSubServices(dev, sb);
DeviceList dl = dev.getDeviceList(); DeviceList dl = dev.getDeviceList();
@@ -713,9 +739,9 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
int downstreamMaxBitRate = getDownstreamMaxBitRate(); int downstreamMaxBitRate = getDownstreamMaxBitRate();
int upstreamMaxBitRate = getUpstreamMaxBitRate(); int upstreamMaxBitRate = getUpstreamMaxBitRate();
if(downstreamMaxBitRate > 0) if(downstreamMaxBitRate > 0)
sb.append("<br>").append(_t("UPnP reports the maximum downstream bit rate is {0}bits/sec", DataHelper.formatSize2(downstreamMaxBitRate))); sb.append("<br>").append(_t("UPnP reports the maximum downstream bit rate is {0}bits/sec", DataHelper.formatSize2Decimal(downstreamMaxBitRate)));
if(upstreamMaxBitRate > 0) if(upstreamMaxBitRate > 0)
sb.append("<br>").append(_t("UPnP reports the maximum upstream bit rate is {0}bits/sec", DataHelper.formatSize2(upstreamMaxBitRate))); sb.append("<br>").append(_t("UPnP reports the maximum upstream bit rate is {0}bits/sec", DataHelper.formatSize2Decimal(upstreamMaxBitRate)));
synchronized(lock) { synchronized(lock) {
for(ForwardPort port : portsToForward) { for(ForwardPort port : portsToForward) {
sb.append("<br>"); sb.append("<br>");
@@ -817,6 +843,32 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
return rv; return rv;
} }
/**
* @return IP or null
* @since 0.9.34
*/
private static String getIP(Device dev) {
// see ControlRequest.setRequestHost()
String rv = null;
String him = dev.getURLBase();
if (him != null && him.length() > 0) {
try {
URI url = new URI(him);
rv = url.getHost();
} catch (URISyntaxException use) {}
}
if (rv == null) {
him = dev.getLocation();
if (him != null && him.length() > 0) {
try {
URI url = new URI(him);
rv = url.getHost();
} catch (URISyntaxException use) {}
}
}
return rv;
}
/** /**
* Bug fix: * Bug fix:
* If the SSDP notify or search response sockets listen on more than one interface, * If the SSDP notify or search response sockets listen on more than one interface,
@@ -834,24 +886,7 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
*/ */
private String getOurAddress(String deflt) { private String getOurAddress(String deflt) {
String rv = deflt; String rv = deflt;
String hisIP = null; String hisIP = getIP(_router);
// see ControlRequest.setRequestHost()
String him = _router.getURLBase();
if (him != null && him.length() > 0) {
try {
URI url = new URI(him);
hisIP = url.getHost();
} catch (URISyntaxException use) {}
}
if (hisIP == null) {
him = _router.getLocation();
if (him != null && him.length() > 0) {
try {
URI url = new URI(him);
hisIP = url.getHost();
} catch (URISyntaxException use) {}
}
}
if (hisIP == null) if (hisIP == null)
return rv; return rv;
try { try {
@@ -1087,29 +1122,34 @@ public class UPnP extends ControlPoint implements DeviceChangeListener, EventLis
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
cp.start(); cp.start();
long s2 = System.currentTimeMillis(); long s2 = System.currentTimeMillis();
System.out.println("Start took " + (s2 - start)); System.err.println("Start took " + (s2 - start) + "ms");
System.out.println("Searching for UPnP devices"); System.err.println("Searching for UPnP devices");
start = System.currentTimeMillis(); start = System.currentTimeMillis();
cp.search(); cp.search();
s2 = System.currentTimeMillis(); s2 = System.currentTimeMillis();
System.out.println("Search kickoff took " + (s2 - start)); System.err.println("Search kickoff took " + (s2 - start) + "ms");
System.out.println("Waiting 10 seconds for responses"); System.err.println("Waiting 10 seconds for responses");
Thread.sleep(10000); Thread.sleep(10000);
//while(true) {
DeviceList list = cp.getDeviceList(); DeviceList list = cp.getDeviceList();
System.out.println("Found " + list.size() + " devices!"); if (list.isEmpty()) {
System.err.println("No UPnP devices found");
System.exit(1);
}
System.err.println("Found " + list.size() + " devices.");
System.err.println("Redirect the following output to an html file and view in a browser.");
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Iterator<Device> it = list.iterator(); Iterator<Device> it = list.iterator();
int i = 0; int i = 0;
while(it.hasNext()) { while(it.hasNext()) {
Device device = it.next(); Device device = it.next();
upnp.listSubDev(device.toString(), device, sb); upnp.listSubDev(device.toString(), device, sb);
System.out.println("Here is the listing for device " + (++i) + System.out.println("<h3>Device " + (++i) +
": " + device.getFriendlyName() + " :"); ": " + DataHelper.escapeHTML(device.getFriendlyName()) + "</h3>");
System.out.println(sb.toString()); System.out.println(sb.toString());
sb.setLength(0); sb.setLength(0);
} }
//}
System.exit(0); System.exit(0);
} }