1 # alloc.tcl --
2 #
3 # Defines AddressAllocator, which basically tries to classify a
4 # multicast address within a certain range.
5 #
6 # Copyright (c) 1998-2002 The Regents of the University of California.
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions are met:
11 #
12 # A. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
14 # B. Redistributions in binary form must reproduce the above copyright notice,
15 # this list of conditions and the following disclaimer in the documentation
16 # and/or other materials provided with the distribution.
17 # C. Neither the names of the copyright holders nor the names of its
18 # contributors may be used to endorse or promote products derived from this
19 # software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
25 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32
33 # Abstract base class for classes that do multicast address allocation.
34 # Different subclasses can be defined (e.g., AddressAllocator/MDHCP,
35 # /AAP, etc..) and then other objects that need to allocate an address
36 # can simply use [AddressAllocator instance] and not worry about the
37 # particular algorithm being used.
38 Class AddressAllocator
39
40 #
41 AddressAllocator public init {} {
42 AddressAllocator set instance_ $self
43 }
44
45 #
46 AddressAllocator proc instance {} {
47 return [AddressAllocator set instance_]
48 }
49
50 # The entry point for request an address. Right now, allocates an
51 # address from the ScopeZone <i>zone</i>. If <i>n</i> is specified,
52 # then that many consecutive addresses should be allocated.
53 # This API needs to be expanded to include address lifetime, etc..
54 AddressAllocator public alloc {zone {n 1}} {
55 $self fatal "AddressAllocator::alloc called -- should be overridden in child"
56 }
57
58 # Implements the AddressAllocator api to allocate a unique addresse
59 # that does not clash with another addresse allocated in a SAP
60 # announcement.
61 Class AddressAllocator/SAP -superclass AddressAllocator
62
63 # Instantiate a new SAP address allocator. The parameter <i>source</i>
64 # is a ProgramSource/SAP object that will be used to extract a list of
65 # SAP announcements to check for address conflicts.
66 AddressAllocator/SAP public init {source} {
67 $self next
68 $self set source_ $source
69 }
70
71 #
72 AddressAllocator/SAP public alloc {zone {n 1}} {
73 $self instvar source_
74 set conflict 1
75 while {$conflict != 0} {
76 set conflict 0
77 set base [$self random-addr $zone]
78 set addr $base
79 for {set i 0} {$i<$n} {incr i} {
80 incr conflict [$self conflict $addr $zone $source_]
81 set addr [$self inet_ntoa [expr [$self inet_addr $addr] + 1]]
82 }
83 }
84 return $addr
85 }
86
87 # Generates a random address from the range of addresses in the
88 # ScopeZone <i>zone</i>.
89 AddressAllocator/SAP private random-addr {zone} {
90 set range [$zone range]
91 set l [split $range /]
92 set bits [lindex $l 1]
93 set mask [expr ~((-1)<<(32 - $bits))]
94 set base [expr [$self inet_addr [lindex $l 0]] &~ $mask]
95 set addr [expr $base + ([random] & $mask)]
96 return [$self inet_ntoa $addr]
97 }
98
99 # Determines whether the address <i>addr</i> clashes with any
100 # addresses already allocated in programs visible to the
101 # ProgramSource/SAP object <i>source</i>.
102 AddressAllocator/SAP private conflict {addr zone source} {
103 set conflict 0
104
105 #FIXME should have a way to quickly examine only programs
106 # in this scope zone
107 $source instvar progs_
108 foreach p [array names progs_] {
109 foreach msg [$progs_($p) set msgs_] {
110 if {[$msg have_field c] && $addr == [$msg set caddr_]} {
111 set conflict 1
112 break
113 }
114 foreach media [$msg set allmedia_] {
115 if {[$media have_field c] && $addr == [$media set caddr_]} {
116 set conflict 1
117 break
118 }
119 }
120 }
121 }
122 return $conflict
123 }
124
125 # Same as the C library <tt>inet_addr</tt> routine. Converts
126 # the IP address <i>a</i> written in dotted decimal notation
127 # (e.g., 224.2.127.254) to a 32 bit integer.
128 AddressAllocator/SAP private inet_addr {a} {
129 set l [split $a .]
130 set addr [expr [lindex $l 0] <<24]
131 incr addr [expr [lindex $l 1] <<16]
132 incr addr [expr [lindex $l 2] <<8]
133 incr addr [lindex $l 3]
134 return $addr
135 }
136
137 # Same as the C library <tt>inet_ntoa</tt> routine. Converts the
138 # 32 bit integer <i>n</i> to dotted decimal notation.
139 AddressAllocator/SAP private inet_ntoa {n} {
140 set a [expr ($n>>24) & 0xff]
141 set b [expr ($n>>16) & 0xff]
142 set c [expr ($n>>8) & 0xff]
143 set d [expr $n & 0xff]
144 return "$a.$b.$c.$d"
145 }
146
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.