golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/udp_other.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.21 && (quicbasicnet || !(darwin || linux))
     6  
     7  package quic
     8  
     9  import (
    10  	"net"
    11  	"net/netip"
    12  )
    13  
    14  // Lowest common denominator network interface: Basic net.UDPConn, no cmsgs.
    15  // We will not be able to send or receive ECN bits,
    16  // and we will not know what our local address is.
    17  //
    18  // The quicbasicnet build tag allows selecting this interface on any platform.
    19  
    20  // See udp.go.
    21  const (
    22  	udpECNSupport              = false
    23  	udpInvalidLocalAddrIsError = false
    24  )
    25  
    26  type netUDPConn struct {
    27  	c *net.UDPConn
    28  }
    29  
    30  func newNetUDPConn(uc *net.UDPConn) (*netUDPConn, error) {
    31  	return &netUDPConn{
    32  		c: uc,
    33  	}, nil
    34  }
    35  
    36  func (c *netUDPConn) Close() error { return c.c.Close() }
    37  
    38  func (c *netUDPConn) LocalAddr() netip.AddrPort {
    39  	a, _ := c.c.LocalAddr().(*net.UDPAddr)
    40  	return a.AddrPort()
    41  }
    42  
    43  func (c *netUDPConn) Read(f func(*datagram)) {
    44  	for {
    45  		dgram := newDatagram()
    46  		n, _, _, peerAddr, err := c.c.ReadMsgUDPAddrPort(dgram.b, nil)
    47  		if err != nil {
    48  			return
    49  		}
    50  		if n == 0 {
    51  			continue
    52  		}
    53  		dgram.peerAddr = unmapAddrPort(peerAddr)
    54  		dgram.b = dgram.b[:n]
    55  		f(dgram)
    56  	}
    57  }
    58  
    59  func (c *netUDPConn) Write(dgram datagram) error {
    60  	_, err := c.c.WriteToUDPAddrPort(dgram.b, dgram.peerAddr)
    61  	return err
    62  }