ants.p2p.utils.net
Class UPnPManager
java.lang.Object
org.cybergarage.upnp.ControlPoint
ants.p2p.utils.net.UPnPManager
- All Implemented Interfaces:
- HTTPRequestListener, ActionListener, QueryListener, DeviceChangeListener
public class UPnPManager
- extends ControlPoint
- implements DeviceChangeListener, ActionListener, QueryListener
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: "ANts/TCP:"
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().
Methods inherited from class org.cybergarage.upnp.ControlPoint |
addDeviceChangeListener, addEventListener, addNotifyListener, addSearchResponseListener, getDevice, getDeviceDisposer, getDeviceList, getEventSubURI, getExpiredDeviceMonitoringInterval, getHTTPPort, getRenewSubscriber, getSearchMx, getSSDPPort, getSubscriberService, hasDevice, httpRequestRecieved, isNMPRMode, isSubscribed, lock, notifyReceived, performAddDeviceListener, performEventListener, performNotifyListener, performRemoveDeviceListener, performSearchResponseListener, print, removeDeviceChangeListener, removeEventListener, removeExpiredDevices, removeNotifyListener, removeSearchResponseListener, renewSubscriberService, renewSubscriberService, renewSubscriberService, search, search, search, searchResponseReceived, setDeviceDisposer, setEventSubURI, setExpiredDeviceMonitoringInterval, setHTTPPort, setNMPRMode, setRenewSubscriber, setSearchMx, setSSDPPort, start, start, start, stop, subscribe, subscribe, subscribe, subscribe, unlock, unsubscribe, unsubscribe, unsubscribe |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ANTS_DEVICE_TYPE
public static final java.lang.String ANTS_DEVICE_TYPE
- See Also:
- Constant Field Values
ANTS_LAN_ADDRESS_SERVICE_TYPE
public static final java.lang.String ANTS_LAN_ADDRESS_SERVICE_TYPE
- See Also:
- Constant Field Values
ANTS_LAN_ADDRESS_ACTION_TYPE
public static final java.lang.String ANTS_LAN_ADDRESS_ACTION_TYPE
- See Also:
- Constant Field Values
instance
public static UPnPManager instance()
getDevice
public Device getDevice()
setCurrentLanAddress
public void setCurrentLanAddress(ServerInfo si)
getCurrentLanAddress
public ServerInfo getCurrentLanAddress()
startDevice
public void startDevice()
stopDevice
public void stopDevice()
queryControlReceived
public boolean queryControlReceived(StateVariable stateVar)
- Specified by:
queryControlReceived
in interface QueryListener
actionControlReceived
public boolean actionControlReceived(Action action)
- Specified by:
actionControlReceived
in interface ActionListener
update
public void update(java.lang.String newValue)
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 java.net.InetAddress getNATAddress()
throws java.net.UnknownHostException
- Returns:
- the external address the NAT thinks we have. Blocking.
null if we can't find it.
- Throws:
java.net.UnknownHostException
deviceAdded
public void deviceAdded(Device dev)
- this method will be called when we discover a UPnP device.
- Specified by:
deviceAdded
in interface DeviceChangeListener
mapPort
public int mapPort(java.lang.String address,
int port)
- adds a mapping on the router to the specified port
- Returns:
- the external port that was actually mapped. 0 if failed
clearMappingsOnShutdown
public void clearMappingsOnShutdown()
- schedules a shutdown hook which will clear the mappings created
this session.
finalize
public void finalize()
- Overrides:
finalize
in class ControlPoint
deviceRemoved
public void deviceRemoved(Device dev)
- stub
- Specified by:
deviceRemoved
in interface DeviceChangeListener
getLocalAddress
public static java.net.InetAddress getLocalAddress()
throws java.net.UnknownHostException
- Returns:
- A non-loopback IPv4 address of a network interface on the
local host.
- Throws:
java.net.UnknownHostException