diff --git a/conn.go b/conn.go index 4db4190..fb08b39 100644 --- a/conn.go +++ b/conn.go @@ -157,7 +157,7 @@ func (conn *Conn) startTicking() { } if i%5 == 0 { // Ping the other end periodically to prevent timeouts. - _ = conn.send(&message.ConnectedPing{ClientTimestamp: timestamp()}) + _ = conn.send(&message.ConnectedPing{PingTime: timestamp()}) conn.mu.Lock() if t.Sub(*conn.lastActivity.Load()) > time.Second*5+conn.retransmission.rtt(t)*2 { diff --git a/dial.go b/dial.go index 7eba232..221cd7c 100644 --- a/dial.go +++ b/dial.go @@ -136,7 +136,7 @@ func (dialer Dialer) PingContext(ctx context.Context, address string) (response } defer conn.Close() - data, _ := (&message.UnconnectedPing{SendTimestamp: timestamp(), ClientGUID: atomic.AddInt64(&dialerID, 1)}).MarshalBinary() + data, _ := (&message.UnconnectedPing{PingTime: timestamp(), ClientGUID: atomic.AddInt64(&dialerID, 1)}).MarshalBinary() if _, err := conn.Write(data); err != nil { return nil, dialer.error("ping", err) } @@ -230,9 +230,11 @@ func (dialer Dialer) DialContext(ctx context.Context, address string) (*Conn, er return dialer.connect(ctx, cs) } +// dial finishes the RakNet connection sequence and returns a Conn if +// successful. func (dialer Dialer) connect(ctx context.Context, state *connState) (*Conn, error) { conn := newConn(internal.ConnToPacketConn(state.conn), state.raddr, state.mtu, dialerConnectionHandler{}) - if err := conn.send((&message.ConnectionRequest{ClientGUID: state.id, RequestTimestamp: timestamp()})); err != nil { + if err := conn.send((&message.ConnectionRequest{ClientGUID: state.id, RequestTime: timestamp()})); err != nil { return nil, dialer.error("dial", fmt.Errorf("send connection request: %w", err)) } @@ -316,16 +318,16 @@ func (state *connState) discoverMTU(ctx context.Context) error { if err := response.UnmarshalBinary(b[1:n]); err != nil { return fmt.Errorf("read open connection reply 1: %w", err) } - state.serverSecurity, state.cookie = response.Secure, response.Cookie - if response.ServerGUID == 0 || response.ServerPreferredMTUSize < 400 || response.ServerPreferredMTUSize > 1500 { + state.serverSecurity, state.cookie = response.ServerHasSecurity, response.Cookie + if response.ServerGUID == 0 || response.MTU < 400 || response.MTU > 1500 { // This is an awful hack we cooked up to deal with OVH 'DDoS' // protection. For some reason they send a broken MTU size // first. Sending a Request2 followed by a Request1 deals with // this. - state.openConnectionRequest2(response.ServerPreferredMTUSize) + state.openConnectionRequest2(response.MTU) continue } - state.mtu = response.ServerPreferredMTUSize + state.mtu = response.MTU return nil case message.IDIncompatibleProtocolVersion: response := &message.IncompatibleProtocolVersion{} @@ -378,7 +380,7 @@ func (state *connState) openConnection(ctx context.Context) error { if err = pk.UnmarshalBinary(b[1:n]); err != nil { return fmt.Errorf("read open connection reply 2: %v", err) } - state.mtu = pk.MTUSize + state.mtu = pk.MTU return nil } } @@ -400,7 +402,7 @@ func (state *connState) request2(ctx context.Context, mtu uint16) { // openConnectionRequest1 sends an open connection request 1 packet to the // server. If not successful, an error is returned. func (state *connState) openConnectionRequest1(mtu uint16) { - data, _ := (&message.OpenConnectionRequest1{Protocol: protocolVersion, MaximumSizeNotDropped: mtu}).MarshalBinary() + data, _ := (&message.OpenConnectionRequest1{ClientProtocol: protocolVersion, MTU: mtu}).MarshalBinary() _, _ = state.conn.Write(data) } @@ -408,11 +410,11 @@ func (state *connState) openConnectionRequest1(mtu uint16) { // server. If not successful, an error is returned. func (state *connState) openConnectionRequest2(mtu uint16) { data, _ := (&message.OpenConnectionRequest2{ - ServerAddress: resolve(state.raddr), - ClientPreferredMTUSize: mtu, - ClientGUID: state.id, - ServerHasSecurity: state.serverSecurity, - Cookie: state.cookie, + ServerAddress: resolve(state.raddr), + MTU: mtu, + ClientGUID: state.id, + ServerHasSecurity: state.serverSecurity, + Cookie: state.cookie, }).MarshalBinary() _, _ = state.conn.Write(data) } diff --git a/handler.go b/handler.go index 4500281..c68bf47 100644 --- a/handler.go +++ b/handler.go @@ -64,7 +64,7 @@ func (h listenerConnectionHandler) handle(conn *Conn, b []byte) (handled bool, e return true, nil case message.IDDetectLostConnections: // Let the other end know the connection is still alive. - return true, conn.send(&message.ConnectedPing{ClientTimestamp: timestamp()}) + return true, conn.send(&message.ConnectedPing{PingTime: timestamp()}) default: return false, nil } @@ -77,7 +77,7 @@ func (h listenerConnectionHandler) handleUnconnectedPing(b []byte, addr net.Addr if err := pk.UnmarshalBinary(b); err != nil { return fmt.Errorf("read UNCONNECTED_PING: %w", err) } - data, _ := (&message.UnconnectedPong{ServerGUID: h.l.id, SendTimestamp: pk.SendTimestamp, Data: *h.l.pongData.Load()}).MarshalBinary() + data, _ := (&message.UnconnectedPong{ServerGUID: h.l.id, PingTime: pk.PingTime, Data: *h.l.pongData.Load()}).MarshalBinary() _, err := h.l.conn.WriteTo(data, addr) return err } @@ -89,15 +89,15 @@ func (h listenerConnectionHandler) handleOpenConnectionRequest1(b []byte, addr n if err := pk.UnmarshalBinary(b); err != nil { return fmt.Errorf("read OPEN_CONNECTION_REQUEST_1: %w", err) } - mtuSize := min(pk.MaximumSizeNotDropped, maxMTUSize) + mtuSize := min(pk.MTU, maxMTUSize) - if pk.Protocol != protocolVersion { + if pk.ClientProtocol != protocolVersion { data, _ := (&message.IncompatibleProtocolVersion{ServerGUID: h.l.id, ServerProtocol: protocolVersion}).MarshalBinary() _, _ = h.l.conn.WriteTo(data, addr) - return fmt.Errorf("handle OPEN_CONNECTION_REQUEST_1: incompatible protocol version %v (listener protocol = %v)", pk.Protocol, protocolVersion) + return fmt.Errorf("handle OPEN_CONNECTION_REQUEST_1: incompatible protocol version %v (listener protocol = %v)", pk.ClientProtocol, protocolVersion) } - data, _ := (&message.OpenConnectionReply1{ServerGUID: h.l.id, Secure: false, ServerPreferredMTUSize: mtuSize}).MarshalBinary() + data, _ := (&message.OpenConnectionReply1{ServerGUID: h.l.id, ServerHasSecurity: false, MTU: mtuSize}).MarshalBinary() _, err := h.l.conn.WriteTo(data, addr) return err } @@ -109,9 +109,9 @@ func (h listenerConnectionHandler) handleOpenConnectionRequest2(b []byte, addr n if err := pk.UnmarshalBinary(b); err != nil { return fmt.Errorf("read OPEN_CONNECTION_REQUEST_2: %w", err) } - mtuSize := min(pk.ClientPreferredMTUSize, maxMTUSize) + mtuSize := min(pk.MTU, maxMTUSize) - data, _ := (&message.OpenConnectionReply2{ServerGUID: h.l.id, ClientAddress: resolve(addr), MTUSize: mtuSize}).MarshalBinary() + data, _ := (&message.OpenConnectionReply2{ServerGUID: h.l.id, ClientAddress: resolve(addr), MTU: mtuSize}).MarshalBinary() if _, err := h.l.conn.WriteTo(data, addr); err != nil { return fmt.Errorf("send OPEN_CONNECTION_REPLY_2: %w", err) } @@ -146,7 +146,7 @@ func (h listenerConnectionHandler) handleConnectionRequest(conn *Conn, b []byte) if err := pk.UnmarshalBinary(b); err != nil { return fmt.Errorf("read CONNECTION_REQUEST: %w", err) } - return conn.send(&message.ConnectionRequestAccepted{ClientAddress: resolve(conn.raddr), RequestTimestamp: pk.RequestTimestamp, AcceptedTimestamp: timestamp()}) + return conn.send(&message.ConnectionRequestAccepted{ClientAddress: resolve(conn.raddr), PingTime: pk.RequestTime, PongTime: timestamp()}) } // handleNewIncomingConnection handles an incoming connection packet from the @@ -194,7 +194,7 @@ func (h dialerConnectionHandler) handle(conn *Conn, b []byte) (handled bool, err return true, nil case message.IDDetectLostConnections: // Let the other end know the connection is still alive. - return true, conn.send(&message.ConnectedPing{ClientTimestamp: timestamp()}) + return true, conn.send(&message.ConnectedPing{PingTime: timestamp()}) default: return false, nil } @@ -212,7 +212,7 @@ func (h dialerConnectionHandler) handleConnectionRequestAccepted(conn *Conn, b [ return errUnexpectedAdditionalCRA default: // Make sure to send NewIncomingConnection before closing conn.connected. - err := conn.send(&message.NewIncomingConnection{ServerAddress: resolve(conn.raddr), RequestTimestamp: pk.AcceptedTimestamp, AcceptedTimestamp: timestamp()}) + err := conn.send(&message.NewIncomingConnection{ServerAddress: resolve(conn.raddr), PingTime: pk.PongTime, PongTime: timestamp()}) close(conn.connected) return err } @@ -227,7 +227,7 @@ func handleConnectedPing(conn *Conn, b []byte) error { } // Respond with a connected pong that has the ping timestamp found in the // connected ping, and our own timestamp for the pong timestamp. - return conn.send(&message.ConnectedPong{ClientTimestamp: pk.ClientTimestamp, ServerTimestamp: timestamp()}) + return conn.send(&message.ConnectedPong{PingTime: pk.PingTime, PongTime: timestamp()}) } // handleConnectedPong handles a connected pong packet inside of buffer b. An @@ -237,7 +237,7 @@ func handleConnectedPong(b []byte) error { if err := pk.UnmarshalBinary(b); err != nil { return fmt.Errorf("read CONNECTED_PONG: %w", err) } - if pk.ClientTimestamp > timestamp() { + if pk.PingTime > timestamp() { return fmt.Errorf("handle CONNECTED_PONG: timestamp is in the future") } // We don't actually use the ConnectedPong to measure rtt. It is too diff --git a/internal/message/connected_ping.go b/internal/message/connected_ping.go index 3518814..ade2ce3 100644 --- a/internal/message/connected_ping.go +++ b/internal/message/connected_ping.go @@ -6,20 +6,20 @@ import ( ) type ConnectedPing struct { - ClientTimestamp int64 + PingTime int64 } func (pk *ConnectedPing) UnmarshalBinary(data []byte) error { if len(data) < 8 { return io.ErrUnexpectedEOF } - pk.ClientTimestamp = int64(binary.BigEndian.Uint64(data)) + pk.PingTime = int64(binary.BigEndian.Uint64(data)) return nil } func (pk *ConnectedPing) MarshalBinary() (data []byte, err error) { b := make([]byte, 9) b[0] = IDConnectedPing - binary.BigEndian.PutUint64(b[1:], uint64(pk.ClientTimestamp)) + binary.BigEndian.PutUint64(b[1:], uint64(pk.PingTime)) return b, nil } diff --git a/internal/message/connected_pong.go b/internal/message/connected_pong.go index 7bd85ce..2f415d4 100644 --- a/internal/message/connected_pong.go +++ b/internal/message/connected_pong.go @@ -6,23 +6,23 @@ import ( ) type ConnectedPong struct { - ClientTimestamp int64 - ServerTimestamp int64 + PingTime int64 + PongTime int64 } func (pk *ConnectedPong) UnmarshalBinary(data []byte) error { if len(data) < 16 { return io.ErrUnexpectedEOF } - pk.ClientTimestamp = int64(binary.BigEndian.Uint64(data)) - pk.ServerTimestamp = int64(binary.BigEndian.Uint64(data[8:])) + pk.PingTime = int64(binary.BigEndian.Uint64(data)) + pk.PongTime = int64(binary.BigEndian.Uint64(data[8:])) return nil } func (pk *ConnectedPong) MarshalBinary() (data []byte, err error) { b := make([]byte, 17) b[0] = IDConnectedPong - binary.BigEndian.PutUint64(b[1:], uint64(pk.ClientTimestamp)) - binary.BigEndian.PutUint64(b[9:], uint64(pk.ServerTimestamp)) + binary.BigEndian.PutUint64(b[1:], uint64(pk.PingTime)) + binary.BigEndian.PutUint64(b[9:], uint64(pk.PongTime)) return b, nil } diff --git a/internal/message/connection_request.go b/internal/message/connection_request.go index 4620b65..48da563 100644 --- a/internal/message/connection_request.go +++ b/internal/message/connection_request.go @@ -6,9 +6,10 @@ import ( ) type ConnectionRequest struct { - ClientGUID int64 - RequestTimestamp int64 - Secure bool + ClientGUID int64 + // RequestTime is a timestamp from the moment the packet is sent. + RequestTime int64 + Secure bool } func (pk *ConnectionRequest) UnmarshalBinary(data []byte) error { @@ -16,7 +17,7 @@ func (pk *ConnectionRequest) UnmarshalBinary(data []byte) error { return io.ErrUnexpectedEOF } pk.ClientGUID = int64(binary.BigEndian.Uint64(data)) - pk.RequestTimestamp = int64(binary.BigEndian.Uint64(data[8:])) + pk.RequestTime = int64(binary.BigEndian.Uint64(data[8:])) pk.Secure = data[16] != 0 return nil } @@ -25,7 +26,7 @@ func (pk *ConnectionRequest) MarshalBinary() (data []byte, err error) { b := make([]byte, 18) b[0] = IDConnectionRequest binary.BigEndian.PutUint64(b[1:], uint64(pk.ClientGUID)) - binary.BigEndian.PutUint64(b[9:], uint64(pk.RequestTimestamp)) + binary.BigEndian.PutUint64(b[9:], uint64(pk.RequestTime)) if pk.Secure { b[17] = 1 } diff --git a/internal/message/connection_request_accepted.go b/internal/message/connection_request_accepted.go index c7c6c65..12efb10 100644 --- a/internal/message/connection_request_accepted.go +++ b/internal/message/connection_request_accepted.go @@ -7,10 +7,13 @@ import ( ) type ConnectionRequestAccepted struct { - ClientAddress netip.AddrPort - SystemAddresses systemAddresses - RequestTimestamp int64 - AcceptedTimestamp int64 + ClientAddress netip.AddrPort + SystemIndex uint16 + SystemAddresses systemAddresses + // PingTime is filled out with ConnectionRequest.RequestTime. + PingTime int64 + // PongTime is a timestamp from the moment the packet is sent. + PongTime int64 } func (pk *ConnectionRequestAccepted) UnmarshalBinary(data []byte) error { @@ -19,7 +22,8 @@ func (pk *ConnectionRequestAccepted) UnmarshalBinary(data []byte) error { } var offset int pk.ClientAddress, offset = addr(data) - offset += 2 // Zero int16. + pk.SystemIndex = binary.BigEndian.Uint16(data[offset:]) + offset += 2 for i := range 20 { if len(data) < addrSize(data[offset:]) { return io.ErrUnexpectedEOF @@ -36,8 +40,8 @@ func (pk *ConnectionRequestAccepted) UnmarshalBinary(data []byte) error { if len(data[offset:]) < 16 { return io.ErrUnexpectedEOF } - pk.RequestTimestamp = int64(binary.BigEndian.Uint64(data[offset:])) - pk.AcceptedTimestamp = int64(binary.BigEndian.Uint64(data[offset+8:])) + pk.PingTime = int64(binary.BigEndian.Uint64(data[offset:])) + pk.PongTime = int64(binary.BigEndian.Uint64(data[offset+8:])) return nil } @@ -45,11 +49,12 @@ func (pk *ConnectionRequestAccepted) MarshalBinary() (data []byte, err error) { nAddr, nSys := sizeofAddr(pk.ClientAddress), pk.SystemAddresses.sizeOf() b := make([]byte, 1+nAddr+2+nSys+16) b[0] = IDConnectionRequestAccepted - offset := 1 + putAddr(b[1:], pk.ClientAddress) + 2 // Zero int16. + offset := 1 + putAddr(b[1:], pk.ClientAddress) + binary.BigEndian.PutUint16(b[offset:], pk.SystemIndex) for _, addr := range pk.SystemAddresses { - offset += putAddr(b[offset:], addr) + offset += putAddr(b[offset+2:], addr) } - binary.BigEndian.PutUint64(b[offset:], uint64(pk.RequestTimestamp)) - binary.BigEndian.PutUint64(b[offset+8:], uint64(pk.AcceptedTimestamp)) + binary.BigEndian.PutUint64(b[offset+2:], uint64(pk.PingTime)) + binary.BigEndian.PutUint64(b[offset+10:], uint64(pk.PongTime)) return b, nil } diff --git a/internal/message/incompatible_protocol_version.go b/internal/message/incompatible_protocol_version.go index 4579e1f..8759860 100644 --- a/internal/message/incompatible_protocol_version.go +++ b/internal/message/incompatible_protocol_version.go @@ -6,7 +6,6 @@ import ( ) type IncompatibleProtocolVersion struct { - Magic [16]byte ServerProtocol byte ServerGUID int64 } diff --git a/internal/message/new_incoming_connection.go b/internal/message/new_incoming_connection.go index 1a62bce..5e33f02 100644 --- a/internal/message/new_incoming_connection.go +++ b/internal/message/new_incoming_connection.go @@ -7,10 +7,12 @@ import ( ) type NewIncomingConnection struct { - ServerAddress netip.AddrPort - SystemAddresses systemAddresses - RequestTimestamp int64 - AcceptedTimestamp int64 + ServerAddress netip.AddrPort + SystemAddresses systemAddresses + // PingTime is filled out with ConnectionRequestAccepted.PongTime. + PingTime int64 + // PongTime is a timestamp from the moment the packet is sent. + PongTime int64 } func (pk *NewIncomingConnection) UnmarshalBinary(data []byte) error { @@ -35,8 +37,8 @@ func (pk *NewIncomingConnection) UnmarshalBinary(data []byte) error { if len(data[offset:]) < 16 { return io.ErrUnexpectedEOF } - pk.RequestTimestamp = int64(binary.BigEndian.Uint64(data[offset:])) - pk.AcceptedTimestamp = int64(binary.BigEndian.Uint64(data[offset+8:])) + pk.PingTime = int64(binary.BigEndian.Uint64(data[offset:])) + pk.PongTime = int64(binary.BigEndian.Uint64(data[offset+8:])) return nil } @@ -48,7 +50,7 @@ func (pk *NewIncomingConnection) MarshalBinary() (data []byte, err error) { for _, addr := range pk.SystemAddresses { offset += putAddr(b[offset:], addr) } - binary.BigEndian.PutUint64(b[offset:], uint64(pk.RequestTimestamp)) - binary.BigEndian.PutUint64(b[offset+8:], uint64(pk.AcceptedTimestamp)) + binary.BigEndian.PutUint64(b[offset:], uint64(pk.PingTime)) + binary.BigEndian.PutUint64(b[offset+8:], uint64(pk.PongTime)) return b, nil } diff --git a/internal/message/open_connection_reply_1.go b/internal/message/open_connection_reply_1.go index eacd59c..176dacf 100644 --- a/internal/message/open_connection_reply_1.go +++ b/internal/message/open_connection_reply_1.go @@ -6,10 +6,10 @@ import ( ) type OpenConnectionReply1 struct { - ServerGUID int64 - Secure bool - ServerPreferredMTUSize uint16 - Cookie uint32 + ServerGUID int64 + ServerHasSecurity bool + Cookie uint32 + MTU uint16 } func (pk *OpenConnectionReply1) UnmarshalBinary(data []byte) error { @@ -19,28 +19,28 @@ func (pk *OpenConnectionReply1) UnmarshalBinary(data []byte) error { } // Magic: 16 bytes. pk.ServerGUID = int64(binary.BigEndian.Uint64(data[16:])) - pk.Secure = data[24] != 0 - if pk.Secure { + pk.ServerHasSecurity = data[24] != 0 + if pk.ServerHasSecurity { offset = 4 pk.Cookie = binary.BigEndian.Uint32(data[25:29]) } - pk.ServerPreferredMTUSize = binary.BigEndian.Uint16(data[25+offset:]) + pk.MTU = binary.BigEndian.Uint16(data[25+offset:]) return nil } func (pk *OpenConnectionReply1) MarshalBinary() (data []byte, err error) { offset := 0 - if pk.Secure { + if pk.ServerHasSecurity { offset = 4 } b := make([]byte, 28+offset) b[0] = IDOpenConnectionReply1 copy(b[1:], unconnectedMessageSequence[:]) binary.BigEndian.PutUint64(b[17:], uint64(pk.ServerGUID)) - if pk.Secure { + if pk.ServerHasSecurity { b[25] = 1 binary.BigEndian.PutUint32(b[26:], pk.Cookie) } - binary.BigEndian.PutUint16(b[26+offset:], pk.ServerPreferredMTUSize) + binary.BigEndian.PutUint16(b[26+offset:], pk.MTU) return b, nil } diff --git a/internal/message/open_connection_reply_2.go b/internal/message/open_connection_reply_2.go index 04dfe4d..aa5f2b5 100644 --- a/internal/message/open_connection_reply_2.go +++ b/internal/message/open_connection_reply_2.go @@ -9,8 +9,8 @@ import ( type OpenConnectionReply2 struct { ServerGUID int64 ClientAddress netip.AddrPort - MTUSize uint16 - Secure bool + MTU uint16 + DoSecurity bool } func (pk *OpenConnectionReply2) UnmarshalBinary(data []byte) error { @@ -21,8 +21,8 @@ func (pk *OpenConnectionReply2) UnmarshalBinary(data []byte) error { pk.ServerGUID = int64(binary.BigEndian.Uint64(data[16:])) pk.ClientAddress, _ = addr(data[24:]) offset := addrSize(data[24:]) - pk.MTUSize = binary.BigEndian.Uint16(data[24+offset:]) - pk.Secure = data[26+offset] != 0 + pk.MTU = binary.BigEndian.Uint16(data[24+offset:]) + pk.DoSecurity = data[26+offset] != 0 return nil } @@ -34,8 +34,8 @@ func (pk *OpenConnectionReply2) MarshalBinary() (data []byte, err error) { copy(b[1:], unconnectedMessageSequence[:]) binary.BigEndian.PutUint64(b[17:], uint64(pk.ServerGUID)) putAddr(b[25:], pk.ClientAddress) - binary.BigEndian.PutUint16(b[25+offset:], pk.MTUSize) - if pk.Secure { + binary.BigEndian.PutUint16(b[25+offset:], pk.MTU) + if pk.DoSecurity { b[27+offset] = 1 } return b, nil diff --git a/internal/message/open_connection_request_1.go b/internal/message/open_connection_request_1.go index d88028f..d367bd9 100644 --- a/internal/message/open_connection_request_1.go +++ b/internal/message/open_connection_request_1.go @@ -5,24 +5,24 @@ import ( ) type OpenConnectionRequest1 struct { - Protocol byte - MaximumSizeNotDropped uint16 + ClientProtocol byte + MTU uint16 } var cachedOCR1 = map[uint16][]byte{} func (pk *OpenConnectionRequest1) MarshalBinary() (data []byte, err error) { - if b, ok := cachedOCR1[pk.MaximumSizeNotDropped]; ok { + if b, ok := cachedOCR1[pk.MTU]; ok { // Cache OpenConnectionRequest1 data. These are independent of any other // inputs and are pretty big. return b, nil } - b := make([]byte, pk.MaximumSizeNotDropped-20-8) // IP Header: 20 bytes, UDP Header: 8 bytes. + b := make([]byte, pk.MTU-20-8) // IP Header: 20 bytes, UDP Header: 8 bytes. b[0] = IDOpenConnectionRequest1 copy(b[1:], unconnectedMessageSequence[:]) - b[17] = pk.Protocol + b[17] = pk.ClientProtocol - cachedOCR1[pk.MaximumSizeNotDropped] = b + cachedOCR1[pk.MTU] = b return b, nil } @@ -31,7 +31,7 @@ func (pk *OpenConnectionRequest1) UnmarshalBinary(data []byte) error { return io.ErrUnexpectedEOF } // Magic: 16 bytes. - pk.Protocol = data[16] - pk.MaximumSizeNotDropped = uint16(len(data) + 20 + 8 + 1) // Headers + packet ID. + pk.ClientProtocol = data[16] + pk.MTU = uint16(len(data) + 20 + 8 + 1) // Headers + packet ID. return nil } diff --git a/internal/message/open_connection_request_2.go b/internal/message/open_connection_request_2.go index ed28f1d..f9b0ce6 100644 --- a/internal/message/open_connection_request_2.go +++ b/internal/message/open_connection_request_2.go @@ -7,11 +7,11 @@ import ( ) type OpenConnectionRequest2 struct { - ServerAddress netip.AddrPort - ClientPreferredMTUSize uint16 - ClientGUID int64 - ServerHasSecurity bool - Cookie uint32 + ServerAddress netip.AddrPort + MTU uint16 + ClientGUID int64 + ServerHasSecurity bool + Cookie uint32 } func (pk *OpenConnectionRequest2) MarshalBinary() (data []byte, err error) { @@ -27,7 +27,7 @@ func (pk *OpenConnectionRequest2) MarshalBinary() (data []byte, err error) { binary.BigEndian.PutUint32(b[17:], pk.Cookie) } putAddr(b[17+cookieOffset:], pk.ServerAddress) - binary.BigEndian.PutUint16(b[17+offset+cookieOffset:], pk.ClientPreferredMTUSize) + binary.BigEndian.PutUint16(b[17+offset+cookieOffset:], pk.MTU) binary.BigEndian.PutUint64(b[19+offset+cookieOffset:], uint64(pk.ClientGUID)) return b, nil @@ -40,7 +40,7 @@ func (pk *OpenConnectionRequest2) UnmarshalBinary(data []byte) error { // Magic: 16 bytes. offset := addrSize(data[16:]) pk.ServerAddress, _ = addr(data[16:]) - pk.ClientPreferredMTUSize = binary.BigEndian.Uint16(data[16+offset:]) + pk.MTU = binary.BigEndian.Uint16(data[16+offset:]) pk.ClientGUID = int64(binary.BigEndian.Uint64(data[18+offset:])) return nil } diff --git a/internal/message/unconnected_ping.go b/internal/message/unconnected_ping.go index 2071f3a..4605dad 100644 --- a/internal/message/unconnected_ping.go +++ b/internal/message/unconnected_ping.go @@ -6,14 +6,14 @@ import ( ) type UnconnectedPing struct { - SendTimestamp int64 - ClientGUID int64 + PingTime int64 + ClientGUID int64 } func (pk *UnconnectedPing) MarshalBinary() (data []byte, err error) { b := make([]byte, 33) b[0] = IDUnconnectedPing - binary.BigEndian.PutUint64(b[1:], uint64(pk.SendTimestamp)) + binary.BigEndian.PutUint64(b[1:], uint64(pk.PingTime)) copy(b[9:], unconnectedMessageSequence[:]) binary.BigEndian.PutUint64(b[25:], uint64(pk.ClientGUID)) return b, nil @@ -23,7 +23,7 @@ func (pk *UnconnectedPing) UnmarshalBinary(data []byte) error { if len(data) < 32 { return io.ErrUnexpectedEOF } - pk.SendTimestamp = int64(binary.BigEndian.Uint64(data)) + pk.PingTime = int64(binary.BigEndian.Uint64(data)) // Magic: 16 bytes. pk.ClientGUID = int64(binary.BigEndian.Uint64(data[24:])) return nil diff --git a/internal/message/unconnected_pong.go b/internal/message/unconnected_pong.go index e69a76c..5d1495f 100644 --- a/internal/message/unconnected_pong.go +++ b/internal/message/unconnected_pong.go @@ -6,17 +6,17 @@ import ( ) type UnconnectedPong struct { - Magic [16]byte - SendTimestamp int64 - ServerGUID int64 - Data []byte + // PingTime is filled out using UnconnectedPing.PingTime. + PingTime int64 + ServerGUID int64 + Data []byte } func (pk *UnconnectedPong) UnmarshalBinary(data []byte) error { if len(data) < 34 || len(data) < 34+int(binary.BigEndian.Uint16(data[32:])) { return io.ErrUnexpectedEOF } - pk.SendTimestamp = int64(binary.BigEndian.Uint64(data)) + pk.PingTime = int64(binary.BigEndian.Uint64(data)) pk.ServerGUID = int64(binary.BigEndian.Uint64(data[8:])) // Magic: 16 bytes. n := binary.BigEndian.Uint16(data[32:]) @@ -27,7 +27,7 @@ func (pk *UnconnectedPong) UnmarshalBinary(data []byte) error { func (pk *UnconnectedPong) MarshalBinary() (data []byte, err error) { b := make([]byte, 35+len(pk.Data)) b[0] = IDUnconnectedPong - binary.BigEndian.PutUint64(b[1:], uint64(pk.SendTimestamp)) + binary.BigEndian.PutUint64(b[1:], uint64(pk.PingTime)) binary.BigEndian.PutUint64(b[9:], uint64(pk.ServerGUID)) copy(b[17:], unconnectedMessageSequence[:]) binary.BigEndian.PutUint16(b[33:], uint16(len(pk.Data)))