diff --git a/error_conn.go b/error_conn.go new file mode 100644 index 00000000..e16180d9 --- /dev/null +++ b/error_conn.go @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: 2026 The Pion community +// SPDX-License-Identifier: MIT + +package ice + +import ( + "net" + "time" +) + +// ErrorConn for net.PacketConn when the value is nil. +type ErrorConn struct{} + +func (e *ErrorConn) ReadFrom([]byte) (n int, addr net.Addr, err error) { + var dummyAddr *net.IPAddr + + return 0, dummyAddr, ErrPacketConnNil +} +func (e *ErrorConn) WriteTo([]byte, net.Addr) (n int, err error) { return 0, ErrPacketConnNil } +func (e *ErrorConn) Close() error { return ErrPacketConnNil } +func (e *ErrorConn) LocalAddr() net.Addr { + var dummyAddr *net.IPAddr + + return dummyAddr +} +func (e *ErrorConn) SetDeadline(time.Time) error { return ErrPacketConnNil } +func (e *ErrorConn) SetReadDeadline(time.Time) error { return ErrPacketConnNil } +func (e *ErrorConn) SetWriteDeadline(time.Time) error { return ErrPacketConnNil } diff --git a/errors.go b/errors.go index 1d0e0d4c..054c7a05 100644 --- a/errors.go +++ b/errors.go @@ -172,6 +172,9 @@ var ( // ErrAgentOptionNotUpdatable indicates an option cannot be updated after construction. ErrAgentOptionNotUpdatable = errors.New("option can only be set during agent construction") + // ErrPacketConnNil indicates an net.PacketConn value was nil. + ErrPacketConnNil = errors.New("net.PacketConn is nil") + errAttributeTooShortICECandidate = errors.New("attribute not long enough to be ICE candidate") errClosingConnection = errors.New("failed to close connection") errConnectionAddrAlreadyExist = errors.New("connection with same remote address already exists") diff --git a/udp_mux.go b/udp_mux.go index 2077981d..0c97700f 100644 --- a/udp_mux.go +++ b/udp_mux.go @@ -67,7 +67,11 @@ func NewUDPMuxDefault(params UDPMuxParams) *UDPMuxDefault { //nolint:cyclop } var localAddrsForUnspecified []net.Addr - if udpAddr, ok := params.UDPConn.LocalAddr().(*net.UDPAddr); !ok { //nolint:nestif + //nolint:nestif + if params.UDPConn == nil { + params.Logger.Errorf("UDPConn is nil") + params.UDPConn = &ErrorConn{} + } else if udpAddr, ok := params.UDPConn.LocalAddr().(*net.UDPAddr); !ok { //nolint:nestif params.Logger.Errorf("LocalAddr is not a net.UDPAddr, got %T", params.UDPConn.LocalAddr()) } else if ok && udpAddr.IP.IsUnspecified() { // For unspecified addresses, the correct behavior is to return errListenUnspecified, but