Skip to content

Commit 08602c9

Browse files
committed
internal/message: Be more consistent with original RakNet in field naming.
1 parent 664f9c4 commit 08602c9

15 files changed

+111
-102
lines changed

conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (conn *Conn) startTicking() {
157157
}
158158
if i%5 == 0 {
159159
// Ping the other end periodically to prevent timeouts.
160-
_ = conn.send(&message.ConnectedPing{ClientTimestamp: timestamp()})
160+
_ = conn.send(&message.ConnectedPing{PingTime: timestamp()})
161161

162162
conn.mu.Lock()
163163
if t.Sub(*conn.lastActivity.Load()) > time.Second*5+conn.retransmission.rtt(t)*2 {

dial.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (dialer Dialer) PingContext(ctx context.Context, address string) (response
136136
}
137137
defer conn.Close()
138138

139-
data, _ := (&message.UnconnectedPing{SendTimestamp: timestamp(), ClientGUID: atomic.AddInt64(&dialerID, 1)}).MarshalBinary()
139+
data, _ := (&message.UnconnectedPing{PingTime: timestamp(), ClientGUID: atomic.AddInt64(&dialerID, 1)}).MarshalBinary()
140140
if _, err := conn.Write(data); err != nil {
141141
return nil, dialer.error("ping", err)
142142
}
@@ -230,9 +230,11 @@ func (dialer Dialer) DialContext(ctx context.Context, address string) (*Conn, er
230230
return dialer.connect(ctx, cs)
231231
}
232232

233+
// dial finishes the RakNet connection sequence and returns a Conn if
234+
// successful.
233235
func (dialer Dialer) connect(ctx context.Context, state *connState) (*Conn, error) {
234236
conn := newConn(internal.ConnToPacketConn(state.conn), state.raddr, state.mtu, dialerConnectionHandler{})
235-
if err := conn.send((&message.ConnectionRequest{ClientGUID: state.id, RequestTimestamp: timestamp()})); err != nil {
237+
if err := conn.send((&message.ConnectionRequest{ClientGUID: state.id, RequestTime: timestamp()})); err != nil {
236238
return nil, dialer.error("dial", fmt.Errorf("send connection request: %w", err))
237239
}
238240

@@ -316,16 +318,16 @@ func (state *connState) discoverMTU(ctx context.Context) error {
316318
if err := response.UnmarshalBinary(b[1:n]); err != nil {
317319
return fmt.Errorf("read open connection reply 1: %w", err)
318320
}
319-
state.serverSecurity, state.cookie = response.Secure, response.Cookie
320-
if response.ServerGUID == 0 || response.ServerPreferredMTUSize < 400 || response.ServerPreferredMTUSize > 1500 {
321+
state.serverSecurity, state.cookie = response.ServerHasSecurity, response.Cookie
322+
if response.ServerGUID == 0 || response.MTU < 400 || response.MTU > 1500 {
321323
// This is an awful hack we cooked up to deal with OVH 'DDoS'
322324
// protection. For some reason they send a broken MTU size
323325
// first. Sending a Request2 followed by a Request1 deals with
324326
// this.
325-
state.openConnectionRequest2(response.ServerPreferredMTUSize)
327+
state.openConnectionRequest2(response.MTU)
326328
continue
327329
}
328-
state.mtu = response.ServerPreferredMTUSize
330+
state.mtu = response.MTU
329331
return nil
330332
case message.IDIncompatibleProtocolVersion:
331333
response := &message.IncompatibleProtocolVersion{}
@@ -378,7 +380,7 @@ func (state *connState) openConnection(ctx context.Context) error {
378380
if err = pk.UnmarshalBinary(b[1:n]); err != nil {
379381
return fmt.Errorf("read open connection reply 2: %v", err)
380382
}
381-
state.mtu = pk.MTUSize
383+
state.mtu = pk.MTU
382384
return nil
383385
}
384386
}
@@ -400,19 +402,19 @@ func (state *connState) request2(ctx context.Context, mtu uint16) {
400402
// openConnectionRequest1 sends an open connection request 1 packet to the
401403
// server. If not successful, an error is returned.
402404
func (state *connState) openConnectionRequest1(mtu uint16) {
403-
data, _ := (&message.OpenConnectionRequest1{Protocol: protocolVersion, MaximumSizeNotDropped: mtu}).MarshalBinary()
405+
data, _ := (&message.OpenConnectionRequest1{ClientProtocol: protocolVersion, MTU: mtu}).MarshalBinary()
404406
_, _ = state.conn.Write(data)
405407
}
406408

407409
// openConnectionRequest2 sends an open connection request 2 packet to the
408410
// server. If not successful, an error is returned.
409411
func (state *connState) openConnectionRequest2(mtu uint16) {
410412
data, _ := (&message.OpenConnectionRequest2{
411-
ServerAddress: resolve(state.raddr),
412-
ClientPreferredMTUSize: mtu,
413-
ClientGUID: state.id,
414-
ServerHasSecurity: state.serverSecurity,
415-
Cookie: state.cookie,
413+
ServerAddress: resolve(state.raddr),
414+
MTU: mtu,
415+
ClientGUID: state.id,
416+
ServerHasSecurity: state.serverSecurity,
417+
Cookie: state.cookie,
416418
}).MarshalBinary()
417419
_, _ = state.conn.Write(data)
418420
}

handler.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (h listenerConnectionHandler) handle(conn *Conn, b []byte) (handled bool, e
6464
return true, nil
6565
case message.IDDetectLostConnections:
6666
// Let the other end know the connection is still alive.
67-
return true, conn.send(&message.ConnectedPing{ClientTimestamp: timestamp()})
67+
return true, conn.send(&message.ConnectedPing{PingTime: timestamp()})
6868
default:
6969
return false, nil
7070
}
@@ -77,7 +77,7 @@ func (h listenerConnectionHandler) handleUnconnectedPing(b []byte, addr net.Addr
7777
if err := pk.UnmarshalBinary(b); err != nil {
7878
return fmt.Errorf("read UNCONNECTED_PING: %w", err)
7979
}
80-
data, _ := (&message.UnconnectedPong{ServerGUID: h.l.id, SendTimestamp: pk.SendTimestamp, Data: *h.l.pongData.Load()}).MarshalBinary()
80+
data, _ := (&message.UnconnectedPong{ServerGUID: h.l.id, PingTime: pk.PingTime, Data: *h.l.pongData.Load()}).MarshalBinary()
8181
_, err := h.l.conn.WriteTo(data, addr)
8282
return err
8383
}
@@ -89,15 +89,15 @@ func (h listenerConnectionHandler) handleOpenConnectionRequest1(b []byte, addr n
8989
if err := pk.UnmarshalBinary(b); err != nil {
9090
return fmt.Errorf("read OPEN_CONNECTION_REQUEST_1: %w", err)
9191
}
92-
mtuSize := min(pk.MaximumSizeNotDropped, maxMTUSize)
92+
mtuSize := min(pk.MTU, maxMTUSize)
9393

94-
if pk.Protocol != protocolVersion {
94+
if pk.ClientProtocol != protocolVersion {
9595
data, _ := (&message.IncompatibleProtocolVersion{ServerGUID: h.l.id, ServerProtocol: protocolVersion}).MarshalBinary()
9696
_, _ = h.l.conn.WriteTo(data, addr)
97-
return fmt.Errorf("handle OPEN_CONNECTION_REQUEST_1: incompatible protocol version %v (listener protocol = %v)", pk.Protocol, protocolVersion)
97+
return fmt.Errorf("handle OPEN_CONNECTION_REQUEST_1: incompatible protocol version %v (listener protocol = %v)", pk.ClientProtocol, protocolVersion)
9898
}
9999

100-
data, _ := (&message.OpenConnectionReply1{ServerGUID: h.l.id, Secure: false, ServerPreferredMTUSize: mtuSize}).MarshalBinary()
100+
data, _ := (&message.OpenConnectionReply1{ServerGUID: h.l.id, ServerHasSecurity: false, MTU: mtuSize}).MarshalBinary()
101101
_, err := h.l.conn.WriteTo(data, addr)
102102
return err
103103
}
@@ -109,9 +109,9 @@ func (h listenerConnectionHandler) handleOpenConnectionRequest2(b []byte, addr n
109109
if err := pk.UnmarshalBinary(b); err != nil {
110110
return fmt.Errorf("read OPEN_CONNECTION_REQUEST_2: %w", err)
111111
}
112-
mtuSize := min(pk.ClientPreferredMTUSize, maxMTUSize)
112+
mtuSize := min(pk.MTU, maxMTUSize)
113113

114-
data, _ := (&message.OpenConnectionReply2{ServerGUID: h.l.id, ClientAddress: resolve(addr), MTUSize: mtuSize}).MarshalBinary()
114+
data, _ := (&message.OpenConnectionReply2{ServerGUID: h.l.id, ClientAddress: resolve(addr), MTU: mtuSize}).MarshalBinary()
115115
if _, err := h.l.conn.WriteTo(data, addr); err != nil {
116116
return fmt.Errorf("send OPEN_CONNECTION_REPLY_2: %w", err)
117117
}
@@ -146,7 +146,7 @@ func (h listenerConnectionHandler) handleConnectionRequest(conn *Conn, b []byte)
146146
if err := pk.UnmarshalBinary(b); err != nil {
147147
return fmt.Errorf("read CONNECTION_REQUEST: %w", err)
148148
}
149-
return conn.send(&message.ConnectionRequestAccepted{ClientAddress: resolve(conn.raddr), RequestTimestamp: pk.RequestTimestamp, AcceptedTimestamp: timestamp()})
149+
return conn.send(&message.ConnectionRequestAccepted{ClientAddress: resolve(conn.raddr), PingTime: pk.RequestTime, PongTime: timestamp()})
150150
}
151151

152152
// handleNewIncomingConnection handles an incoming connection packet from the
@@ -194,7 +194,7 @@ func (h dialerConnectionHandler) handle(conn *Conn, b []byte) (handled bool, err
194194
return true, nil
195195
case message.IDDetectLostConnections:
196196
// Let the other end know the connection is still alive.
197-
return true, conn.send(&message.ConnectedPing{ClientTimestamp: timestamp()})
197+
return true, conn.send(&message.ConnectedPing{PingTime: timestamp()})
198198
default:
199199
return false, nil
200200
}
@@ -212,7 +212,7 @@ func (h dialerConnectionHandler) handleConnectionRequestAccepted(conn *Conn, b [
212212
return errUnexpectedAdditionalCRA
213213
default:
214214
// Make sure to send NewIncomingConnection before closing conn.connected.
215-
err := conn.send(&message.NewIncomingConnection{ServerAddress: resolve(conn.raddr), RequestTimestamp: pk.AcceptedTimestamp, AcceptedTimestamp: timestamp()})
215+
err := conn.send(&message.NewIncomingConnection{ServerAddress: resolve(conn.raddr), PingTime: pk.PongTime, PongTime: timestamp()})
216216
close(conn.connected)
217217
return err
218218
}
@@ -227,7 +227,7 @@ func handleConnectedPing(conn *Conn, b []byte) error {
227227
}
228228
// Respond with a connected pong that has the ping timestamp found in the
229229
// connected ping, and our own timestamp for the pong timestamp.
230-
return conn.send(&message.ConnectedPong{ClientTimestamp: pk.ClientTimestamp, ServerTimestamp: timestamp()})
230+
return conn.send(&message.ConnectedPong{PingTime: pk.PingTime, PongTime: timestamp()})
231231
}
232232

233233
// handleConnectedPong handles a connected pong packet inside of buffer b. An
@@ -237,7 +237,7 @@ func handleConnectedPong(b []byte) error {
237237
if err := pk.UnmarshalBinary(b); err != nil {
238238
return fmt.Errorf("read CONNECTED_PONG: %w", err)
239239
}
240-
if pk.ClientTimestamp > timestamp() {
240+
if pk.PingTime > timestamp() {
241241
return fmt.Errorf("handle CONNECTED_PONG: timestamp is in the future")
242242
}
243243
// We don't actually use the ConnectedPong to measure rtt. It is too

internal/message/connected_ping.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import (
66
)
77

88
type ConnectedPing struct {
9-
ClientTimestamp int64
9+
PingTime int64
1010
}
1111

1212
func (pk *ConnectedPing) UnmarshalBinary(data []byte) error {
1313
if len(data) < 8 {
1414
return io.ErrUnexpectedEOF
1515
}
16-
pk.ClientTimestamp = int64(binary.BigEndian.Uint64(data))
16+
pk.PingTime = int64(binary.BigEndian.Uint64(data))
1717
return nil
1818
}
1919

2020
func (pk *ConnectedPing) MarshalBinary() (data []byte, err error) {
2121
b := make([]byte, 9)
2222
b[0] = IDConnectedPing
23-
binary.BigEndian.PutUint64(b[1:], uint64(pk.ClientTimestamp))
23+
binary.BigEndian.PutUint64(b[1:], uint64(pk.PingTime))
2424
return b, nil
2525
}

internal/message/connected_pong.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ import (
66
)
77

88
type ConnectedPong struct {
9-
ClientTimestamp int64
10-
ServerTimestamp int64
9+
PingTime int64
10+
PongTime int64
1111
}
1212

1313
func (pk *ConnectedPong) UnmarshalBinary(data []byte) error {
1414
if len(data) < 16 {
1515
return io.ErrUnexpectedEOF
1616
}
17-
pk.ClientTimestamp = int64(binary.BigEndian.Uint64(data))
18-
pk.ServerTimestamp = int64(binary.BigEndian.Uint64(data[8:]))
17+
pk.PingTime = int64(binary.BigEndian.Uint64(data))
18+
pk.PongTime = int64(binary.BigEndian.Uint64(data[8:]))
1919
return nil
2020
}
2121

2222
func (pk *ConnectedPong) MarshalBinary() (data []byte, err error) {
2323
b := make([]byte, 17)
2424
b[0] = IDConnectedPong
25-
binary.BigEndian.PutUint64(b[1:], uint64(pk.ClientTimestamp))
26-
binary.BigEndian.PutUint64(b[9:], uint64(pk.ServerTimestamp))
25+
binary.BigEndian.PutUint64(b[1:], uint64(pk.PingTime))
26+
binary.BigEndian.PutUint64(b[9:], uint64(pk.PongTime))
2727
return b, nil
2828
}

internal/message/connection_request.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import (
66
)
77

88
type ConnectionRequest struct {
9-
ClientGUID int64
10-
RequestTimestamp int64
11-
Secure bool
9+
ClientGUID int64
10+
// RequestTime is a timestamp from the moment the packet is sent.
11+
RequestTime int64
12+
Secure bool
1213
}
1314

1415
func (pk *ConnectionRequest) UnmarshalBinary(data []byte) error {
1516
if len(data) < 17 {
1617
return io.ErrUnexpectedEOF
1718
}
1819
pk.ClientGUID = int64(binary.BigEndian.Uint64(data))
19-
pk.RequestTimestamp = int64(binary.BigEndian.Uint64(data[8:]))
20+
pk.RequestTime = int64(binary.BigEndian.Uint64(data[8:]))
2021
pk.Secure = data[16] != 0
2122
return nil
2223
}
@@ -25,7 +26,7 @@ func (pk *ConnectionRequest) MarshalBinary() (data []byte, err error) {
2526
b := make([]byte, 18)
2627
b[0] = IDConnectionRequest
2728
binary.BigEndian.PutUint64(b[1:], uint64(pk.ClientGUID))
28-
binary.BigEndian.PutUint64(b[9:], uint64(pk.RequestTimestamp))
29+
binary.BigEndian.PutUint64(b[9:], uint64(pk.RequestTime))
2930
if pk.Secure {
3031
b[17] = 1
3132
}

internal/message/connection_request_accepted.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
)
88

99
type ConnectionRequestAccepted struct {
10-
ClientAddress netip.AddrPort
11-
SystemAddresses systemAddresses
12-
RequestTimestamp int64
13-
AcceptedTimestamp int64
10+
ClientAddress netip.AddrPort
11+
SystemIndex uint16
12+
SystemAddresses systemAddresses
13+
// PingTime is filled out with ConnectionRequest.RequestTime.
14+
PingTime int64
15+
// PongTime is a timestamp from the moment the packet is sent.
16+
PongTime int64
1417
}
1518

1619
func (pk *ConnectionRequestAccepted) UnmarshalBinary(data []byte) error {
@@ -19,7 +22,8 @@ func (pk *ConnectionRequestAccepted) UnmarshalBinary(data []byte) error {
1922
}
2023
var offset int
2124
pk.ClientAddress, offset = addr(data)
22-
offset += 2 // Zero int16.
25+
pk.SystemIndex = binary.BigEndian.Uint16(data[offset:])
26+
offset += 2
2327
for i := range 20 {
2428
if len(data) < addrSize(data[offset:]) {
2529
return io.ErrUnexpectedEOF
@@ -36,20 +40,21 @@ func (pk *ConnectionRequestAccepted) UnmarshalBinary(data []byte) error {
3640
if len(data[offset:]) < 16 {
3741
return io.ErrUnexpectedEOF
3842
}
39-
pk.RequestTimestamp = int64(binary.BigEndian.Uint64(data[offset:]))
40-
pk.AcceptedTimestamp = int64(binary.BigEndian.Uint64(data[offset+8:]))
43+
pk.PingTime = int64(binary.BigEndian.Uint64(data[offset:]))
44+
pk.PongTime = int64(binary.BigEndian.Uint64(data[offset+8:]))
4145
return nil
4246
}
4347

4448
func (pk *ConnectionRequestAccepted) MarshalBinary() (data []byte, err error) {
4549
nAddr, nSys := sizeofAddr(pk.ClientAddress), pk.SystemAddresses.sizeOf()
4650
b := make([]byte, 1+nAddr+2+nSys+16)
4751
b[0] = IDConnectionRequestAccepted
48-
offset := 1 + putAddr(b[1:], pk.ClientAddress) + 2 // Zero int16.
52+
offset := 1 + putAddr(b[1:], pk.ClientAddress)
53+
binary.BigEndian.PutUint16(b[offset:], pk.SystemIndex)
4954
for _, addr := range pk.SystemAddresses {
50-
offset += putAddr(b[offset:], addr)
55+
offset += putAddr(b[offset+2:], addr)
5156
}
52-
binary.BigEndian.PutUint64(b[offset:], uint64(pk.RequestTimestamp))
53-
binary.BigEndian.PutUint64(b[offset+8:], uint64(pk.AcceptedTimestamp))
57+
binary.BigEndian.PutUint64(b[offset+2:], uint64(pk.PingTime))
58+
binary.BigEndian.PutUint64(b[offset+10:], uint64(pk.PongTime))
5459
return b, nil
5560
}

internal/message/incompatible_protocol_version.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
)
77

88
type IncompatibleProtocolVersion struct {
9-
Magic [16]byte
109
ServerProtocol byte
1110
ServerGUID int64
1211
}

internal/message/new_incoming_connection.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
)
88

99
type NewIncomingConnection struct {
10-
ServerAddress netip.AddrPort
11-
SystemAddresses systemAddresses
12-
RequestTimestamp int64
13-
AcceptedTimestamp int64
10+
ServerAddress netip.AddrPort
11+
SystemAddresses systemAddresses
12+
// PingTime is filled out with ConnectionRequestAccepted.PongTime.
13+
PingTime int64
14+
// PongTime is a timestamp from the moment the packet is sent.
15+
PongTime int64
1416
}
1517

1618
func (pk *NewIncomingConnection) UnmarshalBinary(data []byte) error {
@@ -35,8 +37,8 @@ func (pk *NewIncomingConnection) UnmarshalBinary(data []byte) error {
3537
if len(data[offset:]) < 16 {
3638
return io.ErrUnexpectedEOF
3739
}
38-
pk.RequestTimestamp = int64(binary.BigEndian.Uint64(data[offset:]))
39-
pk.AcceptedTimestamp = int64(binary.BigEndian.Uint64(data[offset+8:]))
40+
pk.PingTime = int64(binary.BigEndian.Uint64(data[offset:]))
41+
pk.PongTime = int64(binary.BigEndian.Uint64(data[offset+8:]))
4042
return nil
4143
}
4244

@@ -48,7 +50,7 @@ func (pk *NewIncomingConnection) MarshalBinary() (data []byte, err error) {
4850
for _, addr := range pk.SystemAddresses {
4951
offset += putAddr(b[offset:], addr)
5052
}
51-
binary.BigEndian.PutUint64(b[offset:], uint64(pk.RequestTimestamp))
52-
binary.BigEndian.PutUint64(b[offset+8:], uint64(pk.AcceptedTimestamp))
53+
binary.BigEndian.PutUint64(b[offset:], uint64(pk.PingTime))
54+
binary.BigEndian.PutUint64(b[offset+8:], uint64(pk.PongTime))
5355
return b, nil
5456
}

0 commit comments

Comments
 (0)