2018-04-04 14:14:22 +02:00
|
|
|
import RNS
|
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:
|
2018-04-08 13:41:03 +02:00
|
|
|
RNS.log("Transmitting "+str(len(raw))+" bytes via: "+str(interface), RNS.LOG_DEBUG)
|
2018-03-20 12:32:41 +01:00
|
|
|
interface.processOutgoing(raw)
|
2016-06-03 19:02:02 +02:00
|
|
|
|
2018-03-19 20:51:26 +01:00
|
|
|
@staticmethod
|
2018-04-04 14:14:22 +02:00
|
|
|
def inbound(raw, interface=None):
|
|
|
|
packet_hash = RNS.Identity.fullHash(raw)
|
|
|
|
RNS.log(str(interface)+" received packet with hash "+RNS.prettyhexrep(packet_hash), RNS.LOG_DEBUG)
|
2018-03-19 20:51:26 +01:00
|
|
|
|
|
|
|
if not packet_hash in Transport.packet_hashlist:
|
|
|
|
Transport.packet_hashlist.append(packet_hash)
|
2018-04-04 14:14:22 +02:00
|
|
|
packet = RNS.Packet(None, raw)
|
2018-03-19 20:51:26 +01:00
|
|
|
packet.unpack()
|
2018-03-20 12:32:41 +01:00
|
|
|
packet.packet_hash = packet_hash
|
2018-03-19 20:51:26 +01:00
|
|
|
|
2018-04-04 14:14:22 +02:00
|
|
|
if packet.packet_type == RNS.Packet.ANNOUNCE:
|
|
|
|
if RNS.Identity.validateAnnounce(packet):
|
2018-03-20 12:32:41 +01:00
|
|
|
Transport.cache(packet)
|
2018-03-19 20:51:26 +01:00
|
|
|
|
2018-04-04 14:14:22 +02:00
|
|
|
if packet.packet_type == RNS.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)
|
|
|
|
|
2018-04-04 14:14:22 +02:00
|
|
|
if packet.packet_type == RNS.Packet.PROOF:
|
2018-03-20 12:32:41 +01:00
|
|
|
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-04-04 14:14:22 +02:00
|
|
|
destination.MTU = RNS.Reticulum.MTU
|
|
|
|
if destination.direction == RNS.Destination.IN:
|
2018-03-20 12:32:41 +01:00
|
|
|
Transport.destinations.append(destination)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cache(packet):
|
2018-04-04 14:14:22 +02:00
|
|
|
RNS.Transport.cacheRaw(packet.raw)
|
2018-03-20 12:32:41 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cacheRaw(raw):
|
|
|
|
try:
|
2018-04-04 14:14:22 +02:00
|
|
|
file = open(RNS.Reticulum.cachepath+"/"+RNS.hexrep(RNS.Identity.fullHash(raw), delimit=False), "w")
|
2018-03-20 12:32:41 +01:00
|
|
|
file.write(raw)
|
|
|
|
file.close()
|
2018-04-04 14:14:22 +02:00
|
|
|
RNS.log("Wrote packet "+RNS.prettyhexrep(RNS.Identity.fullHash(raw))+" to cache", RNS.LOG_DEBUG)
|
2018-03-20 12:32:41 +01:00
|
|
|
except Exception as e:
|
2018-04-04 14:14:22 +02:00
|
|
|
RNS.log("Error writing packet to cache", RNS.LOG_ERROR)
|
|
|
|
RNS.log("The contained exception was: "+str(e))
|