diff options
author | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-03-03 16:51:44 +0000 |
---|---|---|
committer | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-03-03 21:55:43 +0000 |
commit | b38dcaf42ded7e04f5d94c2d7680b7c9508a683a (patch) | |
tree | 437c6ed2d13c0d5b2eb7905016ae8af070e3a1fa /python/vyos/validate.py | |
parent | 3dc1921ea1c5bde86d948df22a7566f71357dd0a (diff) | |
download | vyos-1x-b38dcaf42ded7e04f5d94c2d7680b7c9508a683a.tar.gz vyos-1x-b38dcaf42ded7e04f5d94c2d7680b7c9508a683a.zip |
ifconfig: T2057: generic interface option setting
this patch allows to get or change many interface options (mtu, arp settings, ...)
using get_interface / set_interface functions
Diffstat (limited to 'python/vyos/validate.py')
-rw-r--r-- | python/vyos/validate.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/python/vyos/validate.py b/python/vyos/validate.py index 33c495d91..4dca82dd8 100644 --- a/python/vyos/validate.py +++ b/python/vyos/validate.py @@ -168,3 +168,56 @@ def is_subnet_connected(subnet, primary=False): return True return False + + +def assert_boolean(b): + if int(b) not in (0, 1): + raise ValueError(f'Value {b} out of range') + + +def assert_range(value, lower=0, count=3): + if int(value) not in range(lower,lower+count): + raise ValueError("Value out of range") + + +def assert_list(s, l): + if s not in l: + o = ' or '.join([f'"{n}"' for n in l]) + raise ValueError(f'state must be {o}, got {s}') + + +def assert_number(n): + if not n.isnumeric(): + raise ValueError(f'{n} must be a number') + + +def assert_positive(n, smaller=0): + assert_number(n) + if int(n) < smaller: + raise ValueError(f'{n} is smaller than {limit}') + + +def assert_mtu(mtu, min=68, max=9000): + assert_number(mtu) + if int(mtu) < min or int(mtu) > max: + raise ValueError(f'Invalid MTU size: "{mtu}"') + + +def assert_mac(m): + octets = [int(i, 16) for i in m.split(':')] + + # a mac address consits out of 6 octets + if len(octets) != 6: + raise ValueError(f'wrong number of MAC octets: {octets}') + + # validate against the first mac address byte if it's a multicast + # address + if octets[0] & 1: + raise ValueError(f'{m} is a multicast MAC address') + + # overall mac address is not allowed to be 00:00:00:00:00:00 + if sum(octets) == 0: + raise ValueError('00:00:00:00:00:00 is not a valid MAC address') + + if octets[:5] == (0, 0, 94, 0, 1): + raise ValueError(f'{m} is a VRRP MAC address') |