diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2022-02-02 23:30:31 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2022-02-02 23:30:31 +0100 |
commit | 3b95c81cc178f808f63f2782766ecf190afa5157 (patch) | |
tree | 0de0dd07eb634d16e03621df9d94295573e784e3 /tun | |
parent | b9669b734e30e717835ed44ea01f7ee7cdce5563 (diff) |
tun/netstack: simplify read timeout on ping socket
I'm not 100% sure this is correct, but it certainly is a lot simpler.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tun')
-rw-r--r-- | tun/netstack/tun.go | 57 |
1 files changed, 14 insertions, 43 deletions
diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index 97983b4..94d63a6 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -17,7 +17,6 @@ import ( "regexp" "strconv" "strings" - "sync" "time" "golang.zx2c4.com/go118/netip" @@ -294,13 +293,11 @@ func (net *Net) ListenUDP(laddr *net.UDPAddr) (*gonet.UDPConn, error) { } type PingConn struct { - laddr PingAddr - raddr PingAddr - wq waiter.Queue - ep tcpip.Endpoint - mu sync.RWMutex - deadline time.Time - deadlineBreaker chan struct{} + laddr PingAddr + raddr PingAddr + wq waiter.Queue + ep tcpip.Endpoint + deadline *time.Timer } type PingAddr struct{ addr netip.Addr } @@ -348,9 +345,10 @@ func (net *Net) DialPingAddr(laddr, raddr netip.Addr) (*PingConn, error) { } pc := &PingConn{ - laddr: PingAddr{laddr}, - deadlineBreaker: make(chan struct{}, 1), + laddr: PingAddr{laddr}, + deadline: time.NewTimer(time.Hour << 10), } + pc.deadline.Stop() ep, tcpipErr := net.stack.NewEndpoint(tn, pn, &pc.wq) if tcpipErr != nil { @@ -408,7 +406,7 @@ func (pc *PingConn) RemoteAddr() net.Addr { } func (pc *PingConn) Close() error { - close(pc.deadlineBreaker) + pc.deadline.Reset(0) pc.ep.Close() return nil } @@ -454,33 +452,10 @@ func (pc *PingConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { pc.wq.EventRegister(&e, waiter.EventIn) defer pc.wq.EventUnregister(&e) - ready := false - - for !ready { - pc.mu.RLock() - deadlineBreaker := pc.deadlineBreaker - deadline := pc.deadline - pc.mu.RUnlock() - - if deadline.IsZero() { - select { - case <-deadlineBreaker: - case <-notifyCh: - ready = true - } - } else { - t := time.NewTimer(deadline.Sub(time.Now())) - defer t.Stop() - - select { - case <-t.C: - return 0, nil, os.ErrDeadlineExceeded - - case <-deadlineBreaker: - case <-notifyCh: - ready = true - } - } + select { + case <-pc.deadline.C: + return 0, nil, os.ErrDeadlineExceeded + case <-notifyCh: } w := tcpip.SliceWriter(p) @@ -508,11 +483,7 @@ func (pc *PingConn) SetDeadline(t time.Time) error { } func (pc *PingConn) SetReadDeadline(t time.Time) error { - pc.mu.Lock() - defer pc.mu.Unlock() - close(pc.deadlineBreaker) - pc.deadlineBreaker = make(chan struct{}, 1) - pc.deadline = t + pc.deadline.Reset(t.Sub(time.Now())) return nil } |