chiark / gitweb /
break out SlipProtocol
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 22 Mar 2017 23:34:57 +0000 (23:34 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 22 Mar 2017 23:34:57 +0000 (23:34 +0000)
hippotat/__init__.py

index 8f8a447..3f62113 100644 (file)
@@ -123,23 +123,34 @@ def ipnetwork(input):
 
 #---------- ipif (SLIP) subprocess ----------
 
-class _IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
-  def __init__(self, router):
+class SlipProtocol(twisted.internet.protocol.ProcessProtocol):
+  # caller must define method receivedPacket(packet)
+  def __init__(self):
     self._buffer = b''
-    self._router = router
   def connectionMade(self): pass
   def outReceived(self, data):
-    #print('IPIF-GOT ', repr(data))
+    #print('SLIP-GOT ', repr(data))
     self._buffer += data
     packets = slip.decode(self._buffer)
     self._buffer = packets.pop()
     for packet in packets:
       if not len(packet): continue
-      (saddr, daddr) = packet_addrs(packet)
-      if saddr.is_link_local or daddr.is_link_local:
-        log_discard(packet, saddr, daddr, 'link-local')
-        continue
-      self._router(packet, saddr, daddr)
+      self.receivedPacket(packet)
+  def flush(self):
+    if len(self._buffer):
+      self.receivedPacket(self._buffer)
+      self._buffer = ''
+
+class _IpifProcessProtocol(SlipProtocol):
+  def __init__(self, router):
+    self._router = router
+    super.__init__()
+  def receivedPacket(self, packet):
+    (saddr, daddr) = packet_addrs(packet)
+    if saddr.is_link_local or daddr.is_link_local:
+      log_discard(packet, saddr, daddr, 'link-local')
+      return
+    self._router(packet, saddr, daddr)
   def processEnded(self, status):
     status.raiseException()