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

diff --git a/server b/server
index 775a0c3..df49699 100755 (executable)
--- a/server
+++ b/server
@@ -65,12 +65,18 @@ max_request_time = 121
 
 #---------- "router" ----------
 
-def route(packet, daddr):
+def route(packet, saddr, daddr):
+  print('TRACE ', saddr, daddr, packet)
   try: client = clients[daddr]
   except KeyError: dclient = None
   if dclient is not None:
     dclient.queue_outbound(packet)
+  elif daddr.is_multicast:
+    log_discard(packet, saddr, daddr, 'multicast')
+  elif daddr.is_link_local:
+    log_discard(packet, saddr, daddr, 'link-local')
   elif daddr == host or daddr not in network:
+    print('TRACE INBOUND ', saddr, daddr, packet)
     queue_inbound(packet)
   elif daddr == relay:
     log_discard(packet, saddr, daddr, 'relay')
@@ -78,8 +84,9 @@ def route(packet, daddr):
     log_discard(packet, saddr, daddr, 'no client')
 
 def log_discard(packet, saddr, daddr, why):
-  syslog.syslog(syslog.LOG_DEBUG,
-                'discarded packet %s -> %s (%s)' % (saddr, daddr, why))
+  print('DROP ', saddr, daddr, why, packet)
+#  syslog.syslog(syslog.LOG_DEBUG,
+#                'discarded packet %s -> %s (%s)' % (saddr, daddr, why))
 
 #---------- ipif (slip subprocess) ----------
 
@@ -93,8 +100,9 @@ class IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
     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, daddr)
+      route(packet, saddr, daddr)
   def processEnded(self, status):
     status.raiseException()
 
@@ -147,7 +155,20 @@ def slip_decode(data):
 #---------- packet parsing ----------
 
 def packet_addrs(packet):
-  pass
+  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)
 
 #---------- client ----------
 
@@ -173,7 +194,7 @@ class Client():
         (saddr, daddr) = packet_addrs(packet)
         if saddr != self._ip:
           raise ValueError('wrong source address %s' % saddr)
-        route(packet, daddr)
+        route(packet, saddr, daddr)
 
     def _req_cancel(self, request):
       request.finish()