def NATPMP(self, action): """ Call NAT-PMP on router to get port 5500 forwarded. @author: Dennis Koot """ if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux', sys.platform): if self.enablePMP: if action == 'request': lifetime = 3600 print "Request port 5500 (NAT-PMP)." else: lifetime = 0 print "Give up port 5500 (NAT-PMP)." pubpriv_port = int(5500) protocol = NATPMP.NATPMP_PROTOCOL_TCP try: gateway = NATPMP.get_gateway_addr() print NATPMP.map_port(protocol, pubpriv_port, pubpriv_port, lifetime, gateway_ip=gateway) except: print "Warning: Unable to automap port."
def open(self, port, ptype = 'TCP', manual = False): # Supported? if not _natpmp_supported: return # Check if manual: self.map[ptype][port] = 1 elif not self.map[ptype].has_key(port): return False # Map try: NATPMP.map_port(self.protocols[ptype], port, port, 3600) except NATPMP.NATPMPError, e: print "Error: NAT-PMP operation failed (port %d %s closed)." % (port, ptype), "Details:", e return False
def NATPMP(self, action): """ Call NAT-PMP on router to get port 5500 forwarded. @author: Dennis Koot """ if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform): if self.enablePMP: if action == 'request': lifetime = 3600 print "Request port 5500 (NAT-PMP)." else: lifetime = 0 print "Give up port 5500 (NAT-PMP)." pubpriv_port = int(5500) protocol = NATPMP.NATPMP_PROTOCOL_TCP try: gateway = NATPMP.get_gateway_addr() print NATPMP.map_port(protocol, pubpriv_port, pubpriv_port, lifetime, gateway_ip=gateway) except: print "Warning: Unable to automap port."
def remove_mapping(self, local_port, external_port, protocol): NATPMP.map_port(protocol_from_string(protocol), external_port, local_port, lifetime=0)
def add_mapping(self, local_port, external_port, protocol): response = NATPMP.map_port(protocol_from_string(protocol), external_port, local_port) return response.public_port
def external_ip(self): external_ip = NATPMP.get_public_address() self.logger.info('ip: {0}'.format(external_ip)) return external_ip