Skip to content

Commit

Permalink
scripts: fix potential overflow when cnovert ip to value
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hanqing committed Jul 23, 2021
1 parent 040d6ce commit 6ba0a3c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ function help() {
echo " [-nc|--noConfirm] if specified, deploy no need to confirm"
}

function ip_value() {
echo $(echo $1 | awk -F '[/.]' '{
mask = (2 ^ 8)
printf ("%.0f", (($1 * mask + $2) * mask + $3) * mask + $4)
}')
}

# 从subnet获取ip
function get_ip_from_subnet() {
subnet=$1
prefix=`echo $subnet|awk -F/ '{print $1}'|awk -F. '{printf "%d", ($1*(2^24))+($2*(2^16))+($3*(2^8))+$4}'`
prefix=$(ip_value $subnet)
mod=`echo $subnet|awk -F/ '{print $2}'`
mask=$((2**32-2**(32-$mod)))
# 对prefix再取一次模,为了支持10.182.26.50/22这种格式
Expand All @@ -55,7 +62,7 @@ function get_ip_from_subnet() {
for i in `/sbin/ifconfig -a|grep inet|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
do
# 把ip转换成整数
ip_int=`echo $i|awk -F. '{printf "%d\n", ($1*(2^24))+($2*(2^16))+($3*(2^8))+$4}'`
ip_int=$(ip_value $i)
if [ $(($ip_int&$mask)) -eq $prefix ]
then
ip=$i
Expand Down
11 changes: 9 additions & 2 deletions curve-ansible/roles/install_package/templates/mds-daemon.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ daemonLog=${logPath}/curve-mds-daemon.log
# console output
consoleLog=${logPath}/curve-mds-console.log

function ip_value() {
echo $(echo $1 | awk -F '[/.]' '{
mask = (2 ^ 8)
printf ("%.0f", (($1 * mask + $2) * mask + $3) * mask + $4)
}')
}

# 启动mds
function start_mds() {
# 检查daemon
Expand Down Expand Up @@ -88,7 +95,7 @@ function start_mds() {
then
subnet=`cat $confPath|grep global.subnet|awk -F"=" '{print $2}'`
port=`cat $confPath|grep global.port|awk -F"=" '{print $2}'`
prefix=`echo $subnet|awk -F/ '{print $1}'|awk -F. '{printf "%d", ($1*(2^24))+($2*(2^16))+($3*(2^8))+$4}'`
prefix=$(ip_value $subnet)
mod=`echo $subnet|awk -F/ '{print $2}'`
mask=$((2**32-2**(32-$mod)))
ip=
Expand All @@ -99,7 +106,7 @@ function start_mds() {
for i in `/sbin/ifconfig -a|grep inet|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
do
# 把ip转换成整数
ip_int=`echo $i|awk -F. '{printf "%d\n", ($1*(2^24))+($2*(2^16))+($3*(2^8))+$4}'`
ip_int=$(ip_value $i)
if [ $(($ip_int&$mask)) -eq $prefix ]
then
ip=$i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ daemonLog=${logPath}/curve-snapshot-daemon.log
# console output
consoleLog=${logPath}/curve-snapshot-console.log

function ip_value() {
echo $(echo $1 | awk -F '[/.]' '{
mask = (2 ^ 8)
printf ("%.0f", (($1 * mask + $2) * mask + $3) * mask + $4)
}')
}

# 启动snapshotcloneserver
function start_server() {
# 检查daemon
Expand Down Expand Up @@ -88,7 +95,7 @@ function start_server() {
then
subnet=`cat $confPath|grep server.subnet|awk -F"=" '{print $2}'`
port=`cat $confPath|grep server.port|awk -F"=" '{print $2}'`
prefix=`echo $subnet|awk -F/ '{print $1}'|awk -F. '{printf "%d", ($1*(2^24))+($2*(2^16))+($3*(2^8))+$4}'`
prefix=$(ip_value $subnet)
mod=`echo $subnet|awk -F/ '{print $2}'`
mask=$((2**32-2**(32-$mod)))
ip=
Expand All @@ -99,7 +106,7 @@ function start_server() {
for i in `/sbin/ifconfig -a|grep inet|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
do
# 把ip转换成整数
ip_int=`echo $i|awk -F. '{printf "%d\n", ($1*(2^24))+($2*(2^16))+($3*(2^8))+$4}'`
ip_int=$(ip_value $i)
if [ $(($ip_int&$mask)) -eq $prefix ]
then
ip=$i
Expand Down

0 comments on commit 6ba0a3c

Please sign in to comment.