Packets go brrrr

This commit is contained in:
Mark Qvist 2025-01-11 01:26:46 +01:00
parent c62b66195d
commit 3ace1583da
2 changed files with 43 additions and 70 deletions

View File

@ -197,32 +197,31 @@ class LocalClientInterface(Interface):
try: try:
in_frame = False in_frame = False
escape = False escape = False
frame_buffer = b""
data_in = b""
data_buffer = b"" data_buffer = b""
while True: while True:
data_in = self.socket.recv(4096) data_in = self.socket.recv(4096)
if len(data_in) > 0: if len(data_in) > 0:
pointer = 0 frame_buffer += data_in
while pointer < len(data_in): flags_remaining = True
byte = data_in[pointer] while flags_remaining:
pointer += 1 frame_start = frame_buffer.find(HDLC.FLAG)
if (in_frame and byte == HDLC.FLAG): if frame_start != -1:
in_frame = False frame_end = frame_buffer.find(HDLC.FLAG, frame_start+1)
self.process_incoming(data_buffer) if frame_end != -1:
elif (byte == HDLC.FLAG): frame = frame_buffer[frame_start+1:frame_end]
in_frame = True frame = frame.replace(bytes([HDLC.ESC, HDLC.FLAG ^ HDLC.ESC_MASK]), bytes([HDLC.FLAG]))
data_buffer = b"" frame = frame.replace(bytes([HDLC.ESC, HDLC.ESC ^ HDLC.ESC_MASK]), bytes([HDLC.ESC]))
elif (in_frame and len(data_buffer) < self.HW_MTU): if len(frame) > RNS.Reticulum.HEADER_MINSIZE:
if (byte == HDLC.ESC): self.process_incoming(frame)
escape = True frame_buffer = frame_buffer[frame_end:]
else: else:
if (escape): flags_remaining = False
if (byte == HDLC.FLAG ^ HDLC.ESC_MASK): else:
byte = HDLC.FLAG flags_remaining = False
if (byte == HDLC.ESC ^ HDLC.ESC_MASK):
byte = HDLC.ESC
escape = False
data_buffer = data_buffer+bytes([byte])
else: else:
self.online = False self.online = False
if self.is_connected_to_shared_instance and not self.detached: if self.is_connected_to_shared_instance and not self.detached:
@ -239,6 +238,7 @@ class LocalClientInterface(Interface):
self.online = False self.online = False
RNS.log("An interface error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("An interface error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
RNS.log("Tearing down "+str(self), RNS.LOG_ERROR) RNS.log("Tearing down "+str(self), RNS.LOG_ERROR)
RNS.trace_exception(e)
self.teardown() self.teardown()
def detach(self): def detach(self):

View File

@ -320,63 +320,36 @@ class TCPClientInterface(Interface):
try: try:
in_frame = False in_frame = False
escape = False escape = False
frame_buffer = b""
data_in = b""
data_buffer = b"" data_buffer = b""
command = KISS.CMD_UNKNOWN
while True: while True:
data_in = self.socket.recv(4096) data_in = self.socket.recv(4096)
if len(data_in) > 0: if len(data_in) > 0:
pointer = 0 if self.kiss_framing:
while pointer < len(data_in): # TODO: Add KISS framing parser
byte = data_in[pointer] pass
pointer += 1
if self.kiss_framing: else:
# Read loop for KISS framing frame_buffer += data_in
if (in_frame and byte == KISS.FEND and command == KISS.CMD_DATA): flags_remaining = True
in_frame = False while flags_remaining:
self.process_incoming(data_buffer) frame_start = frame_buffer.find(HDLC.FLAG)
elif (byte == KISS.FEND): if frame_start != -1:
in_frame = True frame_end = frame_buffer.find(HDLC.FLAG, frame_start+1)
command = KISS.CMD_UNKNOWN if frame_end != -1:
data_buffer = b"" frame = frame_buffer[frame_start+1:frame_end]
elif (in_frame and len(data_buffer) < self.HW_MTU): frame = frame.replace(bytes([HDLC.ESC, HDLC.FLAG ^ HDLC.ESC_MASK]), bytes([HDLC.FLAG]))
if (len(data_buffer) == 0 and command == KISS.CMD_UNKNOWN): frame = frame.replace(bytes([HDLC.ESC, HDLC.ESC ^ HDLC.ESC_MASK]), bytes([HDLC.ESC]))
# We only support one HDLC port for now, so if len(frame) > RNS.Reticulum.HEADER_MINSIZE:
# strip off the port nibble self.process_incoming(frame)
byte = byte & 0x0F frame_buffer = frame_buffer[frame_end:]
command = byte
elif (command == KISS.CMD_DATA):
if (byte == KISS.FESC):
escape = True
else:
if (escape):
if (byte == KISS.TFEND):
byte = KISS.FEND
if (byte == KISS.TFESC):
byte = KISS.FESC
escape = False
data_buffer = data_buffer+bytes([byte])
else:
# Read loop for HDLC framing
if (in_frame and byte == HDLC.FLAG):
in_frame = False
self.process_incoming(data_buffer)
elif (byte == HDLC.FLAG):
in_frame = True
data_buffer = b""
elif (in_frame and len(data_buffer) < self.HW_MTU):
if (byte == HDLC.ESC):
escape = True
else: else:
if (escape): flags_remaining = False
if (byte == HDLC.FLAG ^ HDLC.ESC_MASK): else:
byte = HDLC.FLAG flags_remaining = False
if (byte == HDLC.ESC ^ HDLC.ESC_MASK):
byte = HDLC.ESC
escape = False
data_buffer = data_buffer+bytes([byte])
else: else:
self.online = False self.online = False
if self.initiator and not self.detached: if self.initiator and not self.detached: