pub fn decode(buf: &[u8]) -> Result<Self> {
        let ipv4_packet = ipv4::Ipv4Packet::new(buf)
            .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIpv4Packet))?;
        let payload = ipv4_packet.payload();
        let icmp_packet = icmp::IcmpPacket::new(payload)
            .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIcmpv4Packet))?;
        match icmp_packet.get_icmp_type() {
            icmp::IcmpTypes::EchoReply => {
                let icmp_packet = icmp::echo_reply::EchoReplyPacket::new(payload)
                    .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIcmpv4Packet))?;
                let mut packet = Icmpv4Packet::default();
                packet
                    .source(ipv4_packet.get_source())
                    .destination(ipv4_packet.get_destination())
                    .ttl(ipv4_packet.get_ttl())
                    .icmp_type(icmp_packet.get_icmp_type())
                    .icmp_code(icmp_packet.get_icmp_code())
                    .size(icmp_packet.packet().len())
                    .real_dest(ipv4_packet.get_source())
                    .identifier(icmp_packet.get_identifier())
                    .sequence(icmp_packet.get_sequence_number());
                Ok(packet)
            }
            icmp::IcmpTypes::EchoRequest => Err(SurgeError::EchoRequestPacket),
            _ => {
                let icmp_payload = icmp_packet.payload();
                // icmp unused(4) + ip header(20) + echo icmp(4)
                let real_ip_packet = ipv4::Ipv4Packet::new(&icmp_payload[4..])
                    .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIpv4Packet))?;
                let identifier = u16::from_be_bytes(icmp_payload[28..30].try_into().unwrap());
                let sequence = u16::from_be_bytes(icmp_payload[30..32].try_into().unwrap());
                let mut packet = Icmpv4Packet::default();
                packet
                    .source(ipv4_packet.get_source())
                    .destination(ipv4_packet.get_destination())
                    .ttl(ipv4_packet.get_ttl())
                    .icmp_type(icmp_packet.get_icmp_type())
                    .icmp_code(icmp_packet.get_icmp_code())
                    .size(icmp_packet.packet_size())
                    .real_dest(real_ip_packet.get_destination())
                    .identifier(identifier)
                    .sequence(sequence);
                Ok(packet)
            }
        }
    }
/// Decode into icmp packet from the socket message.
Decode into icmp packet from the socket message.
Decode into icmp packet from the socket message .
pub fn decode(buf: &[u8]) -> Result<Self> {
        let ipv4_packet = ipv4::Ipv4Packet::new(buf)
            .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIpv4Packet))?;
        let payload = ipv4_packet.payload();
        let icmp_packet = icmp::IcmpPacket::new(payload)
            .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIcmpv4Packet))?;
        match icmp_packet.get_icmp_type() {
            icmp::IcmpTypes::EchoReply => {
                let icmp_packet = icmp::echo_reply::EchoReplyPacket::new(payload)
                    .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIcmpv4Packet))?;
                let mut packet = Icmpv4Packet::default();
                packet
                    .source(ipv4_packet.get_source())
                    .destination(ipv4_packet.get_destination())
                    .ttl(ipv4_packet.get_ttl())
                    .icmp_type(icmp_packet.get_icmp_type())
                    .icmp_code(icmp_packet.get_icmp_code())
                    .size(icmp_packet.packet().len())
                    .real_dest(ipv4_packet.get_source())
                    .identifier(icmp_packet.get_identifier())
                    .sequence(icmp_packet.get_sequence_number());
                Ok(packet)
            }
            icmp::IcmpTypes::EchoRequest => Err(SurgeError::EchoRequestPacket),
            _ => {
                let icmp_payload = icmp_packet.payload();
                // icmp unused(4) + ip header(20) + echo icmp(4)
                let real_ip_packet = ipv4::Ipv4Packet::new(&icmp_payload[4..])
                    .ok_or_else(|| SurgeError::from(MalformedPacketError::NotIpv4Packet))?;
                let identifier = u16::from_be_bytes(icmp_payload[28..30].try_into().unwrap());
                let sequence = u16::from_be_bytes(icmp_payload[30..32].try_into().unwrap());
                let mut packet = Icmpv4Packet::default();
                packet
                    .source(ipv4_packet.get_source())
                    .destination(ipv4_packet.get_destination())
                    .ttl(ipv4_packet.get_ttl())
                    .icmp_type(icmp_packet.get_icmp_type())
                    .icmp_code(icmp_packet.get_icmp_code())
                    .size(icmp_packet.packet_size())
                    .real_dest(real_ip_packet.get_destination())
                    .identifier(identifier)
                    .sequence(sequence);
                Ok(packet)
            }
        }
    }
pub fn decode ( buf : & [ u8 ] ) -> Result < Self > { let ipv4_packet = ipv4 :: Ipv4Packet :: new ( buf ) . ok_or_else ( | | SurgeError :: from ( MalformedPacketError :: NotIpv4Packet ) ) ? ; let payload = ipv4_packet . payload ( ) ; let icmp_packet = icmp :: IcmpPacket :: new ( payload ) . ok_or_else ( | | SurgeError :: from ( MalformedPacketError :: NotIcmpv4Packet ) ) ? ; match icmp_packet . get_icmp_type ( ) { icmp :: IcmpTypes :: EchoReply => { let icmp_packet = icmp :: echo_reply :: EchoReplyPacket :: new ( payload ) . ok_or_else ( | | SurgeError :: from ( MalformedPacketError :: NotIcmpv4Packet ) ) ? ; let mut packet = Icmpv4Packet :: default ( ) ; packet . source ( ipv4_packet . get_source ( ) ) . destination ( ipv4_packet . get_destination ( ) ) . ttl ( ipv4_packet . get_ttl ( ) ) . icmp_type ( icmp_packet . get_icmp_type ( ) ) . icmp_code ( icmp_packet . get_icmp_code ( ) ) . size ( icmp_packet . packet ( ) . len ( ) ) . real_dest ( ipv4_packet . get_source ( ) ) . identifier ( icmp_packet . get_identifier ( ) ) . sequence ( icmp_packet . get_sequence_number ( ) ) ; Ok ( packet ) } icmp :: IcmpTypes :: EchoRequest => Err ( SurgeError :: EchoRequestPacket ) , _ => { let icmp_payload = icmp_packet . payload ( ) ; // icmp unused(4) + ip header(20) + echo icmp(4) let real_ip_packet = ipv4 :: Ipv4Packet :: new ( & icmp_payload [ 4 .. ] ) . ok_or_else ( | | SurgeError :: from ( MalformedPacketError :: NotIpv4Packet ) ) ? ; let identifier = u16 :: from_be_bytes ( icmp_payload [ 28 .. 30 ] . try_into ( ) . unwrap ( ) ) ; let sequence = u16 :: from_be_bytes ( icmp_payload [ 30 .. 32 ] . try_into ( ) . unwrap ( ) ) ; let mut packet = Icmpv4Packet :: default ( ) ; packet . source ( ipv4_packet . get_source ( ) ) . destination ( ipv4_packet . get_destination ( ) ) . ttl ( ipv4_packet . get_ttl ( ) ) . icmp_type ( icmp_packet . get_icmp_type ( ) ) . icmp_code ( icmp_packet . get_icmp_code ( ) ) . size ( icmp_packet . packet_size ( ) ) . real_dest ( real_ip_packet . get_destination ( ) ) . identifier ( identifier ) . sequence ( sequence ) ; Ok ( packet ) } } }
Decode into icmp packet from the socket message.
Decode into icmp packet from the socket message .
buf
&[u8]