meshcoredecoder 0.2.3__py3-none-any.whl → 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cli.py +5 -2
- index.py +1 -1
- meshcoredecoder/__init__.py +1 -1
- meshcoredecoder/crypto/channel_crypto.py +18 -8
- meshcoredecoder/crypto/ed25519_verifier.py +14 -10
- meshcoredecoder/decoder/packet_decoder.py +137 -65
- meshcoredecoder/decoder/payload_decoders/__init__.py +3 -1
- meshcoredecoder/decoder/payload_decoders/ack.py +1 -1
- meshcoredecoder/decoder/payload_decoders/advert.py +1 -1
- meshcoredecoder/decoder/payload_decoders/anon_request.py +1 -1
- meshcoredecoder/decoder/payload_decoders/control.py +206 -0
- meshcoredecoder/decoder/payload_decoders/path.py +39 -19
- meshcoredecoder/decoder/payload_decoders/request.py +7 -1
- meshcoredecoder/decoder/payload_decoders/response.py +2 -1
- meshcoredecoder/decoder/payload_decoders/text_message.py +2 -1
- meshcoredecoder/types/enums.py +26 -18
- meshcoredecoder/types/packet.py +10 -2
- meshcoredecoder/types/payloads.py +32 -3
- meshcoredecoder/utils/enum_names.py +2 -0
- {meshcoredecoder-0.2.3.dist-info → meshcoredecoder-0.3.1.dist-info}/METADATA +78 -10
- meshcoredecoder-0.3.1.dist-info/RECORD +36 -0
- {meshcoredecoder-0.2.3.dist-info → meshcoredecoder-0.3.1.dist-info}/WHEEL +1 -1
- meshcoredecoder-0.2.3.dist-info/RECORD +0 -35
- {meshcoredecoder-0.2.3.dist-info → meshcoredecoder-0.3.1.dist-info}/entry_points.txt +0 -0
- {meshcoredecoder-0.2.3.dist-info → meshcoredecoder-0.3.1.dist-info}/licenses/LICENSE +0 -0
- {meshcoredecoder-0.2.3.dist-info → meshcoredecoder-0.3.1.dist-info}/top_level.txt +0 -0
cli.py
CHANGED
|
@@ -11,7 +11,7 @@ import sys
|
|
|
11
11
|
import json
|
|
12
12
|
import asyncio
|
|
13
13
|
from typing import List, Optional
|
|
14
|
-
from meshcoredecoder import MeshCoreDecoder
|
|
14
|
+
from meshcoredecoder import MeshCoreDecoder, __version__
|
|
15
15
|
from meshcoredecoder.crypto import MeshCoreKeyStore
|
|
16
16
|
from meshcoredecoder.types.crypto import DecryptionOptions
|
|
17
17
|
from meshcoredecoder.utils.enum_names import get_route_type_name, get_payload_type_name, get_device_role_name
|
|
@@ -36,6 +36,9 @@ def print_formatted_packet(packet, keys: Optional[List[str]] = None):
|
|
|
36
36
|
print(f'{bold("Payload Type:")} {get_payload_type_name(packet.payload_type)}')
|
|
37
37
|
print(f'{bold("Total Bytes:")} {packet.total_bytes}')
|
|
38
38
|
|
|
39
|
+
# Path comes from the decoder (which applies path-length fallback for repeater-style adverts)
|
|
40
|
+
if packet.path_length and packet.path_length > 0:
|
|
41
|
+
print(f'{bold("Path Length:")} {packet.path_length} byte(s)')
|
|
39
42
|
if packet.path and len(packet.path) > 0:
|
|
40
43
|
print(f'{bold("Path:")} {" → ".join(packet.path)}')
|
|
41
44
|
|
|
@@ -877,7 +880,7 @@ def main():
|
|
|
877
880
|
formatter_class=argparse.RawDescriptionHelpFormatter
|
|
878
881
|
)
|
|
879
882
|
|
|
880
|
-
parser.add_argument('--version', action='version', version='
|
|
883
|
+
parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}')
|
|
881
884
|
|
|
882
885
|
subparsers = parser.add_subparsers(dest='command', help='Commands')
|
|
883
886
|
|
index.py
CHANGED
meshcoredecoder/__init__.py
CHANGED
|
@@ -1147,20 +1147,30 @@ class ChannelCrypto:
|
|
|
1147
1147
|
if not decrypted_bytes or len(decrypted_bytes) < 2:
|
|
1148
1148
|
return DecryptionResult(success=False, error='Decrypted content too short')
|
|
1149
1149
|
|
|
1150
|
-
# Parse MeshCore path format
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1150
|
+
# Parse MeshCore path format (same encoding as packet header): path_len_byte(1) with
|
|
1151
|
+
# bits 7:6 = hash_size-1 (1/2/3 bytes per hop), bits 5:0 = hop count (0-63).
|
|
1152
|
+
# path_byte_len = hop_count * hash_size; then extra_type(1) + extra(variable).
|
|
1153
|
+
path_len_byte = decrypted_bytes[0]
|
|
1154
|
+
path_hash_size = (path_len_byte >> 6) + 1 # 1, 2, or 3; 4 = reserved
|
|
1155
|
+
path_hash_count = path_len_byte & 63
|
|
1156
|
+
path_byte_len = path_hash_count * path_hash_size
|
|
1157
|
+
|
|
1158
|
+
if path_hash_size == 4:
|
|
1159
|
+
return DecryptionResult(success=False, error='Reserved path hash size in Path payload')
|
|
1160
|
+
|
|
1161
|
+
if len(decrypted_bytes) < 1 + path_byte_len + 1:
|
|
1154
1162
|
return DecryptionResult(success=False, error='Decrypted content too short for path structure')
|
|
1155
1163
|
|
|
1156
|
-
path = decrypted_bytes[1:1 +
|
|
1157
|
-
extra_type = decrypted_bytes[1 +
|
|
1158
|
-
extra = decrypted_bytes[2 +
|
|
1164
|
+
path = decrypted_bytes[1:1 + path_byte_len]
|
|
1165
|
+
extra_type = decrypted_bytes[1 + path_byte_len]
|
|
1166
|
+
extra = decrypted_bytes[2 + path_byte_len:] if len(decrypted_bytes) > 2 + path_byte_len else b''
|
|
1159
1167
|
|
|
1160
1168
|
return DecryptionResult(
|
|
1161
1169
|
success=True,
|
|
1162
1170
|
data={
|
|
1163
|
-
'
|
|
1171
|
+
'path_len_byte': path_len_byte,
|
|
1172
|
+
'path_hash_size': path_hash_size,
|
|
1173
|
+
'path_hash_count': path_hash_count,
|
|
1164
1174
|
'path': path,
|
|
1165
1175
|
'extra_type': extra_type,
|
|
1166
1176
|
'extra': extra
|
|
@@ -34,24 +34,28 @@ class Ed25519SignatureVerifier:
|
|
|
34
34
|
app_data_hex: str
|
|
35
35
|
) -> bool:
|
|
36
36
|
"""
|
|
37
|
-
Verify an Ed25519 signature for MeshCore advertisement packets
|
|
37
|
+
Verify an Ed25519 signature for MeshCore advertisement packets.
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
Signed message is public_key + timestamp + app_data (docs/payloads.md).
|
|
40
|
+
We also try timestamp + public_key + app_data in case firmware varies.
|
|
41
41
|
"""
|
|
42
42
|
try:
|
|
43
|
-
# Convert hex strings to bytes
|
|
44
43
|
public_key = hex_to_bytes(public_key_hex)
|
|
45
44
|
signature = hex_to_bytes(signature_hex)
|
|
46
45
|
app_data = hex_to_bytes(app_data_hex)
|
|
46
|
+
timestamp_bytes = timestamp.to_bytes(4, byteorder='little')
|
|
47
47
|
|
|
48
|
-
#
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
# Primary: public_key + timestamp (LE) + app_data (per payloads.md)
|
|
49
|
+
message_pk_first = public_key + timestamp_bytes + app_data
|
|
50
|
+
if Ed25519SignatureVerifier._verify_signature(signature, message_pk_first, public_key):
|
|
51
|
+
return True
|
|
52
52
|
|
|
53
|
-
#
|
|
54
|
-
|
|
53
|
+
# Alternative: timestamp + public_key + app_data (in case firmware uses this order)
|
|
54
|
+
message_ts_first = timestamp_bytes + public_key + app_data
|
|
55
|
+
if Ed25519SignatureVerifier._verify_signature(signature, message_ts_first, public_key):
|
|
56
|
+
return True
|
|
57
|
+
|
|
58
|
+
return False
|
|
55
59
|
except Exception as error:
|
|
56
60
|
print(f'Ed25519 signature verification failed: {error}')
|
|
57
61
|
return False
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
Copyright (c) 2025 Michael Hart: https://github.com/michaelhart/meshcore-decoder
|
|
3
3
|
MIT License
|
|
4
4
|
|
|
5
|
-
Main packet decoder - orchestrates all payload decoders
|
|
5
|
+
Main packet decoder - orchestrates all payload decoders.
|
|
6
|
+
Packet layout: docs/packet_format.md. Payload formats: docs/payloads.md.
|
|
6
7
|
"""
|
|
7
8
|
|
|
8
9
|
from typing import Optional, Dict, Any, List, Tuple
|
|
@@ -23,6 +24,7 @@ from .payload_decoders.request import RequestPayloadDecoder
|
|
|
23
24
|
from .payload_decoders.response import ResponsePayloadDecoder
|
|
24
25
|
from .payload_decoders.anon_request import AnonRequestPayloadDecoder
|
|
25
26
|
from .payload_decoders.text_message import TextMessagePayloadDecoder
|
|
27
|
+
from .payload_decoders.control import ControlPayloadDecoder
|
|
26
28
|
|
|
27
29
|
|
|
28
30
|
class MeshCorePacketDecoder:
|
|
@@ -139,53 +141,81 @@ class MeshCorePacketDecoder:
|
|
|
139
141
|
transport_codes = (code1, code2)
|
|
140
142
|
|
|
141
143
|
if include_structure:
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
(bytes_data[offset + 2] << 16) |
|
|
145
|
-
(bytes_data[offset + 3] << 24))
|
|
144
|
+
transport_code_1 = bytes_data[offset] | (bytes_data[offset + 1] << 8)
|
|
145
|
+
transport_code_2 = bytes_data[offset + 2] | (bytes_data[offset + 3] << 8)
|
|
146
146
|
segments.append(PacketSegment(
|
|
147
|
-
name='Transport
|
|
148
|
-
description='
|
|
147
|
+
name='Transport Codes',
|
|
148
|
+
description='2× uint16: transport_code_1 (region scope), transport_code_2 (reserved). See packet_format.md.',
|
|
149
149
|
start_byte=offset,
|
|
150
150
|
end_byte=offset + 3,
|
|
151
|
-
value=f'0x{
|
|
151
|
+
value=f'0x{transport_code_1:04x} 0x{transport_code_2:04x}'
|
|
152
152
|
))
|
|
153
153
|
offset += 4
|
|
154
154
|
|
|
155
|
-
# Parse path
|
|
155
|
+
# Parse path (multi-byte path encoding per meshcore-decoder PR #8 / Packet.h)
|
|
156
|
+
# Bits 7:6 = hash size selector: (path_len_byte >> 6) + 1 = 1, 2, or 3 bytes per hop
|
|
157
|
+
# Bits 5:0 = hop count (0-63). path_byte_length = hop_count * hash_size
|
|
156
158
|
if len(bytes_data) < offset + 1:
|
|
157
159
|
raise ValueError('Packet too short for path length')
|
|
158
|
-
|
|
160
|
+
path_length_byte = bytes_data[offset]
|
|
161
|
+
path_hash_size, path_hop_count, path_byte_length = MeshCorePacketDecoder._decode_path_len_byte(path_length_byte)
|
|
162
|
+
|
|
163
|
+
payload_start = offset + 1
|
|
164
|
+
remaining_after_path = len(bytes_data) - payload_start
|
|
165
|
+
|
|
166
|
+
# Use legacy (raw path byte count) only when:
|
|
167
|
+
# - Reserved hash size (bits 7:6 = 11), or
|
|
168
|
+
# - Path would extend past packet (path_byte_length > remaining_after_path).
|
|
169
|
+
# Do NOT use min_payload to trigger legacy; valid new-encoding packets can have small payloads.
|
|
170
|
+
use_legacy = False
|
|
171
|
+
if path_hash_size == 4:
|
|
172
|
+
use_legacy = True
|
|
173
|
+
elif path_byte_length > remaining_after_path:
|
|
174
|
+
use_legacy = True
|
|
175
|
+
|
|
176
|
+
if use_legacy:
|
|
177
|
+
# Legacy: path_length byte = raw path byte count (1 byte per hop). Cap at packet and MAX_PATH_SIZE (64).
|
|
178
|
+
path_byte_length = min(path_length_byte, remaining_after_path, 64)
|
|
179
|
+
path_hop_count = path_byte_length
|
|
180
|
+
path_hash_size = 1
|
|
159
181
|
|
|
160
182
|
if include_structure:
|
|
161
|
-
if
|
|
162
|
-
|
|
183
|
+
hash_desc = f' × {path_hash_size}-byte hashes' if path_hash_size > 1 else ''
|
|
184
|
+
if path_hop_count == 0:
|
|
185
|
+
path_length_description = f'No path data{hash_desc}' if path_hash_size > 1 else 'No path data'
|
|
186
|
+
elif route_type in (RouteType.Direct, RouteType.TransportDirect):
|
|
187
|
+
path_length_description = f'{path_hop_count} hop(s){hash_desc} of routing instructions (decreases as packet travels)'
|
|
163
188
|
elif route_type in (RouteType.Flood, RouteType.TransportFlood):
|
|
164
|
-
path_length_description = f'{
|
|
189
|
+
path_length_description = f'{path_hop_count} hop(s){hash_desc} showing route taken (increases as packet floods)'
|
|
165
190
|
else:
|
|
166
|
-
path_length_description = f'Path
|
|
191
|
+
path_length_description = f'Path: {path_hop_count} hop(s){hash_desc}'
|
|
167
192
|
|
|
168
193
|
segments.append(PacketSegment(
|
|
169
194
|
name='Path Length',
|
|
170
195
|
description=path_length_description,
|
|
171
196
|
start_byte=offset,
|
|
172
197
|
end_byte=offset,
|
|
173
|
-
value=f'0x{
|
|
198
|
+
value=f'0x{path_length_byte:02x}'
|
|
174
199
|
))
|
|
175
200
|
offset += 1
|
|
176
201
|
|
|
177
|
-
if len(bytes_data) < offset +
|
|
202
|
+
if len(bytes_data) < offset + path_byte_length:
|
|
178
203
|
raise ValueError('Packet too short for path data')
|
|
179
204
|
|
|
180
|
-
# Convert path data to hex strings
|
|
181
|
-
|
|
182
|
-
|
|
205
|
+
# Convert path data to list of hex strings (one per hop, hash_size bytes each)
|
|
206
|
+
path: Optional[List[str]] = None
|
|
207
|
+
if path_hop_count > 0 and path_byte_length > 0:
|
|
208
|
+
path = []
|
|
209
|
+
for i in range(path_hop_count):
|
|
210
|
+
hop_start = offset + i * path_hash_size
|
|
211
|
+
hop_bytes = bytes_data[hop_start:hop_start + path_hash_size]
|
|
212
|
+
path.append(bytes_to_hex(hop_bytes))
|
|
183
213
|
|
|
184
|
-
if include_structure and
|
|
214
|
+
if include_structure and path_byte_length > 0:
|
|
185
215
|
if payload_type == PayloadType.Trace:
|
|
186
|
-
# TRACE packets
|
|
216
|
+
# TRACE packets: path holds SNR values (single-byte entries)
|
|
187
217
|
snr_values = []
|
|
188
|
-
for i in range(
|
|
218
|
+
for i in range(path_byte_length):
|
|
189
219
|
snr_raw = bytes_data[offset + i]
|
|
190
220
|
snr_signed = snr_raw - 256 if snr_raw > 127 else snr_raw
|
|
191
221
|
snr_db = snr_signed / 4.0
|
|
@@ -194,25 +224,24 @@ class MeshCorePacketDecoder:
|
|
|
194
224
|
name='Path SNR Data',
|
|
195
225
|
description=f'SNR values collected during trace: {", ".join(snr_values)}',
|
|
196
226
|
start_byte=offset,
|
|
197
|
-
end_byte=offset +
|
|
198
|
-
value=bytes_to_hex(bytes_data[offset:offset +
|
|
227
|
+
end_byte=offset + path_byte_length - 1,
|
|
228
|
+
value=bytes_to_hex(bytes_data[offset:offset + path_byte_length])
|
|
199
229
|
))
|
|
200
230
|
else:
|
|
231
|
+
path_description = f'Routing path ({path_hash_size}-byte hash per hop)' if path_hash_size > 1 else 'Routing path information'
|
|
201
232
|
if route_type in (RouteType.Direct, RouteType.TransportDirect):
|
|
202
|
-
path_description = 'Routing instructions (
|
|
233
|
+
path_description = f'Routing instructions ({path_hash_size}-byte hashes stripped at each hop)'
|
|
203
234
|
elif route_type in (RouteType.Flood, RouteType.TransportFlood):
|
|
204
|
-
path_description = 'Historical route taken (
|
|
205
|
-
else:
|
|
206
|
-
path_description = 'Routing path information'
|
|
235
|
+
path_description = f'Historical route taken ({path_hash_size}-byte hashes per hop)'
|
|
207
236
|
|
|
208
237
|
segments.append(PacketSegment(
|
|
209
238
|
name='Path Data',
|
|
210
239
|
description=path_description,
|
|
211
240
|
start_byte=offset,
|
|
212
|
-
end_byte=offset +
|
|
213
|
-
value=bytes_to_hex(bytes_data[offset:offset +
|
|
241
|
+
end_byte=offset + path_byte_length - 1,
|
|
242
|
+
value=bytes_to_hex(bytes_data[offset:offset + path_byte_length])
|
|
214
243
|
))
|
|
215
|
-
offset +=
|
|
244
|
+
offset += path_byte_length
|
|
216
245
|
|
|
217
246
|
# Extract payload
|
|
218
247
|
payload_bytes = bytes_data[offset:]
|
|
@@ -296,6 +325,14 @@ class MeshCorePacketDecoder:
|
|
|
296
325
|
decoder_options['include_segments'] = include_structure
|
|
297
326
|
decoder_options['segment_offset'] = 0
|
|
298
327
|
decoded_payload = TextMessagePayloadDecoder.decode(payload_bytes, options if options else decoder_options)
|
|
328
|
+
elif payload_type == PayloadType.Control:
|
|
329
|
+
result = ControlPayloadDecoder.decode(payload_bytes, {'include_segments': include_structure, 'segment_offset': 0})
|
|
330
|
+
decoded_payload = result
|
|
331
|
+
if result and hasattr(result, 'segments') and result.segments:
|
|
332
|
+
payload_segments.extend(result.segments)
|
|
333
|
+
elif payload_type in (PayloadType.Reserved0C, PayloadType.Reserved0D, PayloadType.Reserved0E):
|
|
334
|
+
# Reserved payload types: show raw only
|
|
335
|
+
decoded_payload = None
|
|
299
336
|
|
|
300
337
|
# If no segments were generated and we need structure, show basic payload info
|
|
301
338
|
if include_structure and len(payload_segments) == 0 and len(bytes_data) > offset:
|
|
@@ -307,8 +344,10 @@ class MeshCorePacketDecoder:
|
|
|
307
344
|
value=bytes_to_hex(payload_bytes)
|
|
308
345
|
))
|
|
309
346
|
|
|
310
|
-
# Calculate message hash
|
|
311
|
-
message_hash = MeshCorePacketDecoder._calculate_message_hash(
|
|
347
|
+
# Calculate message hash (use same path_byte_length we used for decoding)
|
|
348
|
+
message_hash = MeshCorePacketDecoder._calculate_message_hash(
|
|
349
|
+
bytes_data, route_type, payload_type, payload_version, path_byte_length_override=path_byte_length
|
|
350
|
+
)
|
|
312
351
|
|
|
313
352
|
packet = DecodedPacket(
|
|
314
353
|
message_hash=message_hash,
|
|
@@ -316,11 +355,13 @@ class MeshCorePacketDecoder:
|
|
|
316
355
|
payload_type=payload_type,
|
|
317
356
|
payload_version=payload_version,
|
|
318
357
|
transport_codes=transport_codes,
|
|
319
|
-
path_length=
|
|
358
|
+
path_length=path_hop_count,
|
|
320
359
|
path=path,
|
|
321
360
|
payload={'raw': payload_hex, 'decoded': decoded_payload},
|
|
322
361
|
total_bytes=len(bytes_data),
|
|
323
|
-
is_valid=True
|
|
362
|
+
is_valid=True,
|
|
363
|
+
path_byte_length=path_byte_length,
|
|
364
|
+
path_hash_size=path_hash_size if path_hash_size != 1 else None,
|
|
324
365
|
)
|
|
325
366
|
|
|
326
367
|
structure = PacketStructure(
|
|
@@ -379,7 +420,7 @@ class MeshCorePacketDecoder:
|
|
|
379
420
|
advert_payload = result['packet'].payload['decoded']
|
|
380
421
|
bytes_data = hex_to_bytes(hex_data)
|
|
381
422
|
|
|
382
|
-
# Calculate payload start offset
|
|
423
|
+
# Calculate payload start offset (use same path_length as decoded packet, which may use fallback)
|
|
383
424
|
offset = 1 # Skip header
|
|
384
425
|
|
|
385
426
|
# Skip transport codes if present
|
|
@@ -387,10 +428,9 @@ class MeshCorePacketDecoder:
|
|
|
387
428
|
if route_type in (RouteType.TransportFlood, RouteType.TransportDirect):
|
|
388
429
|
offset += 4
|
|
389
430
|
|
|
390
|
-
# Skip path data
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
offset += 1 + path_length
|
|
431
|
+
# Skip path data: use the packet's path_byte_length (total path bytes after path_len byte)
|
|
432
|
+
path_byte_length = getattr(result['packet'], 'path_byte_length', result['packet'].path_length) or 0
|
|
433
|
+
offset += 1 + path_byte_length
|
|
394
434
|
|
|
395
435
|
# Get the payload bytes
|
|
396
436
|
payload_bytes = bytes_data[offset:]
|
|
@@ -404,11 +444,8 @@ class MeshCorePacketDecoder:
|
|
|
404
444
|
if verified_advert:
|
|
405
445
|
# Update the payload with verification results
|
|
406
446
|
result['packet'].payload['decoded'] = verified_advert
|
|
407
|
-
|
|
408
|
-
#
|
|
409
|
-
if not verified_advert.is_valid:
|
|
410
|
-
result['packet'].is_valid = False
|
|
411
|
-
result['packet'].errors = verified_advert.errors or ['Invalid advertisement signature']
|
|
447
|
+
# Do not mark the whole packet invalid when only the advert signature fails;
|
|
448
|
+
# packet stays valid so decoded advert and signature_valid/errors are visible.
|
|
412
449
|
|
|
413
450
|
# Update structure segments if needed
|
|
414
451
|
if include_structure and hasattr(verified_advert, 'segments') and verified_advert.segments:
|
|
@@ -439,16 +476,18 @@ class MeshCorePacketDecoder:
|
|
|
439
476
|
errors.append('Packet too short for transport codes')
|
|
440
477
|
offset += 4
|
|
441
478
|
|
|
442
|
-
# Check path
|
|
479
|
+
# Check path (multi-byte encoding)
|
|
443
480
|
if len(bytes_data) < offset + 1:
|
|
444
481
|
errors.append('Packet too short for path length')
|
|
445
482
|
else:
|
|
446
|
-
|
|
483
|
+
path_len_byte = bytes_data[offset]
|
|
447
484
|
offset += 1
|
|
448
|
-
|
|
449
|
-
if
|
|
485
|
+
hash_size, hop_count, byte_length = MeshCorePacketDecoder._decode_path_len_byte(path_len_byte)
|
|
486
|
+
if hash_size == 4:
|
|
487
|
+
errors.append('Invalid path length byte: reserved hash size (bits 7:6 = 11)')
|
|
488
|
+
if len(bytes_data) < offset + byte_length:
|
|
450
489
|
errors.append('Packet too short for path data')
|
|
451
|
-
offset +=
|
|
490
|
+
offset += byte_length
|
|
452
491
|
|
|
453
492
|
# Check if we have payload data
|
|
454
493
|
if offset >= len(bytes_data):
|
|
@@ -460,20 +499,59 @@ class MeshCorePacketDecoder:
|
|
|
460
499
|
return ValidationResult(is_valid=len(errors) == 0, errors=errors if errors else None)
|
|
461
500
|
|
|
462
501
|
@staticmethod
|
|
463
|
-
def
|
|
502
|
+
def _decode_path_len_byte(path_len_byte: int) -> tuple:
|
|
503
|
+
"""
|
|
504
|
+
Decode path length byte per meshcore-decoder PR #8 / Packet.h.
|
|
505
|
+
Bits 7:6 = hash size selector: (path_len_byte >> 6) + 1 = 1, 2, or 3 bytes per hop.
|
|
506
|
+
Bits 5:0 = hop count (0-63).
|
|
507
|
+
Returns (hash_size, hop_count, byte_length). hash_size 4 = reserved.
|
|
508
|
+
"""
|
|
509
|
+
hash_size = (path_len_byte >> 6) + 1 # 1, 2, or 3; 4 if bits 7:6 = 11 (reserved)
|
|
510
|
+
hop_count = path_len_byte & 63
|
|
511
|
+
byte_length = hop_count * hash_size
|
|
512
|
+
return (hash_size, hop_count, byte_length)
|
|
513
|
+
|
|
514
|
+
@staticmethod
|
|
515
|
+
def _min_payload_for_type(payload_type: PayloadType) -> int:
|
|
516
|
+
"""Minimum payload size in bytes for the payload type (for path-length fallback)."""
|
|
517
|
+
mins = {
|
|
518
|
+
PayloadType.Advert: 101, # public_key(32) + timestamp(4) + signature(64) + flags(1)
|
|
519
|
+
PayloadType.AnonRequest: 35, # dest(1) + pubkey(32) + MAC(2)
|
|
520
|
+
PayloadType.Trace: 9,
|
|
521
|
+
PayloadType.Ack: 4,
|
|
522
|
+
PayloadType.Control: 1,
|
|
523
|
+
PayloadType.Request: 4,
|
|
524
|
+
PayloadType.Response: 4,
|
|
525
|
+
PayloadType.TextMessage: 4,
|
|
526
|
+
PayloadType.Path: 4,
|
|
527
|
+
PayloadType.GroupText: 3,
|
|
528
|
+
PayloadType.GroupData: 3,
|
|
529
|
+
}
|
|
530
|
+
return mins.get(payload_type, 0)
|
|
531
|
+
|
|
532
|
+
@staticmethod
|
|
533
|
+
def _calculate_message_hash(
|
|
534
|
+
bytes_data: bytes,
|
|
535
|
+
route_type: RouteType,
|
|
536
|
+
payload_type: PayloadType,
|
|
537
|
+
payload_version: PayloadVersion,
|
|
538
|
+
path_byte_length_override: Optional[int] = None,
|
|
539
|
+
) -> str:
|
|
464
540
|
"""Calculate message hash for a packet"""
|
|
541
|
+
def skip_path(off: int) -> int:
|
|
542
|
+
if path_byte_length_override is not None:
|
|
543
|
+
return off + 1 + path_byte_length_override
|
|
544
|
+
if len(bytes_data) <= off:
|
|
545
|
+
return off
|
|
546
|
+
_, _, path_byte_len = MeshCorePacketDecoder._decode_path_len_byte(bytes_data[off])
|
|
547
|
+
return off + 1 + path_byte_len
|
|
548
|
+
|
|
465
549
|
# For TRACE packets, use the trace tag as hash
|
|
466
550
|
if payload_type == PayloadType.Trace and len(bytes_data) >= 13:
|
|
467
551
|
offset = 1
|
|
468
|
-
|
|
469
|
-
# Skip transport codes if present
|
|
470
552
|
if route_type in (RouteType.TransportFlood, RouteType.TransportDirect):
|
|
471
553
|
offset += 4
|
|
472
|
-
|
|
473
|
-
# Skip path data
|
|
474
|
-
if len(bytes_data) > offset:
|
|
475
|
-
path_len = bytes_data[offset]
|
|
476
|
-
offset += 1 + path_len
|
|
554
|
+
offset = skip_path(offset)
|
|
477
555
|
|
|
478
556
|
# Extract trace tag
|
|
479
557
|
if len(bytes_data) >= offset + 4:
|
|
@@ -486,15 +564,9 @@ class MeshCorePacketDecoder:
|
|
|
486
564
|
# For other packets, create hash from constant parts
|
|
487
565
|
constant_header = (payload_type.value << 2) | (payload_version.value << 6)
|
|
488
566
|
offset = 1
|
|
489
|
-
|
|
490
|
-
# Skip transport codes if present
|
|
491
567
|
if route_type in (RouteType.TransportFlood, RouteType.TransportDirect):
|
|
492
568
|
offset += 4
|
|
493
|
-
|
|
494
|
-
# Skip path data
|
|
495
|
-
if len(bytes_data) > offset:
|
|
496
|
-
path_len = bytes_data[offset]
|
|
497
|
-
offset += 1 + path_len
|
|
569
|
+
offset = skip_path(offset)
|
|
498
570
|
|
|
499
571
|
payload_data = bytes_data[offset:]
|
|
500
572
|
hash_input = [constant_header] + list(payload_data)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Payload decoder modules"""
|
|
1
|
+
"""Payload decoder modules. See docs/payloads.md for formats."""
|
|
2
2
|
from .ack import AckPayloadDecoder
|
|
3
3
|
from .trace import TracePayloadDecoder
|
|
4
4
|
from .path import PathPayloadDecoder
|
|
@@ -9,6 +9,7 @@ from .request import RequestPayloadDecoder
|
|
|
9
9
|
from .response import ResponsePayloadDecoder
|
|
10
10
|
from .anon_request import AnonRequestPayloadDecoder
|
|
11
11
|
from .text_message import TextMessagePayloadDecoder
|
|
12
|
+
from .control import ControlPayloadDecoder
|
|
12
13
|
|
|
13
14
|
__all__ = [
|
|
14
15
|
'AckPayloadDecoder',
|
|
@@ -21,4 +22,5 @@ __all__ = [
|
|
|
21
22
|
'ResponsePayloadDecoder',
|
|
22
23
|
'AnonRequestPayloadDecoder',
|
|
23
24
|
'TextMessagePayloadDecoder',
|
|
25
|
+
'ControlPayloadDecoder',
|
|
24
26
|
]
|
|
@@ -23,7 +23,7 @@ class AckPayloadDecoder:
|
|
|
23
23
|
options = {}
|
|
24
24
|
|
|
25
25
|
try:
|
|
26
|
-
#
|
|
26
|
+
# docs/payloads.md: Ack = checksum(4), CRC of message timestamp, text, and sender pubkey.
|
|
27
27
|
# - checksum (4 bytes) - CRC checksum of message timestamp, text, and sender pubkey
|
|
28
28
|
|
|
29
29
|
if len(payload) < 4:
|
|
@@ -53,7 +53,7 @@ class AdvertPayloadDecoder:
|
|
|
53
53
|
segment_offset = options.get('segment_offset', 0)
|
|
54
54
|
current_offset = 0
|
|
55
55
|
|
|
56
|
-
#
|
|
56
|
+
# docs/payloads.md: Advert = public_key(32) + timestamp(4) + signature(64) + appdata (flags + optional lat/long, feature1/2, name).
|
|
57
57
|
public_key = bytes_to_hex(payload[current_offset:current_offset + 32])
|
|
58
58
|
if options.get('include_segments'):
|
|
59
59
|
segments.append(PayloadSegment(
|
|
@@ -38,7 +38,7 @@ class AnonRequestPayloadDecoder:
|
|
|
38
38
|
)
|
|
39
39
|
|
|
40
40
|
try:
|
|
41
|
-
#
|
|
41
|
+
# docs/payloads.md: AnonRequest = dest_hash(1) + public_key(32) + cipher MAC(2) + ciphertext.
|
|
42
42
|
# - destination_hash (1 byte)
|
|
43
43
|
# - sender_public_key (32 bytes)
|
|
44
44
|
# - cipher_mac (2 bytes)
|