/// A helper function that validates invariants for a single endpoint
///    * IP address is valid (either v4 or v6, correct format)
///    * Port number is valid (<= 65535)
///    * IP address is not unspecified
///    * IP address is not broadcast
///    * IP address is not a multicast address
///
/// Strict check also checks that:
///    * IPv4 address is not private, reserved, documentation address,
///      link-local, benchmarking
///    * IPv6 address is not link-local or unique-local unicast address
A helper function that validates invariants for a single endpoint
IP address is valid (either v4 or v6, correct format)
Port number is valid (<= 65535)
IP address is not unspecified
IP address is not broadcast
IP address is not a multicast address

Strict check also checks that:
IPv4 address is not private, reserved, documentation address,
link-local, benchmarking
IPv6 address is not link-local or unique-local unicast address
A helper function that validates invariants for a single endpoint IP address is valid ( either v4 or v6 correct format ) Port number is valid ( < = 65535 ) IP address is not unspecified IP address is not broadcast IP address is not a multicast address Strict check also checks that : IPv4 address is not private reserved documentation address link - local benchmarking IPv6 address is not link - local or unique - local unicast address
fn validate_endpoint(
    endpoint: &ConnectionEndpoint,
    strict: bool,
) -> Result<(IpAddr, u16), InvariantCheckError> {
    if endpoint.protocol() != Protocol::Http1
        && endpoint.protocol() != Protocol::Http1Tls13
        && endpoint.protocol() != Protocol::P2p1Tls13
    {
        return Err(InvariantCheckError {
            msg: format!(
                "Endpoint protocol is not supported: {:?}",
                endpoint.protocol
            ),
            source: None,
        });
    }
    let ip: IpAddr = endpoint
        .ip_addr
        .parse::<IpAddr>()
        .map_err(|e| InvariantCheckError {
            msg: format!("Failed to parse IP address: {:?}", endpoint.ip_addr),
            source: Some(Box::new(e)),
        })?;
    let port = u16::try_from(endpoint.port).map_err(|e| InvariantCheckError {
        msg: format!("Failed to parse port: {:?}", endpoint.port),
        source: Some(Box::new(e)),
    })?;
    if ip.is_unspecified() {
        return Err(InvariantCheckError {
            msg: format!("IP Address {:?} is unspecified", ip),
            source: None,
        });
    }
    if let IpAddr::V4(ipv4) = ip {
        if ipv4.is_broadcast() {
            return Err(InvariantCheckError {
                msg: format!("IP Address {:?} is a broadcast address", ip),
                source: None,
            });
        }
        if ipv4.is_multicast() {
            return Err(InvariantCheckError {
                msg: format!("IP Address {:?} is a multicast address", ip),
                source: None,
            });
        }
    } else if let IpAddr::V6(ipv6) = ip {
        let multicast_addr_and_mask = Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0);
        if mask_ipv6(ipv6, multicast_addr_and_mask) == multicast_addr_and_mask {
            return Err(InvariantCheckError {
                msg: format!("IP Address {:?} is a multicast address", ip),
                source: None,
            });
        }
    }
    if strict {
        if ip.is_loopback() {
            return Err(InvariantCheckError {
                msg: format!("IP Address {:?} is the loopback address", ip),
                source: None,
            });
        }
        if let IpAddr::V4(ipv4) = ip {
            if ipv4.is_private() {
                return Err(InvariantCheckError {
                    msg: format!("IP Address {:?} is a private address", ip),
                    source: None,
                });
            }
            if ipv4.is_link_local() {
                return Err(InvariantCheckError {
                    msg: format!("IP Address {:?} is a link local address", ip),
                    source: None,
                });
            }
            for (addr, mask, res_type) in &IPV4_STRICT_CHECKS {
                if mask_ipv4(ipv4, *mask) == *addr {
                    return Err(InvariantCheckError {
                        msg: format!("IP Address {:?} is not allowed ({})", ip, res_type),
                        source: None,
                    });
                }
            }
        } else if let IpAddr::V6(ipv6) = ip {
            for (addr, mask, res_type) in &IPV6_STRICT_CHECKS {
                if mask_ipv6(ipv6, *mask) == *addr {
                    return Err(InvariantCheckError {
                        msg: format!("IP Address {:?} is not allowed ({})", ip, res_type),
                        source: None,
                    });
                }
            }
        }
    }
    Ok((ip, port))
}
fn validate_endpoint ( endpoint : & ConnectionEndpoint , strict : bool , ) -> Result < ( IpAddr , u16 ) , InvariantCheckError > { if endpoint . protocol ( ) != Protocol :: Http1 && endpoint . protocol ( ) != Protocol :: Http1Tls13 && endpoint . protocol ( ) != Protocol :: P2p1Tls13 { return Err ( InvariantCheckError { msg : format ! ( " " endpoint protocol ) , source : None , } ) ; } let ip : IpAddr = endpoint . ip_addr . parse :: < IpAddr > ( ) . map_err ( | e | InvariantCheckError { msg : format ! ( " " endpoint ip_addr ) , source : Some ( Box :: new ( e ) ) , } ) ? ; let port = u16 :: try_from ( endpoint . port ) . map_err ( | e | InvariantCheckError { msg : format ! ( " " endpoint port ) , source : Some ( Box :: new ( e ) ) , } ) ? ; if ip . is_unspecified ( ) { return Err ( InvariantCheckError { msg : format ! ( " " ip ) , source : None , } ) ; } if let IpAddr :: V4 ( ipv4 ) = ip { if ipv4 . is_broadcast ( ) { return Err ( InvariantCheckError { msg : format ! ( " " ip ) , source : None , } ) ; } if ipv4 . is_multicast ( ) { return Err ( InvariantCheckError { msg : format ! ( " " ip ) , source : None , } ) ; } } else if let IpAddr :: V6 ( ipv6 ) = ip { let multicast_addr_and_mask = Ipv6Addr :: new ( 0xff00 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ; if mask_ipv6 ( ipv6 , multicast_addr_and_mask ) == multicast_addr_and_mask { return Err ( InvariantCheckError { msg : format ! ( " " ip ) , source : None , } ) ; } } if strict { if ip . is_loopback ( ) { return Err ( InvariantCheckError { msg : format ! ( " " ip ) , source : None , } ) ; } if let IpAddr :: V4 ( ipv4 ) = ip { if ipv4 . is_private ( ) { return Err ( InvariantCheckError { msg : format ! ( " " ip ) , source : None , } ) ; } if ipv4 . is_link_local ( ) { return Err ( InvariantCheckError { msg : format ! ( " " ip ) , source : None , } ) ; } for ( addr , mask , res_type ) in & IPV4_STRICT_CHECKS { if mask_ipv4 ( ipv4 , * mask ) == * addr { return Err ( InvariantCheckError { msg : format ! ( " " ip res_type ) , source : None , } ) ; } } } else if let IpAddr :: V6 ( ipv6 ) = ip { for ( addr , mask , res_type ) in & IPV6_STRICT_CHECKS { if mask_ipv6 ( ipv6 , * mask ) == * addr { return Err ( InvariantCheckError { msg : format ! ( " " ip res_type ) , source : None , } ) ; } } } } Ok ( ( ip , port ) ) }
A helper function that validates invariants for a single endpoint
 IP address is valid (either v4 or v6, correct format)
 Port number is valid (<= 65535)
 IP address is not unspecified
 IP address is not broadcast
 IP address is not a multicast address
A helper function that validates invariants for a single endpoint IP address is valid ( either v4 or v6 correct format ) Port number is valid ( < = 65535 ) IP address is not unspecified IP address is not broadcast IP address is not a multicast address
endpoint
&ConnectionEndpoint
strict
bool