status-go/vendor/github.com/pion/ice/v2/tcp_packet_conn.go

325 lines
6.2 KiB
Go
Raw Normal View History

2024-05-15 23:15:00 +00:00
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
2022-03-10 09:44:48 +00:00
package ice
import (
2024-05-15 23:15:00 +00:00
"errors"
2022-03-10 09:44:48 +00:00
"fmt"
"io"
"net"
"sync"
2024-05-15 23:15:00 +00:00
"sync/atomic"
2022-03-10 09:44:48 +00:00
"time"
"github.com/pion/logging"
2024-05-15 23:15:00 +00:00
"github.com/pion/transport/v2/packetio"
2022-03-10 09:44:48 +00:00
)
2024-05-15 23:15:00 +00:00
type bufferedConn struct {
net.Conn
buf *packetio.Buffer
logger logging.LeveledLogger
closed int32
}
func newBufferedConn(conn net.Conn, bufSize int, logger logging.LeveledLogger) net.Conn {
buf := packetio.NewBuffer()
if bufSize > 0 {
buf.SetLimitSize(bufSize)
}
bc := &bufferedConn{
Conn: conn,
buf: buf,
logger: logger,
}
go bc.writeProcess()
return bc
}
func (bc *bufferedConn) Write(b []byte) (int, error) {
n, err := bc.buf.Write(b)
if err != nil {
return n, err
}
return n, nil
}
func (bc *bufferedConn) writeProcess() {
pktBuf := make([]byte, receiveMTU)
for atomic.LoadInt32(&bc.closed) == 0 {
n, err := bc.buf.Read(pktBuf)
if errors.Is(err, io.EOF) {
return
}
if err != nil {
2024-06-05 20:10:03 +00:00
bc.logger.Warnf("Failed to read from buffer: %s", err)
2024-05-15 23:15:00 +00:00
continue
}
if _, err := bc.Conn.Write(pktBuf[:n]); err != nil {
2024-06-05 20:10:03 +00:00
bc.logger.Warnf("Failed to write: %s", err)
2024-05-15 23:15:00 +00:00
continue
}
}
}
func (bc *bufferedConn) Close() error {
atomic.StoreInt32(&bc.closed, 1)
_ = bc.buf.Close()
return bc.Conn.Close()
}
2022-03-10 09:44:48 +00:00
type tcpPacketConn struct {
params *tcpPacketParams
// conns is a map of net.Conns indexed by remote net.Addr.String()
conns map[string]net.Conn
recvChan chan streamingPacket
mu sync.Mutex
wg sync.WaitGroup
closedChan chan struct{}
closeOnce sync.Once
2024-06-05 20:10:03 +00:00
aliveTimer *time.Timer
2022-03-10 09:44:48 +00:00
}
type streamingPacket struct {
Data []byte
RAddr net.Addr
Err error
}
type tcpPacketParams struct {
2024-06-05 20:10:03 +00:00
ReadBuffer int
LocalAddr net.Addr
Logger logging.LeveledLogger
WriteBuffer int
AliveDuration time.Duration
2022-03-10 09:44:48 +00:00
}
func newTCPPacketConn(params tcpPacketParams) *tcpPacketConn {
p := &tcpPacketConn{
params: &params,
conns: map[string]net.Conn{},
recvChan: make(chan streamingPacket, params.ReadBuffer),
closedChan: make(chan struct{}),
}
2024-06-05 20:10:03 +00:00
if params.AliveDuration > 0 {
p.aliveTimer = time.AfterFunc(params.AliveDuration, func() {
p.params.Logger.Warn("close tcp packet conn by alive timeout")
_ = p.Close()
})
}
2022-03-10 09:44:48 +00:00
return p
}
2024-06-05 20:10:03 +00:00
func (t *tcpPacketConn) ClearAliveTimer() {
t.mu.Lock()
if t.aliveTimer != nil {
t.aliveTimer.Stop()
}
t.mu.Unlock()
}
2022-03-10 09:44:48 +00:00
func (t *tcpPacketConn) AddConn(conn net.Conn, firstPacketData []byte) error {
2024-06-05 20:10:03 +00:00
t.params.Logger.Infof("Added connection: %s remote %s to local %s", conn.RemoteAddr().Network(), conn.RemoteAddr(), conn.LocalAddr())
2022-03-10 09:44:48 +00:00
t.mu.Lock()
defer t.mu.Unlock()
select {
case <-t.closedChan:
return io.ErrClosedPipe
default:
}
if _, ok := t.conns[conn.RemoteAddr().String()]; ok {
return fmt.Errorf("%w: %s", errConnectionAddrAlreadyExist, conn.RemoteAddr().String())
}
2024-05-15 23:15:00 +00:00
if t.params.WriteBuffer > 0 {
conn = newBufferedConn(conn, t.params.WriteBuffer, t.params.Logger)
}
2022-03-10 09:44:48 +00:00
t.conns[conn.RemoteAddr().String()] = conn
t.wg.Add(1)
go func() {
2024-05-15 23:15:00 +00:00
defer t.wg.Done()
2022-03-10 09:44:48 +00:00
if firstPacketData != nil {
2024-05-15 23:15:00 +00:00
select {
case <-t.closedChan:
// NOTE: recvChan can fill up and never drain in edge
// cases while closing a connection, which can cause the
// packetConn to never finish closing. Bail out early
// here to prevent that.
return
case t.recvChan <- streamingPacket{firstPacketData, conn.RemoteAddr(), nil}:
}
2022-03-10 09:44:48 +00:00
}
t.startReading(conn)
}()
return nil
}
func (t *tcpPacketConn) startReading(conn net.Conn) {
buf := make([]byte, receiveMTU)
for {
n, err := readStreamingPacket(conn, buf)
if err != nil {
2024-06-05 20:10:03 +00:00
t.params.Logger.Warnf("Failed to read streaming packet: %s", err)
2022-03-10 09:44:48 +00:00
t.handleRecv(streamingPacket{nil, conn.RemoteAddr(), err})
t.removeConn(conn)
return
}
data := make([]byte, n)
copy(data, buf[:n])
t.handleRecv(streamingPacket{data, conn.RemoteAddr(), nil})
}
}
func (t *tcpPacketConn) handleRecv(pkt streamingPacket) {
t.mu.Lock()
recvChan := t.recvChan
if t.isClosed() {
recvChan = nil
}
t.mu.Unlock()
select {
case recvChan <- pkt:
case <-t.closedChan:
}
}
func (t *tcpPacketConn) isClosed() bool {
select {
case <-t.closedChan:
return true
default:
return false
}
}
// WriteTo is for passive and s-o candidates.
2024-05-15 23:15:00 +00:00
func (t *tcpPacketConn) ReadFrom(b []byte) (n int, rAddr net.Addr, err error) {
2022-03-10 09:44:48 +00:00
pkt, ok := <-t.recvChan
if !ok {
return 0, nil, io.ErrClosedPipe
}
if pkt.Err != nil {
return 0, pkt.RAddr, pkt.Err
}
if cap(b) < len(pkt.Data) {
return 0, pkt.RAddr, io.ErrShortBuffer
}
n = len(pkt.Data)
copy(b, pkt.Data[:n])
return n, pkt.RAddr, err
}
// WriteTo is for active and s-o candidates.
2024-05-15 23:15:00 +00:00
func (t *tcpPacketConn) WriteTo(buf []byte, rAddr net.Addr) (n int, err error) {
2022-03-10 09:44:48 +00:00
t.mu.Lock()
2024-05-15 23:15:00 +00:00
conn, ok := t.conns[rAddr.String()]
t.mu.Unlock()
2022-03-10 09:44:48 +00:00
if !ok {
return 0, io.ErrClosedPipe
}
n, err = writeStreamingPacket(conn, buf)
if err != nil {
2024-06-05 20:10:03 +00:00
t.params.Logger.Tracef("%w %s", errWrite, rAddr)
2022-03-10 09:44:48 +00:00
return n, err
}
return n, err
}
func (t *tcpPacketConn) closeAndLogError(closer io.Closer) {
err := closer.Close()
if err != nil {
2024-05-15 23:15:00 +00:00
t.params.Logger.Warnf("%v: %s", errClosingConnection, err)
2022-03-10 09:44:48 +00:00
}
}
func (t *tcpPacketConn) removeConn(conn net.Conn) {
t.mu.Lock()
defer t.mu.Unlock()
t.closeAndLogError(conn)
delete(t.conns, conn.RemoteAddr().String())
}
func (t *tcpPacketConn) Close() error {
t.mu.Lock()
var shouldCloseRecvChan bool
t.closeOnce.Do(func() {
close(t.closedChan)
shouldCloseRecvChan = true
2024-06-05 20:10:03 +00:00
if t.aliveTimer != nil {
t.aliveTimer.Stop()
}
2022-03-10 09:44:48 +00:00
})
for _, conn := range t.conns {
t.closeAndLogError(conn)
delete(t.conns, conn.RemoteAddr().String())
}
t.mu.Unlock()
t.wg.Wait()
if shouldCloseRecvChan {
close(t.recvChan)
}
return nil
}
func (t *tcpPacketConn) LocalAddr() net.Addr {
return t.params.LocalAddr
}
2024-05-15 23:15:00 +00:00
func (t *tcpPacketConn) SetDeadline(time.Time) error {
2022-03-10 09:44:48 +00:00
return nil
}
2024-05-15 23:15:00 +00:00
func (t *tcpPacketConn) SetReadDeadline(time.Time) error {
2022-03-10 09:44:48 +00:00
return nil
}
2024-05-15 23:15:00 +00:00
func (t *tcpPacketConn) SetWriteDeadline(time.Time) error {
2022-03-10 09:44:48 +00:00
return nil
}
func (t *tcpPacketConn) CloseChannel() <-chan struct{} {
return t.closedChan
}
func (t *tcpPacketConn) String() string {
return fmt.Sprintf("tcpPacketConn{LocalAddr: %s}", t.params.LocalAddr)
}