8 import twisted.internet
9 import twisted.internet.endpoints
10 from twisted.web.server import NOT_DONE_YET
12 #import twisted.web.server import Site
13 #from twisted.web.resource import Resource
21 max_batch_down = 65536
24 target_requests_outstanding = 3
33 ipif = userv root ipif %(host)s,%(relay)s,%(mtu)s,slip %(network)s
38 max_batch_down = 262144
40 max_request_time = 121
41 target_requests_outstanding = 10
44 #---------- "router" ----------
46 def route(packet, saddr, daddr):
47 print('TRACE ', saddr, daddr, packet)
48 try: client = clients[daddr]
49 except KeyError: dclient = None
50 if dclient is not None:
51 dclient.queue_outbound(packet)
52 elif saddr.is_link_local or daddr.is_link_local:
53 log_discard(packet, saddr, daddr, 'link-local')
54 elif daddr == host or daddr not in network:
55 print('TRACE INBOUND ', saddr, daddr, packet)
58 log_discard(packet, saddr, daddr, 'relay')
60 log_discard(packet, saddr, daddr, 'no client')
62 def log_discard(packet, saddr, daddr, why):
63 print('DROP ', saddr, daddr, why)
64 # syslog.syslog(syslog.LOG_DEBUG,
65 # 'discarded packet %s -> %s (%s)' % (saddr, daddr, why))
67 #---------- client ----------
70 def __init__(self, ip, cs):
71 # instance data members
74 self.pw = cfg.get(cs, 'password')
75 self._rq = collections.deque() # requests
76 # self._pq = PacketQueue(...)
81 # .target_requests_outstanding
82 for k in ('max_batch_down','max_queue_time','max_request_time',
83 'target_requests_outstanding'):
84 req = cfg.getint(cs, k)
85 limit = cfg.getint('limits',k)
86 self.__dict__[k] = min(req, limit)
87 self._pq = PacketQueue(self.max_queue_time)
89 def process_arriving_data(self, d):
90 for packet in slip.decode(d):
91 (saddr, daddr) = packet_addrs(packet)
93 raise ValueError('wrong source address %s' % saddr)
94 route(packet, saddr, daddr)
96 def _req_cancel(self, request):
99 def _req_error(self, err, request):
100 self._req_cancel(request)
102 def queue_outbound(self, packet):
103 self._pq.append(packet)
105 def http_request(self, request):
106 request.setHeader('Content-Type','application/octet-stream')
107 reactor.callLater(self.max_request_time, self._req_cancel, request)
108 request.notifyFinish().addErrback(self._req_error, request)
109 self._rq.append(request)
110 self._check_outbound()
112 def _check_outbound(self):
114 try: request = self._rq[0]
115 except IndexError: request = None
116 if request and request.finished:
120 if not self._pq.nonempty():
121 # no packets, oh well
128 # request, and also some non-expired packets
130 packet = self.pq.popleft()
131 if packet is None: break
133 encoded = slip.encode(packet)
135 if request.sentLength > 0:
136 if (request.sentLength + len(slip.delimiter)
137 + len(encoded) > self.max_batch_down):
139 request.write(slip.delimiter)
141 request.write(encoded)
144 assert(request.sentLength)
147 # round again, looking for more to do
149 while len(self._rq) > self.target_requests_outstanding:
150 request = self._rq.popleft()
153 class IphttpResource(twisted.web.resource.Resource):
155 def render_POST(self, request):
156 # find client, update config, etc.
157 ci = ipaddr(request.args['i'])
159 pw = request.args['pw']
160 if pw != c.pw: raise ValueError('bad password')
163 for r, w in (('mbd', 'max_batch_down'),
164 ('mqt', 'max_queue_time'),
165 ('mrt', 'max_request_time'),
166 ('tro', 'target_requests_outstanding')):
167 try: v = request.args[r]
168 except KeyError: continue
172 try: d = request.args['d']
173 except KeyError: d = ''
175 c.process_arriving_data(d)
176 c.new_request(request)
178 def render_GET(self, request):
179 return b'<html><body>hippotat</body></html>'
182 resource = IphttpResource()
183 site = twisted.web.server.Site(resource)
184 for addrspec in cfg.get('server','addrs').split():
186 addr = ipaddress.IPv4Address(addrspec)
187 endpointfactory = twisted.internet.endpoints.TCP4ServerEndpoint
188 except AddressValueError:
189 addr = ipaddress.IPv6Address(addrspec)
190 endpointfactory = twisted.internet.endpoints.TCP6ServerEndpoint
191 ep = endpointfactory(reactor, cfg.getint('server','port'), addr)
192 crash_on_defer(ep.listen(site))
194 #---------- config and setup ----------
202 network = ipnetwork(cfg.get('virtual','network'))
203 if network.num_addresses < 3 + 2:
204 raise ValueError('network needs at least 2^3 addresses')
207 host = cfg.get('virtual','host')
208 except NoOptionError:
209 host = next(network.hosts())
212 relay = cfg.get('virtual','relay')
213 except NoOptionError:
214 for search in network.hosts():
215 if search == host: continue
219 for cs in cfg.sections():
220 if not (':' in cs or '.' in cs): continue
222 if ci not in network:
223 raise ValueError('client %s not in network' % ci)
225 raise ValueError('multiple client cfg sections for %s' % ci)
226 clients[ci] = Client(ci, cs)
229 mtu = cfg.get('virtual','mtu')
232 for k in ('host','relay','mtu','network'):
233 iic_vars[k] = globals()[k]
235 ipif_command = cfg.get('server','ipif', vars=iic_vars)
238 common_startup(defcfg)
240 start_ipif(ipif_command, route)