2016-06-03 17:02:02 +00:00
|
|
|
import struct
|
|
|
|
from Transport import *
|
|
|
|
|
|
|
|
class Packet:
|
|
|
|
|
|
|
|
def __init__(self, destination, data):
|
|
|
|
self.destination = destination
|
|
|
|
self.data = data
|
|
|
|
self.flags = 0x00
|
|
|
|
self.header = 0x00
|
|
|
|
self.raw = None
|
|
|
|
self.sent = False
|
|
|
|
self.mtu = 0
|
|
|
|
|
|
|
|
def send(self):
|
|
|
|
if not self.sent:
|
|
|
|
self.MTU = self.destination.MTU
|
|
|
|
self.header = struct.pack("!B", self.header ^ self.destination.type ^ self.flags)
|
|
|
|
self.header += self.destination.hash
|
|
|
|
self.ciphertext = self.destination.encrypt(self.data)
|
|
|
|
self.raw = self.header + self.ciphertext
|
|
|
|
|
|
|
|
if len(self.raw) > self.MTU:
|
2018-03-16 09:50:37 +00:00
|
|
|
raise IOError("Packet size of "+str(len(self.raw))+" exceeds MTU of "+str(self.MTU)+" bytes")
|
2016-06-03 17:02:02 +00:00
|
|
|
|
2018-03-16 09:50:37 +00:00
|
|
|
print("Size: "+str(len(self.raw)))
|
2016-06-03 17:02:02 +00:00
|
|
|
Transport.outbound(self.raw)
|
|
|
|
self.sent = True
|
|
|
|
else:
|
|
|
|
raise IOError("Packet was already sent")
|
|
|
|
|
|
|
|
def resend(self):
|
|
|
|
if self.sent:
|
|
|
|
Transport.outbound(self.raw)
|
|
|
|
else:
|
|
|
|
raise IOError("Packet was not sent yet")
|