Skip to content

Commit 16cc0ad

Browse files
committed
Fix packet id type
1 parent 766fcba commit 16cc0ad

File tree

3 files changed

+15
-45
lines changed

3 files changed

+15
-45
lines changed

hysteria/packet.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"encoding/binary"
77
"errors"
88
"io"
9-
"math"
109
"net"
1110
"os"
1211
"sync"
1312
"time"
1413

1514
"github.com/sagernet/quic-go"
1615
"github.com/sagernet/sing/common"
17-
"github.com/sagernet/sing/common/atomic"
1816
"github.com/sagernet/sing/common/buf"
1917
"github.com/sagernet/sing/common/cache"
2018
E "github.com/sagernet/sing/common/exceptions"
@@ -125,7 +123,7 @@ type udpPacketConn struct {
125123
quicConn quic.Connection
126124
data chan *udpMessage
127125
udpMTU int
128-
packetId atomic.Uint32
126+
packetId uint16
129127
closeOnce sync.Once
130128
defragger *udpDefragger
131129
onDestroy func()
@@ -184,15 +182,11 @@ func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr)
184182
if buffer.Len() > 0xffff {
185183
return quic.ErrMessageTooLarge(0xffff)
186184
}
187-
packetId := c.packetId.Add(1)
188-
if packetId > math.MaxUint16 {
189-
c.packetId.Store(0)
190-
packetId = 0
191-
}
185+
c.packetId++
192186
message := allocMessage()
193187
*message = udpMessage{
194188
sessionID: c.sessionID,
195-
packetID: uint16(packetId),
189+
packetID: c.packetId,
196190
fragmentTotal: 1,
197191
host: destination.AddrString(),
198192
port: destination.Port,
@@ -224,16 +218,12 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
224218
if len(p) > 0xffff {
225219
return 0, quic.ErrMessageTooLarge(0xffff)
226220
}
227-
packetId := c.packetId.Add(1)
228-
if packetId > math.MaxUint16 {
229-
c.packetId.Store(0)
230-
packetId = 0
231-
}
221+
c.packetId++
232222
message := allocMessage()
233223
destination := M.SocksaddrFromNet(addr)
234224
*message = udpMessage{
235225
sessionID: c.sessionID,
236-
packetID: uint16(packetId),
226+
packetID: uint16(c.packetId),
237227
fragmentTotal: 1,
238228
host: destination.AddrString(),
239229
port: destination.Port,

hysteria2/packet.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/binary"
77
"errors"
88
"io"
9-
"math"
109
"net"
1110
"os"
1211
"sync"
@@ -16,7 +15,6 @@ import (
1615
"github.com/sagernet/quic-go/quicvarint"
1716
"github.com/sagernet/sing-quic/hysteria2/internal/protocol"
1817
"github.com/sagernet/sing/common"
19-
"github.com/sagernet/sing/common/atomic"
2018
"github.com/sagernet/sing/common/buf"
2119
"github.com/sagernet/sing/common/cache"
2220
M "github.com/sagernet/sing/common/metadata"
@@ -121,7 +119,7 @@ type udpPacketConn struct {
121119
quicConn quic.Connection
122120
data chan *udpMessage
123121
udpMTU int
124-
packetId atomic.Uint32
122+
packetId uint16
125123
closeOnce sync.Once
126124
defragger *udpDefragger
127125
onDestroy func()
@@ -180,15 +178,11 @@ func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr)
180178
if buffer.Len() > 0xffff {
181179
return quic.ErrMessageTooLarge(0xffff)
182180
}
183-
packetId := c.packetId.Add(1)
184-
if packetId > math.MaxUint16 {
185-
c.packetId.Store(0)
186-
packetId = 0
187-
}
181+
c.packetId++
188182
message := allocMessage()
189183
*message = udpMessage{
190184
sessionID: c.sessionID,
191-
packetID: uint16(packetId),
185+
packetID: uint16(c.packetId),
192186
fragmentTotal: 1,
193187
destination: destination.String(),
194188
data: buffer,
@@ -219,15 +213,11 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
219213
if len(p) > 0xffff {
220214
return 0, quic.ErrMessageTooLarge(0xffff)
221215
}
222-
packetId := c.packetId.Add(1)
223-
if packetId > math.MaxUint16 {
224-
c.packetId.Store(0)
225-
packetId = 0
226-
}
216+
c.packetId++
227217
message := allocMessage()
228218
*message = udpMessage{
229219
sessionID: c.sessionID,
230-
packetID: uint16(packetId),
220+
packetID: uint16(c.packetId),
231221
fragmentTotal: 1,
232222
destination: addr.String(),
233223
data: buf.As(p),

tuic/packet.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"encoding/binary"
77
"errors"
88
"io"
9-
"math"
109
"net"
1110
"os"
1211
"sync"
1312
"time"
1413

1514
"github.com/sagernet/quic-go"
1615
"github.com/sagernet/sing/common"
17-
"github.com/sagernet/sing/common/atomic"
1816
"github.com/sagernet/sing/common/buf"
1917
"github.com/sagernet/sing/common/cache"
2018
E "github.com/sagernet/sing/common/exceptions"
@@ -129,7 +127,7 @@ type udpPacketConn struct {
129127
udpStream bool
130128
udpMTU int
131129
udpMTUTime time.Time
132-
packetId atomic.Uint32
130+
packetId uint16
133131
closeOnce sync.Once
134132
isServer bool
135133
defragger *udpDefragger
@@ -204,15 +202,11 @@ func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr)
204202
if !destination.IsValid() {
205203
return E.New("invalid destination address")
206204
}
207-
packetId := c.packetId.Add(1)
208-
if packetId > math.MaxUint16 {
209-
c.packetId.Store(0)
210-
packetId = 0
211-
}
205+
c.packetId++
212206
message := allocMessage()
213207
*message = udpMessage{
214208
sessionID: c.sessionID,
215-
packetID: uint16(packetId),
209+
packetID: uint16(c.packetId),
216210
fragmentTotal: 1,
217211
destination: destination,
218212
data: buffer,
@@ -249,15 +243,11 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
249243
if !destination.IsValid() {
250244
return 0, E.New("invalid destination address")
251245
}
252-
packetId := c.packetId.Add(1)
253-
if packetId > math.MaxUint16 {
254-
c.packetId.Store(0)
255-
packetId = 0
256-
}
246+
c.packetId++
257247
message := allocMessage()
258248
*message = udpMessage{
259249
sessionID: c.sessionID,
260-
packetID: uint16(packetId),
250+
packetID: uint16(c.packetId),
261251
fragmentTotal: 1,
262252
destination: destination,
263253
data: buf.As(p),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy