Permalink
Browse files

indentation and imports cleaned up

  • Loading branch information...
1 parent d79042c commit 34d0a5a42597af636eac4075bf9bc8bfb62b6d17 @forrestv forrestv committed Aug 10, 2011
@@ -18,8 +18,6 @@
from twisted.internet.protocol import DatagramProtocol
from twisted.internet.error import CannotListenError
-from twisted.internet.interfaces import IReactorMulticast
-from twisted.python import log
from nattraverso.utils import is_rfc1918_ip, is_bogus_ip
@@ -16,103 +16,103 @@
# Public API
def get_port_mapper(proto="TCP"):
- """
- Returns a L{NATMapper} instance, suited to map a port for
- the given protocol. Defaults to TCP.
-
- For the moment, only upnp mapper is available. It accepts both UDP and TCP.
-
- @param proto: The protocol: 'TCP' or 'UDP'
- @type proto: string
- @return: A deferred called with a L{NATMapper} instance
- @rtype: L{twisted.internet.defer.Deferred}
- """
- import nattraverso.pynupnp
- return nattraverso.pynupnp.get_port_mapper()
-
-class NATMapper:
- """
- Define methods to map port objects (as returned by twisted's listenXX).
- This allows NAT to be traversed from incoming packets.
-
- Currently the only implementation of this class is the UPnP Mapper, which
- can map UDP and TCP ports, if an UPnP Device exists.
- """
- def __init__(self):
- raise NotImplementedError("Cannot instantiate the class")
-
- def map(self, port):
- """
- Create a mapping for the given twisted's port object.
-
- The deferred will call back with a tuple (extaddr, extport):
- - extaddr: The ip string of the external ip address of this host
- - extport: the external port number used to map the given Port object
-
- When called multiple times with the same Port,
- callback with the existing mapping.
-
- @param port: The port object to map
- @type port: a L{twisted.internet.interfaces.IListeningPort} object
- @return: A deferred called with the above defined tuple
- @rtype: L{twisted.internet.defer.Deferred}
- """
- raise NotImplementedError
-
- def info(self, port):
- """
- Returns the existing mapping for the given port object. That means map()
- has to be called before.
-
- @param port: The port object to retreive info from
- @type port: a L{twisted.internet.interfaces.IListeningPort} object
- @raise ValueError: When there is no such existing mapping
- @return: a tuple (extaddress, extport).
- @see: L{map() function<map>}
- """
- raise NotImplementedError
+ """
+ Returns a L{NATMapper} instance, suited to map a port for
+ the given protocol. Defaults to TCP.
+
+ For the moment, only upnp mapper is available. It accepts both UDP and TCP.
+
+ @param proto: The protocol: 'TCP' or 'UDP'
+ @type proto: string
+ @return: A deferred called with a L{NATMapper} instance
+ @rtype: L{twisted.internet.defer.Deferred}
+ """
+ import nattraverso.pynupnp
+ return nattraverso.pynupnp.get_port_mapper()
- def unmap(self, port):
- """
- Remove an existing mapping for the given twisted's port object.
-
- @param port: The port object to unmap
- @type port: a L{twisted.internet.interfaces.IListeningPort} object
- @return: A deferred called with None
- @rtype: L{twisted.internet.defer.Deferred}
- @raise ValueError: When there is no such existing mapping
- """
- raise NotImplementedError
-
- def get_port_mappings(self):
- """
- Returns a deferred that will be called with a dictionnary of the
- existing mappings.
-
- The dictionnary structure is the following:
- - Keys: tuple (protocol, external_port)
- - protocol is "TCP" or "UDP".
- - external_port is the external port number, as see on the
- WAN side.
- - Values:tuple (internal_ip, internal_port)
- - internal_ip is the LAN ip address of the host.
- - internal_port is the internal port number mapped
- to external_port.
-
- @return: A deferred called with the above defined dictionnary
- @rtype: L{twisted.internet.defer.Deferred}
- """
- raise NotImplementedError
-
- def _check_valid_port(self, port):
- """Various Port object validity checks. Raise a ValueError."""
- if not isinstance(port, BasePort):
- raise ValueError("expected a Port, got %r"%(port))
-
- if not port.connected:
- raise ValueError("Port %r is not listening"%(port))
+class NATMapper:
+ """
+ Define methods to map port objects (as returned by twisted's listenXX).
+ This allows NAT to be traversed from incoming packets.
+
+ Currently the only implementation of this class is the UPnP Mapper, which
+ can map UDP and TCP ports, if an UPnP Device exists.
+ """
+ def __init__(self):
+ raise NotImplementedError("Cannot instantiate the class")
+
+ def map(self, port):
+ """
+ Create a mapping for the given twisted's port object.
+
+ The deferred will call back with a tuple (extaddr, extport):
+ - extaddr: The ip string of the external ip address of this host
+ - extport: the external port number used to map the given Port object
+
+ When called multiple times with the same Port,
+ callback with the existing mapping.
+
+ @param port: The port object to map
+ @type port: a L{twisted.internet.interfaces.IListeningPort} object
+ @return: A deferred called with the above defined tuple
+ @rtype: L{twisted.internet.defer.Deferred}
+ """
+ raise NotImplementedError
+
+ def info(self, port):
+ """
+ Returns the existing mapping for the given port object. That means map()
+ has to be called before.
+
+ @param port: The port object to retreive info from
+ @type port: a L{twisted.internet.interfaces.IListeningPort} object
+ @raise ValueError: When there is no such existing mapping
+ @return: a tuple (extaddress, extport).
+ @see: L{map() function<map>}
+ """
+ raise NotImplementedError
+
+ def unmap(self, port):
+ """
+ Remove an existing mapping for the given twisted's port object.
+
+ @param port: The port object to unmap
+ @type port: a L{twisted.internet.interfaces.IListeningPort} object
+ @return: A deferred called with None
+ @rtype: L{twisted.internet.defer.Deferred}
+ @raise ValueError: When there is no such existing mapping
+ """
+ raise NotImplementedError
+
+ def get_port_mappings(self):
+ """
+ Returns a deferred that will be called with a dictionnary of the
+ existing mappings.
+
+ The dictionnary structure is the following:
+ - Keys: tuple (protocol, external_port)
+ - protocol is "TCP" or "UDP".
+ - external_port is the external port number, as see on the
+ WAN side.
+ - Values:tuple (internal_ip, internal_port)
+ - internal_ip is the LAN ip address of the host.
+ - internal_port is the internal port number mapped
+ to external_port.
+
+ @return: A deferred called with the above defined dictionnary
+ @rtype: L{twisted.internet.defer.Deferred}
+ """
+ raise NotImplementedError
+
+ def _check_valid_port(self, port):
+ """Various Port object validity checks. Raise a ValueError."""
+ if not isinstance(port, BasePort):
+ raise ValueError("expected a Port, got %r"%(port))
+
+ if not port.connected:
+ raise ValueError("Port %r is not listening"%(port))
+
+ loc_addr = port.getHost()
+ if loc_addr.port == 0:
+ raise ValueError("Port %r has port number of 0"%(port))
- loc_addr = port.getHost()
- if loc_addr.port == 0:
- raise ValueError("Port %r has port number of 0"%(port))
-
@@ -13,21 +13,21 @@
from nattraverso.pynupnp.upnp import search_upnp_device, UPnPMapper
def get_external_ip():
- """
- Returns a deferred which will be called with the WAN ip address
- retreived through UPnP. The ip is a string of the form "x.x.x.x"
-
- @return: A deferred called with the external ip address of this host
- @rtype: L{twisted.internet.defer.Deferred}
- """
- return search_upnp_device().addCallback(lambda x: x.get_external_ip())
+ """
+ Returns a deferred which will be called with the WAN ip address
+ retreived through UPnP. The ip is a string of the form "x.x.x.x"
+
+ @return: A deferred called with the external ip address of this host
+ @rtype: L{twisted.internet.defer.Deferred}
+ """
+ return search_upnp_device().addCallback(lambda x: x.get_external_ip())
def get_port_mapper():
- """
- Returns a deferred which will be called with a L{UPnPMapper} instance.
- This is a L{nattraverso.portmapper.NATMapper} implementation.
-
- @return: A deferred called with the L{UPnPMapper} instance.
- @rtype: L{twisted.internet.defer.Deferred}
- """
- return search_upnp_device().addCallback(lambda x: UPnPMapper(x))
+ """
+ Returns a deferred which will be called with a L{UPnPMapper} instance.
+ This is a L{nattraverso.portmapper.NATMapper} implementation.
+
+ @return: A deferred called with the L{UPnPMapper} instance.
+ @rtype: L{twisted.internet.defer.Deferred}
+ """
+ return search_upnp_device().addCallback(lambda x: UPnPMapper(x))
Oops, something went wrong.

0 comments on commit 34d0a5a

Please sign in to comment.