This repository has been archived on 2026-09-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
opencoreRS/kernel/src/net/icmp.rs
T

40 lines
1.2 KiB
Rust

//! ICMP (Internet Control Message Protocol) — Echo Request & Reply (Ping).
use alloc::vec::Vec;
use super::ipv4::calculate_checksum;
pub const ICMP_TYPE_ECHO_REPLY: u8 = 0;
pub const ICMP_TYPE_ECHO_REQUEST: u8 = 8;
pub fn create_echo_request(seq: u16, id: u16, payload: &[u8]) -> Vec<u8> {
let mut packet = Vec::with_capacity(8 + payload.len());
packet.push(ICMP_TYPE_ECHO_REQUEST);
packet.push(0); // Code 0
packet.extend_from_slice(&[0, 0]); // Checksum placeholder
packet.extend_from_slice(&id.to_be_bytes());
packet.extend_from_slice(&seq.to_be_bytes());
packet.extend_from_slice(payload);
let csum = calculate_checksum(&packet);
packet[2] = (csum >> 8) as u8;
packet[3] = (csum & 0xFF) as u8;
packet
}
pub fn create_echo_reply(id: u16, seq: u16, payload: &[u8]) -> Vec<u8> {
let mut packet = Vec::with_capacity(8 + payload.len());
packet.push(ICMP_TYPE_ECHO_REPLY);
packet.push(0);
packet.extend_from_slice(&[0, 0]);
packet.extend_from_slice(&id.to_be_bytes());
packet.extend_from_slice(&seq.to_be_bytes());
packet.extend_from_slice(payload);
let csum = calculate_checksum(&packet);
packet[2] = (csum >> 8) as u8;
packet[3] = (csum & 0xFF) as u8;
packet
}