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