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 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 |
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# |
76f_validate_ipaddr() |
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" |
115 |
116 [ ${error:-0} -ne 0 ] || return $SUCCESS 117 118 case "$error" in |
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} |
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 |
147 return $retval 148} 149 |
150# f_validate_ipaddr6 $ipv6_addr |
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. |
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. |
172# |
173f_validate_ipaddr6() |
174{ 175 local ip="$1" 176 177 ( # Operate within a sub-shell to protect the parent environment 178 |
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 |
246 f_validate_ipaddr "$segment" || 247 exit $(( 5 | $? << 4 )) |
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} |
284 |
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 |
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" )";; |
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 |
310 esac |
311} |
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 |
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 --- |