Skip to content

Commit 58ca659

Browse files
committed
conn.go: Fixed non-raknet packets not retaining their first byte.
1 parent 1beb5ce commit 58ca659

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

conn.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
)
1616

1717
const (
18-
// currentProtocol is the current RakNet protocol version. This is Minecraft
18+
// protocolVersion is the current RakNet protocol version. This is Minecraft
1919
// specific.
20-
currentProtocol byte = 11
20+
protocolVersion byte = 11
2121

2222
minMTUSize = 576
2323
maxMTUSize = 1492
@@ -352,11 +352,11 @@ func (conn *Conn) Close() error {
352352
func (conn *Conn) closeImmediately() {
353353
conn.once.Do(func() {
354354
_, _ = conn.Write([]byte{message.IDDisconnectNotification})
355-
close(conn.closed)
356355
if conn.close != nil {
357356
conn.close()
358357
conn.close = nil
359358
}
359+
close(conn.closed)
360360
})
361361
}
362362

@@ -472,18 +472,14 @@ func (conn *Conn) receive(b *bytes.Buffer) error {
472472
if err != nil {
473473
return fmt.Errorf("error reading datagram header flags: %v", err)
474474
}
475-
if headerFlags&bitFlagDatagram == 0 {
476-
// Ignore packets that do not have the datagram bitflag.
477-
return nil
478-
}
479475
t := time.Now()
480476
conn.lastActivity.Store(&t)
481477
switch {
482478
case headerFlags&bitFlagACK != 0:
483479
return conn.handleACK(b)
484480
case headerFlags&bitFlagNACK != 0:
485481
return conn.handleNACK(b)
486-
default:
482+
case headerFlags&bitFlagDatagram != 0:
487483
return conn.receiveDatagram(b)
488484
}
489485
}
@@ -599,7 +595,7 @@ func (conn *Conn) handlePacket(b []byte) error {
599595
// try to escape if the connection was closed.
600596
select {
601597
case <-conn.closed:
602-
case conn.packets <- bytes.NewBuffer(b[1:]):
598+
case conn.packets <- bytes.NewBuffer(b):
603599
}
604600
}
605601
return nil

dial.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func (state *connState) discoverMTU(ctx context.Context) error {
332332
if err := response.UnmarshalBinary(b[1:n]); err != nil {
333333
return fmt.Errorf("read incompatible protocol version: %w", err)
334334
}
335-
return fmt.Errorf("mismatched protocol: client protocol = %v, server protocol = %v", currentProtocol, response.ServerProtocol)
335+
return fmt.Errorf("mismatched protocol: client protocol = %v, server protocol = %v", protocolVersion, response.ServerProtocol)
336336
}
337337
}
338338
}
@@ -404,7 +404,7 @@ func (state *connState) request2(ctx context.Context) {
404404
// openConnectionRequest1 sends an open connection request 1 packet to the
405405
// server. If not successful, an error is returned.
406406
func (state *connState) openConnectionRequest1(mtu uint16) {
407-
data, _ := (&message.OpenConnectionRequest1{Protocol: currentProtocol, MaximumSizeNotDropped: mtu}).MarshalBinary()
407+
data, _ := (&message.OpenConnectionRequest1{Protocol: protocolVersion, MaximumSizeNotDropped: mtu}).MarshalBinary()
408408
_, _ = state.conn.Write(data)
409409
}
410410

listener.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ func (listener *Listener) handle(b *bytes.Buffer, addr net.Addr) error {
185185
if !found {
186186
// If there was no session yet, it means the packet is an offline
187187
// message. It is not contained in a datagram.
188-
packetID, err := b.ReadByte()
188+
id, err := b.ReadByte()
189189
if err != nil {
190190
return fmt.Errorf("error reading packet ID byte: %v", err)
191191
}
192-
switch packetID {
192+
switch id {
193193
case message.IDUnconnectedPing, message.IDUnconnectedPingOpenConnections:
194194
return listener.handleUnconnectedPing(b, addr)
195195
case message.IDOpenConnectionRequest1:
@@ -200,8 +200,8 @@ func (listener *Listener) handle(b *bytes.Buffer, addr net.Addr) error {
200200
// In some cases, the client will keep trying to send datagrams
201201
// while it has already timed out. In this case, we should not print
202202
// an error.
203-
if packetID&bitFlagDatagram == 0 {
204-
return fmt.Errorf("unknown packet received (len=%v, id=%x): %x", b.Len(), packetID, b.Bytes())
203+
if id&bitFlagDatagram == 0 {
204+
return fmt.Errorf("unknown packet received (len=%v, id=%x): %x", b.Len(), id, b.Bytes())
205205
}
206206
}
207207
return nil
@@ -212,11 +212,11 @@ func (listener *Listener) handle(b *bytes.Buffer, addr net.Addr) error {
212212
// Connection was closed already.
213213
return nil
214214
default:
215-
err := conn.receive(b)
216-
if err != nil {
215+
if err := conn.receive(b); err != nil {
217216
conn.closeImmediately()
217+
return err
218218
}
219-
return err
219+
return nil
220220
}
221221
}
222222

@@ -274,10 +274,10 @@ func (listener *Listener) handleOpenConnectionRequest1(b *bytes.Buffer, addr net
274274
b.Reset()
275275
mtuSize := min(pk.MaximumSizeNotDropped, maxMTUSize)
276276

277-
if pk.Protocol != currentProtocol {
278-
data, _ := (&message.IncompatibleProtocolVersion{ServerGUID: listener.id, ServerProtocol: currentProtocol}).MarshalBinary()
277+
if pk.Protocol != protocolVersion {
278+
data, _ := (&message.IncompatibleProtocolVersion{ServerGUID: listener.id, ServerProtocol: protocolVersion}).MarshalBinary()
279279
_, _ = listener.conn.WriteTo(data, addr)
280-
return fmt.Errorf("error handling open connection request 1: incompatible protocol version %v (listener protocol = %v)", pk.Protocol, currentProtocol)
280+
return fmt.Errorf("error handling open connection request 1: incompatible protocol version %v (listener protocol = %v)", pk.Protocol, protocolVersion)
281281
}
282282

283283
data, _ := (&message.OpenConnectionReply1{ServerGUID: listener.id, Secure: false, ServerPreferredMTUSize: mtuSize}).MarshalBinary()

packet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const (
4949
)
5050

5151
// packet is an encapsulation around every packet sent after the connection is
52-
// established. It is
52+
// established.
5353
type packet struct {
5454
reliability byte
5555

0 commit comments

Comments
 (0)