Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error replies, improve header marshalling, marshal all header fields #118

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example_keywallet/src/bin/service/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn default_handler(
enum ObjectType<'a> {
Collection(&'a str),
Item { col: &'a str, item: &'a str },
Session(&'a str),
Session(#[allow(dead_code)] &'a str),
}

fn get_object_type_and_id<'a>(path: &'a ObjectPath<&'a str>) -> Option<ObjectType<'a>> {
Expand Down
2 changes: 1 addition & 1 deletion rustbus/src/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl DynamicHeader {
error_msg: Option<String>,
) -> crate::message_builder::MarshalledMessage {
let mut err_resp = crate::message_builder::MarshalledMessage {
typ: MessageType::Reply,
typ: MessageType::Error,
dynheader: DynamicHeader {
interface: None,
member: None,
Expand Down
203 changes: 103 additions & 100 deletions rustbus/src/wire/marshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::num::NonZeroU32;

use crate::message_builder;
use crate::params;
use crate::wire::HeaderField;
use crate::ByteOrder;

use crate::wire::util::*;
Expand Down Expand Up @@ -92,125 +91,129 @@ fn marshal_header(
let pos = buf.len();
buf.extend_from_slice(&[0, 0, 0, 0]);

if let Some(serial) = &msg.dynheader.response_serial {
marshal_header_field(byteorder, &HeaderField::ReplySerial(*serial), buf)?;
if let Some(serial) = msg.dynheader.response_serial {
marshal_header_reply_serial(byteorder, serial, buf)?;
}
if let Some(int) = &msg.dynheader.interface {
marshal_header_field(byteorder, &HeaderField::Interface(int.clone()), buf)?;
marshal_header_interface(byteorder, int, buf)?;
}
if let Some(dest) = &msg.dynheader.destination {
marshal_header_field(byteorder, &HeaderField::Destination(dest.clone()), buf)?;
marshal_header_destination(byteorder, dest, buf)?;
}
if let Some(sender) = &msg.dynheader.sender {
marshal_header_sender(byteorder, sender, buf)?;
}
if let Some(mem) = &msg.dynheader.member {
marshal_header_field(byteorder, &HeaderField::Member(mem.clone()), buf)?;
marshal_header_member(byteorder, mem, buf)?;
}
if let Some(obj) = &msg.dynheader.object {
marshal_header_field(byteorder, &HeaderField::Path(obj.clone()), buf)?;
marshal_header_path(byteorder, obj, buf)?;
}
if !msg.body.get_fds().is_empty() {
marshal_header_field(
byteorder,
&HeaderField::UnixFds(msg.body.get_fds().len() as u32),
buf,
)?;
if let Some(err_name) = &msg.dynheader.error_name {
marshal_header_errorname(byteorder, err_name, buf)?;
}

if !msg.get_buf().is_empty() {
let sig_str = msg.get_sig().to_owned();
marshal_header_field(byteorder, &HeaderField::Signature(sig_str), buf)?;
marshal_header_signature(msg.get_sig(), buf)?;
}
if !msg.body.get_fds().is_empty() {
marshal_header_unix_fds(byteorder, msg.body.get_fds().len() as u32, buf)?;
}
let len = buf.len() - pos - 4; // -4 the bytes for the length indicator do not count
insert_u32(byteorder, len as u32, &mut buf[pos..pos + 4]);

Ok(())
}

fn marshal_header_field(
fn marshal_header_field(field_no: u8, sig: &str, buf: &mut Vec<u8>) {
pad_to_align(8, buf);
buf.push(field_no);
buf.push(sig.len() as u8);
buf.extend_from_slice(sig.as_bytes());
buf.push(0);
pad_to_align(4, buf);
}

fn marshal_header_path(byteorder: ByteOrder, path: &str, buf: &mut Vec<u8>) -> MarshalResult<()> {
params::validate_object_path(path)?;
marshal_header_field(1, "o", buf);
write_string(path, byteorder, buf);
Ok(())
}

fn marshal_header_interface(
byteorder: ByteOrder,
field: &HeaderField,
interface: &str,
buf: &mut Vec<u8>,
) -> MarshalResult<()> {
pad_to_align(8, buf);
match field {
HeaderField::Path(path) => {
params::validate_object_path(path)?;
buf.push(1);
buf.push(1);
buf.push(b'o');
buf.push(0);
pad_to_align(4, buf);
write_string(path, byteorder, buf);
}
HeaderField::Interface(int) => {
params::validate_interface(int)?;
buf.push(2);
buf.push(1);
buf.push(b's');
buf.push(0);
pad_to_align(4, buf);
write_string(int, byteorder, buf);
}
HeaderField::Member(mem) => {
params::validate_membername(mem)?;
buf.push(3);
buf.push(1);
buf.push(b's');
buf.push(0);
pad_to_align(4, buf);
write_string(mem, byteorder, buf);
}
HeaderField::ErrorName(name) => {
params::validate_errorname(name)?;
buf.push(4);
buf.push(1);
buf.push(b's');
buf.push(0);
pad_to_align(4, buf);
write_string(name, byteorder, buf);
}
HeaderField::ReplySerial(rs) => {
buf.push(5);
buf.push(1);
buf.push(b'u');
buf.push(0);
pad_to_align(4, buf);
write_u32(rs.get(), byteorder, buf);
}
HeaderField::Destination(dest) => {
params::validate_busname(dest)?;
buf.push(6);
buf.push(1);
buf.push(b's');
buf.push(0);
pad_to_align(4, buf);
write_string(dest, byteorder, buf);
}
HeaderField::Sender(snd) => {
params::validate_busname(snd)?;
buf.push(7);
buf.push(1);
buf.push(b's');
buf.push(0);
pad_to_align(4, buf);
write_string(snd, byteorder, buf);
}
HeaderField::Signature(sig) => {
params::validate_signature(sig)?;
buf.push(8);
buf.push(1);
buf.push(b'g');
buf.push(0);
pad_to_align(4, buf);
write_signature(sig, buf);
}
HeaderField::UnixFds(fds) => {
buf.push(9);
buf.push(1);
buf.push(b'u');
buf.push(0);
pad_to_align(4, buf);
write_u32(*fds, byteorder, buf);
}
}
params::validate_interface(interface)?;
marshal_header_field(2, "s", buf);
write_string(interface, byteorder, buf);
Ok(())
}

fn marshal_header_member(
byteorder: ByteOrder,
member: &str,
buf: &mut Vec<u8>,
) -> MarshalResult<()> {
params::validate_membername(member)?;
marshal_header_field(3, "s", buf);
write_string(member, byteorder, buf);
Ok(())
}

fn marshal_header_errorname(
byteorder: ByteOrder,
error: &str,
buf: &mut Vec<u8>,
) -> MarshalResult<()> {
params::validate_errorname(error)?;
marshal_header_field(4, "s", buf);
write_string(error, byteorder, buf);
Ok(())
}

fn marshal_header_reply_serial(
byteorder: ByteOrder,
serial: NonZeroU32,
buf: &mut Vec<u8>,
) -> MarshalResult<()> {
marshal_header_field(5, "u", buf);
write_u32(serial.get(), byteorder, buf);
Ok(())
}

fn marshal_header_destination(
byteorder: ByteOrder,
destination: &str,
buf: &mut Vec<u8>,
) -> MarshalResult<()> {
params::validate_busname(destination)?;
marshal_header_field(6, "s", buf);
write_string(destination, byteorder, buf);
Ok(())
}

fn marshal_header_sender(
byteorder: ByteOrder,
sender: &str,
buf: &mut Vec<u8>,
) -> MarshalResult<()> {
params::validate_busname(sender)?;
marshal_header_field(7, "s", buf);
write_string(sender, byteorder, buf);
Ok(())
}

fn marshal_header_signature(signature: &str, buf: &mut Vec<u8>) -> MarshalResult<()> {
params::validate_signature(signature)?;
marshal_header_field(8, "g", buf);
write_signature(signature, buf);
Ok(())
}

fn marshal_header_unix_fds(byteorder: ByteOrder, fds: u32, buf: &mut Vec<u8>) -> MarshalResult<()> {
marshal_header_field(9, "u", buf);
write_u32(fds, byteorder, buf);
Ok(())
}
Loading