aboutsummaryrefslogtreecommitdiff
path: root/contrib/netjail/topo.sh
blob: d94fa0bace0e1b5be31227de28a40b8caf24c4e3 (plain) (blame)
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
#!/bin/bash

declare -A K_PLUGIN
declare -A R_TCP
declare -A R_TCP_ALLOWED
declare -i -A R_TCP_ALLOWED_NUMBER
declare -A R_UDP
declare -A R_UDP_ALLOWED
declare -i -A R_UDP_ALLOWED_NUMBER
declare -A P_PLUGIN

extract_attributes()
{
    line_key=$1
    line=$2

    if [ "$line_key" = "P" ]
    then
	n=$(echo $line|cut -d \| -f 1|awk -F: '{print $2}')
	echo $n
	m=$(echo $line|cut -d \| -f 1|awk -F: '{print $3}')
	echo $m
    else
	number=$(echo $line|cut -d \| -f 1| cut -c 2-|cut -d : -f 2 )
	echo $number
    fi

    #nf=$(echo $line|awk -F: '{print NF}')
    nf=$(echo $line|awk -F\| '{print NF}')
    for ((i=2;i<=$nf;i++))
    do
        entry=$(echo $line |awk -v i=$i -F\| '{print $i}')
        echo $entry
        if [ "$(echo $entry|grep P)" = "" ]; then
	        key=$(echo $entry|cut -d { -f 2|cut -d } -f 1|cut -d : -f 1)
	        echo $key
	        value=$(echo $entry|cut -d { -f 2|cut -d } -f 1|cut -d : -f 2)
	        echo $value
            if [ "$key" = "tcp_port" ]
	        then
                R_TCP_ALLOWED_NUMBER[$number]=0
	            echo tcp port: $value
	            R_TCP[$number]=$value
	        elif [ "$key" = "udp_port" ]
	        then
                R_UDP_ALLOWED_NUMBER[$number]=0
	            echo udp port: $value
	            R_UDP[$number]=$value
	        elif [ "$key" = "plugin" ]
	        then
	            echo plugin: $value
	            echo $line_key
	            if [ "$line_key" = "P" ]
	            then
		            P_PLUGIN[$n,$m]=$value
		            echo $n $m ${P_PLUGIN[$n,$m]}
	            elif [ "$line_key" = "K" ]
	            then
		            K_PLUGIN[$number]=$value
	            fi
	        fi
        else
	        p1=$(echo $entry|cut -d P -f 2|cut -d } -f 1|cut -d : -f 2)
            echo $p1
            p2=$(echo $entry|cut -d P -f 2|cut -d } -f 1|cut -d : -f 3)
	        echo $p2
            if [ "$key" = "tcp_port" ]
	        then
                R_TCP_ALLOWED_NUMBER[$number]+=1
	            R_TCP_ALLOWED[$number,${R_TCP_ALLOWED_NUMBER[$number]},1]=$p1
                R_TCP_ALLOWED[$number,${R_TCP_ALLOWED_NUMBER[$number]},2]=$p2
                echo ${R_TCP_ALLOWED_NUMBER[$number]}
                echo ${R_TCP_ALLOWED[$number,${R_TCP_ALLOWED_NUMBER[$number]},1]}
                echo ${R_TCP_ALLOWED[$number,${R_TCP_ALLOWED_NUMBER[$number]},2]}
	        elif [ "$key" = "udp_port" ]
	        then
                R_UDP_ALLOWED_NUMBER[$number]+=1
	            R_UDP_ALLOWED[$number,${R_UDP_ALLOWED_NUMBER[$number]},1]=$p1
                R_UDP_ALLOWED[$number,${R_UDP_ALLOWED_NUMBER[$number]},2]=$p2
            fi
        fi
    done
    #for ((i=2;i<=$nf;i++))
    # do
	#entry=$(echo $line |awk -v i=$i -F\| '{print $i}')
	key=$(echo $entry|cut -d { -f 2|cut -d } -f 1|cut -d : -f 1)
	value=$(echo $entry|cut -d { -f 2|cut -d } -f 1|cut -d : -f 2)
	
    #done
}

parse_line(){
    line=$1
    echo $line
    key=$(cut -c -1 <<< $line)
    if [ "$key" = "M" ]
    then
	LOCAL_M=$(cut -d : -f 2 <<< $line)
	echo $LOCAL_M
    elif [ "$key" = "N" ]
    then
	GLOBAL_N=$(cut -d : -f 2 <<< $line)
	echo $GLOBAL_N
    for ((i=1;i<=$GLOBAL_N;i++))
    do
        R_TCP[$i]=0
        R_UDP[$i]=0
    done    
    elif [ "$key" = "X" ]
    then
	KNOWN=$(cut -d : -f 2 <<< $line)
	echo $KNOWN
    elif [ "$key" = "T" ]
    then
	PLUGIN=$(cut -d : -f 2 <<< $line)
	echo $PLUGIN
    elif [ "$key" = "B" ]
    then
	BROADCAST=$(cut -d : -f 2 <<< $line)
	echo $BROADCAST
    elif [ "$key" = "K" ]
    then
	echo know node
	extract_attributes $key $line
    elif [ "$key" = "R" ]
    then
	echo router
	extract_attributes $key $line
    elif [ "$key" = "P" ]
    then
	echo node
	extract_attributes $key $line
    fi
}

read_topology_string(){
    string=$1
    IFS=' ' read -r -a array <<< $string
    for element in "${array[@]}"
    do
        echo $element
        parse_line $element
    done
}

read_topology(){
    local filename=$1
    while read line; do
        # reading each line
        parse_line $line
    done < $filename
}