mirror of
https://git.zx2c4.com/wireguard-go
synced 2025-10-05 08:36:57 +08:00
all: use Go 1.19 and its atomic types
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:

committed by
Jason A. Donenfeld

parent
d1d08426b2
commit
b51010ba13
@@ -74,7 +74,7 @@ type afWinRingBind struct {
|
||||
type WinRingBind struct {
|
||||
v4, v6 afWinRingBind
|
||||
mu sync.RWMutex
|
||||
isOpen uint32
|
||||
isOpen atomic.Uint32 // 0, 1, or 2
|
||||
}
|
||||
|
||||
func NewDefaultBind() Bind { return NewWinRingBind() }
|
||||
@@ -212,7 +212,7 @@ func (bind *afWinRingBind) CloseAndZero() {
|
||||
}
|
||||
|
||||
func (bind *WinRingBind) closeAndZero() {
|
||||
atomic.StoreUint32(&bind.isOpen, 0)
|
||||
bind.isOpen.Store(0)
|
||||
bind.v4.CloseAndZero()
|
||||
bind.v6.CloseAndZero()
|
||||
}
|
||||
@@ -276,7 +276,7 @@ func (bind *WinRingBind) Open(port uint16) (recvFns []ReceiveFunc, selectedPort
|
||||
bind.closeAndZero()
|
||||
}
|
||||
}()
|
||||
if atomic.LoadUint32(&bind.isOpen) != 0 {
|
||||
if bind.isOpen.Load() != 0 {
|
||||
return nil, 0, ErrBindAlreadyOpen
|
||||
}
|
||||
var sa windows.Sockaddr
|
||||
@@ -299,17 +299,17 @@ func (bind *WinRingBind) Open(port uint16) (recvFns []ReceiveFunc, selectedPort
|
||||
return nil, 0, err
|
||||
}
|
||||
}
|
||||
atomic.StoreUint32(&bind.isOpen, 1)
|
||||
bind.isOpen.Store(1)
|
||||
return []ReceiveFunc{bind.receiveIPv4, bind.receiveIPv6}, selectedPort, err
|
||||
}
|
||||
|
||||
func (bind *WinRingBind) Close() error {
|
||||
bind.mu.RLock()
|
||||
if atomic.LoadUint32(&bind.isOpen) != 1 {
|
||||
if bind.isOpen.Load() != 1 {
|
||||
bind.mu.RUnlock()
|
||||
return nil
|
||||
}
|
||||
atomic.StoreUint32(&bind.isOpen, 2)
|
||||
bind.isOpen.Store(2)
|
||||
windows.PostQueuedCompletionStatus(bind.v4.rx.iocp, 0, 0, nil)
|
||||
windows.PostQueuedCompletionStatus(bind.v4.tx.iocp, 0, 0, nil)
|
||||
windows.PostQueuedCompletionStatus(bind.v6.rx.iocp, 0, 0, nil)
|
||||
@@ -345,8 +345,8 @@ func (bind *afWinRingBind) InsertReceiveRequest() error {
|
||||
//go:linkname procyield runtime.procyield
|
||||
func procyield(cycles uint32)
|
||||
|
||||
func (bind *afWinRingBind) Receive(buf []byte, isOpen *uint32) (int, Endpoint, error) {
|
||||
if atomic.LoadUint32(isOpen) != 1 {
|
||||
func (bind *afWinRingBind) Receive(buf []byte, isOpen *atomic.Uint32) (int, Endpoint, error) {
|
||||
if isOpen.Load() != 1 {
|
||||
return 0, nil, net.ErrClosed
|
||||
}
|
||||
bind.rx.mu.Lock()
|
||||
@@ -359,7 +359,7 @@ retry:
|
||||
count = 0
|
||||
for tries := 0; count == 0 && tries < receiveSpins; tries++ {
|
||||
if tries > 0 {
|
||||
if atomic.LoadUint32(isOpen) != 1 {
|
||||
if isOpen.Load() != 1 {
|
||||
return 0, nil, net.ErrClosed
|
||||
}
|
||||
procyield(1)
|
||||
@@ -378,7 +378,7 @@ retry:
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
if atomic.LoadUint32(isOpen) != 1 {
|
||||
if isOpen.Load() != 1 {
|
||||
return 0, nil, net.ErrClosed
|
||||
}
|
||||
count = winrio.DequeueCompletion(bind.rx.cq, results[:])
|
||||
@@ -395,7 +395,7 @@ retry:
|
||||
// huge packets. Just try again when this happens. The infinite loop this could cause is still limited to
|
||||
// attacker bandwidth, just like the rest of the receive path.
|
||||
if windows.Errno(results[0].Status) == windows.WSAEMSGSIZE {
|
||||
if atomic.LoadUint32(isOpen) != 1 {
|
||||
if isOpen.Load() != 1 {
|
||||
return 0, nil, net.ErrClosed
|
||||
}
|
||||
goto retry
|
||||
@@ -421,8 +421,8 @@ func (bind *WinRingBind) receiveIPv6(buf []byte) (int, Endpoint, error) {
|
||||
return bind.v6.Receive(buf, &bind.isOpen)
|
||||
}
|
||||
|
||||
func (bind *afWinRingBind) Send(buf []byte, nend *WinRingEndpoint, isOpen *uint32) error {
|
||||
if atomic.LoadUint32(isOpen) != 1 {
|
||||
func (bind *afWinRingBind) Send(buf []byte, nend *WinRingEndpoint, isOpen *atomic.Uint32) error {
|
||||
if isOpen.Load() != 1 {
|
||||
return net.ErrClosed
|
||||
}
|
||||
if len(buf) > bytesPerPacket {
|
||||
@@ -444,7 +444,7 @@ func (bind *afWinRingBind) Send(buf []byte, nend *WinRingEndpoint, isOpen *uint3
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if atomic.LoadUint32(isOpen) != 1 {
|
||||
if isOpen.Load() != 1 {
|
||||
return net.ErrClosed
|
||||
}
|
||||
count = winrio.DequeueCompletion(bind.tx.cq, results[:])
|
||||
@@ -538,7 +538,7 @@ func (bind *StdNetBind) BindSocketToInterface6(interfaceIndex uint32, blackhole
|
||||
func (bind *WinRingBind) BindSocketToInterface4(interfaceIndex uint32, blackhole bool) error {
|
||||
bind.mu.RLock()
|
||||
defer bind.mu.RUnlock()
|
||||
if atomic.LoadUint32(&bind.isOpen) != 1 {
|
||||
if bind.isOpen.Load() != 1 {
|
||||
return net.ErrClosed
|
||||
}
|
||||
err := bindSocketToInterface4(bind.v4.sock, interfaceIndex)
|
||||
@@ -552,7 +552,7 @@ func (bind *WinRingBind) BindSocketToInterface4(interfaceIndex uint32, blackhole
|
||||
func (bind *WinRingBind) BindSocketToInterface6(interfaceIndex uint32, blackhole bool) error {
|
||||
bind.mu.RLock()
|
||||
defer bind.mu.RUnlock()
|
||||
if atomic.LoadUint32(&bind.isOpen) != 1 {
|
||||
if bind.isOpen.Load() != 1 {
|
||||
return net.ErrClosed
|
||||
}
|
||||
err := bindSocketToInterface6(bind.v6.sock, interfaceIndex)
|
||||
|
Reference in New Issue
Block a user