chiark / gitweb /
introduce c. etc.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 21 Mar 2017 00:21:26 +0000 (00:21 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 21 Mar 2017 00:21:38 +0000 (00:21 +0000)
client
hippotat/__init__.py
server

diff --git a/client b/client
index b9f8ce5..a77600e 100755 (executable)
--- a/client
+++ b/client
@@ -17,7 +17,12 @@ mtu = 1500
 # password = <password>
 '''
 
-def startup():
-  common_startup(defcfg)
+def process_cfg():
+  global url
+  global max_requests_outstanding
 
-startup()
+  process_cfg_common_always()
+  
+
+common_startup(defcfg)
+process_cfg()
index 6af49b6..8219238 100644 (file)
@@ -18,9 +18,18 @@ from configparser import NoOptionError
 
 import collections
 
+# these need to be defined here so that they can be imported by import *
 cfg = ConfigParser()
 optparser = OptionParser()
 
+class ConfigResults:
+  def __init__(self, d = { }):
+    self.__dict__ = d
+  def __repr__(self):
+    return 'ConfigResults('+repr(self.__dict__)+')'
+
+c = ConfigResults()
+
 #---------- packet parsing ----------
 
 def packet_addrs(packet):
@@ -129,6 +138,12 @@ def crash_on_critical(event):
   if event.get('log_level') >= LogLevel.critical:
     crash(twisted.logger.formatEvent(event))
 
+#---------- config processing ----------
+
+def process_cfg_common_always():
+  global mtu
+  c.mtu = cfg.get('virtual','mtu')
+
 #---------- startup ----------
 
 def common_startup(defcfg):
diff --git a/server b/server
index 66de6e5..f1b9ca4 100755 (executable)
--- a/server
+++ b/server
@@ -51,7 +51,7 @@ def route(packet, saddr, daddr):
     dclient.queue_outbound(packet)
   elif saddr.is_link_local or daddr.is_link_local:
     log_discard(packet, saddr, daddr, 'link-local')
-  elif daddr == host or daddr not in network:
+  elif daddr == c.host or daddr not in c.network:
     print('TRACE INBOUND ', saddr, daddr, packet)
     queue_inbound(packet)
   elif daddr == relay:
@@ -194,51 +194,44 @@ def start_http():
 #---------- config and setup ----------
         
 def process_cfg():
-  global network
-  global host
-  global relay
-  global ipif_command
+  process_cfg_common_always()
 
-  network = ipnetwork(cfg.get('virtual','network'))
-  if network.num_addresses < 3 + 2:
+  c.network = ipnetwork(cfg.get('virtual','network'))
+  if c.network.num_addresses < 3 + 2:
     raise ValueError('network needs at least 2^3 addresses')
 
   try:
-    host = cfg.get('virtual','host')
+    c.host = cfg.get('virtual','host')
   except NoOptionError:
-    host = next(network.hosts())
+    c.host = next(c.network.hosts())
 
   try:
-    relay = cfg.get('virtual','relay')
+    c.relay = cfg.get('virtual','relay')
   except NoOptionError:
-    for search in network.hosts():
-      if search == host: continue
-      relay = search
+    for search in c.network.hosts():
+      if search == c.host: continue
+      c.relay = search
       break
 
   for cs in cfg.sections():
     if not (':' in cs or '.' in cs): continue
     ci = ipaddr(cs)
-    if ci not in network:
+    if ci not in c.network:
       raise ValueError('client %s not in network' % ci)
     if ci in clients:
       raise ValueError('multiple client cfg sections for %s' % ci)
     clients[ci] = Client(ci, cs)
 
-  global mtu
-  mtu = cfg.get('virtual','mtu')
+  print(repr(c))
 
   iic_vars = { }
   for k in ('host','relay','mtu','network'):
-    iic_vars[k] = globals()[k]
+    iic_vars[k] = getattr(c,k)
 
-  ipif_command = cfg.get('server','ipif', vars=iic_vars)
+  c.ipif_command = cfg.get('server','ipif', vars=iic_vars)
 
-def startup():
-  common_startup(defcfg)
-  process_cfg()
-  start_ipif(ipif_command, route)
-  start_http()
-
-startup()
+common_startup(defcfg)
+process_cfg()
+start_ipif(c.ipif_command, route)
+start_http()
 common_run()