chiark / gitweb /
move ipif slip process handling
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 20 Mar 2017 19:32:12 +0000 (19:32 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 20 Mar 2017 19:32:12 +0000 (19:32 +0000)
hippotat/__init__.py
server

index ee844cd..2b13003 100644 (file)
@@ -1,10 +1,13 @@
 # -*- python -*-
 
-import hippotat.slip as slip
+import twisted
+from twisted.internet import reactor
 
 import ipaddress
 from ipaddress import AddressValueError
 
+import hippotat.slip as slip
+
 #---------- packet parsing ----------
 
 def packet_addrs(packet):
@@ -38,3 +41,35 @@ def ipnetwork(input):
   except NetworkValueError:
     r = ipaddress.IPv6Network(input)
   return r
+
+#---------- ipif (SLIP) subprocess ----------
+
+class _IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
+  def __init__(self, router):
+    self._buffer = b''
+    self._router = router
+  def connectionMade(self): pass
+  def outReceived(self, data):
+    #print('RECV ', 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)
+      self._router(packet, saddr, daddr)
+  def processEnded(self, status):
+    status.raiseException()
+
+def start_ipif(command, router):
+  global ipif
+  ipif = _IpifProcessProtocol(router)
+  reactor.spawnProcess(ipif,
+                       '/bin/sh',['sh','-xc', command],
+                       childFDs={0:'w', 1:'r', 2:2})
+
+def queue_inbound(packet):
+  ipif.transport.write(slip.delimiter)
+  ipif.transport.write(slip.encode(packet))
+  ipif.transport.write(slip.delimiter)
+
diff --git a/server b/server
index 6650073..80bdd80 100755 (executable)
--- a/server
+++ b/server
@@ -24,7 +24,7 @@ import collections
 
 import syslog
 
-from hippotit import *
+from hippotat import *
 
 clients = { }
 
@@ -88,36 +88,6 @@ def log_discard(packet, saddr, daddr, why):
 #  syslog.syslog(syslog.LOG_DEBUG,
 #                'discarded packet %s -> %s (%s)' % (saddr, daddr, why))
 
-#---------- ipif (slip subprocess) ----------
-
-class IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
-  def __init__(self):
-    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()
-    for packet in packets:
-      if not len(packet): continue
-      (saddr, daddr) = packet_addrs(packet)
-      route(packet, saddr, daddr)
-  def processEnded(self, status):
-    status.raiseException()
-
-def start_ipif():
-  global ipif
-  ipif = IpifProcessProtocol()
-  reactor.spawnProcess(ipif,
-                       '/bin/sh',['sh','-xc', ipif_command],
-                       childFDs={0:'w', 1:'r', 2:2})
-
-def queue_inbound(packet):
-  ipif.transport.write(slip.delimiter)
-  ipif.transport.write(slip.encode(packet))
-  ipif.transport.write(slip.delimiter)
-
 #---------- client ----------
 
 class Client():
@@ -229,7 +199,7 @@ class IphttpResource(twisted.web.resource.Resource):
     c.new_request(request)
 
   def render_GET(self, request):
-    return b'<html><body>hippotit</body></html>'
+    return b'<html><body>hippotat</body></html>'
 
 def start_http():
   resource = IphttpResource()
@@ -304,7 +274,7 @@ def startup():
   cfg.read(opts.configfile)
   process_cfg()
 
-  start_ipif()
+  start_ipif(ipif_command, route)
   start_http()
 
 startup()