LimeWire Consolidated API

com.limegroup.gnutella
Class UPnPManager

java.lang.Object
  extended by com.limegroup.gnutella.UPnPManager

public class UPnPManager
extends Object

Manages the mapping of ports to limewire on UPnP-enabled routers. According to the UPnP Standards, Internet Gateway Devices must have a specific hierarchy. The parts of that hierarchy that we care about are: Device: urn:schemas-upnp-org:device:InternetGatewayDevice:1 SubDevice: urn:schemas-upnp-org:device:WANDevice:1 SubDevice: urn:schemas-upnp-org:device:WANConnectionDevice:1 Service: urn:schemas-upnp-org:service:WANIPConnection:1 Every port mapping is a tuple of: - External address ("" is wildcard) - External port - Internal address - Internal port - Protocol (TCP|UDP) - Description Port mappings can be removed, but that is a blocking network operation which will slow down the shutdown process of Limewire. It is safe to let port mappings persist between limewire sessions. In the meantime however, the NAT may assign a different ip address to the local node. That's why we need to find any previous mappings the node has created and update them with our new address. In order to uniquely distinguish which mappings were made by us, we put part of our client GUID in the description field. For the TCP mapping, we use the following description: "Lime/TCP:" For the UDP mapping, we use "Lime/UDP:" NOTES: Not all NATs support mappings with different external port and internal ports. Therefore if we were unable to map our desired port but were able to map another one, we should pass this information on to Acceptor. Some buggy NATs do not distinguish mappings by the Protocol field. Therefore we first map the UDP port, and then the TCP port since it is more important should the first mapping get overwritten. The cyberlink library uses an internal thread that tries to discover any UPnP devices. After we discover a router or give up on trying to, we should call stop().


Method Summary
 void addListener(UPnPListener uPnPListener)
           
 void clearMappings()
          schedules a shutdown hook which will clear the mappings created this session.
 void finalize()
           
 InetAddress getNATAddress()
           
 boolean isNATPresent()
           
 boolean mappingsExist()
           
 int mapPort(int port, byte[] address)
          adds a mapping on the router to the specified port
 void start()
           
 void stop()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addListener

public void addListener(UPnPListener uPnPListener)

start

public void start()

stop

public void stop()

isNATPresent

public boolean isNATPresent()
Returns:
whether we are behind an UPnP-enabled NAT/router

mappingsExist

public boolean mappingsExist()
Returns:
whether we have created mappings this session

getNATAddress

public InetAddress getNATAddress()
                          throws UnknownHostException
Returns:
the external address the NAT thinks we have. Blocking. null if we can't find it.
Throws:
UnknownHostException

mapPort

public int mapPort(int port,
                   byte[] address)
adds a mapping on the router to the specified port

Returns:
the external port that was actually mapped. 0 if failed

clearMappings

public void clearMappings()
schedules a shutdown hook which will clear the mappings created this session.


finalize

public void finalize()
Overrides:
finalize in class Object

LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.