forked from imunes/imunes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimunes.tcl
358 lines (312 loc) · 8.93 KB
/
imunes.tcl
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
#
# Copyright 2004-2013 University of Zagreb.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# This work was supported in part by Croatian Ministry of Science
# and Technology through the research contract #IP-2003-143.
#
#****h* imunes/imunes.tcl
# NAME
# imunes.tcl
# FUNCTION
# Starts imunes in batch or interactive mode. Include procedures from
# external files and initializes global variables.
#
# imunes [-b] [-e experiment_id] [filename]
#
# When starting the program in batch mode the option -b must be
# specified.
#
# When starting the program with defined filename, configuration for
# file "filename" is loaded to imunes.
#****
#
# Include procedure definitions from external files. There must be
# some better way to accomplish the same goal, but that's how we do it
# for the moment.
#
# The ROOTDIR and LIBDIR variables will be automatically set to the proper
# value by the installation script.
#
#****v* imunes.tcl/ROOTDIR
# NAME
# ROOTDIR
# FUNCTION
# The location of imunes library files. The ROOTDIR and LIBDIR variables
# will be automatically set to the proper value by the installation script.
#*****
#****v* imunes.tcl/LIBDIR
# NAME
# LIBDIR
# FUNCTION
# The location of imunes library files. The ROOTDIR and LIBDIR variables
# will be automatically set to the proper value by the installation script.
#*****
set LIBDIR ""
set ROOTDIR "."
if { $ROOTDIR == "." } {
set BINDIR ""
} else {
set BINDIR "bin"
}
try {
source "$ROOTDIR/$LIBDIR/helpers.tcl"
} on error { result options } {
puts stderr "Could not find file helpers.tcl in $ROOTDIR/$LIBDIR:"
puts stderr $result
exit 1
}
safePackageRequire [list cmdline platform ip base64]
set initMode 0
set execMode interactive
set debug 0
set printVersion 0
set prepareFlag 0
set forceFlag 0
set options {
{e.arg "" "Specify experiment ID"}
{eid.arg "" "Specify experiment ID"}
{b "Turn on batch mode"}
{batch "Turn on batch mode"}
{d.secret "Turn on debug mode"}
{p "Prepare virtual root file system"}
{prepare "Prepare virtual root file system"}
{f "Force virtual root preparation (delete existing vroot)"}
{force "Force virtual root preparation (delete existing vroot)"}
{i "Setup devfs rules for virtual nodes (Only on FreeBSD)"}
{v "Print IMUNES version"}
{version "Print IMUNES version"}
{h "Print this message"}
}
set usage [getPrettyUsage $options]
parseCmdArgs $options $usage
set baseTitle "IMUNES"
set imunesVersion "Unknown"
set imunesChangedDate ""
set imunesLastYear ""
set imunesAdditions ""
fetchImunesVersion
if { $printVersion } {
printImunesVersion
exit
}
set isOSfreebsd false
set isOSlinux false
set isOSwin false
setPlatformVariables
if { $prepareFlag } {
prepareVroot
exit
}
# Runtime libriaries
foreach file [glob -directory $ROOTDIR/$LIBDIR/runtime *.tcl] {
if { [string match -nocase "*linux.tcl" $file] != 1 } {
safeSourceFile $file
}
}
if {! [info exists eid_base]} {
set eid_base [genExperimentId]
}
set option_defaults {
auto_etc_hosts 0
}
set gui_option_defaults {
show_interface_names 1
show_interface_ipv4 1
show_interface_ipv6 1
show_node_labels 1
show_link_labels 1
show_background_image 0
show_annotations 1
show_grid 1
icon_size "normal"
zoom 1
}
foreach {option default_value} [concat $option_defaults $gui_option_defaults] {
global $option
set $option $default_value
}
# Set default L2 node list
set l2nodes "hub lanswitch rj45 stpswitch filter packgen ext extnat"
# Set default L3 node list
set l3nodes "router host pc nat64 extelem"
# Set default supported router models
set supp_router_models "frr quagga static"
if { $isOSlinux } {
# Limit default nodes on linux
set l2nodes "hub lanswitch rj45 ext extnat"
set l3nodes "router pc host nat64 extelem"
set supp_router_models "frr quagga static"
safeSourceFile $ROOTDIR/$LIBDIR/runtime/linux.tcl
if { $initMode == 1 } {
#puts "INFO: devfs preparation is done only on FreeBSD."
exit
}
}
if { $isOSfreebsd } {
safeSourceFile $ROOTDIR/$LIBDIR/runtime/freebsd.tcl
if { $initMode == 1 } {
prepareDevfs 1
exit
}
}
if { $execMode == "batch" } {
set err [checkSysPrerequisites]
if { $err != "" } {
puts $err
exit
}
}
# Configuration libraries
foreach file [glob -directory $ROOTDIR/$LIBDIR/config *.tcl] {
safeSourceFile $file
}
# The following files need to be sourced in this particular order. If not
# the placement of the toolbar icons will be altered.
# L2 nodes
foreach file $l2nodes {
safeSourceFile "$ROOTDIR/$LIBDIR/nodes/$file.tcl"
}
# L3 nodes
foreach file $l3nodes {
safeSourceFile "$ROOTDIR/$LIBDIR/nodes/$file.tcl"
}
# additional nodes
safeSourceFile "$ROOTDIR/$LIBDIR/nodes/localnodes.tcl"
safeSourceFile "$ROOTDIR/$LIBDIR/nodes/annotations.tcl"
#
# Global variables are initialized here
#
#****v* imunes.tcl/prefs
# NAME
# prefs
# FUNCTION
# Contains the list of preferences. When starting a program
# this list is empty.
#*****
# Clipboard
namespace eval cf::clipboard {}
set cf::clipboard::node_list {}
set cf::clipboard::link_list {}
set cf::clipboard::annotation_list {}
set cf::clipboard::canvas_list {}
set cf::clipboard::image_list {}
set cfg_list {}
set curcfg ""
#****v* imunes.tcl/editor_only
# NAME
# editor_only -- if set, Experiment -> Execute is disabled
# FUNCTION
# IMUNES GUI can be used in editor-only mode.i
# This variable can be modified in .imunesrc.
set editor_only false
set winOS false
if { $isOSwin } {
set winOS true
}
if { !$isOSwin } {
catch {exec magick -version | head -1 | cut -d " " -f 1,2,3} imInfo
} else {
set imInfo $env(PATH)
}
set hasIM true
if { [string match -nocase "*imagemagick*" $imInfo] != 1} {
set hasIM false
}
set runtimeDir "/var/run/imunes"
#
# Read config files, the first one found: .imunesrc, $HOME/.imunesrc
#
# XXX
readConfigFile
#
# Initialization should be complete now, so let's start doing something...
#
if {$execMode == "interactive"} {
safePackageRequire Tk "To run the IMUNES GUI, Tk must be installed."
foreach file "canvas copypaste drawing editor help theme linkcfgGUI \
mouse nodecfgGUI widgets" {
safeSourceFile "$ROOTDIR/$LIBDIR/gui/$file.tcl"
}
source "$ROOTDIR/$LIBDIR/gui/initgui.tcl"
source "$ROOTDIR/$LIBDIR/gui/topogen.tcl"
if { $debug == 1 } {
source "$ROOTDIR/$LIBDIR/gui/debug.tcl"
}
newProject
if { $argv != "" && [file exists $argv] } {
set ::cf::[set curcfg]::currentFile $argv
openFile
}
updateProjectMenu
# Fire up the animation loop
animate
# Event scheduler - should be started / stopped on per-experiment base?
# evsched
} else {
if {$argv != ""} {
if { ![file exists $argv] } {
puts "Error: file '$argv' doesn't exist"
exit
}
global currentFileBatch
set currentFileBatch $argv
set fileId [open $argv r]
set cfg ""
foreach entry [read $fileId] {
lappend cfg $entry
}
close $fileId
set curcfg [newObjectId $cfg_list "cfg"]
lappend cfg_list $curcfg
namespace eval ::cf::[set curcfg] {}
loadCfg $cfg
if { [checkExternalInterfaces] } {
return
}
if { [allSnapshotsAvailable] == 1 } {
deployCfg
createExperimentFilesFromBatch
}
} else {
set configFile "$runtimeDir/$eid_base/config.imn"
if { [file exists $configFile] && $regular_termination } {
set fileId [open $configFile r]
set cfg ""
foreach entry [read $fileId] {
lappend cfg $entry
}
close $fileId
set curcfg [newObjectId $cfg_list "cfg"]
lappend cfg_list $curcfg
namespace eval ::cf::[set curcfg] {}
upvar 0 ::cf::[set ::curcfg]::eid eid
set eid $eid_base
loadCfg $cfg
terminateAllNodes $eid_base
} else {
vimageCleanup $eid_base
}
deleteExperimentFiles $eid_base
}
}