Reticulum/FPE/Transport.py

70 lines
2.1 KiB
Python
Raw Normal View History

2018-03-19 16:39:08 +01:00
import FPE
2016-06-03 19:02:02 +02:00
class Transport:
2018-03-19 16:39:08 +01:00
# Constants
BROADCAST = 0x00;
TRANSPORT = 0x01;
RELAY = 0x02;
TUNNEL = 0x03;
types = [BROADCAST, TRANSPORT, RELAY, TUNNEL]
2018-03-20 12:32:41 +01:00
interfaces = []
destinations = []
2018-03-19 20:51:26 +01:00
packet_hashlist = []
2016-06-03 19:02:02 +02:00
@staticmethod
def outbound(raw):
2018-03-20 12:32:41 +01:00
Transport.cacheRaw(raw)
for interface in Transport.interfaces:
if interface.OUT:
FPE.log("Transmitting via: "+str(interface), FPE.LOG_DEBUG)
interface.processOutgoing(raw)
2016-06-03 19:02:02 +02:00
2018-03-19 20:51:26 +01:00
@staticmethod
def inbound(raw):
packet_hash = FPE.Identity.fullHash(raw)
if not packet_hash in Transport.packet_hashlist:
Transport.packet_hashlist.append(packet_hash)
packet = FPE.Packet(None, raw)
packet.unpack()
2018-03-20 12:32:41 +01:00
packet.packet_hash = packet_hash
2018-03-19 20:51:26 +01:00
if packet.packet_type == FPE.Packet.ANNOUNCE:
2018-03-20 12:32:41 +01:00
if FPE.Identity.validateAnnounce(packet):
Transport.cache(packet)
2018-03-19 20:51:26 +01:00
if packet.packet_type == FPE.Packet.RESOURCE:
2018-03-20 12:32:41 +01:00
for destination in Transport.destinations:
2018-03-19 20:51:26 +01:00
if destination.hash == packet.destination_hash and destination.type == packet.destination_type:
2018-03-20 12:32:41 +01:00
packet.destination = destination
destination.receive(packet)
Transport.cache(packet)
if packet.packet_type == FPE.Packet.PROOF:
for destination in Transport.destinations:
if destination.hash == packet.destination_hash:
if destination.proofcallback != None:
destination.proofcallback(packet)
# TODO: add universal proof handling
2018-03-19 20:51:26 +01:00
2016-06-03 19:02:02 +02:00
@staticmethod
def registerDestination(destination):
2018-03-20 12:32:41 +01:00
destination.MTU = FPE.FlexPE.MTU
if destination.direction == FPE.Destination.IN:
Transport.destinations.append(destination)
@staticmethod
def cache(packet):
FPE.Transport.cacheRaw(packet.raw)
@staticmethod
def cacheRaw(raw):
try:
file = open(FPE.FlexPE.cachepath+"/"+FPE.hexrep(FPE.Identity.fullHash(raw), delimit=False), "w")
file.write(raw)
file.close()
FPE.log("Wrote packet "+FPE.prettyhexrep(FPE.Identity.fullHash(raw))+" to cache", FPE.LOG_DEBUG)
except Exception as e:
FPE.log("Error writing packet to cache", FPE.LOG_ERROR)
FPE.log("The contained exception was: "+str(e))