less terrible warning if uPnP fails

pull/1/head
Michael Christen 13 years ago
parent 0d1042363c
commit 943b670738

@ -47,23 +47,33 @@
*/ */
package net.yacy.upnp; package net.yacy.upnp;
import java.io.*; import java.io.IOException;
import java.net.*; import java.net.DatagramPacket;
import java.util.*; import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.apache.commons.logging.*; import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** /**
* SSDP messages listener Thread, notify registered objects implementing the interface DiscoveryEventHandler</br> * SSDP messages listener Thread, notify registered objects implementing the interface
* when a device joins the networks or leaves it.<br/> * DiscoveryEventHandler</br> when a device joins the networks or leaves it.<br/>
* The listener thread is set to only accept matching device description and broadcast message sender IP * The listener thread is set to only accept matching device description and broadcast message sender IP to
* to avoid a security flaw with the protocol. If you are not happy with such behaviour * avoid a security flaw with the protocol. If you are not happy with such behaviour you can set the
* you can set the net.yacy.upnp.ddos.matchip system property to false to avoid this check. * net.yacy.upnp.ddos.matchip system property to false to avoid this check.
*
* @author <a href="mailto:superbonbon@sbbi.net">SuperBonBon</a> * @author <a href="mailto:superbonbon@sbbi.net">SuperBonBon</a>
* @version 1.0 * @version 1.0
*/ */
public class DiscoveryAdvertisement implements Runnable { public class DiscoveryAdvertisement implements Runnable
{
private final static Log log = LogFactory.getLog(DiscoveryAdvertisement.class); private final static Log log = LogFactory.getLog(DiscoveryAdvertisement.class);
@ -71,7 +81,9 @@ public class DiscoveryAdvertisement implements Runnable {
static { static {
String prop = System.getProperty("net.yacy.upnp.ddos.matchip"); String prop = System.getProperty("net.yacy.upnp.ddos.matchip");
if ( prop != null && prop.equals( "false" ) ) MATCH_IP = false; if ( prop != null && prop.equals("false") ) {
MATCH_IP = false;
}
} }
private static final int DEFAULT_TIMEOUT = 250; private static final int DEFAULT_TIMEOUT = 250;
@ -83,9 +95,11 @@ public class DiscoveryAdvertisement implements Runnable {
private final static String NTS_SSDP_BYE_BYE = "ssdp:byebye"; private final static String NTS_SSDP_BYE_BYE = "ssdp:byebye";
private final static String NT_ALL_EVENTS = "DiscoveryAdvertisement:nt:allevents"; private final static String NT_ALL_EVENTS = "DiscoveryAdvertisement:nt:allevents";
private Map<String, Set<DiscoveryEventHandler>> byeByeRegistered = new HashMap<String, Set<DiscoveryEventHandler>>(); private final Map<String, Set<DiscoveryEventHandler>> byeByeRegistered =
private Map<String, Set<DiscoveryEventHandler>> aliveRegistered = new HashMap<String, Set<DiscoveryEventHandler>>(); new HashMap<String, Set<DiscoveryEventHandler>>();
private Map<String, InetAddress> USNPerIP = new HashMap<String, InetAddress>(); private final Map<String, Set<DiscoveryEventHandler>> aliveRegistered =
new HashMap<String, Set<DiscoveryEventHandler>>();
private final Map<String, InetAddress> USNPerIP = new HashMap<String, InetAddress>();
private final Object REGISTRATION_PROCESS = new Object(); private final Object REGISTRATION_PROCESS = new Object();
@ -109,29 +123,35 @@ public class DiscoveryAdvertisement implements Runnable {
/** /**
* Registers an event category sent by UPNP devices * Registers an event category sent by UPNP devices
* @param notificationEvent the event type, either DiscoveryAdvertisement.EVENT_SSDP_ALIVE *
* or DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE * @param notificationEvent the event type, either DiscoveryAdvertisement.EVENT_SSDP_ALIVE or
* @param nt the type of device advertisement, upnp:rootdevice will return you all advertisement in relation with nt upnp:rootdevice * DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE
* a null value specify that all nt type are wanted * @param nt the type of device advertisement, upnp:rootdevice will return you all advertisement in
* relation with nt upnp:rootdevice a null value specify that all nt type are wanted
* @param eventHandler the events handler, this objet will receive notifications.. * @param eventHandler the events handler, this objet will receive notifications..
* @throws IOException if an error ocurs when the SSDP events listeners threads starts * @throws IOException if an error ocurs when the SSDP events listeners threads starts
*/ */
public void registerEvent( int notificationEvent, String nt, DiscoveryEventHandler eventHandler ) throws IOException { public void registerEvent(int notificationEvent, String nt, DiscoveryEventHandler eventHandler)
synchronized( REGISTRATION_PROCESS ) { throws IOException {
if ( !inService ) startDevicesListenerThread(); synchronized ( this.REGISTRATION_PROCESS ) {
if ( nt == null ) nt = NT_ALL_EVENTS; if ( !this.inService ) {
startDevicesListenerThread();
}
if ( nt == null ) {
nt = NT_ALL_EVENTS;
}
if ( notificationEvent == EVENT_SSDP_ALIVE ) { if ( notificationEvent == EVENT_SSDP_ALIVE ) {
Set<DiscoveryEventHandler> handlers = aliveRegistered.get( nt ); Set<DiscoveryEventHandler> handlers = this.aliveRegistered.get(nt);
if ( handlers == null ) { if ( handlers == null ) {
handlers = new HashSet<DiscoveryEventHandler>(); handlers = new HashSet<DiscoveryEventHandler>();
aliveRegistered.put( nt, handlers ); this.aliveRegistered.put(nt, handlers);
} }
handlers.add(eventHandler); handlers.add(eventHandler);
} else if ( notificationEvent == EVENT_SSDP_BYE_BYE ) { } else if ( notificationEvent == EVENT_SSDP_BYE_BYE ) {
Set<DiscoveryEventHandler> handlers = byeByeRegistered.get( nt ); Set<DiscoveryEventHandler> handlers = this.byeByeRegistered.get(nt);
if ( handlers == null ) { if ( handlers == null ) {
handlers = new HashSet<DiscoveryEventHandler>(); handlers = new HashSet<DiscoveryEventHandler>();
byeByeRegistered.put( nt, handlers ); this.byeByeRegistered.put(nt, handlers);
} }
handlers.add(eventHandler); handlers.add(eventHandler);
} else { } else {
@ -142,35 +162,38 @@ public class DiscoveryAdvertisement implements Runnable {
/** /**
* Unregisters an event category sent by UPNP devices * Unregisters an event category sent by UPNP devices
* @param notificationEvent the event type, either DiscoveryAdvertisement.EVENT_SSDP_ALIVE *
* or DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE * @param notificationEvent the event type, either DiscoveryAdvertisement.EVENT_SSDP_ALIVE or
* @param nt the type of device advertisement, upnp:rootdevice will unregister all advertisement in relation with nt upnp:rootdevice * DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE
* a null value specify that all nt type are unregistered * @param nt the type of device advertisement, upnp:rootdevice will unregister all advertisement in
* relation with nt upnp:rootdevice a null value specify that all nt type are unregistered
* @param eventHandler the events handler that needs to be unregistred. * @param eventHandler the events handler that needs to be unregistred.
*/ */
public void unRegisterEvent(int notificationEvent, String nt, DiscoveryEventHandler eventHandler) { public void unRegisterEvent(int notificationEvent, String nt, DiscoveryEventHandler eventHandler) {
synchronized( REGISTRATION_PROCESS ) { synchronized ( this.REGISTRATION_PROCESS ) {
if ( nt == null ) nt = NT_ALL_EVENTS; if ( nt == null ) {
nt = NT_ALL_EVENTS;
}
if ( notificationEvent == EVENT_SSDP_ALIVE ) { if ( notificationEvent == EVENT_SSDP_ALIVE ) {
Set<DiscoveryEventHandler> handlers = aliveRegistered.get( nt ); Set<DiscoveryEventHandler> handlers = this.aliveRegistered.get(nt);
if ( handlers != null ) { if ( handlers != null ) {
handlers.remove(eventHandler); handlers.remove(eventHandler);
if ( handlers.isEmpty() ) { if ( handlers.isEmpty() ) {
aliveRegistered.remove( nt ); this.aliveRegistered.remove(nt);
} }
} }
} else if ( notificationEvent == EVENT_SSDP_BYE_BYE ) { } else if ( notificationEvent == EVENT_SSDP_BYE_BYE ) {
Set<DiscoveryEventHandler> handlers = byeByeRegistered.get( nt ); Set<DiscoveryEventHandler> handlers = this.byeByeRegistered.get(nt);
if ( handlers != null ) { if ( handlers != null ) {
handlers.remove(eventHandler); handlers.remove(eventHandler);
if ( handlers.isEmpty() ) { if ( handlers.isEmpty() ) {
byeByeRegistered.remove( nt ); this.byeByeRegistered.remove(nt);
} }
} }
} else { } else {
throw new IllegalArgumentException("Unknown notificationEvent type"); throw new IllegalArgumentException("Unknown notificationEvent type");
} }
if (aliveRegistered.isEmpty() && byeByeRegistered.isEmpty()) { if ( this.aliveRegistered.isEmpty() && this.byeByeRegistered.isEmpty() ) {
stopDevicesListenerThread(); stopDevicesListenerThread();
} }
} }
@ -178,13 +201,13 @@ public class DiscoveryAdvertisement implements Runnable {
private void startDevicesListenerThread() throws IOException { private void startDevicesListenerThread() throws IOException {
synchronized ( singleton ) { synchronized ( singleton ) {
if ( !inService ) { if ( !this.inService ) {
this.startMultiCastSocket(); this.startMultiCastSocket();
Thread deamon = new Thread(this, "DiscoveryAdvertisement daemon"); Thread deamon = new Thread(this, "DiscoveryAdvertisement daemon");
deamon.setDaemon( daemon ); deamon.setDaemon(this.daemon);
deamon.start(); deamon.start();
// wait for the thread to be started // wait for the thread to be started
while( !inService ) { while ( !this.inService ) {
// let's wait a few ms // let's wait a few ms
try { try {
Thread.sleep(2); Thread.sleep(2);
@ -198,47 +221,49 @@ public class DiscoveryAdvertisement implements Runnable {
private void stopDevicesListenerThread() { private void stopDevicesListenerThread() {
synchronized ( singleton ) { synchronized ( singleton ) {
inService = false; this.inService = false;
} }
} }
private void startMultiCastSocket() throws IOException { private void startMultiCastSocket() throws IOException {
skt = new java.net.MulticastSocket( null ); this.skt = new java.net.MulticastSocket(null);
skt.bind( new InetSocketAddress( InetAddress.getByName( "0.0.0.0" ), Discovery.SSDP_PORT ) ); this.skt.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), Discovery.SSDP_PORT));
skt.setTimeToLive( Discovery.DEFAULT_TTL ); this.skt.setTimeToLive(Discovery.DEFAULT_TTL);
skt.setSoTimeout( DEFAULT_TIMEOUT ); this.skt.setSoTimeout(DEFAULT_TIMEOUT);
skt.joinGroup( InetAddress.getByName( Discovery.SSDP_IP ) ); this.skt.joinGroup(InetAddress.getByName(Discovery.SSDP_IP));
byte[] buf = new byte[2048]; byte[] buf = new byte[2048];
input = new DatagramPacket( buf, buf.length ); this.input = new DatagramPacket(buf, buf.length);
} }
@Override
public void run() { public void run() {
if ( !Thread.currentThread().getName().equals("DiscoveryAdvertisement daemon") ) { if ( !Thread.currentThread().getName().equals("DiscoveryAdvertisement daemon") ) {
throw new RuntimeException("No right to call this method"); throw new RuntimeException("No right to call this method");
} }
inService = true; this.inService = true;
while ( inService ) { while ( this.inService ) {
try { try {
listenBroadCast(); listenBroadCast();
} catch ( SocketTimeoutException ex ) { } catch ( SocketTimeoutException ex ) {
// ignoring // ignoring
} catch ( IOException ioEx ) { } catch ( IOException ioEx ) {
log.error( "IO Exception during UPNP DiscoveryAdvertisement messages listening thread", ioEx ); log.warn("IO Exception during UPNP DiscoveryAdvertisement messages listening thread");
} catch ( Exception ex ) { } catch ( Exception ex ) {
log.error( "Fatal Error during UPNP DiscoveryAdvertisement messages listening thread, thread will exit", ex ); log
inService = false; .warn("Fatal Error during UPNP DiscoveryAdvertisement messages listening thread, thread will exit");
aliveRegistered.clear(); this.inService = false;
byeByeRegistered.clear(); this.aliveRegistered.clear();
USNPerIP.clear(); this.byeByeRegistered.clear();
this.USNPerIP.clear();
} }
} }
try { try {
skt.leaveGroup( InetAddress.getByName( Discovery.SSDP_IP ) ); this.skt.leaveGroup(InetAddress.getByName(Discovery.SSDP_IP));
skt.close(); this.skt.close();
} catch ( Exception ex ) { } catch ( Exception ex ) {
// ignoring // ignoring
} }
@ -246,75 +271,91 @@ public class DiscoveryAdvertisement implements Runnable {
private void listenBroadCast() throws IOException { private void listenBroadCast() throws IOException {
skt.receive( input ); this.skt.receive(this.input);
InetAddress from = input.getAddress(); InetAddress from = this.input.getAddress();
String received = new String( input.getData(), input.getOffset(), input.getLength() ); String received = new String(this.input.getData(), this.input.getOffset(), this.input.getLength());
HttpResponse msg = null; HttpResponse msg = null;
try { try {
msg = new HttpResponse(received); msg = new HttpResponse(received);
} catch ( IllegalArgumentException ex ) { } catch ( IllegalArgumentException ex ) {
// crappy http sent // crappy http sent
if ( log.isDebugEnabled() ) log.debug( "Skipping uncompliant HTTP message " + received ); if ( log.isDebugEnabled() ) {
log.debug("Skipping uncompliant HTTP message " + received);
}
return; return;
} }
String header = msg.getHeader(); String header = msg.getHeader();
if ( header != null && header.startsWith("NOTIFY") ) { if ( header != null && header.startsWith("NOTIFY") ) {
if ( log.isDebugEnabled() ) log.debug( received ); if ( log.isDebugEnabled() ) {
log.debug(received);
}
String ntsField = msg.getHTTPHeaderField("nts"); String ntsField = msg.getHTTPHeaderField("nts");
if ( ntsField == null || ntsField.trim().length() == 0 ) { if ( ntsField == null || ntsField.trim().length() == 0 ) {
if ( log.isDebugEnabled() ) log.debug( "Skipping SSDP message, missing HTTP header 'ntsField' field" ); if ( log.isDebugEnabled() ) {
log.debug("Skipping SSDP message, missing HTTP header 'ntsField' field");
}
return; return;
} }
if ( ntsField.equals(NTS_SSDP_ALIVE) ) { if ( ntsField.equals(NTS_SSDP_ALIVE) ) {
String deviceDescrLoc = msg.getHTTPHeaderField("location"); String deviceDescrLoc = msg.getHTTPHeaderField("location");
if ( deviceDescrLoc == null || deviceDescrLoc.trim().length() == 0 ) { if ( deviceDescrLoc == null || deviceDescrLoc.trim().length() == 0 ) {
if ( log.isDebugEnabled() ) log.debug( "Skipping SSDP message, missing HTTP header 'location' field" ); if ( log.isDebugEnabled() ) {
log.debug("Skipping SSDP message, missing HTTP header 'location' field");
}
return; return;
} }
URL loc = new URL(deviceDescrLoc); URL loc = new URL(deviceDescrLoc);
if ( MATCH_IP ) { if ( MATCH_IP ) {
InetAddress locHost = InetAddress.getByName(loc.getHost()); InetAddress locHost = InetAddress.getByName(loc.getHost());
if ( !from.equals(locHost) ) { if ( !from.equals(locHost) ) {
log.warn( "Discovery message sender IP " + from + log.warn("Discovery message sender IP "
" does not match device description IP " + locHost + + from
" skipping message, set the net.yacy.upnp.ddos.matchip system property" + + " does not match device description IP "
" to false to avoid this check" ); + locHost
+ " skipping message, set the net.yacy.upnp.ddos.matchip system property"
+ " to false to avoid this check");
return; return;
} }
} }
String nt = msg.getHTTPHeaderField("nt"); String nt = msg.getHTTPHeaderField("nt");
if ( nt == null || nt.trim().length() == 0 ) { if ( nt == null || nt.trim().length() == 0 ) {
if ( log.isDebugEnabled() ) log.debug( "Skipping SSDP message, missing HTTP header 'nt' field" ); if ( log.isDebugEnabled() ) {
log.debug("Skipping SSDP message, missing HTTP header 'nt' field");
}
return; return;
} }
String maxAge = msg.getHTTPFieldElement("Cache-Control", "max-age"); String maxAge = msg.getHTTPFieldElement("Cache-Control", "max-age");
if ( maxAge == null || maxAge.trim().length() == 0 ) { if ( maxAge == null || maxAge.trim().length() == 0 ) {
if ( log.isDebugEnabled() ) log.debug( "Skipping SSDP message, missing HTTP header 'max-age' field" ); if ( log.isDebugEnabled() ) {
log.debug("Skipping SSDP message, missing HTTP header 'max-age' field");
}
return; return;
} }
String usn = msg.getHTTPHeaderField("usn"); String usn = msg.getHTTPHeaderField("usn");
if ( usn == null || usn.trim().length() == 0 ) { if ( usn == null || usn.trim().length() == 0 ) {
if ( log.isDebugEnabled() ) log.debug( "Skipping SSDP message, missing HTTP header 'usn' field" ); if ( log.isDebugEnabled() ) {
log.debug("Skipping SSDP message, missing HTTP header 'usn' field");
}
return; return;
} }
USNPerIP.put( usn, from ); this.USNPerIP.put(usn, from);
String udn = usn; String udn = usn;
int index = udn.indexOf("::"); int index = udn.indexOf("::");
if ( index != -1 ) udn = udn.substring( 0, index ); if ( index != -1 ) {
synchronized( REGISTRATION_PROCESS ) { udn = udn.substring(0, index);
Set<DiscoveryEventHandler> handlers = aliveRegistered.get( NT_ALL_EVENTS ); }
synchronized ( this.REGISTRATION_PROCESS ) {
Set<DiscoveryEventHandler> handlers = this.aliveRegistered.get(NT_ALL_EVENTS);
if ( handlers != null ) { if ( handlers != null ) {
for ( Iterator<DiscoveryEventHandler> i = handlers.iterator(); i.hasNext(); ) { for ( DiscoveryEventHandler eventHandler : handlers ) {
DiscoveryEventHandler eventHandler = i.next();
eventHandler.eventSSDPAlive(usn, udn, nt, maxAge, loc); eventHandler.eventSSDPAlive(usn, udn, nt, maxAge, loc);
} }
} }
handlers = aliveRegistered.get( nt ); handlers = this.aliveRegistered.get(nt);
if ( handlers != null ) { if ( handlers != null ) {
for ( Iterator<DiscoveryEventHandler> i = handlers.iterator(); i.hasNext(); ) { for ( DiscoveryEventHandler eventHandler : handlers ) {
DiscoveryEventHandler eventHandler = i.next();
eventHandler.eventSSDPAlive(usn, udn, nt, maxAge, loc); eventHandler.eventSSDPAlive(usn, udn, nt, maxAge, loc);
} }
} }
@ -322,16 +363,20 @@ public class DiscoveryAdvertisement implements Runnable {
} else if ( ntsField.equals(NTS_SSDP_BYE_BYE) ) { } else if ( ntsField.equals(NTS_SSDP_BYE_BYE) ) {
String usn = msg.getHTTPHeaderField("usn"); String usn = msg.getHTTPHeaderField("usn");
if ( usn == null || usn.trim().length() == 0 ) { if ( usn == null || usn.trim().length() == 0 ) {
if ( log.isDebugEnabled() ) log.debug( "Skipping SSDP message, missing HTTP header 'usn' field" ); if ( log.isDebugEnabled() ) {
log.debug("Skipping SSDP message, missing HTTP header 'usn' field");
}
return; return;
} }
String nt = msg.getHTTPHeaderField("nt"); String nt = msg.getHTTPHeaderField("nt");
if ( nt == null || nt.trim().length() == 0 ) { if ( nt == null || nt.trim().length() == 0 ) {
if ( log.isDebugEnabled() ) log.debug( "Skipping SSDP message, missing HTTP header 'nt' field" ); if ( log.isDebugEnabled() ) {
log.debug("Skipping SSDP message, missing HTTP header 'nt' field");
}
return; return;
} }
InetAddress originalAliveSenderIp = USNPerIP.get( usn ); InetAddress originalAliveSenderIp = this.USNPerIP.get(usn);
if ( originalAliveSenderIp != null ) { if ( originalAliveSenderIp != null ) {
// we check that the sender ip of message for the usn // we check that the sender ip of message for the usn
// match the sender ip of the alive message for wich the usn // match the sender ip of the alive message for wich the usn
@ -345,25 +390,29 @@ public class DiscoveryAdvertisement implements Runnable {
String udn = usn; String udn = usn;
int index = udn.indexOf("::"); int index = udn.indexOf("::");
if ( index != -1 ) udn = udn.substring( 0, index ); if ( index != -1 ) {
synchronized( REGISTRATION_PROCESS ) { udn = udn.substring(0, index);
Set<DiscoveryEventHandler> handlers = byeByeRegistered.get( NT_ALL_EVENTS ); }
synchronized ( this.REGISTRATION_PROCESS ) {
Set<DiscoveryEventHandler> handlers = this.byeByeRegistered.get(NT_ALL_EVENTS);
if ( handlers != null ) { if ( handlers != null ) {
for ( Iterator<DiscoveryEventHandler> i = handlers.iterator(); i.hasNext(); ) { for ( DiscoveryEventHandler eventHandler : handlers ) {
DiscoveryEventHandler eventHandler = i.next();
eventHandler.eventSSDPByeBye(usn, udn, nt); eventHandler.eventSSDPByeBye(usn, udn, nt);
} }
} }
handlers = byeByeRegistered.get( nt ); handlers = this.byeByeRegistered.get(nt);
if ( handlers != null ) { if ( handlers != null ) {
for ( Iterator<DiscoveryEventHandler> i = handlers.iterator(); i.hasNext(); ) { for ( DiscoveryEventHandler eventHandler : handlers ) {
DiscoveryEventHandler eventHandler = i.next();
eventHandler.eventSSDPByeBye(usn, udn, nt); eventHandler.eventSSDPByeBye(usn, udn, nt);
} }
} }
} }
} else { } else {
log.warn( "Unvalid NTS field value (" + ntsField + ") received in NOTIFY message :" + received ); log
.warn("Unvalid NTS field value ("
+ ntsField
+ ") received in NOTIFY message :"
+ received);
} }
} }
} }

Loading…
Cancel
Save