chiark / gitweb /
wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Mar 2017 22:03:55 +0000 (22:03 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Mar 2017 22:03:55 +0000 (22:03 +0000)
server

diff --git a/server b/server
index b3285ed..775a0c3 100755 (executable)
--- a/server
+++ b/server
@@ -88,6 +88,7 @@ class IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
     self._buffer = b''
   def connectionMade(self): pass
   def outReceived(self, data):
+    #print('RECV ', repr(data))
     self._buffer += data
     packets = slip_decode(self._buffer)
     self._buffer = packets.pop()
@@ -101,7 +102,7 @@ def start_ipif():
   global ipif
   ipif = IpifProcessProtocol()
   reactor.spawnProcess(ipif,
-                       '/bin/sh',['sh','-c', ipif_command],
+                       '/bin/sh',['sh','-xc', ipif_command],
                        childFDs={0:'w', 1:'r', 2:2})
 
 def queue_inbound(packet):
@@ -109,6 +110,45 @@ def queue_inbound(packet):
   ipif.transport.write(slip_encode(packet))
   ipif.transport.write(slip_delimiter)
 
+#---------- SLIP handling ----------
+
+slip_end = b'\300'
+slip_esc = b'\333'
+slip_esc_end = b'\334'
+slip_esc_esc = b'\335'
+slip_delimiter = slip_end
+
+def slip_encode(packet):
+  return (packet
+          .replace(slip_esc, slip_esc + slip_esc_esc)
+          .replace(slip_end, slip_esc + slip_esc_end))
+
+def slip_decode(data):
+  print('DECODE ', repr(data))
+  out = []
+  for packet in data.split(slip_end):
+    pdata = b''
+    while True:
+      eix = packet.find(slip_esc)
+      if eix == -1:
+        pdata += packet
+        break
+      #print('ESC ', repr((pdata, packet, eix)))
+      pdata += packet[0 : eix]
+      ck = packet[eix+1]
+      if   ck == slip_esc_esc: pdata += slip_esc
+      elif ck == slip_esc_end: pdata += slip_end
+      else: raise ValueError('invalid SLIP escape')
+      packet = packet[eix+2 : ]
+    out.append(pdata)
+  print('DECODED ', repr(out))
+  return out
+
+#---------- packet parsing ----------
+
+def packet_addrs(packet):
+  pass
+
 #---------- client ----------
 
 class Client():