udp_muxed_conn.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package ice
  4. import (
  5. "io"
  6. "net"
  7. "sync"
  8. "time"
  9. "github.com/pion/logging"
  10. )
  11. type udpMuxedConnState int
  12. const (
  13. udpMuxedConnOpen udpMuxedConnState = iota
  14. udpMuxedConnWaiting
  15. udpMuxedConnClosed
  16. )
  17. type udpMuxedConnAddr struct {
  18. ip [16]byte
  19. port uint16
  20. }
  21. func newUDPMuxedConnAddr(addr *net.UDPAddr) (a udpMuxedConnAddr) {
  22. copy(a.ip[:], addr.IP.To16())
  23. a.port = uint16(addr.Port)
  24. return a
  25. }
  26. type udpMuxedConnParams struct {
  27. Mux *UDPMuxDefault
  28. AddrPool *sync.Pool
  29. Key string
  30. LocalAddr net.Addr
  31. Logger logging.LeveledLogger
  32. }
  33. // udpMuxedConn represents a logical packet conn for a single remote as identified by ufrag
  34. type udpMuxedConn struct {
  35. params *udpMuxedConnParams
  36. // Remote addresses that we have sent to on this conn
  37. addresses []udpMuxedConnAddr
  38. // FIFO queue holding incoming packets
  39. bufHead, bufTail *bufferHolder
  40. notify chan struct{}
  41. closedChan chan struct{}
  42. state udpMuxedConnState
  43. mu sync.Mutex
  44. }
  45. func newUDPMuxedConn(params *udpMuxedConnParams) *udpMuxedConn {
  46. return &udpMuxedConn{
  47. params: params,
  48. notify: make(chan struct{}, 1),
  49. closedChan: make(chan struct{}),
  50. }
  51. }
  52. func (c *udpMuxedConn) ReadFrom(b []byte) (n int, rAddr net.Addr, err error) {
  53. for {
  54. c.mu.Lock()
  55. if c.bufTail != nil {
  56. pkt := c.bufTail
  57. c.bufTail = pkt.next
  58. if pkt == c.bufHead {
  59. c.bufHead = nil
  60. }
  61. c.mu.Unlock()
  62. if len(b) < len(pkt.buf) {
  63. err = io.ErrShortBuffer
  64. } else {
  65. n = copy(b, pkt.buf)
  66. rAddr = pkt.addr
  67. }
  68. pkt.reset()
  69. c.params.AddrPool.Put(pkt)
  70. return
  71. }
  72. if c.state == udpMuxedConnClosed {
  73. c.mu.Unlock()
  74. return 0, nil, io.EOF
  75. }
  76. c.state = udpMuxedConnWaiting
  77. c.mu.Unlock()
  78. select {
  79. case <-c.notify:
  80. case <-c.closedChan:
  81. return 0, nil, io.EOF
  82. }
  83. }
  84. }
  85. func (c *udpMuxedConn) WriteTo(buf []byte, rAddr net.Addr) (n int, err error) {
  86. if c.isClosed() {
  87. return 0, io.ErrClosedPipe
  88. }
  89. // Each time we write to a new address, we'll register it with the mux
  90. addr := newUDPMuxedConnAddr(rAddr.(*net.UDPAddr))
  91. if !c.containsAddress(addr) {
  92. c.addAddress(addr)
  93. }
  94. return c.params.Mux.writeTo(buf, rAddr)
  95. }
  96. func (c *udpMuxedConn) LocalAddr() net.Addr {
  97. return c.params.LocalAddr
  98. }
  99. func (c *udpMuxedConn) SetDeadline(time.Time) error {
  100. return nil
  101. }
  102. func (c *udpMuxedConn) SetReadDeadline(time.Time) error {
  103. return nil
  104. }
  105. func (c *udpMuxedConn) SetWriteDeadline(time.Time) error {
  106. return nil
  107. }
  108. func (c *udpMuxedConn) CloseChannel() <-chan struct{} {
  109. return c.closedChan
  110. }
  111. func (c *udpMuxedConn) Close() error {
  112. c.mu.Lock()
  113. defer c.mu.Unlock()
  114. if c.state != udpMuxedConnClosed {
  115. for pkt := c.bufTail; pkt != nil; {
  116. next := pkt.next
  117. pkt.reset()
  118. c.params.AddrPool.Put(pkt)
  119. pkt = next
  120. }
  121. c.bufHead = nil
  122. c.bufTail = nil
  123. c.state = udpMuxedConnClosed
  124. close(c.closedChan)
  125. }
  126. return nil
  127. }
  128. func (c *udpMuxedConn) isClosed() bool {
  129. c.mu.Lock()
  130. defer c.mu.Unlock()
  131. return c.state == udpMuxedConnClosed
  132. }
  133. func (c *udpMuxedConn) getAddresses() []udpMuxedConnAddr {
  134. c.mu.Lock()
  135. defer c.mu.Unlock()
  136. addresses := make([]udpMuxedConnAddr, len(c.addresses))
  137. copy(addresses, c.addresses)
  138. return addresses
  139. }
  140. func (c *udpMuxedConn) addAddress(addr udpMuxedConnAddr) {
  141. c.mu.Lock()
  142. c.addresses = append(c.addresses, addr)
  143. c.mu.Unlock()
  144. // Map it on mux
  145. c.params.Mux.registerConnForAddress(c, addr)
  146. }
  147. func (c *udpMuxedConn) removeAddress(addr udpMuxedConnAddr) {
  148. c.mu.Lock()
  149. defer c.mu.Unlock()
  150. newAddresses := make([]udpMuxedConnAddr, 0, len(c.addresses))
  151. for _, a := range c.addresses {
  152. if a != addr {
  153. newAddresses = append(newAddresses, a)
  154. }
  155. }
  156. c.addresses = newAddresses
  157. }
  158. func (c *udpMuxedConn) containsAddress(addr udpMuxedConnAddr) bool {
  159. c.mu.Lock()
  160. defer c.mu.Unlock()
  161. for _, a := range c.addresses {
  162. if addr == a {
  163. return true
  164. }
  165. }
  166. return false
  167. }
  168. func (c *udpMuxedConn) writePacket(data []byte, addr *net.UDPAddr) error {
  169. pkt := c.params.AddrPool.Get().(*bufferHolder) //nolint:forcetypeassert
  170. if cap(pkt.buf) < len(data) {
  171. c.params.AddrPool.Put(pkt)
  172. return io.ErrShortBuffer
  173. }
  174. pkt.buf = append(pkt.buf[:0], data...)
  175. pkt.addr = addr
  176. c.mu.Lock()
  177. if c.state == udpMuxedConnClosed {
  178. c.mu.Unlock()
  179. pkt.reset()
  180. c.params.AddrPool.Put(pkt)
  181. return io.ErrClosedPipe
  182. }
  183. if c.bufHead != nil {
  184. c.bufHead.next = pkt
  185. }
  186. c.bufHead = pkt
  187. if c.bufTail == nil {
  188. c.bufTail = pkt
  189. }
  190. state := c.state
  191. c.state = udpMuxedConnOpen
  192. c.mu.Unlock()
  193. if state == udpMuxedConnWaiting {
  194. select {
  195. case c.notify <- struct{}{}:
  196. default:
  197. }
  198. }
  199. return nil
  200. }