the babeld configuration now allows /128 subnets
[re6stnet.git] / re6st / plib.py
1 import os
2 import subprocess
3 import logging
4 import utils
5
6 verbose = 0
7 here = os.path.realpath(os.path.dirname(__file__))
8 ovpn_server = os.path.join(here, 'ovpn-server')
9 ovpn_client = os.path.join(here, 'ovpn-client')
10
11
12 def openvpn(hello_interval, encrypt, *args, **kw):
13 args = ['openvpn',
14 '--dev-type', 'tap',
15 '--persist-tun',
16 '--persist-key',
17 '--script-security', '2',
18 '--user', 'nobody',
19 '--ping-exit', str(4 * hello_interval),
20 '--group', 'nogroup',
21 ] + list(args)
22 if not encrypt:
23 args.extend(['--cipher', 'none'])
24 logging.trace('%s' % (args,))
25 return subprocess.Popen(args, **kw)
26
27
28 def server(server_ip, ip_length, max_clients, dh_path, pipe_fd, port, proto, hello_interval, encrypt, *args, **kw):
29 logging.debug('Starting server...')
30 if server_ip != '':
31 script_up = '%s %s/%u' % (ovpn_server, server_ip, 64)
32 else:
33 script_up = '%s none' % (ovpn_server)
34 return openvpn(hello_interval, encrypt,
35 '--tls-server',
36 '--mode', 'server',
37 '--up', script_up,
38 '--client-connect', ovpn_server + ' ' + str(pipe_fd),
39 '--client-disconnect', ovpn_server + ' ' + str(pipe_fd),
40 '--dh', dh_path,
41 '--max-clients', str(max_clients),
42 '--port', str(port),
43 '--proto', proto,
44 *args, **kw)
45
46
47 def client(server_address, pipe_fd, hello_interval, encrypt, *args, **kw):
48 logging.debug('Starting client...')
49 remote = ['--nobind',
50 '--client',
51 '--up', ovpn_client,
52 '--route-up', ovpn_client + ' ' + str(pipe_fd)]
53 try:
54 for ip, port, proto in utils.address_list(server_address):
55 if proto == 'tcp-server':
56 proto = 'tcp-client'
57 remote += '--remote', ip, port, proto
58 except ValueError, e:
59 logging.warning('Error "%s" in unpacking address %s for openvpn client'
60 % (e, server_address,))
61 remote += args
62 return openvpn(hello_interval, encrypt, *remote, **kw)
63
64
65 def router(network, subnet, subnet_size, interface_list,
66 wireless, hello_interval, state_path, **kw):
67 logging.info('Starting babel...')
68 args = ['babeld',
69 '-C', 'redistribute local ip %s/%s le %s' % (subnet, subnet_size, subnet_size),
70 '-C', 'redistribute local deny',
71 '-C', 'redistribute ip %s/%s le %s' % (subnet, subnet_size, subnet_size),
72 '-C', 'redistribute deny',
73 '-C', 'out local ip %s/%s le %s' % (subnet, subnet_size, subnet_size),
74 '-C', 'out local deny',
75 # Route VIFIB ip adresses
76 '-C', 'in ip %s::/%u' % (utils.ipFromBin(network), len(network)),
77 # Route only addresse in the 'local' network,
78 # or other entire networks
79 #'-C', 'in ip %s' % (config.internal_ip),
80 #'-C', 'in ip ::/0 le %s' % network_mask,
81 # Don't route other addresses
82 '-C', 'in deny',
83 '-d', str(verbose),
84 '-h', str(hello_interval),
85 '-H', str(hello_interval),
86 '-S', state_path,
87 '-s',
88 ]
89 if wireless:
90 args.append('-w')
91 args = args + interface_list
92 logging.trace('%s' % args)
93 return subprocess.Popen(args, **kw)