diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-05-12 18:44:20 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2020-05-16 15:30:26 +0200 |
commit | 728e1c6073cb216d3cb8b66f519bd590458165e6 (patch) | |
tree | f51952557bbeb66d998a81e1dc4ba72cba4673da /src/validators | |
parent | cc2ad34ce61e205454c4676a5bde77629d463964 (diff) | |
download | vyos-1x-728e1c6073cb216d3cb8b66f519bd590458165e6.tar.gz vyos-1x-728e1c6073cb216d3cb8b66f519bd590458165e6.zip |
nat: T2198: add new ipv4-range validator
Diffstat (limited to 'src/validators')
-rwxr-xr-x | src/validators/ipv4-range | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/validators/ipv4-range b/src/validators/ipv4-range new file mode 100755 index 000000000..0d707d6c5 --- /dev/null +++ b/src/validators/ipv4-range @@ -0,0 +1,30 @@ +#!/bin/bash + +# snippet from https://stackoverflow.com/questions/10768160/ip-address-converter +ip2dec () { + local a b c d ip=$@ + IFS=. read -r a b c d <<< "$ip" + printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))" +} + +# This only works with real bash (<<<) - split IP addresses into array with +# hyphen as delimiter +readarray -d - -t strarr <<< $1 + +ipaddrcheck --is-ipv4-single ${strarr[0]} +if [ $? -gt 0 ]; then + exit 1 +fi + +ipaddrcheck --is-ipv4-single ${strarr[1]} +if [ $? -gt 0 ]; then + exit 1 +fi + +start=$(ip2dec ${strarr[0]}) +stop=$(ip2dec ${strarr[1]}) +if [ $start -ge $stop ]; then + exit 1 +fi + +exit 0 |