-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntios_wln_ipfunc.sh
executable file
·1767 lines (1427 loc) · 54.8 KB
/
ntios_wln_ipfunc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#---ALL IPV4 FUNCTIONS TO SUPPORT FUNCTION 'Ipv4_Check_And_Generate_Iprange_Handler'
NetMaskV4_DdmaskToCidrprefix() {
#Input args
local netmask=${1}
#Convert to Bit-mask
local cidrprefix=0
local dot_ctr=0
local netmask_ddval=0
local netmask_tmp=${netmask}
local ret=0
while [[ ${dot_ctr} -le ${WLN_IPV4_NUMOF_DOTS_MAX} ]]
do
#Get dotted-decimal value BEFORE the 1st dot
netmask_ddval=${netmask_tmp%%.*}
#Convert dotted-decimal to cidr
cidrprefix=$(NetMaskV4_DdvalToCidr "${netmask_ddval}")
#Accumulate 'cidrprefix' with 'ret'
ret=$(( ret + cidrprefix ))
#Get dotted-decimal value AFTER the 1st dot
netmask_tmp=${netmask_tmp#*.}
#Increment 'dot_ctr' by 1
dot_ctr=$(( dot_ctr + 1 ))
done
#Output
echo "${ret}"
return 0;
}
NetMaskV4_DdvalToCidr() {
#Input args
local ddval=${1}
#Define variables
local ret=0
#Conversion
case "${ddval}" in
255)
ret=8
;;
254)
ret=7
;;
252)
ret=6
;;
248)
ret=5
;;
240)
ret=4
;;
224)
ret=3
;;
192)
ret=2
;;
128)
ret=1
;;
0)
ret=0
;;
*)
#In case 'block_dd_strval' is none of the above
ret=255
;;
esac
#Output
echo "${ret}"
return 0;
}
NetMaskV4_CidrprefixToDdmask() {
#Input args
local cidrprefix=${1}
#Define variables
local cidr=0
local cidrprefix_tmp=${cidrprefix}
local dot_ctr=0
local netmask=${WLN_EMPTYSTRING}
local netmask_ddval=${WLN_EMPTYSTRING}
#Loop and everytime take a piece from 'cidrprefix'.
while [[ ${dot_ctr} -le ${WLN_IPV4_NUMOF_DOTS_MAX} ]]
do
#Determine the 'cidr' for 1 segment
if [[ ${cidrprefix_tmp} -eq 0 ]]; then
cidr=0
elif [[ ${cidrprefix_tmp} -lt ${WLN_IPV4_CIDR_BITS_PER_SEGMENT} ]]; then
cidr=${cidrprefix_tmp}
cidrprefix_tmp=0
else
cidr=${WLN_IPV4_CIDR_BITS_PER_SEGMENT}
cidrprefix_tmp=$(( cidrprefix_tmp - WLN_IPV4_CIDR_BITS_PER_SEGMENT ))
fi
#Get the netmask of 1 segment
netmask_ddval=$(NetMaskV4_CidrToDdval "${cidr}")
#Add/append 'netmask_ddval' to 'netmask'
if [[ "${netmask}" == "${WLN_EMPTYSTRING}" ]]; then
netmask="${netmask_ddval}"
else
netmask="${netmask}.${netmask_ddval}"
fi
#Increment 'dot_ctr' by 1
dot_ctr=$(( dot_ctr + 1 ))
done
#Output
echo "${netmask}"
return 0;
}
NetMaskV4_CidrToDdval() {
#Input args
local cidr=${1}
#Define variables
local ret=0
#Conversion
case "${cidr}" in
8)
ret=255
;;
7)
ret=254
;;
6)
ret=252
;;
5)
ret=248
;;
4)
ret=240
;;
3)
ret=224
;;
2)
ret=192
;;
1)
ret=128
;;
0)
ret=0
;;
esac
#Output
echo "${ret}"
return 0;
}
IPv4_Get_Subnet_Data() {
#Input args
local ipaddr=${1}
local netmask=${2}
#Define variables
local ipaddr_tmp=${ipaddr}
local netmask_tmp=${netmask}
local ipaddr_ddval=${WLN_EMPTYSTRING}
local netmask_ddval=${WLN_EMPTYSTRING}
local subnet_networkidddval=${WLN_EMPTYSTRING}
local subnet_lowerbound_ddval=${WLN_EMPTYSTRING}
local subnet_upperbound_ddval=${WLN_EMPTYSTRING}
local subnet_lowerbound_to_upperbound_range=0
local subnet_networkid="${WLN_EMPTYSTRING}"
local subnet_lowerbound="${WLN_EMPTYSTRING}"
local subnet_upperbound="${WLN_EMPTYSTRING}"
local cidrprefix=0
local dot_ctr=0
#------------------
# subnet_networkid
#------------------
while [[ ${dot_ctr} -le ${WLN_IPV4_NUMOF_DOTS_MAX} ]]
do
#Get part before dot (.)
ipaddr_ddval=${ipaddr_tmp%%.*}
netmask_ddval=${netmask_tmp%%.*}
#Convert dotted-decimal to cidr
subnet_networkid_ddval=$(( ipaddr_ddval & netmask_ddval ))
#Update 'subnet_networkid'
if [[ ${subnet_networkid} == "${WLN_EMPTYSTRING}" ]]; then
#Add the first networkid-dotted-decimal value
subnet_networkid="${subnet_networkid_ddval}"
else
#Append the rest of the networkid-dotted-decimal values
subnet_networkid="${subnet_networkid}.${subnet_networkid_ddval}"
fi
#Update 'subnet_lowerbound' and 'subnet_upperbound'
if [[ ${netmask_ddval} -eq ${WLN_IPV4_MAX} ]]; then #netmask_ddval = 255
subnet_lowerbound_ddval="${subnet_networkid_ddval}"
subnet_upperbound_ddval="${subnet_networkid_ddval}"
else
#Get the 'subnet_lowerbound_to_upperbound_range'
#Remark:
# This is actually the difference between...
# ...the 'lowerbound' and 'upperbound'.
# Example:
# lowerbound = 192.168. 0. 0
# | | | |
# upperbound = 192.168.128.255
# In this example, for each loop-cycle that is the difference between:
# 192 - 192 = 0
# 168 - 168 = 0
# 128 - 0 = 128
# 255 - 0 = 255
#
subnet_lowerbound_to_upperbound_range=$(( WLN_IPV4_MAX - netmask_ddval ))
#Determine the 'subnet_lowerbound_ddval' and 'subnet_upperbound_ddval'
if [[ ${dot_ctr} -lt ${WLN_IPV4_NUMOF_DOTS_MAX} ]]; then #not the last dotted-decimal
subnet_lowerbound_ddval=${subnet_networkid_ddval}
subnet_upperbound_ddval=$(( subnet_networkid_ddval + subnet_lowerbound_to_upperbound_range ))
else #the last dotted-decimal
subnet_lowerbound_ddval=$(( subnet_networkid_ddval + 1 ))
subnet_upperbound_ddval=$(( (subnet_networkid_ddval + subnet_lowerbound_to_upperbound_range) -1 ))
fi
fi
if [[ ${subnet_lowerbound} == "${WLN_EMPTYSTRING}" ]]; then
#Add the first networkid-dotted-decimal value
subnet_lowerbound="${subnet_lowerbound_ddval}"
else #netmask_ddval< 255
#Append the rest of the networkid-dotted-decimal values
subnet_lowerbound="${subnet_lowerbound}.${subnet_lowerbound_ddval}"
fi
if [[ ${subnet_upperbound} == "${WLN_EMPTYSTRING}" ]]; then
#Add the first networkid-dotted-decimal value
subnet_upperbound="${subnet_upperbound_ddval}"
else
#Append the rest of the networkid-dotted-decimal values
subnet_upperbound="${subnet_upperbound}.${subnet_upperbound_ddval}"
fi
#Update variables:
# Get dotted-decimal value AFTER the 1st dot.
ipaddr_tmp=${ipaddr_tmp#*.}
netmask_tmp=${netmask_tmp#*.}
#Increment 'dot_ctr' by 1
dot_ctr=$(( dot_ctr + 1 ))
done
#Get convert 'netmask' to 'cidrprefix'
cidrprefix=$(NetMaskV4_DdvalToCidr "${netmask}")
#SPECIAL CASE: Check if 'cidrprefix = 31 or 32'
if [[ ${cidrprefix} -eq ${WLN_IPV4_CIDR_PREFIX_32} ]] || \
[[ ${cidrprefix} -eq ${WLN_IPV4_CIDR_PREFIX_31} ]]; then
subnet_lowerbound=${WLN_EMPTYSTRING}
subnet_upperbound=${WLN_EMPTYSTRING}
echo "${subnet_networkid},${subnet_lowerbound},${subnet_upperbound}"
return 0;
fi
#Output
echo "${subnet_networkid},${subnet_lowerbound},${subnet_upperbound}"
return 0;
}
Ipv4_Check_And_Generate_Iprange_Handler() {
#Input args
local ipaddr=${1}
local netmask=${2}
local iprange_lowerbound=${3}
local iprange_upperbound=${4}
#Define constants
local PHASE_START=0
local PHASE_CHECK_LOWERBOUND=1
local PHASE_CHECK_UPPERBOUND=2
local PHASE_COMPARE_LOWERBOUND_UPPERBOUND=3
local PHASE_EXIT=4
#Define variables
local phase=${PHASE_START}
local subnet_result=${WLN_EMPTYSTRING}
local subnet_networkid=${WLN_EMPTYSTRING}
local subnet_lowerbound=${WLN_EMPTYSTRING}
local subnet_upperbound=${WLN_EMPTYSTRING}
local ret=${WLN_EMPTYSTRING}
local check_result=false
#Start phase-loop
while true
do
case "${phase}" in
"${PHASE_START}")
#Update variables
ret="${iprange_lowerbound},${iprange_upperbound}"
#Get Subnet NetworkID, Lowerbound and Upperbound
subnet_result=$(IPv4_Get_Subnet_Data "${ipaddr}" "${netmask}")
subnet_networkid=$(echo "${subnet_result}" | cut -d"," -f1)
subnet_lowerbound=$(echo "${subnet_result}" | cut -d"," -f2)
subnet_upperbound=$(echo "${subnet_result}" | cut -d"," -f3)
#Goto next-phase
phase=${PHASE_CHECK_LOWERBOUND}
;;
"${PHASE_CHECK_LOWERBOUND}")
#Check if 'iprange_lowerbound' is part-of 'ipaddr/netmask'
check_result=$(Ipv4_CheckIf_Ip_Is_PartOf_Specified_IpRange \
"${iprange_lowerbound}" \
"${subnet_lowerbound}" \
"${subnet_upperbound}")
#Counteract if 'check_result = false'
if [[ ${check_result} == false ]]; then
#Validate and Generate IP-ranges
ret=$(Ipv4_Generate_Iprange "${netmask}" "${subnet_lowerbound}" "${subnet_upperbound}")
#Goto next-phase
phase=${PHASE_EXIT}
else
#Goto next-phase
phase=${PHASE_CHECK_UPPERBOUND}
fi
;;
"${PHASE_CHECK_UPPERBOUND}")
#Check if 'iprange_upperbound' is part-of 'ipaddr/netmask'
check_result=$(Ipv4_CheckIf_Ip_Is_PartOf_Specified_IpRange \
"${iprange_upperbound}" \
"${subnet_lowerbound}" \
"${subnet_upperbound}")
#Counteract if 'check_result = false'
if [[ ${check_result} == false ]]; then
#Validate and Generate IP-ranges
ret=$(Ipv4_Generate_Iprange "${netmask}" "${subnet_lowerbound}" "${subnet_upperbound}")
#Goto next-phase
phase=${PHASE_EXIT}
else
#Goto next-phase
phase=${PHASE_COMPARE_LOWERBOUND_UPPERBOUND}
fi
;;
"${PHASE_COMPARE_LOWERBOUND_UPPERBOUND}")
#Check if 'iprange_lowerbound' is smaller than 'iprange_upperbound'
check_result=$(Ipv4_CheckIf_FirstIp_IsSmallerThan_SecondIp \
"${iprange_lowerbound}" \
"${iprange_upperbound}")
#Counteract if 'check_result = false'
if [[ ${check_result} == false ]]; then
#Validate and Generate IP-ranges
ret=$(Ipv4_Generate_Iprange "${netmask}" "${subnet_lowerbound}" "${subnet_upperbound}")
fi
#Goto next-phase
phase=${PHASE_EXIT}
;;
"${PHASE_EXIT}")
#Output
echo "${ret}"
return 0;
;;
esac
done
}
Ipv4_CheckIf_FirstIp_IsSmallerThan_SecondIp() {
#Input args
local ipaddr1=${1}
local ipaddr2=${2}
#Define and initialize variables
local ret=false
local ipaddr1_ddval=${WLN_EMPTYSTRING}
local ipaddr2_ddval=${iprange_upperbound}
local ipaddr1_tmp=${ipaddr1}
local ipaddr2_tmp=${ipaddr2}
#Compare 'ipaddr1_conv' with 'ipaddr2_conv'
# shellcheck disable=SC2004
for (( s=1; s<=${WLN_IPV4_NUMOF_SEGMENTS_MAX}; s++ ))
do
#Get the value BEFORE the 1st dot (.)
ipaddr1_ddval=${ipaddr1_tmp%%.*}
ipaddr2_ddval=${ipaddr2_tmp%%.*}
#Get the remaining value AFTER the 1st dot (.)
ipaddr1_tmp=${ipaddr1_tmp#*.}
ipaddr2_tmp=${ipaddr2_tmp#*.}
#Check if 'ipaddr1_conv_block_dec > ipaddr2_conv_block_dec'
if [[ ${ipaddr1_ddval} -lt ${ipaddr2_ddval} ]]; then
ret=true
break
elif [[ ${ipaddr1_ddval} -gt ${ipaddr2_ddval} ]]; then
ret=false
break
fi
done
#Output
echo "${ret}"
return 0;
}
Ipv4_CheckIf_Ip_Is_PartOf_Specified_IpRange() {
#Input args
local ipaddr=${1}
local iprange_lowerbound=${2}
local iprange_upperbound=${3}
#Compare 'ipaddr_conv' with 'iprange_lowerbound_conv'
local ret=false
local ipaddr_tmp=${ipaddr}
local iprange_lowerbound_tmp=${iprange_lowerbound}
local iprange_upperbound_tmp=${iprange_upperbound}
local ipaddr_ddval=${WLN_EMPTYSTRING}
local iprange_lowerbound_ddval=${WLN_EMPTYSTRING}
local iprange_upperbound_ddval=${WLN_EMPTYSTRING}
local ipaddr_isabove_lowerbound=false
local ipaddr_isbelow_upperbound=false
# shellcheck disable=SC2004
for (( s=1; s<=${WLN_IPV4_NUMOF_SEGMENTS_MAX}; s++ ))
do
#Get the value BEFORE the 1st dot (.)
ipaddr_ddval=${ipaddr_tmp%%.*}
iprange_lowerbound_ddval=${iprange_lowerbound_tmp%%.*}
iprange_upperbound_ddval=${iprange_upperbound_tmp%%.*}
#Get the remaining value AFTER the 1st dot (.)
ipaddr_tmp=${ipaddr_tmp#*.}
iprange_lowerbound_tmp=${iprange_lowerbound_tmp#*.}
iprange_upperbound_tmp=${iprange_upperbound_tmp#*.}
#Compare 'ipaddr_ddval' with 'iprange_lowerbound_ddval'
#If Greater than, set boolean 'ipaddr_isabove_lowerbound' to 'true'.
#If Smaller than, set boolean 'ret' to 'false'.
#If equal, do nothing.
if [[ ${ipaddr_ddval} -gt ${iprange_lowerbound_ddval} ]]; then
ipaddr_isabove_lowerbound=true
elif [[ ${ipaddr_ddval} -lt ${iprange_lowerbound_ddval} ]]; then
ret=false
break
fi
#Compare 'ipaddr_ddval' with 'iprange_upperbound_ddval'
#If Smaller than, set boolean 'ipaddr_isbelow_upperbound' to 'true'.
#If Greater than, set boolean 'ret' to 'false'.
#If equal, do nothing.
if [[ ${ipaddr_ddval} -lt ${iprange_upperbound_ddval} ]]; then #less
ipaddr_isbelow_upperbound=true
elif [[ ${ipaddr_ddval} -gt ${iprange_upperbound_ddval} ]]; then #Greater
ret=false
break
fi
#Check if both 'ipaddr_isabove_lowerbound' and 'ipaddr_isbelow_upperbound' are 'true'
if [[ ${ipaddr_isabove_lowerbound} == true ]] && [[ ${ipaddr_isbelow_upperbound} == true ]]; then
ret=true
break
fi
done
#Output
echo "${ret}"
return 0;
}
Ipv4_Generate_Iprange() {
#Input args
local netmask=${1}
local subnet_lowerbound=${2}
local subnet_upperbound=${3}
#Define and initialize variables
local cidrprefix=0
cidrprefix=$(NetMaskV4_DdvalToCidr "${netmask}")
local subnet_lowerbound_output=${subnet_lowerbound}
local subnet_upperbound_output=${subnet_upperbound}
local subnet_lowerbound_wo_last_hexblock=${subnet_upperbound%.*} #use the 'subnet_upperbound'
local ret="${subnet_lowerbound_output},${subnet_upperbound_output}"
#Check if 'netmask => 24'
#Remark:
# If 'netmask = 24', then a maximum of 254 IP-addresses are available.
# However, the first and last IP-addresses can NOT be used:
# first IP-address -> network-number
# last IP-address -> broadcast IP
if [[ ${cidrprefix} -ge ${WLN_IPV4_CIDR_PREFIX_24} ]]; then
echo "${ret}"
return 0;
fi
#In case 'netmask < 112'
#Update 'subnet_lowerbound_output' which should ALWAYS end with the 4 hex-digits (0000)
subnet_lowerbound_output="${subnet_lowerbound_wo_last_hexblock}.${WLN_IPV4_SUBNET_LOWERBOUND_MINVAL}"
#Update 'subnet_upperbound_output' which should ALWAYS end with the 4 hex-digits (ffff)
subnet_upperbound_output="${subnet_lowerbound_wo_last_hexblock}.${WLN_IPV4_SUBNET_UPPERBOUND_MAXVAL}"
#Update 'ret'
ret="${subnet_lowerbound_output},${subnet_upperbound_output}"
#Output
echo "${ret}"
return 0;
}
#---ALL IPV6 FUNCTIONS TO SUPPORT FUNCTION 'Ipv6_Check_And_Generate_Iprange_Handler'
Append_Chars() {
#Input args
local str=${1}
local char=${2}
local numofchars=${3}
#Define and initialize variables
local ret=${str}
#Append chars to 'ret'
# shellcheck disable=SC2004
for (( c=0; c<${numofchars}; c++ ))
do
ret="${ret}${char}"
done
#Output
echo "${ret}"
return 0;
}
CheckIf_Char_IsPresent() {
#Input args
local str=${1}
local substr=${2}
#Define variables
local noccur=0
#Count number of 'substr' within 'str'
noccur=$(Count_NumOf_Substring_Within_String "${str}" "${substr}")
#Output
if [[ ${noccur} -gt 0 ]]; then
echo true
else
echo false
fi
return 0;
}
Count_NumOf_Substring_Within_String() {
#Input args
local str=${1}
local substr=${2}
#Define variables
local noccur=0
#Count number of 'substr' within 'str'
noccur=$(echo "${str}" | grep -oF "${substr}" | wc -l)
#Output
echo "${noccur}"
return 0;
}
Get_First_Nchar() {
#Input args
local str=${1}
local numofchars=${2}
#Check if 'str = <Empty String>'
if [[ "${str}" == "${WLN_EMPTYSTRING}" ]]; then
echo ${WLN_EMPTYSTRING}
return 0;
fi
#Get length of 'str'
local str_len=${#str}
#Get the last char of 'str'
local lastChar=${str:0:numofchars}
#Output
echo "${lastChar}"
return 0;
}
Get_Last_Nchar() {
#Input args
local str=${1}
local numofchars=${2}
#Check if 'str = <Empty String>'
if [[ "${str}" == "${WLN_EMPTYSTRING}" ]]; then
echo ${WLN_EMPTYSTRING}
return 0;
fi
#Get length of 'str'
local str_len=${#str}
#Get position
local str_lastchar_pos=$(( str_len - numofchars ))
#Get the last char of 'str'
local lastChar=${str:str_lastchar_pos:numofchars}
#Output
echo "${lastChar}"
return 0;
}
Get_StringLen_Exluding_Specified_Char() {
#Input args
local str=${1}
local char=${2}
#Define variables
local str_wo_char=${WLN_EMPTYSTRING}
#Substitute 'char' with an <Empty String>
# shellcheck disable=SC2001
str_wo_char=$(echo "${str}" | sed "s/${char}//g")
#Get the length of 'str_wo_char'
local str_wo_char_len=${#str_wo_char}
#Output
echo "${str_wo_char_len}"
return 0;
}
Dec64ToHex() {
#Input args
local decval=${1}
#Conversion
printf '%x\n' "${decval}"
return 0;
}
HexToDec64() {
#Input args
local hexval=${1}
#Conversion
local dec=$((16#${hexval}))
#Output
printf "%u\n" ${dec}
return 0;
}
Ipv6_Subst_DoubleColons_With_Zeros() {
#Input args
local ipaddr=${1}
#Define variables
local doubleColonIsPresent=false
local noccur=0
local ret=${WLN_EMPTYSTRING}
#Check if there are any double-colons
doubleColonIsPresent=$(CheckIf_Char_IsPresent "${ipaddr}" "${WLN_DOUBLECOLON}")
if [[ ${doubleColonIsPresent} == false ]]; then
echo "${ipaddr}"
return 0;
fi
#Count the number of colon's (:)
noccur=$(Count_NumOf_Substring_Within_String "${ipaddr}" "${WLN_COLON}")
#Substract the double-colon(::) from 'noccur'
noccur_wo_doubleColon=$((noccur-2))
#Determine the substitute string (substStr) for the double-colon
#Remark:
# 'substStr' depends on the number of colons found within 'ipaddr'
case "${noccur_wo_doubleColon}" in
0)
substStr=":0:0:0:0:0:0:"
;;
1)
substStr=":0:0:0:0:0:"
;;
2)
substStr=":0:0:0:0:"
;;
3)
substStr=":0:0:0:"
;;
4)
substStr=":0:0:"
;;
5)
substStr=":0:"
;;
esac
#Substiute double-colon (::) with 'substr'
# shellcheck disable=SC2001
ret=$(echo "${ipaddr}" | sed "s/${WLN_DOUBLECOLON}/${substStr}/g")
#Output
echo "${ret}"
return 0;
}
Ipv6_Determine_Increment_Value_Of_4Bit_Block() {
#Input args
# Definition:
# decimal: 8 4 2 1
# binary of a 4-bit Hex: 0 0 0 0
# Meaning:
# binary position: 1 -> binary: 1 0 0 0 -> decimal = 8
# binary position: 2 -> binary: 0 1 0 0 -> decimal = 4
# binary position: 3 -> binary: 0 0 1 0 -> decimal = 2
# binary position: 3 -> binary: 0 0 0 1 -> decimal = 1
local netmask=${1}
#Check if 'netmask > 128'
if [[ ${netmask} -gt ${WLN_IPV6_ADDRLEN_IN_BITS} ]]; then
echo "${WLN_IPV6_BOGUS_INTVAL_255}"
return 0;
fi
#Determine the 'subnet_lowerval_incr'
local subnet_lowerval_incr=0
local mod_netmask=$(( netmask % WLN_IPV6_ONEHEXLEN_IN_BITS ))
case "${mod_netmask}" in
1)
subnet_lowerval_incr=8
#####################################
# The ranges are:
# startVal| 0 | 8
# --------+---+---
# endVal | 7 | f
#####################################
# For example: netmask=41
# subnet_lowerval_dec
# |
# subnet_lowerbound #1: cdef:2222:33[0]0:0000:0000:0000:0000:0000
# subnet_upperbound #1: cdef:2222:33[7]f:ffff:ffff:ffff:ffff:ffff
# |
# subnet_upperval_dec
#
# subnet_lowerval_dec
# |
# subnet_lowerbound #2: cdef:2222:33[8]0:0000:0000:0000:0000:0000
# subnet_upperbound #2: cdef:2222:33[f]f:ffff:ffff:ffff:ffff:ffff
# |
# subnet_upperval_dec
;;
2)
subnet_lowerval_incr=4
#####################################
# The ranges are:
# startVal| 0 | 4 | 8 | c
# --------+---+---+---+---
# endVal | 3 | 7 | b | f
#####################################
# For example: netmask=42
# subnet_lowerbound #1: cdef:2222:33[0]0:0000:0000:0000:0000:0000
# subnet_upperbound #1: cdef:2222:33[3]f:ffff:ffff:ffff:ffff:ffff
# subnet_lowerbound #2: cdef:2222:33[4]0:0000:0000:0000:0000:0000
# subnet_upperbound #2: cdef:2222:33[7]f:ffff:ffff:ffff:ffff:ffff
# subnet_lowerbound #3: cdef:2222:33[8]0:0000:0000:0000:0000:0000
# subnet_upperbound #3: cdef:2222:33[b]f:ffff:ffff:ffff:ffff:ffff
# subnet_lowerbound #3: cdef:2222:33[c]0:0000:0000:0000:0000:0000
# subnet_upperbound #3: cdef:2222:33[f]f:ffff:ffff:ffff:ffff:ffff
;;
3)
subnet_lowerval_incr=2
#####################################
# The ranges are:
# startVal| 0 | 2 | .. | c | e |
# --------+---+---+----+---+---+
# endVal | 1 | 3 | .. | d | f |
#####################################
# For example: netmask=43
# subnet_lowerbound #1: cdef:2222:33[0]0:0000:0000:0000:0000:0000
# subnet_upperbound #1: cdef:2222:33[1]f:ffff:ffff:ffff:ffff:ffff
# subnet_lowerbound #2: cdef:2222:33[2]0:0000:0000:0000:0000:0000
# subnet_upperbound #2: cdef:2222:33[3]f:ffff:ffff:ffff:ffff:ffff
# ...
# subnet_lowerbound #3: cdef:2222:33[c]0:0000:0000:0000:0000:0000
# subnet_upperbound #3: cdef:2222:33[d]f:ffff:ffff:ffff:ffff:ffff
# subnet_lowerbound #3: cdef:2222:33[e]0:0000:0000:0000:0000:0000
# subnet_upperbound #3: cdef:2222:33[f]f:ffff:ffff:ffff:ffff:ffff
;;
4)
subnet_lowerval_incr=0
#####################################
# The ranges are:
# startVal| 0 | 1 | .. | e | f |
# --------+---+---+----+----+----
# endVal | 0 | 1 | .. | e | f |
#####################################
# For example: netmask=44
# subnet_lowerbound: cdef:2222:33[3]0:0000:0000:0000:0000:0000
# subnet_upperbound: cdef:2222:33[3]f:ffff:ffff:ffff:ffff:ffff
#
# REMARK:
# In this SPECIAL case it means that the hex-digit '3',...
# at hexpos_abs=11 belongs to the networkID and can NOT be
# used for the HostID.
#####################################
;;
esac
#Output
echo "${subnet_lowerval_incr}"
return 0;
}
Ipv6_Get_Trailing_HexBlock_Len() {
#Input args
local ipaddr=${1}
#Define variables
local ipaddr_wo_colons=${WLN_EMPTYSTRING}
local hexblock_trail=${WLN_EMPTYSTRING}
#IMPORTANT: Remove trailing colon's (:)
# shellcheck disable=SC2001
ipaddr_wo_colons=$(echo "${ipaddr}" | sed 's/:*$//g')
#Get the substring on the right-side of the last colon (:)
# shellcheck disable=SC2001
hexblock_trail=$(echo "${ipaddr_wo_colons}" | sed 's/.*://')
#Calculate the length of 'hexblock_trail'
local hexblock_trail_hexlen=${#hexblock_trail}
#Output
echo "${hexblock_trail_hexlen}"
return 0;
}
Ipv6_Prepend_Zeros(){
#Input args
local ipaddr=${1}
#Fill up each IP-block with zeros to consist of 4 hex-digits
local ipaddr_new=${WLN_EMPTYSTRING}
local ipblock_curr=${WLN_EMPTYSTRING}
local ipblock_curr_len=0
local ipblock_new=${WLN_EMPTYSTRING}
# shellcheck disable=SC2004
for (( blocknum=1; blocknum<=${WLN_IPV6_ADDRLEN_IN_BLOCKS}; blocknum++ ))
do
#Get the 4 hex-digits of each blocknum
ipblock_curr=$(echo "${ipaddr}" | cut -d":" -f"${blocknum}")
#Get the length of 'ipblock'
ipblock_curr_len=${#ipblock_curr}
case "${ipblock_curr_len}" in
1)
ipblock_new="000${ipblock_curr}"
;;
2)
ipblock_new="00${ipblock_curr}"
;;
3)
ipblock_new="0${ipblock_curr}"
;;
*)
ipblock_new="${ipblock_curr}"
;;
esac
#Reconstruct 'ipaddr_new'
if [[ -z "${ipaddr_new}" ]]; then
ipaddr_new="${ipblock_new}"
else
ipaddr_new="${ipaddr_new}:${ipblock_new}"
fi
done
#Output
echo "${ipaddr_new}"
return 0;
}
Ipv6_Retrieve_Partial_Of_IpAddress() {
#Input args
local ipaddr=${1}
local netmask=${2}
#Check if 'netmask > 128'
if [[ ${netmask} -gt ${WLN_IPV6_ADDRLEN_IN_BITS} ]]; then
echo "${WLN_IPV6_BOGUS_STRVAL}"
return 0;
fi
#Check if 'netmask < 4'
if [[ ${netmask} -lt ${WLN_IPV6_ONEHEXLEN_IN_BITS} ]]; then
echo "${WLN_EMPTYSTRING}"
return 0;
fi
#Check if 'netmask = 128'
if [[ ${netmask} -eq ${WLN_IPV6_ADDRLEN_IN_BITS} ]]; then
echo "${networkid}"
return 0;
fi
#Initialize variables
local char=${WLN_EMPTYSTRING}
local networkid=${WLN_EMPTYSTRING}
local hexdigit_ctr=0
local netmask_hexdigits=$(( netmask/WLN_IPV6_ONEHEXLEN_IN_BITS ))
#Loop thru 'ipaddr'
local ipaddr_len=${#ipaddr}
# shellcheck disable=SC2004
for (( h=0; h<${ipaddr_len}; h++ ))
do
#Get next 'char' from 'ipaddr'
char="${ipaddr:${h}:1}"
#Append 'char' to 'networkid'
networkid="${networkid}${char}"
#Check if 'char != WLN_COLON'
# if not, then increment counter 'hexdigit_ctr'.
if [[ "${char}" != "${WLN_COLON}" ]]; then