chiark / gitweb /
init debug
[hippotat.git] / server
diff --git a/server b/server
index 6cb8d98..3781d56 100755 (executable)
--- a/server
+++ b/server
@@ -16,7 +16,7 @@ clients = { }
 
 #---------- "router" ----------
 
-def route(packet, saddr, daddr):
+def route(packet, iface, saddr, daddr):
   def lt(dest):
     log_debug(DBG.ROUTE, 'route: %s -> %s: %s' % (saddr,daddr,dest), d=packet)
   try: dclient = clients[daddr]
@@ -29,10 +29,10 @@ def route(packet, saddr, daddr):
     queue_inbound(packet)
   elif daddr == relay:
     lt('discard relay')
-    log_discard(packet, saddr, daddr, 'relay')
+    log_discard(packet, iface, saddr, daddr, 'relay')
   else:
     lt('discard no-client')
-    log_discard(packet, saddr, daddr, 'no-client')
+    log_discard(packet, iface, saddr, daddr, 'no-client')
 
 #---------- client ----------
 
@@ -47,19 +47,18 @@ class Client():
     # plus from config:
     #  .max_batch_down
     #  .max_queue_time
-    #  .max_request_time
     #  .target_requests_outstanding
 
     if ip not in c.network:
       raise ValueError('client %s not in network' % ip)
 
-    for k in ('max_batch_down','max_queue_time','max_request_time',
+    for k in ('max_batch_down','max_queue_time','http_timeout',
               'target_requests_outstanding'):
       req = cfg.getint(cs, k)
       limit = cfg.getint('limits',k)
       self.__dict__[k] = min(req, limit)
 
-    self._pq = PacketQueue(ip, self.max_queue_time)
+    self._pq = PacketQueue(str(ip), self.max_queue_time)
 
     if ip in clients:
       raise ValueError('multiple client cfg sections for %s' % ip)
@@ -67,23 +66,24 @@ class Client():
 
     self._log(DBG.INIT, 'new')
 
-  def _log(self, pri, msg, **kwargs):
-    log_debug(pri, 'client '+self._ip+': '+msg, **kwargs)
+  def _log(self, dflag, msg, **kwargs):
+    log_debug(dflag, ('client %s: ' % self._ip)+msg, **kwargs)
 
   def process_arriving_data(self, d):
-    self._log(DBG.FLOW, 'req data', d=d)
+    self._log(DBG.FLOW, "req data (enc'd)", d=d)
+    if not len(d): return
     for packet in slip.decode(d):
       (saddr, daddr) = packet_addrs(packet)
       if saddr != self._ip:
         raise ValueError('wrong source address %s' % saddr)
-      route(packet, saddr, daddr)
+      route(packet, self._ip, saddr, daddr)
 
   def _req_cancel(self, request):
-    self._log(DBG.HTTPCTRL, 'cancel', idof=request)
+    self._log(DBG.HTTP_CTRL, 'cancel', idof=request)
     request.finish()
 
   def _req_error(self, err, request):
-    self._log(DBG.HTTPCTRL, 'error %s' % err, idof=request)
+    self._log(DBG.HTTP_CTRL, 'error %s' % err, idof=request)
     self._req_cancel(request)
 
   def queue_outbound(self, packet):
@@ -92,13 +92,17 @@ class Client():
 
   def new_request(self, request):
     request.setHeader('Content-Type','application/octet-stream')
-    reactor.callLater(self.max_request_time, self._req_cancel, request)
+    reactor.callLater(self.http_timeout, self._req_cancel, request)
     request.notifyFinish().addErrback(self._req_error, request)
     self._rq.append(request)
     self._check_outbound()
 
+  def _req_write(self, req, d):
+    self._log(DBG.HTTP, 'req_write ', idof=req, d=d)
+    req.write(d)
+
   def _check_outbound(self):
-    log_debug(DBG.HTTPCTRL, 'CHKO')
+    log_debug(DBG.HTTP_CTRL, 'CHKO')
     while True:
       try: request = self._rq[0]
       except IndexError: request = None
@@ -120,7 +124,7 @@ class Client():
       self._log(DBG.HTTP_CTRL, 'CHKO processing', idof=request)
       # request, and also some non-expired packets
       self._pq.process((lambda: request.sentLength),
-                       request.write,
+                       (lambda d: self._req_write(request, d)),
                        self.max_batch_down)
 
       assert(request.sentLength)
@@ -136,47 +140,63 @@ class Client():
 
 def process_request(request, desca):
   # find client, update config, etc.
-  metadata = request.args['m']
-  metadata = metadata.split(b'\n')
-  (ci_s, pw, tro) = metadata.split(b'\n')[0:3]
-  desca['m'] = [ci_s, tro]
+  metadata = request.args[b'm'][0]
+  metadata = metadata.split(b'\r\n')
+  (ci_s, pw, tro, cto) = metadata[0:4]
+  desca['m[0,2:3]'] = [ci_s, tro, cto]
+  ci_s = ci_s.decode('utf-8')
+  tro = int(tro); desca['tro']= tro
+  cto = int(cto); desca['cto']= cto
   ci = ipaddr(ci_s)
   desca['ci'] = ci
   cl = clients[ci]
   if pw != cl.pw: raise ValueError('bad password')
-  desca['pwok'=True]
+  desca['pwok']=True
 
   if tro != cl.target_requests_outstanding:
     raise ValueError('tro must be %d' % cl.target_requests_outstanding)
 
+  if cto < cl.http_timeout:
+    raise ValueError('cto must be >= %d' % cl.http_timeout)
+
   try:
-    d = request.args['d']
+    d = request.args[b'd'][0]
     desca['d'] = d
-  except KeyError: d = ''
+    desca['dlen'] = len(d)
+  except KeyError:
+    d = b''
+    desca['dlen'] = None
+
+  log_http(desca, 'processing', idof=id(request), d=d)
+
+  d = mime_translate(d)
 
   cl.process_arriving_data(d)
   cl.new_request(request)
 
-def log_http(desca, msg):
+def log_http(desca, msg, **kwargs):
   try:
-    d = desca['d']
+    kwargs['d'] = desca['d']
     del desca['d']
   except KeyError:
-    d = None
-  log_debug(DBG.HTTP, msg + repr(desca), d=d)
+    pass
+  log_debug(DBG.HTTP, msg + repr(desca), **kwargs)
 
 class IphttpResource(twisted.web.resource.Resource):
   isLeaf = True
   def render_POST(self, request):
+    log_debug(DBG.HTTP_FULL,
+              'req recv: ' + repr(request) + ' ' + repr(request.args),
+              idof=id(request))
     desca = {'d': None}
     try: process_request(request, desca)
     except Exception as e:
-      emsg = str(e).encode('utf-8')
-      log_http(desca, 'EXCEPTION ' + emsg)
+      emsg = traceback.format_exc()
+      log_http(desca, 'RETURNING EXCEPTION ' + emsg)
       request.setHeader('Content-Type','text/plain; charset="utf-8"')
       request.setResponseCode(400)
-      return emsg
-    log_http(desca, '... [%s]' % id(request))
+      return (emsg + ' # ' + repr(desca) + '\r\n').encode('utf-8')
+    log_debug(DBG.HTTP_CTRL, '...', idof=id(request))
     return NOT_DONE_YET
 
   def render_GET(self, request):
@@ -216,6 +236,6 @@ def process_cfg():
 
 common_startup()
 process_cfg()
-start_ipif(c.ipif_command, route)
+start_ipif(c.ipif_command, (lambda p,s,d: route(p,"[ipif]",s,d)))
 start_http()
 common_run()