Added note on the python~rc dns bug
[re6stnet.git] / utils.py
1 import argparse, time, struct, socket
2 from OpenSSL import crypto
3
4 verbose = 0
5
6 def log(message, verbose_level):
7 if verbose >= verbose_level:
8 print time.strftime("%d-%m-%Y %H:%M:%S : " + message)
9
10 def binFromIp(ip):
11 ip1, ip2 = struct.unpack('>QQ', socket.inet_pton(socket.AF_INET6, ip))
12 return bin(ip1)[2:].rjust(64, '0') + bin(ip2)[2:].rjust(64, '0')
13
14 def ipFromBin(prefix):
15 prefix = hex(int(prefix, 2))[2:]
16 ip = ''
17 for i in xrange(0, len(prefix) - 1, 4):
18 ip += prefix[i:i+4] + ':'
19 return ip.rstrip(':')
20
21 def ipFromPrefix(vifibnet, prefix, prefix_len):
22 prefix = bin(int(prefix))[2:].rjust(prefix_len, '0')
23 ip_t = (vifibnet + prefix).ljust(128, '0')
24 return ipFromBin(ip_t), prefix
25
26 def networkFromCa(ca_path):
27 # Get network prefix from ca.crt
28 with open(ca_path, 'r') as f:
29 ca = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
30 return bin(ca.get_serial_number())[3:]
31
32 def ipFromCert(network, cert_path):
33 # Get ip from cert.crt
34 with open(cert_path, 'r') as f:
35 cert = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
36 subject = cert.get_subject()
37 prefix, prefix_len = subject.CN.split('/')
38 return ipFromPrefix(network, prefix, int(prefix_len))
39
40 def address_list(address_set):
41 return ';'.join(map(','.join, address_set))
42
43 def address_set(address_list):
44 return list(tuple(address.split(','))
45 for address in address_list.split(';'))