fwknop: init script improvements
[feed/packages.git] / net / unbound / files / iptools.sh
1 #!/bin/sh
2 ##############################################################################
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # Copyright (C) 2016 Eric Luehrsen
14 #
15 ##############################################################################
16 #
17 # These are iptools that might be useful in a larger package, if provided
18 # elsewhere for common use. One example that many may find useful is turning
19 # flexible IPV6 colon dividers into PTR. Otherwise these are incomplete and
20 # would need robustness improvements for more generic applications.
21 #
22 ##############################################################################
23
24 domain_ptr_ip6() {
25 # Get the nibble rounded /CIDR ...ip6.arpa.
26 echo "$1" | awk -F: \
27 'BEGIN { OFS = "" ; }
28 { CIDR = $0 ;
29 sub(/.*\//,"",CIDR) ;
30 CIDR = (CIDR / 4) ;
31 sub(/\/[0-9]+/,"",$0) ;
32 ct_stop = 9 - NF ;
33 for(i=1; i<=NF; i++) {
34 if(length($i) == 0) {
35 for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
36 else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
37 y = $0 ;
38 ct_start = length(y) - 32 + CIDR ;
39 for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
40 gsub(/./,"&\.",x) ;
41 x = (x "ip6.arpa") ;
42 print x }'
43 }
44
45 ##############################################################################
46
47 host_ptr_ip6() {
48 # Get complete host ...ip6.arpa.
49 echo "$1" | awk -F: \
50 'BEGIN { OFS = "" ; }
51 { sub(/\/[0-9]+/,"",$0) ;
52 ct_stop = 9 - NF ;
53 for(i=1; i<=NF; i++) {
54 if(length($i) == 0) {
55 for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
56 else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
57 y = $0 ;
58 ct_start = length(y);
59 for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
60 sub(/[0-9]+\//,"",x) ;
61 gsub(/./,"&\.",x) ;
62 x = (x "ip6.arpa") ;
63 print x }'
64 }
65
66 ##############################################################################
67
68 domain_ptr_ip4() {
69 # Get the byte rounded /CIDR ...in-addr.arpa.
70 echo "$1" | awk \
71 '{ CIDR = $0 ;
72 sub(/.*\//,"",CIDR) ;
73 CIDR = (CIDR / 8) ;
74 dtxt = $0 ;
75 sub(/\/.*/,"",dtxt) ;
76 split(dtxt, dtxt, ".") ;
77 for(i=1; i<=CIDR; i++) { x = (dtxt[i] "." x) ; }
78 x = (x "in-addr.arpa") ;
79 print x }'
80 }
81
82 ##############################################################################
83
84 host_ptr_ip4() {
85 # Get omplete host ...in-addr.arpa.
86 echo "$1" | awk -F. \
87 '{ x = ( $4"."$3"."$2"."$1".in-addr.arpa" ) ;
88 sub(/\/[0-9]+/,"",x) ;
89 print x }'
90 }
91
92 ##############################################################################
93
94 valid_subnet6() {
95 case "$1" in
96 # GA
97 [1-9][0-9a-f][0-9a-f][0-9a-f]":"*) echo "ok" ;;
98 # ULA
99 f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
100 # fe80::, ::1, and such
101 *) echo "not" ;;
102 esac
103 }
104
105 ##############################################################################
106
107 valid_subnet4() {
108 case "$1" in
109 # Link, Local, and Such
110 169"."254"."*) echo "not" ;;
111 127"."*) echo "not" ;;
112 0"."*) echo "not" ;;
113 255"."*) echo "not" ;;
114 # Other Normal
115 25[0-4]"."[0-9]*) echo "ok" ;;
116 2[0-4][0-9]"."[0-9]*) echo "ok" ;;
117 1[0-9][0-9]"."[0-9]*) echo "ok" ;;
118 [0-9][0-9]"."[0-9]*) echo "ok" ;;
119 [0-9]"."[0-9]*) echo "ok" ;;
120 # Not Right
121 *) echo "not";;
122 esac
123 }
124
125 ##############################################################################
126
127 valid_subnet_any() {
128 local subnet=$1
129 local validip4=$( valid_subnet4 $subnet )
130 local validip6=$( valid_subnet6 $subnet )
131
132
133 if [ "$validip4" = "ok" ] || [ "$validip6" = "ok" ] ; then
134 echo "ok"
135 else
136 echo "not"
137 fi
138 }
139 ##############################################################################
140
141 private_subnet() {
142 case "$1" in
143 10"."*) echo "ok" ;;
144 172"."1[6-9]"."*) echo "ok" ;;
145 172"."2[0-9]"."*) echo "ok" ;;
146 172"."3[0-1]"."*) echo "ok" ;;
147 192"."168"."*) echo "ok" ;;
148 f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
149 *) echo "not" ;;
150 esac
151 }
152
153 ##############################################################################
154
155 domain_ptr_any() {
156 local subnet=$1
157 local arpa validip4 validip6
158
159 validip4=$( valid_subnet4 $subnet )
160 validip6=$( valid_subnet6 $subnet )
161
162
163 if [ "$validip4" = "ok" ] ; then
164 arpa=$( domain_ptr_ip4 "$subnet" )
165 elif [ "$validip6" = "ok" ] ; then
166 arpa=$( domain_ptr_ip6 "$subnet" )
167 fi
168
169
170 if [ -n "$arpa" ] ; then
171 echo $arpa
172 fi
173 }
174
175 ##############################################################################
176
177 host_ptr_any() {
178 local subnet=$1
179 local arpa validip4 validip6
180
181 validip4=$( valid_subnet4 $subnet )
182 validip6=$( valid_subnet6 $subnet )
183
184
185 if [ "$validip4" = "ok" ] ; then
186 arpa=$( host_ptr_ip4 "$subnet" )
187 elif [ "$validip6" = "ok" ] ; then
188 arpa=$( host_ptr_ip6 "$subnet" )
189 fi
190
191
192 if [ -n "$arpa" ] ; then
193 echo $arpa
194 fi
195 }
196
197 ##############################################################################
198