Struct CheckedPacket

Source
pub struct CheckedPacket { /* private fields */ }

Implementations§

Source§

impl CheckedPacket

Source

pub fn assume_checked(unchecked: UncheckedPacket) -> Self

If you know the packet isn’t corrupted, this function bypasses the checksum verification.

Source

pub fn invalidate_check(self) -> UncheckedPacket

If you intend to mutate the packet’s internals, you must first convert it to an unchecked packet so it isn’t marked as checked.

Source

pub fn empty() -> Self

The empty packet is used when you get a packet which you just don’t understand. Replying an empty packet means “I don’t support this feature”.

let mut encoded = Vec::new();
CheckedPacket::empty().encode(&mut encoded);
assert_eq!(encoded, b"$#00")
Source

pub fn from_data(kind: Kind, data: Vec<u8>) -> Self

Creates a packet from the inputted binary data, and generates the checksum from it.

assert_eq!(
    CheckedPacket::from_data(Kind::Packet, b"Hello, World!".to_vec()).invalidate_check(),
    UncheckedPacket {
        kind: Kind::Packet,
        data: b"Hello, World!".to_vec(),
        checksum: *b"69"
    },
)
Examples found in repository?
examples/repl.rs (line 23)
4fn main() -> Result<(), Error> {
5    println!("Listening on port 1337...");
6    let mut server = GdbServer::listen("0.0.0.0:1337")?;
7    println!("Connected!");
8
9    while let Some(packet) = server.next_packet()? {
10        println!(
11            "-> {:?} {:?}",
12            packet.kind,
13            std::str::from_utf8(&packet.data)
14        );
15
16        print!(": ");
17        io::stdout().flush()?;
18        let mut response = String::new();
19        io::stdin().read_line(&mut response)?;
20        if response.ends_with('\n') {
21            response.truncate(response.len() - 1);
22        }
23        let response = CheckedPacket::from_data(Kind::Packet, response.into_bytes());
24
25        let mut bytes = Vec::new();
26        response.encode(&mut bytes).unwrap();
27        println!("<- {:?}", std::str::from_utf8(&bytes));
28
29        server.dispatch(&response)?;
30    }
31
32    println!("EOF");
33    Ok(())
34}

Methods from Deref<Target = UncheckedPacket>§

Source

pub fn expected_checksum(&self) -> Result<u8, Error>

Return the integer parsed from the hexadecimal expected checksum.

let packet = UncheckedPacket {
    kind: Kind::Packet,
    data: b"Hello, World!".to_vec(),
    checksum: *b"BA",
};
assert_eq!(packet.expected_checksum().unwrap(), 186);
Source

pub fn actual_checksum(&self) -> u8

Return the actual checksum, derived from the data.

let packet = UncheckedPacket {
    kind: Kind::Packet,
    data: b"Hello, World!".to_vec(),
    checksum: *b"BA",
};
assert_eq!(packet.actual_checksum(), 105);

As per the GDB specification, this is currently a sum of all characters, modulo 256. The same result can be compared with

(input.bytes().map(|x| usize::from(x)).sum::<usize>() % 256) as u8

however, this function is more efficient and won’t go out of bounds.

Source

pub fn is_valid(&self) -> bool

Returns true if the checksums match.

assert_eq!(UncheckedPacket {
    kind: Kind::Packet,
    data: b"Rust is an amazing programming language".to_vec(),
    checksum: *b"00",
}.is_valid(), false);
assert_eq!(UncheckedPacket {
    kind: Kind::Packet,
    data: b"Rust is an amazing programming language".to_vec(),
    checksum: *b"ZZ",
}.is_valid(), false);
assert_eq!(UncheckedPacket {
    kind: Kind::Packet,
    data: b"Rust is an amazing programming language".to_vec(),
    checksum: *b"C7",
}.is_valid(), true);
Source

pub fn encode<W>(&self, w: &mut W) -> Result<()>
where W: Write,

Encode the packet into a long binary string, written to a writer of choice. You can receive a Vec by taking advantage of the fact that they implement io::Write:

let mut encoded = Vec::new();
UncheckedPacket {
    kind: Kind::Packet,
    data: b"these must be escaped: # $ } *".to_vec(),
    checksum: *b"00",
}.encode(&mut encoded);
assert_eq!(
    encoded,
    b"$these must be escaped: }\x03 }\x04 }] }\x0a#00".to_vec()
);

Currently multiple series repeated characters aren’t shortened, however, this may change at any time and you should not rely on the output of this function being exactly one of multiple representations.

Examples found in repository?
examples/repl.rs (line 26)
4fn main() -> Result<(), Error> {
5    println!("Listening on port 1337...");
6    let mut server = GdbServer::listen("0.0.0.0:1337")?;
7    println!("Connected!");
8
9    while let Some(packet) = server.next_packet()? {
10        println!(
11            "-> {:?} {:?}",
12            packet.kind,
13            std::str::from_utf8(&packet.data)
14        );
15
16        print!(": ");
17        io::stdout().flush()?;
18        let mut response = String::new();
19        io::stdin().read_line(&mut response)?;
20        if response.ends_with('\n') {
21            response.truncate(response.len() - 1);
22        }
23        let response = CheckedPacket::from_data(Kind::Packet, response.into_bytes());
24
25        let mut bytes = Vec::new();
26        response.encode(&mut bytes).unwrap();
27        println!("<- {:?}", std::str::from_utf8(&bytes));
28
29        server.dispatch(&response)?;
30    }
31
32    println!("EOF");
33    Ok(())
34}

Trait Implementations§

Source§

impl Clone for CheckedPacket

Source§

fn clone(&self) -> CheckedPacket

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CheckedPacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for CheckedPacket

Source§

type Target = UncheckedPacket

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl PartialEq for CheckedPacket

Source§

fn eq(&self, other: &CheckedPacket) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for CheckedPacket

Source§

impl StructuralPartialEq for CheckedPacket

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.