Skip to content

Commit

Permalink
Merge pull request #82 from shellrow/fix-rtm-error-handling
Browse files Browse the repository at this point in the history
Fix RTM error handling
  • Loading branch information
shellrow authored Jul 6, 2024
2 parents 88726d0 + 572d693 commit ce575b1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/gateway/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,16 @@ fn get_arp_table() -> io::Result<HashMap<IpAddr, MacAddr>> {
let mut offset = 0;
while offset < len as usize {
let rt_hdr = &*(buf.as_ptr().add(offset) as *const rt_msghdr);
assert_eq!(rt_hdr.rtm_version as u32, RTM_VERSION);
if rt_hdr.rtm_version as u32 != RTM_VERSION {
eprintln!(
"Unexpected RTM_VERSION: {} in {:?}",
rt_hdr.rtm_version, rt_hdr
);
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Unexpected RTM_VERSION: {}", rt_hdr.rtm_version),
));
}

let msg_len = rt_hdr.rtm_msglen as usize;
offset += msg_len;
Expand Down
15 changes: 12 additions & 3 deletions src/gateway/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ fn list_routes() -> io::Result<Vec<Route>> {
let rt_hdr = unsafe { std::mem::transmute::<_, &rt_msghdr>(buf.as_ptr()) };
if rt_hdr.rtm_version as u32 != RTM_VERSION {
eprintln!(
"unexpected RTM_VERSION: {} in {:?}",
"Unexpected RTM_VERSION: {} in {:?}",
rt_hdr.rtm_version, rt_hdr
);
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("unexpected RTM_VERSION: {}", rt_hdr.rtm_version),
format!("Unexpected RTM_VERSION: {}", rt_hdr.rtm_version),
));
}

Expand Down Expand Up @@ -425,7 +425,16 @@ fn get_arp_table() -> io::Result<HashMap<IpAddr, MacAddr>> {
}

let rt_hdr = unsafe { std::mem::transmute::<_, &rt_msghdr>(buf.as_ptr()) };
assert_eq!(rt_hdr.rtm_version as u32, RTM_VERSION);
if rt_hdr.rtm_version as u32 != RTM_VERSION {
eprintln!(
"Unexpected RTM_VERSION: {} in {:?}",
rt_hdr.rtm_version, rt_hdr
);
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Unexpected RTM_VERSION: {}", rt_hdr.rtm_version),
));
}
if rt_hdr.rtm_errno != 0 {
return Err(code_to_error(rt_hdr.rtm_errno));
}
Expand Down
7 changes: 6 additions & 1 deletion src/interface/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ pub mod netlink {
packet.finalize();

let mut buf = vec![0; packet.header.length as usize];
assert_eq!(buf.len(), packet.buffer_len());
if buf.len() != packet.buffer_len() {
return Err(io::Error::new(
io::ErrorKind::Other,
"Buffer length mismatch in netlink packet",
));
}
packet.serialize(&mut buf[..]);
socket.send(&buf[..], 0)?;

Expand Down

0 comments on commit ce575b1

Please sign in to comment.