mirror of
https://github.com/EFForg/rayhunter.git
synced 2026-06-03 19:53:33 -07:00
fix packet reading and leftover bytes checks
This commit is contained in:
+1
-23
@@ -1,7 +1,7 @@
|
|||||||
//! Diag protocol serialization/deserialization
|
//! Diag protocol serialization/deserialization
|
||||||
|
|
||||||
use chrono::{DateTime, FixedOffset};
|
use chrono::{DateTime, FixedOffset};
|
||||||
use deku::{prelude::*, bitvec::{BitSlice, Msb0}};
|
use deku::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, DekuWrite)]
|
#[derive(Debug, Clone, DekuWrite)]
|
||||||
pub struct RequestContainer {
|
pub struct RequestContainer {
|
||||||
@@ -148,28 +148,6 @@ pub enum LogBody {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
item_struct = namedtuple('QcDiagLteRrcOtaPacket', 'rrc_rel_maj rrc_rel_min rbid pci earfcn sfn_subfn pdu_num len')
|
|
||||||
item_struct_v5 = namedtuple('QcDiagLteRrcOtaPacketV5', 'rrc_rel_maj rrc_rel_min rbid pci earfcn sfn_subfn pdu_num sib_mask len')
|
|
||||||
item_struct_v25 = namedtuple('QcDiagLteRrcOtaPacketV25', 'rrc_rel_maj rrc_rel_min nr_rrc_rel_maj nr_rrc_rel_min rbid pci earfcn sfn_subfn pdu_num sib_mask len')
|
|
||||||
if pkt_version >= 25:
|
|
||||||
# Version 25, 26, 27
|
|
||||||
item = item_struct_v25._make(struct.unpack('<BBBB BHLH BLH', pkt_body[1:21]))
|
|
||||||
msg_content = pkt_body[21:]
|
|
||||||
elif pkt_version >= 8:
|
|
||||||
# Version 8, 9, 12, 13, 15, 16, 19, 20, 22, 24
|
|
||||||
item = item_struct_v5._make(struct.unpack('<BB BHLH BLH', pkt_body[1:19]))
|
|
||||||
msg_content = pkt_body[19:]
|
|
||||||
elif pkt_version >= 5:
|
|
||||||
# Version 6, 7
|
|
||||||
item = item_struct_v5._make(struct.unpack('<BB BHHH BLH', pkt_body[1:17]))
|
|
||||||
msg_content = pkt_body[17:]
|
|
||||||
else:
|
|
||||||
# Version 2, 3, 4
|
|
||||||
item = item_struct._make(struct.unpack('<BB BHHH BH', pkt_body[1:13]))
|
|
||||||
msg_content = pkt_body[13:]
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, DekuRead)]
|
#[derive(Debug, Clone, PartialEq, DekuRead)]
|
||||||
#[deku(ctx = "ext_header_version: u8", id = "ext_header_version")]
|
#[deku(ctx = "ext_header_version: u8", id = "ext_header_version")]
|
||||||
pub enum LteRrcOtaPacket {
|
pub enum LteRrcOtaPacket {
|
||||||
|
|||||||
+16
-9
@@ -63,7 +63,7 @@ pub const LOG_CODES_FOR_RAW_PACKET_LOGGING: [u32; 11] = [
|
|||||||
log_codes::LOG_DATA_PROTOCOL_LOGGING_C // 0x11eb
|
log_codes::LOG_DATA_PROTOCOL_LOGGING_C // 0x11eb
|
||||||
];
|
];
|
||||||
|
|
||||||
const BUFFER_LEN: usize = 1024 * 1024 * 10;
|
const BUFFER_LEN: usize = 1024 * 10;
|
||||||
const MEMORY_DEVICE_MODE: i32 = 2;
|
const MEMORY_DEVICE_MODE: i32 = 2;
|
||||||
const DIAG_IOCTL_REMOTE_DEV: u32 = 32;
|
const DIAG_IOCTL_REMOTE_DEV: u32 = 32;
|
||||||
const DIAG_IOCTL_SWITCH_LOGGING: u32 = 7;
|
const DIAG_IOCTL_SWITCH_LOGGING: u32 = 7;
|
||||||
@@ -97,9 +97,9 @@ impl DiagDevice {
|
|||||||
for msg in container.messages {
|
for msg in container.messages {
|
||||||
match hdlc_decapsulate(&msg.data, &self.crc) {
|
match hdlc_decapsulate(&msg.data, &self.crc) {
|
||||||
Ok(data) => match Message::from_bytes((&data, 0)) {
|
Ok(data) => match Message::from_bytes((&data, 0)) {
|
||||||
Ok(((_, leftover_bytes), res)) => {
|
Ok(((leftover_bytes, _), res)) => {
|
||||||
if leftover_bytes > 0 {
|
if leftover_bytes.len() > 0 {
|
||||||
println!("warning: {} leftover bytes when parsing Message", leftover_bytes);
|
println!("warning: {} leftover bytes when parsing Message", leftover_bytes.len());
|
||||||
}
|
}
|
||||||
result.push(res);
|
result.push(res);
|
||||||
},
|
},
|
||||||
@@ -118,13 +118,20 @@ impl DiagDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_response(&mut self) -> DiagResult<Vec<Message>> {
|
pub fn read_response(&mut self) -> DiagResult<Vec<Message>> {
|
||||||
let mut buf = vec![0; BUFFER_LEN];
|
let mut packet_buf = vec![0; BUFFER_LEN];
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let _ = self.file.read(&mut buf)?;
|
let mut packet = vec![];
|
||||||
let ((_, leftover_bytes), res_container) = MessagesContainer::from_bytes((&buf, 0))?;
|
while !packet.ends_with(&[0x7e]) {
|
||||||
if leftover_bytes > 0 {
|
let bytes_read = self.file.read(&mut packet_buf)?;
|
||||||
println!("warning: {} leftover bytes when parsing ResponseContainer", leftover_bytes);
|
packet.extend(&packet_buf[0..bytes_read]);
|
||||||
|
// clear out the buffer so we don't accidentally read stale data
|
||||||
|
packet_buf.clear();
|
||||||
|
packet_buf.resize(BUFFER_LEN, 0);
|
||||||
|
}
|
||||||
|
let ((leftover_bytes, _), res_container) = MessagesContainer::from_bytes((&packet, 0))?;
|
||||||
|
if leftover_bytes.len() > 0 {
|
||||||
|
println!("warning: {} leftover bytes when parsing ResponseContainer", leftover_bytes.len());
|
||||||
}
|
}
|
||||||
if res_container.data_type == DataType::UserSpace {
|
if res_container.data_type == DataType::UserSpace {
|
||||||
return self.parse_response_container(res_container);
|
return self.parse_response_container(res_container);
|
||||||
|
|||||||
+1
-2
@@ -4,7 +4,7 @@
|
|||||||
//! https://github.com/P1sec/QCSuper/blob/master/docs/The%20Diag%20protocol.md#the-diag-protocol-over-usb
|
//! https://github.com/P1sec/QCSuper/blob/master/docs/The%20Diag%20protocol.md#the-diag-protocol-over-usb
|
||||||
|
|
||||||
use crc::Crc;
|
use crc::Crc;
|
||||||
use bytes::{Buf, BufMut};
|
use bytes::Buf;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Error, PartialEq)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
@@ -45,7 +45,6 @@ pub fn hdlc_encapsulate(data: &[u8], crc: &Crc<u16>) -> Vec<u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn hdlc_decapsulate(data: &[u8], crc: &Crc<u16>) -> Result<Vec<u8>, HdlcError> {
|
pub fn hdlc_decapsulate(data: &[u8], crc: &Crc<u16>) -> Result<Vec<u8>, HdlcError> {
|
||||||
// TODO: return errors instead of panicking
|
|
||||||
if data.len() < 3 {
|
if data.len() < 3 {
|
||||||
return Err(HdlcError::TooShort);
|
return Err(HdlcError::TooShort);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user