1 # -*- coding: utf-8 -*-
6 from __future__ import division
9 from ais.ntools import str_split_column_ipv6
15 CONFIG_FILENAME = '/etc/ais/config'
19 __source_normalized__ = False
20 def peers_get_config():
22 SOURCES normalization & checks
24 global __source_normalized__
25 if __source_normalized__:
28 config_file = file(CONFIG_FILENAME)
29 for lineno_e, line in enumerate(config_file.readlines()):
31 #logging.debug('considering line #%s: %s', lineno, repr(line))
32 line = line.strip(' \r\n')
33 if len(line)==0 or line[0]=='#':
34 logging.debug('ignoring line #%s: %s', lineno, repr(line))
36 spl = line.split(' ', 2)
37 assert len(spl) == 3, \
38 'line #%s: There should be at least 3 strings sepatated by spaces' % \
40 id4, io, definition = spl
41 assert len(id4)==4, 'source name (first parameter) must be 4 char long.'
42 logging.debug('%s %s %s', id4, io, definition)
43 if id4 not in SOURCES:
47 'line #%s: missing information after \'in\'' % \
49 SOURCES[id4]['in'] = definition
52 'line #%s: missing information after \'out\'' % \
54 if not SOURCES[id4].has_key('out'):
55 SOURCES[id4]['out'] = []
56 spl = definition.split(' ')
57 SOURCES[id4]['out'].append(spl)
59 SOURCES[id4]['name'] = definition
61 raise AssertionError( \
62 "line #%s: second parameter must be either 'in' or 'out'.\r%s" % \
65 logging.debug('SOURCES \n%s', pprint.pformat(SOURCES))
67 logging.debug('SOURCES (raw):\n%s', pprint.pformat(SOURCES))
68 for id4, settings in SOURCES.iteritems():
69 assert len(id4) == 4, \
70 'Invalid ID %s in SOURCES. It should be 4 characters long.'
72 if 'name' not in settings:
79 assert type(in_) == str, \
80 "SOURCES['%s']['in'] should be of type str, not %s'." % \
82 in_ = settings.get('in')
83 spl = str_split_column_ipv6(in_)
84 protocol, parameters = spl[0], spl[1:]
86 "SOURCES['%s']['in'] should contain parameters after %s" % \
90 assert len(parameters) == 4, \
91 "SOURCES['%s']['in'] udp: protocol requires 4 parameters." % \
93 host_local = parameters[0]
96 port_local = parameters[1]
97 assert port_local.isdigit(), \
98 "SOURCES['%s']['in'] udp: illegal local port" % \
100 port_local = int(port_local)
101 host_remote = parameters[2]
102 if host_remote == '*':
104 port_remote = parameters[3]
105 source_port_discreminate = False
106 if port_remote == '*':
108 elif port_remote == '!':
110 source_port_discreminate = True
112 assert port_remote.isdigit(), \
113 "SOURCES['%s']['in'] udp: illegal remote port" % \
115 port_remote = int(port_remote)
117 SOURCES[id4]['in'] = (protocol.title(), host_local, port_local, host_remote, port_remote, source_port_discreminate)
119 elif protocol == 'serial':
120 assert len(parameters) == 1, \
121 "SOURCES['%s']['in'] serial: protocol requires 1 parameter." % \
123 SOURCES[id4]['in'] = tuple(['Serial'] + parameters)
125 elif protocol == 'tcpout':
126 assert len(parameters) == 2, \
127 "SOURCES['%s']['in'] tcpout: protocol requires 2 parameters." % \
130 port = int(parameters[1])
131 SOURCES[id4]['in'] = ('TcpOut', host, port)
135 "Unsupported source protocol '%s' for SOURCES['%s']: %s",
137 __source_normalized__ = True
138 logging.debug('SOURCES (normalised):\n%s', pprint.pformat(SOURCES))
143 def source_get_infoin(*args):
145 Gets a list of matching info in SOURCES[*]['in']:
146 example: get_source_infoin('Udp', '', 1234)
147 might return [ ['SRC1', '12.23.34.45', '', False],
148 ['SRC2', '98.87.76.65', '', True]]
151 for id4, source_info in peers_get_config().iteritems():
152 rawin = source_info.get('in', None)
155 if args == rawin[:len(args)]:
156 newmatch = [id4] + [rawin[i] for i in range(len(args), len(rawin))]
157 result.append(newmatch)