1 # application-sdfor.tcl --
2 #
3 # FIXME: This file needs a description here.
4 #
5 # Copyright (c) 1998-2002 The Regents of the University of California.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # A. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # B. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # C. Neither the names of the copyright holders nor the names of its
17 # contributors may be used to endorse or promote products derived from this
18 # software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
32 import Application ScopeZone SAPForwarder \
33 ProgramCache ProgramSource ProgramSource/SAP \
34 ERSServer/Sdfor TCP/Server SdforServer ErrorWindow
35
36 #
37 Class SDForwarderApplication -superclass Application
38
39 #
40 SDForwarderApplication public init argv {
41 $self next sdfor
42
43 set o [$self options]
44 $self init_args $o
45 $self init_resources $o
46
47 $self parse_args $argv
48 $self init_scopes
49 $self init_forwarders
50 $self init_server
51 }
52
53 #
54 SDForwarderApplication instproc init_args o {
55 $o register_boolean_option -tcpServer useTCP
56 $o register_boolean_option -noTcpServer useTCP 0
57 $o register_boolean_option -noFwdToLocal forwardGlobalToLocal 0
58
59 $o register_list_option -zone sapZones
60 $o register_list_option -forward forwards
61
62 $o register_option -p serverPort
63 }
64
65 #
66 SDForwarderApplication instproc init_resources o {
67 $o load_preferences "sdfor"
68
69 $o add_default globalSAP 224.2.128.0/17
70 $o add_default localSAP 239.255.0.0/16
71 $o add_default localSAPBW 2000
72
73 $o add_default useGlobalSAP 1
74 $o add_default useLocalSAP 1
75 $o add_default forwardGlobalToLocal 1
76
77 #FIXME
78 global env
79 if {![info exists env(HOME)]} {
80 new ErrorWindow {Your HOME environment variable must be set.}
81 exit 1
82 }
83 $o add_default cachedir [file join $env(HOME) .mash nsdr-cache]
84
85 #FIXME ttl for reannouncements
86 $o add_default sapTTL 15
87
88 $o add_default useTCP 1
89
90 # defaults
91 $o add_default serverPort 12000
92 }
93
94 #
95 SDForwarderApplication private parse_args {argv} {
96 set o [$self options]
97 set args [$o parse_args $argv]
98 if {[llength $args] > 0} {
99 $self fatal "Extra args: $args"
100 }
101 }
102
103 #
104 SDForwarderApplication private get_scope {range} {
105 $self instvar scopes_
106 foreach s $scopes_ {
107 if {$range == [$s range]} {
108 return $s
109 }
110 }
111 return ""
112 }
113
114 #
115 SDForwarderApplication private init_scopes {} {
116 $self instvar scopes_
117
118 #
119 # first, instantiate necessary scope objects
120 #
121 set scopes_ {}
122 foreach range [$self get_option sapZones] {
123 #FIXME
124 set bw [$self get_option localSAPBW]
125 lappend scopes_ [new ScopeZone $range $bw]
126 }
127 if [$self get_option forwardGlobalToLocal] {
128 $self add_option useGlobalSAP 1
129 $self add_option useLocalSAP 1
130 }
131 if [$self get_option useGlobalSAP] {
132 set range [$self get_option globalSAP]
133 if {[$self get_scope $range] == ""} {
134 lappend scopes_ [new ScopeZone $range]
135 }
136 }
137 if [$self get_option useLocalSAP] {
138 set range [$self get_option localSAP]
139 if {[$self get_scope $range] == ""} {
140 set bw [$self get_option localSAPBW]
141 lappend scopes_ [new ScopeZone $range $bw]
142 }
143 }
144 }
145
146 #
147 SDForwarderApplication private init_forwarders {} {
148 $self instvar forwarders_
149 #
150 # instantiate forwarder objects
151 #
152 set forwarders_ {}
153 foreach fwd [$self get_option forwards] {
154 set l [split $fwd ,]
155 set range1 [lindex $l 0]
156 set range2 [lindex $l 1]
157 set zone1 [$self get_scope $range1]
158 set zone2 [$self get_scope $range2]
159
160 if {$zone1 == "" || $zone2 == ""} {
161 $self fatal "Can't forward unknown zone ($range1 or $range2)"
162 }
163
164 lappend forwarders_ [new SAPForwarder $zone1 $zone2]
165 }
166 }
167
168 #
169 SDForwarderApplication private init_server {} {
170 $self instvar scopes_ cache_ source_ server_ ers_
171
172 if ![$self get_option useTCP] { return }
173
174 set server_port [$self get_option serverPort]
175
176 set cache_ [new ProgramCache]
177 set source_ [eval new ProgramSource/SAP $cache_ $scopes_]
178 set server_ [new TCP/Server]
179 $server_ open $server_port "$self create_server"
180
181 set ers_ {}
182 foreach scope $scopes_ {
183 set spec [split [$scope sapAddr] /]
184 set addr [lindex $spec 0]
185 set port [lindex $spec 1]
186 set ersspec "$addr/[expr $port + 1]"
187 lappend ers_ [new ERSServer/Sdfor $server_port $ersspec]
188 }
189 }
190
191
192 SDForwarderApplication private create_server { chan } {
193 set o [new SdforServer $self]
194 $o open $chan
195 return $o
196 }
197
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.