@@ -35,6 +35,8 @@ type Allocation struct {
35
35
channelBindings []* ChannelBind
36
36
lifetimeTimer * time.Timer
37
37
closed chan interface {}
38
+ username , realm string
39
+ callback EventHandler
38
40
log logging.LeveledLogger
39
41
40
42
// Some clients (Firefox or others using resiprocate's nICE lib) may retry allocation
@@ -45,12 +47,18 @@ type Allocation struct {
45
47
}
46
48
47
49
// NewAllocation creates a new instance of NewAllocation.
48
- func NewAllocation (turnSocket net.PacketConn , fiveTuple * FiveTuple , log logging.LeveledLogger ) * Allocation {
50
+ func NewAllocation (
51
+ turnSocket net.PacketConn ,
52
+ fiveTuple * FiveTuple ,
53
+ callback EventHandler ,
54
+ log logging.LeveledLogger ,
55
+ ) * Allocation {
49
56
return & Allocation {
50
57
TurnSocket : turnSocket ,
51
58
fiveTuple : fiveTuple ,
52
59
permissions : make (map [string ]* Permission , 64 ),
53
60
closed : make (chan interface {}),
61
+ callback : callback ,
54
62
log : log ,
55
63
}
56
64
}
@@ -82,6 +90,21 @@ func (a *Allocation) AddPermission(perms *Permission) {
82
90
a .permissions [fingerprint ] = perms
83
91
a .permissionsLock .Unlock ()
84
92
93
+ if a .callback != nil {
94
+ if u , ok := perms .Addr .(* net.UDPAddr ); ok {
95
+ a .callback (EventHandlerArgs {
96
+ Type : OnPermissionCreated ,
97
+ SrcAddr : a .fiveTuple .SrcAddr ,
98
+ DstAddr : a .fiveTuple .DstAddr ,
99
+ Protocol : a .fiveTuple .Protocol ,
100
+ Username : a .username ,
101
+ Realm : a .realm ,
102
+ RelayAddr : a .RelayAddr ,
103
+ PeerIP : u .IP ,
104
+ })
105
+ }
106
+ }
107
+
85
108
perms .start (permissionTimeout )
86
109
}
87
110
@@ -90,6 +113,33 @@ func (a *Allocation) RemovePermission(addr net.Addr) {
90
113
a .permissionsLock .Lock ()
91
114
defer a .permissionsLock .Unlock ()
92
115
delete (a .permissions , ipnet .FingerprintAddr (addr ))
116
+
117
+ if a .callback != nil {
118
+ if u , ok := addr .(* net.UDPAddr ); ok {
119
+ a .callback (EventHandlerArgs {
120
+ Type : OnPermissionDeleted ,
121
+ SrcAddr : a .fiveTuple .SrcAddr ,
122
+ DstAddr : a .fiveTuple .DstAddr ,
123
+ Protocol : a .fiveTuple .Protocol ,
124
+ Username : a .username ,
125
+ Realm : a .realm ,
126
+ RelayAddr : a .RelayAddr ,
127
+ PeerIP : u .IP ,
128
+ })
129
+ }
130
+ }
131
+ }
132
+
133
+ // ListPermissions returns the permissions associated with an allocation.
134
+ func (a * Allocation ) ListPermissions () []* Permission {
135
+ ps := []* Permission {}
136
+ a .permissionsLock .RLock ()
137
+ defer a .permissionsLock .RUnlock ()
138
+ for _ , p := range a .permissions {
139
+ ps = append (ps , p )
140
+ }
141
+
142
+ return ps
93
143
}
94
144
95
145
// AddChannelBind adds a new ChannelBind to the allocation, it also updates the
@@ -114,6 +164,20 @@ func (a *Allocation) AddChannelBind(chanBind *ChannelBind, lifetime time.Duratio
114
164
115
165
// Channel binds also refresh permissions.
116
166
a .AddPermission (NewPermission (chanBind .Peer , a .log ))
167
+
168
+ if a .callback != nil {
169
+ a .callback (EventHandlerArgs {
170
+ Type : OnChannelCreated ,
171
+ SrcAddr : a .fiveTuple .SrcAddr ,
172
+ DstAddr : a .fiveTuple .DstAddr ,
173
+ Protocol : a .fiveTuple .Protocol ,
174
+ Username : a .username ,
175
+ Realm : a .realm ,
176
+ RelayAddr : a .RelayAddr ,
177
+ PeerAddr : chanBind .Peer ,
178
+ ChannelNumber : uint16 (chanBind .Number ),
179
+ })
180
+ }
117
181
} else {
118
182
channelByNumber .refresh (lifetime )
119
183
@@ -131,6 +195,20 @@ func (a *Allocation) RemoveChannelBind(number proto.ChannelNumber) bool {
131
195
132
196
for i := len (a .channelBindings ) - 1 ; i >= 0 ; i -- {
133
197
if a .channelBindings [i ].Number == number {
198
+ if a .callback != nil {
199
+ a .callback (EventHandlerArgs {
200
+ Type : OnChannelDeleted ,
201
+ SrcAddr : a .fiveTuple .SrcAddr ,
202
+ DstAddr : a .fiveTuple .DstAddr ,
203
+ Protocol : a .fiveTuple .Protocol ,
204
+ Username : a .username ,
205
+ Realm : a .realm ,
206
+ RelayAddr : a .RelayAddr ,
207
+ PeerAddr : a .channelBindings [i ].Peer ,
208
+ ChannelNumber : uint16 (a .channelBindings [i ].Number ),
209
+ })
210
+ }
211
+
134
212
a .channelBindings = append (a .channelBindings [:i ], a .channelBindings [i + 1 :]... )
135
213
136
214
return true
@@ -166,6 +244,16 @@ func (a *Allocation) GetChannelByAddr(addr net.Addr) *ChannelBind {
166
244
return nil
167
245
}
168
246
247
+ // ListChannelBindings returns the channel bindings associated with an allocation.
248
+ func (a * Allocation ) ListChannelBindings () []* ChannelBind {
249
+ cs := []* ChannelBind {}
250
+ a .channelBindingsLock .RLock ()
251
+ defer a .channelBindingsLock .RUnlock ()
252
+ cs = append (cs , a .channelBindings ... )
253
+
254
+ return cs
255
+ }
256
+
169
257
// Refresh updates the allocations lifetime.
170
258
func (a * Allocation ) Refresh (lifetime time.Duration ) {
171
259
if ! a .lifetimeTimer .Reset (lifetime ) {
@@ -201,17 +289,15 @@ func (a *Allocation) Close() error {
201
289
202
290
a .lifetimeTimer .Stop ()
203
291
204
- a . permissionsLock . RLock ()
205
- for _ , p := range a . permissions {
292
+ for _ , p := range a . ListPermissions () {
293
+ a . RemovePermission ( p . Addr )
206
294
p .lifetimeTimer .Stop ()
207
295
}
208
- a .permissionsLock .RUnlock ()
209
296
210
- a . channelBindingsLock . RLock ()
211
- for _ , c := range a . channelBindings {
297
+ for _ , c := range a . ListChannelBindings () {
298
+ a . RemoveChannelBind ( c . Number )
212
299
c .lifetimeTimer .Stop ()
213
300
}
214
- a .channelBindingsLock .RUnlock ()
215
301
216
302
return a .RelaySocket .Close ()
217
303
}
0 commit comments