Skip to content

Commit 521a97c

Browse files
committed
Seed CRC with a session ID to prevent session cross-talk.
1 parent 70ec569 commit 521a97c

12 files changed

Lines changed: 127 additions & 71 deletions

Common/CRC32/pabb_CRC32_AVR8.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ extern const PROGMEM uint32_t PABB_CRC32_TABLE8[];
2020

2121
void pabb_crc32_buffer(uint32_t* crc, const void* data, uint8_t length);
2222

23-
static inline void pabb_crc32_write_to_message(void* data, size_t full_message_length){
23+
static inline void pabb_crc32_write_to_message(uint32_t seed, void* data, size_t full_message_length){
2424
char* ptr = (char*)data;
2525
size_t length_before_crc = full_message_length - sizeof(uint32_t);
2626
uint32_t* crc = (uint32_t*)(ptr + length_before_crc);
27-
*crc = 0xffffffff;
27+
*crc = seed;
2828
pabb_crc32_buffer(crc, ptr, length_before_crc);
2929
}
3030

Common/CRC32/pabb_CRC32_Basic.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ extern const uint32_t PABB_CRC32_TABLE8[];
2020

2121
void pabb_crc32_buffer(uint32_t* crc, const void* data, uint8_t length);
2222

23-
static inline void pabb_crc32_write_to_message(void* data, size_t full_message_length){
23+
static inline void pabb_crc32_write_to_message(uint32_t crc, void* data, size_t full_message_length){
2424
char* ptr = (char*)data;
2525
size_t length_before_crc = full_message_length - sizeof(uint32_t);
26-
uint32_t crc = 0xffffffff;
2726
pabb_crc32_buffer(&crc, ptr, length_before_crc);
2827
memcpy(ptr + length_before_crc, &crc, sizeof(uint32_t));
2928
}

Common/CRC32/pabb_CRC32_x86_SSE4.1.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ static inline void pabb_crc32_buffer(uint32_t* crc, const void* data, size_t len
2626
*crc = tmp;
2727
}
2828

29-
static inline void pabb_crc32_write_to_message(void* data, size_t full_message_length){
29+
static inline void pabb_crc32_write_to_message(uint32_t crc, void* data, size_t full_message_length){
3030
char* ptr = (char*)data;
3131
size_t length_before_crc = full_message_length - sizeof(uint32_t);
32-
uint32_t crc = 0xffffffff;
3332
pabb_crc32_buffer(&crc, ptr, length_before_crc);
3433
memcpy(ptr + length_before_crc, &crc, sizeof(uint32_t));
3534
}

Common/PABotBase2/PABotBase2_MessageProtocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace PABotBase2{
2323

2424

2525

26-
#define PABB2_MESSAGE_PROTOCOL_VERSION 2026041105
26+
#define PABB2_MESSAGE_PROTOCOL_VERSION 2026041106
2727

2828

2929
struct PABB_PACK MessageHeader{

Common/PABotBase2/ReliableConnectionLayer/PABotBase2CC_ReliableStreamConnection.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Common/Cpp/PrettyPrint.h"
99
//#include "Common/Cpp/Exceptions.h"
1010
#include "Common/PABotBase2/PABotBase2CC_MessageDumper.h"
11+
#include "CommonTools/Random.h"
1112
//#include "PABotBase2_ConnectionDebug.h"
1213
#include "PABotBase2CC_ReliableStreamConnection.h"
1314

@@ -32,7 +33,7 @@ ReliableStreamConnection::ReliableStreamConnection(
3233
, m_unreliable_connection(unreliable_connection)
3334
, m_retransmit_timeout(retransmit_timeout)
3435
, m_print_lock(print_lock)
35-
, m_reliable_sender(*this, 20)
36+
, m_reliable_sender(*this, 24, random_u32())
3637
, m_log_everything(log_everything)
3738
// , m_version_verified(false)
3839
, m_remote_protocol_compatible(false)
@@ -123,7 +124,11 @@ void ReliableStreamConnection::on_recv(const void* data, size_t bytes){
123124
cout << "ReliableStreamConnection::on_recv(): " << bytes << endl;
124125
}
125126
#endif
126-
m_parser.push_bytes(*this, (const uint8_t*)data, bytes);
127+
m_parser.push_bytes(
128+
*this,
129+
m_reliable_sender.session_id(),
130+
(const uint8_t*)data, bytes
131+
);
127132
}
128133

129134

@@ -168,11 +173,11 @@ size_t ReliableStreamConnection::unreliable_send(const void* data, size_t bytes)
168173
bool ReliableStreamConnection::reset(WallDuration timeout){
169174
{
170175
std::lock_guard<Mutex> lg(m_lock);
171-
m_reliable_sender.reset();
176+
m_reliable_sender.reset(m_reliable_sender.session_id() + 1);
172177
m_parser.reset();
173178
m_stream_coalescer.reset();
174179
throw_if_cancelled();
175-
m_reliable_sender.send_packet(PABB2_CONNECTION_OPCODE_ASK_RESET, 0, nullptr);
180+
m_reliable_sender.send_reset();
176181
}
177182
m_cv.notify_all();
178183
return wait_for_pending(timeout);
@@ -217,7 +222,7 @@ void ReliableStreamConnection::send_ack(uint8_t seqnum, uint8_t opcode){
217222
packet.header.seqnum = seqnum;
218223
packet.header.packet_bytes = sizeof(packet);
219224
packet.header.opcode = opcode;
220-
pabb_crc32_write_to_message(&packet, sizeof(packet));
225+
pabb_crc32_write_to_message(m_reliable_sender.session_id(), &packet, sizeof(packet));
221226
unreliable_send(&packet, sizeof(packet));
222227
}
223228
void ReliableStreamConnection::send_ack_u16(uint8_t seqnum, uint8_t opcode, uint16_t data){
@@ -231,7 +236,7 @@ void ReliableStreamConnection::send_ack_u16(uint8_t seqnum, uint8_t opcode, uint
231236
packet.header.packet_bytes = sizeof(packet);
232237
packet.header.opcode = opcode;
233238
packet.header.data = data;
234-
pabb_crc32_write_to_message(&packet, sizeof(packet));
239+
pabb_crc32_write_to_message(m_reliable_sender.session_id(), &packet, sizeof(packet));
235240
unreliable_send(&packet, sizeof(packet));
236241
}
237242

@@ -376,6 +381,7 @@ void ReliableStreamConnection::on_packet(const PacketHeader* packet){
376381
case PABB2_CONNECTION_OPCODE_INFO_LABEL_H32:
377382
case PABB2_CONNECTION_OPCODE_INFO_LABEL_U32:
378383
case PABB2_CONNECTION_OPCODE_INFO_LABEL_I32:
384+
// case PABB2_CONNECTION_OPCODE_WRONG_SESSION:
379385
// cout << "Received ack" << endl;
380386
if (!m_log_everything){
381387
m_logger.log("[RSC]: Receive: (0x" + tostr_hex(packet->opcode) + ") " + tostr(packet), COLOR_PURPLE);

Common/PABotBase2/ReliableConnectionLayer/PABotBase2FW_ReliableStreamConnection.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ bool ReliableStreamConnectionFW::run_recv_events(const WallDuration& timeout){
113113
? POLL_RATE
114114
: timeout;
115115

116-
const PacketHeader* header = m_parser.pull_bytes(m_unreliable_connection, adjusted_timeout);
116+
const PacketHeader* header = m_parser.pull_bytes(
117+
m_unreliable_connection,
118+
m_reliable_sender.session_id(),
119+
adjusted_timeout
120+
);
117121
if (header == nullptr){
118122
return false;
119123
}
@@ -149,22 +153,33 @@ bool ReliableStreamConnectionFW::run_recv_events(const WallDuration& timeout){
149153

150154
// Now handle the different opcodes.
151155
uint8_t opcode = header->opcode & PABB2_CONNECTION_OPCODE_MASK;
156+
152157
switch (opcode){
153-
case PABB2_CONNECTION_OPCODE_ASK_RESET:
158+
case PABB2_CONNECTION_OPCODE_ASK_RESET:{
159+
if (header->packet_bytes < sizeof(PacketHeader_u32)){
160+
return true;
161+
}
162+
163+
const PacketHeader_u32* packet = (const PacketHeader_u32*)header;
164+
165+
#ifdef PABB2_SUPPORTS_PRINTF_LOGGING
166+
printf("Resetting to session ID: %zx\n", (size_t)packet->data);
167+
#endif
154168
m_stream_ready = false;
155169
m_send_is_currently_full = false;
156-
m_reliable_sender.reset();
170+
m_reliable_sender.reset(packet->data);
157171
m_parser.reset();
158172
m_stream_coalescer.reset();
159173
m_stream_coalescer.push_packet(0);
174+
#ifdef PABB2_ENABLE
175+
issue_reset_to_all();
176+
#endif
160177
m_reliable_sender.send_oob_packet_empty(
161178
header->seqnum,
162179
PABB2_CONNECTION_OPCODE_RET_RESET
163180
);
164-
#ifdef PABB2_ENABLE
165-
issue_reset_to_all();
166-
#endif
167181
return true;
182+
}
168183
case PABB2_CONNECTION_OPCODE_ASK_VERSION:
169184
m_stream_coalescer.push_packet(header->seqnum);
170185
m_reliable_sender.send_oob_packet_u32(

Common/PABotBase2/ReliableConnectionLayer/PABotBase2_PacketParser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace PABotBase2{
2525

2626
const PacketHeader* PacketParser::pull_bytes(
2727
UnreliableStreamConnectionPolling& connection,
28+
const uint32_t& session_id,
2829
WallDuration timeout
2930
){
3031
const uint8_t MIN_PACKET_SIZE = sizeof(PacketHeader) + sizeof(uint32_t);
@@ -114,7 +115,9 @@ const PacketHeader* PacketParser::pull_bytes(
114115

115116
// Verify the CRC.
116117

117-
uint32_t actual_crc = 0xffffffff;
118+
uint32_t actual_crc = header->opcode == PABB2_CONNECTION_OPCODE_ASK_RESET
119+
? 0xffffffff
120+
: session_id;
118121
pabb_crc32_buffer(&actual_crc, m_buffer, packet_bytes - sizeof(uint32_t));
119122

120123
uint32_t expected_crc;
@@ -137,6 +140,7 @@ const PacketHeader* PacketParser::pull_bytes(
137140

138141
void PacketParser::push_bytes(
139142
PacketRunner& packet_runner,
143+
const uint32_t& session_id,
140144
const uint8_t* data, size_t bytes
141145
){
142146
// cout << std::string((const char*)data, bytes) << endl;
@@ -206,7 +210,9 @@ void PacketParser::push_bytes(
206210

207211
// Verify the CRC.
208212

209-
uint32_t actual_crc = 0xffffffff;
213+
uint32_t actual_crc = header->opcode == PABB2_CONNECTION_OPCODE_ASK_RESET
214+
? 0xffffffff
215+
: session_id;
210216
pabb_crc32_buffer(&actual_crc, m_buffer, packet_bytes - sizeof(uint32_t));
211217

212218
uint32_t expected_crc;

Common/PABotBase2/ReliableConnectionLayer/PABotBase2_PacketParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct PacketParser{
6262
//
6363
const PacketHeader* pull_bytes(
6464
UnreliableStreamConnectionPolling& connection,
65+
const uint32_t& session_id,
6566
WallDuration timeout
6667
);
6768

@@ -72,6 +73,7 @@ struct PacketParser{
7273
//
7374
void push_bytes(
7475
PacketRunner& packet_runner,
76+
const uint32_t& session_id,
7577
const uint8_t* data, size_t bytes
7678
);
7779

Common/PABotBase2/ReliableConnectionLayer/PABotBase2_PacketProtocol.h

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,35 @@ namespace PABotBase2{
3232
//
3333

3434
#define PABB2_CONNECTION_MAGIC_NUMBER 0x81
35-
#define PABB2_CONNECTION_PROTOCOL_VERSION 2026041102
35+
#define PABB2_CONNECTION_PROTOCOL_VERSION 2026050100
3636

3737

3838
#define PABB2_CONNECTION_RETRANSMIT_FLAG 0x80
3939
#define PABB2_CONNECTION_OPCODE_MASK 0x7f
4040

41+
#define PABB2_CONNECTION_OPCODE_INVALID 0x00
42+
4143

4244
//
43-
// Special
45+
// Packets
4446
//
4547

48+
using SessionId = uint32_t;
49+
4650
struct PABB_PACK PacketHeader{
4751
uint8_t magic_number;
4852
uint8_t seqnum;
4953
uint8_t packet_bytes;
5054
uint8_t opcode;
5155
};
5256

53-
#define PABB2_CONNECTION_OPCODE_INVALID 0x00
54-
55-
#define PABB2_CONNECTION_OPCODE_ASK_RESET 0x01
56-
#define PABB2_CONNECTION_OPCODE_RET_RESET 0x41
57-
58-
59-
struct PABB_PACK PacketHeader_u8{
60-
uint8_t magic_number;
61-
uint8_t seqnum;
62-
uint8_t packet_bytes;
63-
uint8_t opcode;
57+
struct PABB_PACK PacketHeader_u8 : PacketHeader{
6458
uint8_t data;
6559
};
66-
struct PABB_PACK PacketHeader_u16{
67-
uint8_t magic_number;
68-
uint8_t seqnum;
69-
uint8_t packet_bytes;
70-
uint8_t opcode;
60+
struct PABB_PACK PacketHeader_u16 : PacketHeader{
7161
uint16_t data;
7262
};
73-
struct PABB_PACK PacketHeader_u32{
74-
uint8_t magic_number;
75-
uint8_t seqnum;
76-
uint8_t packet_bytes;
77-
uint8_t opcode;
63+
struct PABB_PACK PacketHeader_u32 : PacketHeader{
7864
uint32_t data;
7965
};
8066

@@ -84,6 +70,9 @@ struct PABB_PACK PacketHeader_u32{
8470
// Requests (acks required)
8571
//
8672

73+
#define PABB2_CONNECTION_OPCODE_ASK_RESET 0x01
74+
#define PABB2_CONNECTION_OPCODE_RET_RESET 0x41
75+
8776
#define PABB2_CONNECTION_OPCODE_ASK_VERSION 0x02
8877
#define PABB2_CONNECTION_OPCODE_RET_VERSION 0x42
8978

@@ -99,11 +88,7 @@ struct PABB_PACK PacketHeader_u32{
9988
#define PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA 0x12
10089
#define PABB2_CONNECTION_OPCODE_RET_STREAM_DATA 0x52
10190
#define PABB2_CONNECTION_OPCODE_ASK_STREAM_REQUEST 0x13 // Unused for now.
102-
struct PABB_PACK PacketHeaderData{
103-
uint8_t magic_number;
104-
uint8_t seqnum;
105-
uint8_t packet_bytes;
106-
uint8_t opcode;
91+
struct PABB_PACK PacketHeaderData : PacketHeader{
10792
uint16_t stream_offset;
10893
};
10994

@@ -135,7 +120,6 @@ struct PABB_PACK PacketHeaderData{
135120
#define PABB2_CONNECTION_OPCODE_UNKNOWN_OPCODE 0x32
136121

137122

138-
139123
}
140124
}
141125

0 commit comments

Comments
 (0)