// Handle pending requests for a newly received rule.
// Emit necessary 'BehaviourEvents' depending on rules and direction.
// The method return the requests for which the `NetBehaviour` should query a `FirewallRequest::RequestApproval`.
Handle pending requests for a newly received rule.
Emit necessary 'BehaviourEvents' depending on rules and direction.
The method return the requests for which the `NetBehaviour` should query a `FirewallRequest::RequestApproval`.
Handle pending requests for a newly received rule . Emit necessary ' BehaviourEvents ' depending on rules and direction . The method return the requests for which the ` NetBehaviour ` should query a ` FirewallRequest :: RequestApproval ` .
pub fn on_peer_rule(
        &mut self,
        peer: PeerId,
        rules: FirewallRules<TRq>,
        direction: RuleDirection,
    ) -> Option<Vec<(RequestId, TRq, RequestDirection)>> {
        let mut await_rule = self.awaiting_peer_rule.remove(&peer)?;
        // Affected requests.
        let mut requests = vec![];
        if direction.is_inbound() {
            if let Some(in_rqs) = await_rule.remove(&RequestDirection::Inbound) {
                requests.extend(in_rqs.into_iter().map(|rq| (rq, RequestDirection::Inbound)));
            }
        }
        if direction.is_outbound() {
            if let Some(out_rqs) = await_rule.remove(&RequestDirection::Outbound) {
                requests.extend(out_rqs.into_iter().map(|rq| (rq, RequestDirection::Outbound)));
            }
        }
        // Handle the requests according to the new rule.
        let require_ask = requests
            .into_iter()
            .filter_map(|(request_id, dir)| {
                let rule = match dir {
                    RequestDirection::Inbound => rules.inbound.as_ref(),
                    RequestDirection::Outbound => rules.outbound.as_ref(),
                };
                match rule {
                    Some(Rule::Ask) => {
                        // Requests need to await individual approval.
                        let rq = self.get_request_value_ref(&request_id, &dir)?;
                        self.awaiting_approval.push((request_id, dir.clone()));
                        Some((request_id, rq, dir))
                    }
                    Some(Rule::AllowAll) => {
                        self.handle_request_approval(request_id, &dir, true);
                        None
                    }
                    Some(Rule::RejectAll) => {
                        self.handle_request_approval(request_id, &dir, false);
                        None
                    }
                    Some(Rule::Restricted { restriction, .. }) => {
                        // Checking the individual restriction for the request.
                        if let Some(rq) = self.get_request_value_ref(&request_id, &dir) {
                            let is_allowed = restriction(&rq);
                            self.handle_request_approval(request_id, &dir, is_allowed);
                        }
                        None
                    }
                    None => {
                        // Reject request if no rule was provided.
                        self.handle_request_approval(request_id, &dir, false);
                        None
                    }
                }
            })
            .collect();
        // Keep unaffected requests in map.
        if !await_rule.is_empty() {
            self.awaiting_peer_rule.insert(peer, await_rule);
        }
        Some(require_ask)
    }
pub fn on_peer_rule ( & mut self , peer : PeerId , rules : FirewallRules < TRq > , direction : RuleDirection , ) -> Option < Vec < ( RequestId , TRq , RequestDirection ) > > { let mut await_rule = self . awaiting_peer_rule . remove ( & peer ) ? ; // Affected requests. let mut requests = vec ! [ ] ; if direction . is_inbound ( ) { if let Some ( in_rqs ) = await_rule . remove ( & RequestDirection :: Inbound ) { requests . extend ( in_rqs . into_iter ( ) . map ( | rq | ( rq , RequestDirection :: Inbound ) ) ) ; } } if direction . is_outbound ( ) { if let Some ( out_rqs ) = await_rule . remove ( & RequestDirection :: Outbound ) { requests . extend ( out_rqs . into_iter ( ) . map ( | rq | ( rq , RequestDirection :: Outbound ) ) ) ; } } // Handle the requests according to the new rule. let require_ask = requests . into_iter ( ) . filter_map ( | ( request_id , dir ) | { let rule = match dir { RequestDirection :: Inbound => rules . inbound . as_ref ( ) , RequestDirection :: Outbound => rules . outbound . as_ref ( ) , } ; match rule { Some ( Rule :: Ask ) => { // Requests need to await individual approval. let rq = self . get_request_value_ref ( & request_id , & dir ) ? ; self . awaiting_approval . push ( ( request_id , dir . clone ( ) ) ) ; Some ( ( request_id , rq , dir ) ) } Some ( Rule :: AllowAll ) => { self . handle_request_approval ( request_id , & dir , true ) ; None } Some ( Rule :: RejectAll ) => { self . handle_request_approval ( request_id , & dir , false ) ; None } Some ( Rule :: Restricted { restriction , .. } ) => { // Checking the individual restriction for the request. if let Some ( rq ) = self . get_request_value_ref ( & request_id , & dir ) { let is_allowed = restriction ( & rq ) ; self . handle_request_approval ( request_id , & dir , is_allowed ) ; } None } None => { // Reject request if no rule was provided. self . handle_request_approval ( request_id , & dir , false ) ; None } } } ) . collect ( ) ; // Keep unaffected requests in map. if ! await_rule . is_empty ( ) { self . awaiting_peer_rule . insert ( peer , await_rule ) ; } Some ( require_ask ) }
Handle pending requests for a newly received rule.
Handle pending requests for a newly received rule .
&mut self
peer
PeerId
rules
FirewallRules<TRq>
direction
RuleDirection