ping: Update style for socket_linux_unprivileged.go

This commit is contained in:
世界
2025-08-24 18:43:22 +08:00
parent ccfe5c0f0f
commit 9532c7f1f6

View File

@@ -24,8 +24,8 @@ type UnprivilegedConn struct {
destination netip.Addr destination netip.Addr
receiveChan chan *unprivilegedResponse receiveChan chan *unprivilegedResponse
readDeadline pipe.Deadline readDeadline pipe.Deadline
natMap map[uint16]net.Conn mappingAccess sync.Mutex
natMapMutex sync.Mutex mapping map[uint16]net.Conn
} }
type unprivilegedResponse struct { type unprivilegedResponse struct {
@@ -48,7 +48,7 @@ func newUnprivilegedConn(ctx context.Context, controlFunc control.Func, destinat
destination: destination, destination: destination,
receiveChan: make(chan *unprivilegedResponse), receiveChan: make(chan *unprivilegedResponse),
readDeadline: pipe.MakeDeadline(), readDeadline: pipe.MakeDeadline(),
natMap: make(map[uint16]net.Conn), mapping: make(map[uint16]net.Conn),
}, nil }, nil
} }
@@ -59,10 +59,10 @@ func (c *UnprivilegedConn) Read(b []byte) (n int, err error) {
packet.Buffer.Release() packet.Buffer.Release()
packet.Cmsg.Release() packet.Cmsg.Release()
return return
case <-c.ctx.Done():
return 0, os.ErrClosed
case <-c.readDeadline.Wait(): case <-c.readDeadline.Wait():
return 0, os.ErrDeadlineExceeded return 0, os.ErrDeadlineExceeded
case <-c.ctx.Done():
return 0, os.ErrClosed
} }
} }
@@ -75,10 +75,10 @@ func (c *UnprivilegedConn) ReadMsg(b []byte, oob []byte) (n, oobn int, addr neti
packet.Buffer.Release() packet.Buffer.Release()
packet.Cmsg.Release() packet.Cmsg.Release()
return return
case <-c.ctx.Done():
return 0, 0, netip.Addr{}, os.ErrClosed
case <-c.readDeadline.Wait(): case <-c.readDeadline.Wait():
return 0, 0, netip.Addr{}, os.ErrDeadlineExceeded return 0, 0, netip.Addr{}, os.ErrDeadlineExceeded
case <-c.ctx.Done():
return 0, 0, netip.Addr{}, os.ErrClosed
} }
} }
@@ -92,26 +92,23 @@ func (c *UnprivilegedConn) Write(b []byte) (n int, err error) {
identifier = icmpHdr.Ident() identifier = icmpHdr.Ident()
} }
c.natMapMutex.Lock() c.mappingAccess.Lock()
if err = c.ctx.Err(); err != nil { if c.ctx.Err() != nil {
c.natMapMutex.Unlock() return 0, c.ctx.Err()
return 0, err
} }
conn, ok := c.natMap[identifier] conn, loaded := c.mapping[identifier]
if !ok { if !loaded {
conn, err = connect(false, c.controlFunc, c.destination) conn, err = connect(false, c.controlFunc, c.destination)
if err != nil { if err != nil {
c.natMapMutex.Unlock() c.mappingAccess.Unlock()
return 0, err return
} }
go c.fetchResponse(conn.(*net.UDPConn), identifier) go c.fetchResponse(conn.(*net.UDPConn), identifier)
} }
c.natMapMutex.Unlock() c.mappingAccess.Unlock()
n, err = conn.Write(b) n, err = conn.Write(b)
if err != nil { if err != nil {
c.removeConn(conn.(*net.UDPConn), identifier) c.removeConn(conn.(*net.UDPConn), identifier)
return
} }
return return
} }
@@ -154,22 +151,20 @@ func (c *UnprivilegedConn) fetchResponse(conn *net.UDPConn, identifier uint16) {
} }
func (c *UnprivilegedConn) removeConn(conn *net.UDPConn, identifier uint16) { func (c *UnprivilegedConn) removeConn(conn *net.UDPConn, identifier uint16) {
c.natMapMutex.Lock() c.mappingAccess.Lock()
defer c.mappingAccess.Unlock()
_ = conn.Close() _ = conn.Close()
if c.natMap[identifier] == conn { delete(c.mapping, identifier)
delete(c.natMap, identifier)
}
c.natMapMutex.Unlock()
} }
func (c *UnprivilegedConn) Close() error { func (c *UnprivilegedConn) Close() error {
c.natMapMutex.Lock() c.mappingAccess.Lock()
defer c.mappingAccess.Unlock()
c.cancel() c.cancel()
for _, conn := range c.natMap { for _, conn := range c.mapping {
_ = conn.Close() _ = conn.Close()
} }
common.ClearMap(c.natMap) common.ClearMap(c.mapping)
c.natMapMutex.Unlock()
return nil return nil
} }
@@ -182,7 +177,7 @@ func (c *UnprivilegedConn) RemoteAddr() net.Addr {
} }
func (c *UnprivilegedConn) SetDeadline(t time.Time) error { func (c *UnprivilegedConn) SetDeadline(t time.Time) error {
return c.SetReadDeadline(t) return os.ErrInvalid
} }
func (c *UnprivilegedConn) SetReadDeadline(t time.Time) error { func (c *UnprivilegedConn) SetReadDeadline(t time.Time) error {
@@ -191,5 +186,5 @@ func (c *UnprivilegedConn) SetReadDeadline(t time.Time) error {
} }
func (c *UnprivilegedConn) SetWriteDeadline(t time.Time) error { func (c *UnprivilegedConn) SetWriteDeadline(t time.Time) error {
return nil return os.ErrInvalid
} }