--- /dev/null
+# -*- 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
--- /dev/null
+# 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 -*-
+
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
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]
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
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 ----------
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)
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()