Skip to content

Commit cdc9bae

Browse files
committed
Random generation of IPv4 subnets
1 parent 347261c commit cdc9bae

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

auxiliary_functions.sh

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function ping_function(){
9393
## @fn curl_function()
9494
## @brief runs curl for the given IPv6 address from
9595
## the given virtual machine
96-
curl_function(){
96+
function curl_function(){
9797
local where_addr=$1
9898
local ip_port=$2
9999
local from_intf=$3
@@ -329,19 +329,45 @@ function check_state_result() {
329329
return $check_res
330330
}
331331

332+
## @fn wait_for_condition
333+
## @brief the function waits for a successful execution of a function or a
334+
## command. The function or a command should return 0 in case of success and
335+
## 1 in case of failure.
332336
function wait_for_condition() {
333337
local sleep_time="$2"
334338
if [ "$sleep_time" = "" ]
335339
then
336340
sleep_time=0.1 #100ms
337341
fi
338-
local cond_res=`$1` #execute condition 1
339-
while [ $? -ne 0 ]
342+
$1 #execute the provided command
343+
local cond_res=$?
344+
while [ $cond_res -ne 0 ]
340345
do
341-
cond_res=`$1`
346+
sleep $sleep_time
347+
$1 #execute the provided command
348+
cond_res=$?
342349
done
343350
}
344351

352+
## @fn is_process_nonexistent
353+
## @brief the function determines whether the process is not running. Returns
354+
## 0 if there are no processes with the specified name and 1 otherwise.
355+
function is_process_nonexistent() {
356+
local proc_name=$1
357+
n_processes=`pgrep $proc_name | wc -l`
358+
if [ $n_processes -ne 0 ]
359+
then
360+
#echo "There are $n_processes copies of $proc_name have been left in the memory" 1>&2
361+
return 1
362+
fi
363+
#echo "No processes $proc_name have been left in the memory" 1>&2
364+
return 0
365+
}
366+
367+
## @fn results_are_ok
368+
## @brief the function checks whether all the specified results (return codes)
369+
## contain correct values. Returns 0 if there are no non-zero return codes and
370+
## 1 otherwise.
345371
function results_are_ok() {
346372
local all_ok=0
347373
for res in "$@"

basic_rest_functions.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ fi
1515
# Constants and a configuration
1616
#
1717

18+
## @brief Max value of $RANDOM function
19+
declare RANDOM_MAX=32768
20+
1821
## @brief Ordered list of digits for hexadecimal numerical system
1922
declare -a HEX_DIGITS=("0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F")
2023

2124
## @brief Ordered list of capital latin letters
2225
declare -a LAT_LETTERS=(A B C D E F G H I J K L M N O P Q R S T U V U W X Y Z)
2326

27+
## @brief Powers of 2
28+
declare -a POWERS_OF_2=("1" "2" "4" "8" "16" "32" "64" "128" "256")
29+
2430
## @brief Contains name of log file
2531
declare MESG_LOG=log
2632

@@ -336,6 +342,65 @@ function make_random_ipv4_address(){
336342
echo "$subnet_prefix"
337343
}
338344

345+
## @fn make_random_ipv4_subnet_octet
346+
## @brief Creates a random octet for subnet with the given length (maximum
347+
## number of the most significant bits)
348+
function make_random_ipv4_subnet_octet() {
349+
local k=$1
350+
if [ $k -lt 1 ] || [ $k -gt 8 ]
351+
then
352+
echo "0"
353+
return 1
354+
fi
355+
local c=${POWERS_OF_2[$k]}
356+
local r=`expr $RANDOM \* $c / $RANDOM_MAX`
357+
local delta=`expr 256 / $c`
358+
local addr=`expr $delta \* $r`
359+
echo $addr
360+
return 0
361+
}
362+
363+
## @fn
364+
## @brief
365+
function make_random_ipv4_subnet() {
366+
local prefix_len=$1
367+
if [ $prefix_len -le 0 ] || [ $prefix_len -ge 32 ]
368+
then
369+
echo "0.0.0.0"
370+
return 1
371+
fi
372+
local n_octets=4
373+
local i_octet=0
374+
local bits_left=$prefix_len
375+
local bits_in_octet=
376+
local subnet_str=
377+
local octet_str=
378+
while [ $i_octet -lt $n_octets ]
379+
do
380+
bits_in_octet=$bits_left
381+
if [ $bits_in_octet -lt 0 ]
382+
then
383+
subnet_str=$subnet_str".0"
384+
else
385+
if [ $bits_in_octet -ge 8 ]
386+
then
387+
octet_str=`make_random_ipv4_subnet_octet 8`
388+
else
389+
octet_str=`make_random_ipv4_subnet_octet $bits_in_octet`
390+
fi
391+
if [ $i_octet -eq 0 ]
392+
then
393+
subnet_str=$octet_str
394+
else
395+
subnet_str=$subnet_str"."$octet_str
396+
fi
397+
fi
398+
bits_left=`expr $bits_left - 8`
399+
i_octet=`expr $i_octet + 1`
400+
done
401+
echo $subnet_str
402+
}
403+
339404
## @fn make_random_ipam_subnet_prefix_ipv4()
340405
## @brief Creates random IPv4 subnet prefix of a given length
341406
function make_random_ipam_subnet_prefix_ipv4(){

0 commit comments

Comments
 (0)