summaryrefslogtreecommitdiff
path: root/src/conf_mode/bcast_relay.py
blob: 8d4c4a89a1ff96d45d8b26ba0b426c490c86bf03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
#
# Copyright (C) 2017-2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import fnmatch

from sys import exit
from copy import deepcopy
from jinja2 import FileSystemLoader, Environment

from vyos.config import Config
from vyos.defaults import directories as vyos_data_dir
from vyos import ConfigError
from vyos.util import call

config_file = r'/etc/default/udp-broadcast-relay'

default_config_data = {
    'disabled': False,
    'instances': []
}

def get_config():
    relay = deepcopy(default_config_data)
    conf = Config()
    base = ['service', 'broadcast-relay']

    if not conf.exists(base):
        return None
    else:
        conf.set_level(base)

    # Service can be disabled by user
    if conf.exists('disable'):
        relay['disabled'] = True
        return relay

    # Parse configuration of each individual instance
    if conf.exists('id'):
        for id in conf.list_nodes('id'):
            conf.set_level(base + ['id', id])
            config = {
                'id': id,
                'disabled': False,
                'address': '',
                'description': '',
                'interfaces': [],
                'port': ''
            }

            # Check if individual broadcast relay service is disabled
            if conf.exists(['disable']):
                config['disabled'] = True

            # Source IP of forwarded packets, if empty original senders address is used
            if conf.exists(['address']):
                config['address'] = conf.return_value(['address'])

            # A description for each individual broadcast relay service
            if conf.exists(['description']):
                config['description'] = conf.return_value(['description'])

            # UDP port to listen on for broadcast frames
            if conf.exists(['port']):
                config['port'] = conf.return_value(['port'])

            # Network interfaces to listen on for broadcast frames to be relayed
            if conf.exists(['interface']):
                config['interfaces'] = conf.return_values(['interface'])

            relay['instances'].append(config)

    return relay

def verify(relay):
    if relay is None:
        return None

    if relay['disabled']:
        return None

    for r in relay['instances']:
        # we don't have to check this instance when it's disabled
        if r['disabled']:
            continue

        # we certainly require a UDP port to listen to
        if not r['port']:
            raise ConfigError('UDP broadcast relay "{0}" requires a port number'.format(r['id']))

        # Relaying data without two interface is kinda senseless ...
        if len(r['interfaces']) < 2:
            raise ConfigError('UDP broadcast relay "id {0}" requires at least 2 interfaces'.format(r['id']))

    return None


def generate(relay):
    if relay is None:
        return None

    # Prepare Jinja2 template loader from files
    tmpl_path = os.path.join(vyos_data_dir['data'], 'templates', 'bcast-relay')
    fs_loader = FileSystemLoader(tmpl_path)
    env = Environment(loader=fs_loader)

    config_dir = os.path.dirname(config_file)
    config_filename = os.path.basename(config_file)
    active_configs = []

    for config in fnmatch.filter(os.listdir(config_dir), config_filename + '*'):
        # determine prefix length to identify service instance
        prefix_len = len(config_filename)
        active_configs.append(config[prefix_len:])

    # sort our list
    active_configs.sort()

    # delete old configuration files
    for id in active_configs[:]:
        if os.path.exists(config_file + id):
            os.unlink(config_file + id)

    # If the service is disabled, we can bail out here
    if relay['disabled']:
        print('Warning: UDP broadcast relay service will be deactivated because it is disabled')
        return None

    for r in relay['instances']:
        # Skip writing instance config when it's disabled
        if r['disabled']:
            continue

        # configuration filename contains instance id
        file = config_file + str(r['id'])
        tmpl = env.get_template('udp-broadcast-relay.tmpl')
        config_text = tmpl.render(r)
        with open(file, 'w') as f:
            f.write(config_text)

    return None

def apply(relay):
    # first stop all running services
    call('sudo systemctl stop udp-broadcast-relay@{1..99}')

    if (relay is None) or relay['disabled']:
        return None

    # start only required service instances
    for r in relay['instances']:
        # Don't start individual instance when it's disabled
        if r['disabled']:
            continue
        call('sudo systemctl start udp-broadcast-relay@{0}'.format(r['id']))

    return None

if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        exit(1)