Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dial event is dispatched to all active channels #129

Open
rashikzaman opened this issue Aug 3, 2021 · 1 comment
Open

Dial event is dispatched to all active channels #129

rashikzaman opened this issue Aug 3, 2021 · 1 comment

Comments

@rashikzaman
Copy link

Problem:
When asterisk fires a dial event, all active channels that have subscribed to this event receive this event, regardless of whether this event is intended for them.

Reason:
When a dial event is dispatched by asterisk, it provides three IDs, Caller ID, Peer ID and Forwarded ID. Among them, Caller ID and Forwarded ID are optional.

At events.go file, for dial events, these three ids are added to the Keys array. Here, Caller ID and Forwarded ID can be empty.

// Keys returns the list of keys associated with this event
func (evt *Dial) Keys() (sx Keys) {
	sx = append(sx, evt.Key(ChannelKey, evt.Caller.ID))
	sx = append(sx, evt.Key(ChannelKey, evt.Peer.ID))
	sx = append(sx, evt.Key(ChannelKey, evt.Forwarded.ID))

	return
}

Now at stdbus/bus.go, Send method sends all events to their appropriate subscribers. This is done by matching subscriber key with event keys. But this Match method returns true even if event key is empty. So for dial event, as two of three keys can be empty, s.key.Match(k) provides true and as a result, regardless of the subscriber, this dial event is dispatched to all subscribers.

// Send sends the message to the bus
func (b *bus) Send(e ari.Event) {
	var matched bool

	b.rwMux.RLock()

	// Disseminate the message to the subscribers
	for _, s := range b.subs {
		matched = false
		for _, k := range e.Keys() {
			if matched {
				break
			}

			if s.key.Match(k) {
				matched = true
				for _, topic := range s.events {
					if topic == e.GetType() || topic == ari.Events.All {
						select {
						case s.C <- e:
						default: // never block
						}
					}
				}
			}
		}
	}

	b.rwMux.RUnlock()
}

Possible Solution:
Prevent adding empty IDs to event keys, as of for Dial event, don't add Caller ID and Forwarded ID if they are empty.

@Ulexus
Copy link
Member

Ulexus commented Jan 7, 2022

Interesting. I haven't run into problems with this before (just hasn't mattered, not that it hasn't ocurred), but I can certainly see the issue.

It seems like the most obvious solution would be empty checks for those IDs before they are added to the event. Feel free to PR if you are up for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants