status-go/vendor/github.com/pion/turn/v2/internal/client/permission.go

78 lines
1.5 KiB
Go
Raw Normal View History

2024-06-05 20:10:03 +00:00
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
2022-03-10 09:44:48 +00:00
package client
import (
"net"
"sync"
"sync/atomic"
2024-06-05 20:10:03 +00:00
"github.com/pion/turn/v2/internal/ipnet"
2022-03-10 09:44:48 +00:00
)
type permState int32
const (
permStateIdle permState = iota
permStatePermitted
)
type permission struct {
2024-06-05 20:10:03 +00:00
addr net.Addr
st permState // Thread-safe (atomic op)
mutex sync.RWMutex // Thread-safe
2022-03-10 09:44:48 +00:00
}
func (p *permission) setState(state permState) {
atomic.StoreInt32((*int32)(&p.st), int32(state))
}
func (p *permission) state() permState {
return permState(atomic.LoadInt32((*int32)(&p.st)))
}
// Thread-safe permission map
type permissionMap struct {
permMap map[string]*permission
mutex sync.RWMutex
}
func (m *permissionMap) insert(addr net.Addr, p *permission) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
2024-06-05 20:10:03 +00:00
p.addr = addr
m.permMap[ipnet.FingerprintAddr(addr)] = p
2022-03-10 09:44:48 +00:00
return true
}
func (m *permissionMap) find(addr net.Addr) (*permission, bool) {
m.mutex.RLock()
defer m.mutex.RUnlock()
2024-06-05 20:10:03 +00:00
p, ok := m.permMap[ipnet.FingerprintAddr(addr)]
2022-03-10 09:44:48 +00:00
return p, ok
}
func (m *permissionMap) delete(addr net.Addr) {
m.mutex.Lock()
defer m.mutex.Unlock()
2024-06-05 20:10:03 +00:00
delete(m.permMap, ipnet.FingerprintAddr(addr))
2022-03-10 09:44:48 +00:00
}
func (m *permissionMap) addrs() []net.Addr {
m.mutex.RLock()
defer m.mutex.RUnlock()
addrs := []net.Addr{}
2024-06-05 20:10:03 +00:00
for _, p := range m.permMap {
addrs = append(addrs, p.addr)
2022-03-10 09:44:48 +00:00
}
return addrs
}
func newPermissionMap() *permissionMap {
return &permissionMap{
permMap: map[string]*permission{},
}
}