Skip to content

Commit

Permalink
ensure non-existing handles are not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
lulf committed Aug 26, 2024
1 parent 5728c34 commit 416d517
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
36 changes: 30 additions & 6 deletions host/src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,14 @@ impl<'d> ConnectionManager<'d> {
None
}

pub(crate) fn is_handle_disconnected(&self, h: ConnHandle) -> bool {
pub(crate) fn is_handle_connected(&self, h: ConnHandle) -> bool {
let mut state = self.state.borrow_mut();
for storage in state.connections.iter_mut() {
match (storage.handle, &storage.state) {
(Some(handle), ConnectionState::DisconnectRequest(_)) if handle == h => {
return true;
}
(Some(handle), ConnectionState::Disconnecting(_)) if handle == h => {
(Some(handle), ConnectionState::Connected) if handle == h => {
return true;
}
(Some(handle), ConnectionState::Disconnected) if handle == h => {
(Some(handle), ConnectionState::Connecting) if handle == h => {
return true;
}
_ => {}
Expand Down Expand Up @@ -790,4 +787,31 @@ mod tests {

assert!(mgr.poll_disconnecting(None).is_pending());
}

#[test]
fn nonexisting_handle_is_disconnected() {
let mut storage = [ConnectionStorage::DISCONNECTED; 3];
let mgr = ConnectionManager::new(&mut storage[..]);

assert!(!mgr.is_handle_connected(ConnHandle::new(5)));

unwrap!(mgr.connect(
ConnHandle::new(3),
AddrKind::RANDOM,
BdAddr::new(ADDR_1),
LeConnRole::Peripheral
));

assert!(mgr.is_handle_connected(ConnHandle::new(3)));

let Poll::Ready(handle) = mgr.poll_accept(LeConnRole::Peripheral, &[], None) else {
panic!("expected connection to be accepted");
};

assert!(mgr.is_handle_connected(ConnHandle::new(3)));

handle.disconnect();

assert!(!mgr.is_handle_connected(ConnHandle::new(3)));
}
}
2 changes: 1 addition & 1 deletion host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ where
}

fn handle_acl(&self, acl: AclPacket<'_>) -> Result<(), Error> {
if self.connections.is_handle_disconnected(acl.handle()) {
if !self.connections.is_handle_connected(acl.handle()) {
return Err(Error::Disconnected);
}
let (header, mut packet) = match acl.boundary_flag() {
Expand Down

0 comments on commit 416d517

Please sign in to comment.