Deleted Added
full compact
ipaddr.subr (243475) ipaddr.subr (243504)
1if [ ! "$_NETWORKING_IPADDR_SUBR" ]; then _NETWORKING_IPADDR_SUBR=1
2#
3# Copyright (c) 2006-2012 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:

--- 10 unchanged lines hidden (view full) ---

19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
1if [ ! "$_NETWORKING_IPADDR_SUBR" ]; then _NETWORKING_IPADDR_SUBR=1
2#
3# Copyright (c) 2006-2012 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:

--- 10 unchanged lines hidden (view full) ---

19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/networking/share/ipaddr.subr 243475 2012-11-24 06:27:46Z dteske $
27# $FreeBSD: head/usr.sbin/bsdconfig/networking/share/ipaddr.subr 243504 2012-11-25 10:37:10Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_include $BSDCFG_SHARE/sysrc.subr
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/strings.subr

--- 19 unchanged lines hidden (view full) ---

55 print $2
56 found = 1
57 exit
58 }
59 END { exit ! found }
60 '
61}
62
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_include $BSDCFG_SHARE/sysrc.subr
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/strings.subr

--- 19 unchanged lines hidden (view full) ---

55 print $2
56 found = 1
57 exit
58 }
59 END { exit ! found }
60 '
61}
62
63# f_dialog_validate_ipaddr $ipaddr
63# f_validate_ipaddr $ipaddr
64#
65# Returns zero if the given argument (an IP address) is of the proper format.
66#
67# The return status for invalid IP address is one of:
68# 1 One or more individual octets within the IP address (separated
69# by dots) contains one or more invalid characters.
70# 2 One or more individual octets within the IP address are null
71# and/or missing.
72# 3 One or more individual octets within the IP address exceeds the
73# maximum of 255 (or 2^8, being an octet comprised of 8 bits).
74# 4 The IP address has either too few or too many octets.
75#
64#
65# Returns zero if the given argument (an IP address) is of the proper format.
66#
67# The return status for invalid IP address is one of:
68# 1 One or more individual octets within the IP address (separated
69# by dots) contains one or more invalid characters.
70# 2 One or more individual octets within the IP address are null
71# and/or missing.
72# 3 One or more individual octets within the IP address exceeds the
73# maximum of 255 (or 2^8, being an octet comprised of 8 bits).
74# 4 The IP address has either too few or too many octets.
75#
76# If the IP address is determined to be invalid, the appropriate error will be
77# displayed using the f_dialog_msgbox function.
78#
79f_dialog_validate_ipaddr()
76f_validate_ipaddr()
80{
81 local ip="$1"
82
83 ( # Operate within a sub-shell to protect the parent environment
84
85 # Track number of octets for error checking
86 noctets=0
87

--- 13 unchanged lines hidden (view full) ---

101 [ $octet -gt 255 ] && exit 3
102
103 noctets=$(( $noctets + 1 ))
104
105 done
106
107 [ $noctets -eq 4 ] || exit 4
108 )
77{
78 local ip="$1"
79
80 ( # Operate within a sub-shell to protect the parent environment
81
82 # Track number of octets for error checking
83 noctets=0
84

--- 13 unchanged lines hidden (view full) ---

98 [ $octet -gt 255 ] && exit 3
99
100 noctets=$(( $noctets + 1 ))
101
102 done
103
104 [ $noctets -eq 4 ] || exit 4
105 )
106}
107# f_dialog_iperror $error $ipaddr
108#
109# Display a msgbox with the appropriate error message for an error returned by
110# f_validate_ipaddr above.
111#
112f_dialog_iperror()
113{
114 local error="$1" ip="$2"
109
115
110 #
111 # Produce an appropriate error message if necessary.
112 #
113 local retval=$?
114 case $retval in
116 [ ${error:-0} -ne 0 ] || return $SUCCESS
117
118 case "$error" in
115 1) f_dialog_msgbox "$( printf \
116 "$msg_ipv4_addr_octet_contains_invalid_chars" "$ip" )";;
117 2) f_dialog_msgbox "$( printf \
118 "$msg_ipv4_addr_octet_is_null" "$ip" )";;
119 3) f_dialog_msgbox "$( printf \
120 "$msg_ipv4_addr_octet_exceeds_max_value" "$ip" )";;
121 4) f_dialog_msgbox "$( printf \
122 "$msg_ipv4_addr_octet_missing_or_extra" "$ip" )";;
123 esac
119 1) f_dialog_msgbox "$( printf \
120 "$msg_ipv4_addr_octet_contains_invalid_chars" "$ip" )";;
121 2) f_dialog_msgbox "$( printf \
122 "$msg_ipv4_addr_octet_is_null" "$ip" )";;
123 3) f_dialog_msgbox "$( printf \
124 "$msg_ipv4_addr_octet_exceeds_max_value" "$ip" )";;
125 4) f_dialog_msgbox "$( printf \
126 "$msg_ipv4_addr_octet_missing_or_extra" "$ip" )";;
127 esac
128}
124
129
130# f_dialog_validate_ipaddr $ipaddr
131#
132# Returns zero if the given argument (an IP address) is of the proper format.
133#
134# If the IP address is determined to be invalid, the appropriate error will be
135# displayed using the f_dialog_iperror function above.
136#
137f_dialog_validate_ipaddr()
138{
139 local ip="$1"
140
141 f_validate_ipaddr "$ip"
142 local retval=$?
143
144 # Produce an appropriate error message if necessary.
145 [ $retval -eq $SUCCESS ] || f_dialog_iperror $retval "$ip"
146
125 return $retval
126}
127
147 return $retval
148}
149
128# f_dialog_validate_ipaddr6 $ipv6_addr
150# f_validate_ipaddr6 $ipv6_addr
129#
130# Returns zero if the given argument (an IPv6 address) is of the proper format.
131#
132# The return status for invalid IP address is one of:
133# 1 One or more individual segments within the IP address
134# (separated by colons) contains one or more invalid characters.
151#
152# Returns zero if the given argument (an IPv6 address) is of the proper format.
153#
154# The return status for invalid IP address is one of:
155# 1 One or more individual segments within the IP address
156# (separated by colons) contains one or more invalid characters.
135# 2 More than two segments within the IP address are null or the
136# the second null segment is not at the end of the address.
137# 3 One or more individual segments within the IP address exceeds
138# the word length of 32-bits (segments are always hexadecimal).
139# 4 The IP address has either too few or too many segments.
140# 5 The IPv4 address at the end of the IPv6 address is invalid.
157# Segments must contain only combinations of the characters 0-9,
158# A-F, or a-f.
159# 2 Too many/incorrect null segments. A single null segment is
160# allowed within the IP address (separated by colons) but not
161# allowed at the beginning or end (unless a double-null segment;
162# i.e., "::*" or "*::").
163# 3 One or more individual segments within the IP address
164# (separated by colons) exceeds the length of 4 hex-digits.
165# 4 The IP address entered has either too few (less than 3), too
166# many (more than 8), or not enough segments, separated by
167# colons.
168# 5* The IPv4 address at the end of the IPv6 address is invalid.
169# * When there is an error with the dotted-quad IPv4 address at the
170# end of the IPv6 address, the return value of 5 is OR'd with a
171# bit-shifted (<< 4) return of f_validate_ipaddr.
141#
172#
142# If the IP address is determined to be invalid, the appropriate error will be
143# displayed using the f_dialog_msgbox function.
144#
145f_dialog_validate_ipaddr6()
173f_validate_ipaddr6()
146{
147 local ip="$1"
148
149 ( # Operate within a sub-shell to protect the parent environment
150
174{
175 local ip="$1"
176
177 ( # Operate within a sub-shell to protect the parent environment
178
151 oldIFS="$IFS"
152 IFS=":" # Split on `colon'
153 set -- $ip:
154
155 # Return error if too many or too few segments
156 # Using 9 as max in case of leading or trailing null spanner
157 [ $# -gt 9 -o $# -lt 3 ] && exit 4
158
159 h="[0-9A-Fa-f]"

--- 51 unchanged lines hidden (view full) ---

211 # Return error if not enough segments
212 if [ $nulls -eq 0 ]; then
213 [ $nsegments -eq 7 ] || exit 4
214 fi
215
216 contains_ipv4_segment=1
217
218 # Validate the IPv4 address
179 IFS=":" # Split on `colon'
180 set -- $ip:
181
182 # Return error if too many or too few segments
183 # Using 9 as max in case of leading or trailing null spanner
184 [ $# -gt 9 -o $# -lt 3 ] && exit 4
185
186 h="[0-9A-Fa-f]"

--- 51 unchanged lines hidden (view full) ---

238 # Return error if not enough segments
239 if [ $nulls -eq 0 ]; then
240 [ $nsegments -eq 7 ] || exit 4
241 fi
242
243 contains_ipv4_segment=1
244
245 # Validate the IPv4 address
219 IFS="$oldIFS"
220 f_dialog_validate_ipaddr "$segment" || exit 5
221 IFS=":"
246 f_validate_ipaddr "$segment" ||
247 exit $(( 5 | $? << 4 ))
222 ;;
223 *)
224 # Segment characters are all valid but too many
225 exit 3
226 esac
227
228 done
229

--- 19 unchanged lines hidden (view full) ---

249 # Return error if null spanner with too many segments
250 1) [ $nsegments -le $maxsegments ] || exit 4 ;;
251 # Return error if leading/trailing `::' with too many segments
252 2) [ $nsegments -le $(( $maxsegments + 1 )) ] || exit 4 ;;
253 esac
254
255 exit $SUCCESS
256 )
248 ;;
249 *)
250 # Segment characters are all valid but too many
251 exit 3
252 esac
253
254 done
255

--- 19 unchanged lines hidden (view full) ---

275 # Return error if null spanner with too many segments
276 1) [ $nsegments -le $maxsegments ] || exit 4 ;;
277 # Return error if leading/trailing `::' with too many segments
278 2) [ $nsegments -le $(( $maxsegments + 1 )) ] || exit 4 ;;
279 esac
280
281 exit $SUCCESS
282 )
283}
257
284
258 #
259 # Produce an appropriate error message if necessary.
260 #
261 local retval=$?
262 case $retval in
285# f_dialog_ip6error $error $ipv6_addr
286#
287# Display a msgbox with the appropriate error message for an error returned by
288# f_validate_ipaddr6 above.
289#
290f_dialog_ip6error()
291{
292 local error="$1" ip="$2"
293
294 [ ${error:-0} -ne 0 ] || return $SUCCESS
295
296 case "$error" in
263 1) f_dialog_msgbox "$( printf \
264 "$msg_ipv6_addr_segment_contains_invalid_chars" "$ip" )";;
265 2) f_dialog_msgbox "$( printf \
266 "$msg_ipv6_addr_too_many_null_segments" "$ip" )";;
267 3) f_dialog_msgbox "$( printf \
268 "$msg_ipv6_addr_segment_contains_too_many_chars" "$ip" )";;
269 4) f_dialog_msgbox "$( printf \
270 "$msg_ipv6_addr_too_few_or_extra_segments" "$ip" )";;
297 1) f_dialog_msgbox "$( printf \
298 "$msg_ipv6_addr_segment_contains_invalid_chars" "$ip" )";;
299 2) f_dialog_msgbox "$( printf \
300 "$msg_ipv6_addr_too_many_null_segments" "$ip" )";;
301 3) f_dialog_msgbox "$( printf \
302 "$msg_ipv6_addr_segment_contains_too_many_chars" "$ip" )";;
303 4) f_dialog_msgbox "$( printf \
304 "$msg_ipv6_addr_too_few_or_extra_segments" "$ip" )";;
271 5) : IPv4 at the end of IPv6 address is invalid ;;
272 # Don't display an error because f_dialog_validate_ipaddr
273 # already displayed one for the particular issue encountered.
305 *)
306 if [ $(( $error & 0xF )) -eq 5 ]; then
307 # IPv4 at the end of IPv6 address is invalid
308 f_dialog_iperror $(( $error >> 4 )) "$ip"
309 fi
274 esac
310 esac
311}
275
312
313# f_dialog_validate_ipaddr6 $ipv6_addr
314#
315# Returns zero if the given argument (an IPv6 address) is of the proper format.
316#
317# If the IP address is determined to be invalid, the appropriate error will be
318# displayed using the f_dialog_ip6error function above.
319#
320f_dialog_validate_ipaddr6()
321{
322 local ip="$1"
323
324 f_validate_ipaddr6 "$ip"
325 local retval=$?
326
327 # Produce an appropriate error message if necessary.
328 [ $retval -eq $SUCCESS ] || f_dialog_ip6error $retval "$ip"
329
276 return $retval
277}
278
279# f_dialog_input_ipaddr $interface $ipaddr
280#
281# Allows the user to edit a given IP address. If the user does not cancel or
282# press ESC, the $ipaddr environment variable will hold the newly-configured
283# value upon return.

--- 107 unchanged lines hidden ---
330 return $retval
331}
332
333# f_dialog_input_ipaddr $interface $ipaddr
334#
335# Allows the user to edit a given IP address. If the user does not cancel or
336# press ESC, the $ipaddr environment variable will hold the newly-configured
337# value upon return.

--- 107 unchanged lines hidden ---