| 1 |
package vnet |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"net" |
| 6 |
"sync" |
| 7 |
) |
| 8 |
|
| 9 |
var ( |
| 10 |
errAddressAlreadyInUse = errors.New("address already in use") |
| 11 |
errNoSuchUDPConn = errors.New("no such UDPConn") |
| 12 |
errCannotRemoveUnspecifiedIP = errors.New("cannot remove unspecified IP by the specified IP") |
| 13 |
) |
| 14 |
|
| 15 |
type udpConnMap struct { |
| 16 |
portMap map[int][]*UDPConn |
| 17 |
mutex sync.RWMutex |
| 18 |
} |
| 19 |
|
| 20 |
func newUDPConnMap() *udpConnMap { |
| 21 |
return &udpConnMap{ |
| 22 |
portMap: map[int][]*UDPConn{}, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func (m *udpConnMap) insert(conn *UDPConn) error { |
| 27 |
m.mutex.Lock() |
| 28 |
defer m.mutex.Unlock() |
| 29 |
|
| 30 |
udpAddr := conn.LocalAddr().(*net.UDPAddr) |
| 31 |
|
| 32 |
|
| 33 |
conns, ok := m.portMap[udpAddr.Port] |
| 34 |
if ok { |
| 35 |
if udpAddr.IP.IsUnspecified() { |
| 36 |
return errAddressAlreadyInUse |
| 37 |
} |
| 38 |
|
| 39 |
for _, conn := range conns { |
| 40 |
laddr := conn.LocalAddr().(*net.UDPAddr) |
| 41 |
if laddr.IP.IsUnspecified() || laddr.IP.Equal(udpAddr.IP) { |
| 42 |
return errAddressAlreadyInUse |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
conns = append(conns, conn) |
| 47 |
} else { |
| 48 |
conns = []*UDPConn{conn} |
| 49 |
} |
| 50 |
|
| 51 |
m.portMap[udpAddr.Port] = conns |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
func (m *udpConnMap) find(addr net.Addr) (*UDPConn, bool) { |
| 56 |
m.mutex.Lock() |
| 57 |
defer m.mutex.Unlock() |
| 58 |
|
| 59 |
udpAddr := addr.(*net.UDPAddr) |
| 60 |
|
| 61 |
if conns, ok := m.portMap[udpAddr.Port]; ok { |
| 62 |
if udpAddr.IP.IsUnspecified() { |
| 63 |
|
| 64 |
if len(conns) == 0 { |
| 65 |
|
| 66 |
delete(m.portMap, udpAddr.Port) |
| 67 |
return nil, false |
| 68 |
} |
| 69 |
return conns[0], true |
| 70 |
} |
| 71 |
|
| 72 |
for _, conn := range conns { |
| 73 |
laddr := conn.LocalAddr().(*net.UDPAddr) |
| 74 |
if laddr.IP.IsUnspecified() || laddr.IP.Equal(udpAddr.IP) { |
| 75 |
return conn, ok |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return nil, false |
| 81 |
} |
| 82 |
|
| 83 |
func (m *udpConnMap) delete(addr net.Addr) error { |
| 84 |
m.mutex.Lock() |
| 85 |
defer m.mutex.Unlock() |
| 86 |
|
| 87 |
udpAddr := addr.(*net.UDPAddr) |
| 88 |
|
| 89 |
conns, ok := m.portMap[udpAddr.Port] |
| 90 |
if !ok { |
| 91 |
return errNoSuchUDPConn |
| 92 |
} |
| 93 |
|
| 94 |
if udpAddr.IP.IsUnspecified() { |
| 95 |
|
| 96 |
delete(m.portMap, udpAddr.Port) |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
newConns := []*UDPConn{} |
| 101 |
|
| 102 |
for _, conn := range conns { |
| 103 |
laddr := conn.LocalAddr().(*net.UDPAddr) |
| 104 |
if laddr.IP.IsUnspecified() { |
| 105 |
|
| 106 |
return errCannotRemoveUnspecifiedIP |
| 107 |
} |
| 108 |
|
| 109 |
if laddr.IP.Equal(udpAddr.IP) { |
| 110 |
continue |
| 111 |
} |
| 112 |
|
| 113 |
newConns = append(newConns, conn) |
| 114 |
} |
| 115 |
|
| 116 |
if len(newConns) == 0 { |
| 117 |
delete(m.portMap, udpAddr.Port) |
| 118 |
} else { |
| 119 |
m.portMap[udpAddr.Port] = newConns |
| 120 |
} |
| 121 |
|
| 122 |
return nil |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (m *udpConnMap) size() int { |
| 127 |
m.mutex.RLock() |
| 128 |
defer m.mutex.RUnlock() |
| 129 |
|
| 130 |
n := 0 |
| 131 |
for _, conns := range m.portMap { |
| 132 |
n += len(conns) |
| 133 |
} |
| 134 |
|
| 135 |
return n |
| 136 |
} |