blob: 9524f4ffe0fbc807cb6f10bab696aeb16a174b20 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/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# while useful (sh)ellcheck is pedantic and noisy
25# shellcheck disable=1091,2002,2004,2034,2039,2086,2094,2140,2154,2155
26
27UB_IPTOOLS_BLANK=
28
29##############################################################################
30
31domain_ptr_ip6() {
32 # Get the nibble rounded /CIDR ...ip6.arpa.
33 echo "$1" | awk -F: \
34 'BEGIN { OFS = "" ; }
35 { CIDR = $0 ;
36 sub(/.*\//,"",CIDR) ;
37 CIDR = (CIDR / 4) ;
38 sub(/\/[0-9]+/,"",$0) ;
39 ct_stop = 9 - NF ;
40 for(i=1; i<=NF; i++) {
41 if(length($i) == 0) {
42 for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
43 else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
44 y = $0 ;
45 ct_start = length(y) - 32 + CIDR ;
46 for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
47 gsub(/./,"&\.",x) ;
48 x = (x "ip6.arpa") ;
49 print x }'
50}
51
52##############################################################################
53
54host_ptr_ip6() {
55 # Get complete host ...ip6.arpa.
56 echo "$1" | awk -F: \
57 'BEGIN { OFS = "" ; }
58 { sub(/\/[0-9]+/,"",$0) ;
59 ct_stop = 9 - NF ;
60 for(i=1; i<=NF; i++) {
61 if(length($i) == 0) {
62 for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
63 else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
64 y = $0 ;
65 ct_start = length(y);
66 for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
67 sub(/[0-9]+\//,"",x) ;
68 gsub(/./,"&\.",x) ;
69 x = (x "ip6.arpa") ;
70 print x }'
71}
72
73##############################################################################
74
75domain_ptr_ip4() {
76 # Get the byte rounded /CIDR ...in-addr.arpa.
77 echo "$1" | awk \
78 '{ CIDR = $0 ;
79 sub(/.*\//,"",CIDR) ;
80 CIDR = (CIDR / 8) ;
81 dtxt = $0 ;
82 sub(/\/.*/,"",dtxt) ;
83 split(dtxt, dtxt, ".") ;
84 for(i=1; i<=CIDR; i++) { x = (dtxt[i] "." x) ; }
85 x = (x "in-addr.arpa") ;
86 print x }'
87}
88
89##############################################################################
90
91host_ptr_ip4() {
92 # Get complete host ...in-addr.arpa.
93 echo "$1" | awk -F. \
94 '{ x = ( $4"."$3"."$2"."$1".in-addr.arpa" ) ;
95 sub(/\/[0-9]+/,"",x) ;
96 print x }'
97}
98
99##############################################################################
100
101valid_subnet6() {
102 case "$1" in
103 # GA
104 [1-9][0-9a-f][0-9a-f][0-9a-f]":"*) echo "ok" ;;
105 # ULA
106 f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
107 # fe80::, ::1, and such
108 *) echo "not" ;;
109 esac
110}
111
112##############################################################################
113
114valid_subnet4() {
115 case "$1" in
116 # Link, Local, and Such
117 169"."254"."*) echo "not" ;;
118 127"."*) echo "not" ;;
119 0"."*) echo "not" ;;
120 255"."*) echo "not" ;;
121 # Other Normal
122 25[0-4]"."[0-9]*) echo "ok" ;;
123 2[0-4][0-9]"."[0-9]*) echo "ok" ;;
124 1[0-9][0-9]"."[0-9]*) echo "ok" ;;
125 [0-9][0-9]"."[0-9]*) echo "ok" ;;
126 [0-9]"."[0-9]*) echo "ok" ;;
127 # Not Right
128 *) echo "not";;
129 esac
130}
131
132##############################################################################
133
134valid_subnet_any() {
135 local subnet=$1
136 local validip4=$( valid_subnet4 $subnet )
137 local validip6=$( valid_subnet6 $subnet )
138
139
140 if [ "$validip4" = "ok" ] || [ "$validip6" = "ok" ] ; then
141 echo "ok"
142 else
143 echo "not"
144 fi
145}
146
147##############################################################################
148
149private_subnet() {
150 case "$1" in
151 10"."*) echo "ok" ;;
152 172"."1[6-9]"."*) echo "ok" ;;
153 172"."2[0-9]"."*) echo "ok" ;;
154 172"."3[0-1]"."*) echo "ok" ;;
155 192"."168"."*) echo "ok" ;;
156 f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
157 *) echo "not" ;;
158 esac
159}
160
161##############################################################################
162
163local_subnet() {
164 # local subnet 2nd place is limited to one digit to improve the filter
165 case "$1" in
166 127"."[0-9]"."[0-9]*) echo "ok" ;;
167 ::1) echo "ok" ;;
168 *) echo "not" ;;
169 esac
170}
171
172##############################################################################
173
174domain_ptr_any() {
175 local subnet=$1
176 local arpa validip4 validip6
177
178 validip4=$( valid_subnet4 $subnet )
179 validip6=$( valid_subnet6 $subnet )
180
181
182 if [ "$validip4" = "ok" ] ; then
183 arpa=$( domain_ptr_ip4 "$subnet" )
184 elif [ "$validip6" = "ok" ] ; then
185 arpa=$( domain_ptr_ip6 "$subnet" )
186 fi
187
188
189 if [ -n "$arpa" ] ; then
190 echo $arpa
191 fi
192}
193
194##############################################################################
195
196host_ptr_any() {
197 local subnet=$1
198 local arpa validip4 validip6
199
200 validip4=$( valid_subnet4 $subnet )
201 validip6=$( valid_subnet6 $subnet )
202
203
204 if [ "$validip4" = "ok" ] ; then
205 arpa=$( host_ptr_ip4 "$subnet" )
206 elif [ "$validip6" = "ok" ] ; then
207 arpa=$( host_ptr_ip6 "$subnet" )
208 fi
209
210
211 if [ -n "$arpa" ] ; then
212 echo $arpa
213 fi
214}
215
216##############################################################################
217