chiark / gitweb /
start splitting up
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 20 Mar 2017 09:34:58 +0000 (09:34 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 20 Mar 2017 09:34:58 +0000 (09:34 +0000)
hippotit/__init__.py [new file with mode: 0644]
hippotit/slip.py [new file with mode: 0644]
server

diff --git a/hippotit/__init__.py b/hippotit/__init__.py
new file mode 100644 (file)
index 0000000..08272ef
--- /dev/null
@@ -0,0 +1,40 @@
+# -*- python -*-
+
+import hippotit.slip as slip
+
+import ipaddress
+from ipaddress import AddressValueError
+
+#---------- packet parsing ----------
+
+def packet_addrs(packet):
+  version = packet[0] >> 4
+  if version == 4:
+    addrlen = 4
+    saddroff = 3*4
+    factory = ipaddress.IPv4Address
+  elif version == 6:
+    addrlen = 16
+    saddroff = 2*4
+    factory = ipaddress.IPv6Address
+  else:
+    raise ValueError('unsupported IP version %d' % version)
+  saddr = factory(packet[ saddroff           : saddroff + addrlen   ])
+  daddr = factory(packet[ saddroff + addrlen : saddroff + addrlen*2 ])
+  return (saddr, daddr)
+
+#---------- address handling ----------
+
+def ipaddr(input):
+  try:
+    r = ipaddress.IPv4Address(input)
+  except AddressValueError:
+    r = ipaddress.IPv6Address(input)
+  return r
+
+def ipnetwork(input):
+  try:
+    r = ipaddress.IPv4Network(input)
+  except NetworkValueError:
+    r = ipaddress.IPv6Network(input)
+  return r
diff --git a/hippotit/slip.py b/hippotit/slip.py
new file mode 100644 (file)
index 0000000..4e84194
--- /dev/null
@@ -0,0 +1,36 @@
+# SLIP handling
+
+end = b'\300'
+esc = b'\333'
+esc_end = b'\334'
+esc_esc = b'\335'
+delimiter = end
+
+def encode(packet):
+  return (packet
+          .replace(esc, esc + esc_esc)
+          .replace(end, esc + esc_end))
+
+def decode(data):
+  print('DECODE ', repr(data))
+  out = []
+  for packet in data.split(end):
+    pdata = b''
+    while True:
+      eix = packet.find(esc)
+      if eix == -1:
+        pdata += packet
+        break
+      #print('ESC ', repr((pdata, packet, eix)))
+      pdata += packet[0 : eix]
+      ck = packet[eix+1]
+      #print('ESC... %o' % ck)
+      if   ck == esc_esc[0]: pdata += esc
+      elif ck == esc_end[0]: pdata += end
+      else: raise ValueError('invalid SLIP escape')
+      packet = packet[eix+2 : ]
+    out.append(pdata)
+  print('DECODED ', repr(out))
+  return out
+# -*- python -*-
+
diff --git a/server b/server
index 243c3e7ceadbd0f966fce7ef864b2e17a5dba885..6650073ac4e54f532a0dc481c70b63914e8d8210 100755 (executable)
--- a/server
+++ b/server
@@ -13,9 +13,6 @@ from twisted.internet import reactor
 from twisted.web.server import NOT_DONE_YET
 from twisted.logger import LogLevel
 
-import ipaddress
-from ipaddress import AddressValueError
-
 #import twisted.web.server import Site
 #from twisted.web.resource import Resource
 
@@ -27,21 +24,9 @@ import collections
 
 import syslog
 
-clients = { }
-
-def ipaddr(input):
-  try:
-    r = ipaddress.IPv4Address(input)
-  except AddressValueError:
-    r = ipaddress.IPv6Address(input)
-  return r
+from hippotit import *
 
-def ipnetwork(input):
-  try:
-    r = ipaddress.IPv4Network(input)
-  except NetworkValueError:
-    r = ipaddress.IPv6Network(input)
-  return r
+clients = { }
 
 defcfg = '''
 [DEFAULT]
@@ -112,7 +97,7 @@ class IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
   def outReceived(self, data):
     #print('RECV ', repr(data))
     self._buffer += data
-    packets = slip_decode(self._buffer)
+    packets = slip.decode(self._buffer)
     self._buffer = packets.pop()
     for packet in packets:
       if not len(packet): continue
@@ -129,62 +114,9 @@ def start_ipif():
                        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)
-
-#---------- 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]
-      #print('ESC... %o' % ck)
-      if   ck == slip_esc_esc[0]: pdata += slip_esc
-      elif ck == slip_esc_end[0]: 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):
-  version = packet[0] >> 4
-  if version == 4:
-    addrlen = 4
-    saddroff = 3*4
-    factory = ipaddress.IPv4Address
-  elif version == 6:
-    addrlen = 16
-    saddroff = 2*4
-    factory = ipaddress.IPv6Address
-  else:
-    raise ValueError('unsupported IP version %d' % version)
-  saddr = factory(packet[ saddroff           : saddroff + addrlen   ])
-  daddr = factory(packet[ saddroff + addrlen : saddroff + addrlen*2 ])
-  return (saddr, daddr)
+  ipif.transport.write(slip.delimiter)
+  ipif.transport.write(slip.encode(packet))
+  ipif.transport.write(slip.delimiter)
 
 #---------- client ----------
 
@@ -206,7 +138,7 @@ class Client():
       self.__dict__[k] = min(req, limit)
 
     def process_arriving_data(self, d):
-      for packet in slip_decode(d):
+      for packet in slip.decode(d):
         (saddr, daddr) = packet_addrs(packet)
         if saddr != self._ip:
           raise ValueError('wrong source address %s' % saddr)
@@ -256,13 +188,13 @@ class Client():
           try: (dummy, packet) = self._pq[0]
           except IndexError: break
 
-          encoded = slip_encode(packet)
+          encoded = slip.encode(packet)
           
           if request.sentLength > 0:
-            if (request.sentLength + len(slip_delimiter)
+            if (request.sentLength + len(slip.delimiter)
                 + len(encoded) > self.max_batch_down):
               break
-            request.write(slip_delimiter)
+            request.write(slip.delimiter)
 
           request.write(encoded)
           self._pq.popLeft()