blob: a781722833e40c953d93ff5b1d69787df4741e85 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001#!/bin/bash
2
3##################################################################################
4#
5# Copyright (C) 2017 Craig Miller
6#
7# See the file "LICENSE" for information on usage and redistribution
8# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9# Distributed under GPLv2 License
10#
11##################################################################################
12
13
14# IPv6 Address Expansion functions
15#
16# by Craig Miller 19 Feb 2017
17#
18# 16 Nov 2017 v0.93 - added CLI functionality
19
20
21VERSION=0.93
22
23empty_addr="0000:0000:0000:0000:0000:0000:0000:0000"
24empty_addr_len=${#empty_addr}
25
26function usage {
27 echo " $0 - expand compressed IPv6 addresss "
28 echo " e.g. $0 2001:db8:1:12:123::456 "
29 echo " "
30 echo " -t self test"
31 echo " "
32 echo " By Craig Miller - Version: $VERSION"
33 exit 1
34 }
35
36if [ "$1" == "-h" ]; then
37 #call help
38 usage
39fi
40
41#
42# Expands IPv6 quibble to 4 digits with leading zeros e.g. db8 -> 0db8
43#
44# Returns string with expanded quibble
45
46function expand_quibble() {
47 addr=$1
48 # create array of quibbles
49 addr_array=(${addr//:/ })
50 addr_array_len=${#addr_array[@]}
51 # step thru quibbles
52 for ((i=0; i< $addr_array_len ; i++ ))
53 do
54 quibble=${addr_array[$i]}
55 quibble_len=${#quibble}
56 case $quibble_len in
57 1) quibble="000$quibble";;
58 2) quibble="00$quibble";;
59 3) quibble="0$quibble";;
60 esac
61 addr_array[$i]=$quibble
62 done
63 # reconstruct addr from quibbles
64 return_str=${addr_array[*]}
65 return_str="${return_str// /:}"
66 echo $return_str
67}
68
69#
70# Expands IPv6 address :: format to full zeros
71#
72# Returns string with expanded address
73
74function expand() {
75 if [[ $1 == *"::"* ]]; then
76 # check for leading zeros on front_addr
77 if [[ $1 == "::"* ]]; then
78 front_addr=0
79 else
80 front_addr=$(echo $1 | sed -r 's;([^ ]+)::.*;\1;')
81 fi
82 # check for trailing zeros on back_addr
83 if [[ $1 == *"::" ]]; then
84 back_addr=0
85 else
86 back_addr=$(echo $1 | sed -r 's;.*::([^ ]+);\1;')
87 fi
88 front_addr=$(expand_quibble $front_addr)
89 back_addr=$(expand_quibble $back_addr)
90
91 new_addr=$empty_addr
92 front_addr_len=${#front_addr}
93 back_addr_len=${#back_addr}
94 # calculate fill needed
95 num_zeros=$(($empty_addr_len - $front_addr_len - $back_addr_len - 1))
96
97 #fill_str=${empty_addr[0]:0:$num_zeros}
98 new_addr="$front_addr:${empty_addr[0]:0:$num_zeros}$back_addr"
99
100 # return expanded address
101 echo $new_addr
102 else
103 # return input with expandd quibbles
104 expand_quibble $1
105 fi
106}
107
108# self test - call with '-t' parameter
109if [ "$1" == "-t" ]; then
110 # add address examples to test
111 expand fd11::1d70:cf84:18ef:d056
112 expand 2a01::1
113 expand fe80::f203:8cff:fe3f:f041
114 expand 2001:db8:123::5
115 expand 2001:470:ebbd:0:f203:8cff:fe3f:f041
116 # special cases
117 expand ::1
118 expand fd32:197d:3022:1101::
119 exit 1
120fi
121
122# allow script to be sourced (with no arguements)
123if [[ $1 != "" ]]; then
124 # validate input is an IPv6 address
125 if [[ $1 == *":"* ]]; then
126 expand $1
127 else
128 echo "ERROR: unregcognized IPv6 address $1"
129 exit 1
130 fi
131fi