added arp packet handler

This commit is contained in:
Stefan Friese 2020-05-15 19:37:23 +02:00
parent dd49870da4
commit 86b9051277
1 changed files with 69 additions and 3 deletions

View File

@ -5,7 +5,7 @@ extern crate bitfield;
use byteorder::{ByteOrder, LittleEndian};
use eui48::{MacAddress, Eui48};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use bitfield::bitfield;
use bitfield::{bitfield, BitRange};
/* ethernet */
@ -199,7 +199,7 @@ pub struct TcpHeader {
bitfield! {
struct BitfieldTcpHeader (MSB0 [u8]);
struct BitfieldTcpHeader ( MSB0 [u8] );
u32;
get_source_port, _: 15, 0;
get_destination_port, _: 31,16;
@ -252,10 +252,76 @@ pub fn tcp_handler ( ip_hlen: u32, packet_data: &[u8] ) ->TcpHeader {
}
/* arp */
#[derive(Debug, Clone)]
pub struct ArpHeader {
pub htype: u32,
pub ptype: u32,
pub hlen: u32,
pub plen: u32,
pub oper: u32,
pub sha: String,
pub spa: IpAddr,
pub tha: String,
pub tpa: IpAddr,
}
// u8, get_source_address, _: 103, 96, 4;
// u32, into Ipv4Addr, get_destination_address, _: 159, 128;
bitfield!{
struct BitfieldArpHeader ( MSB0 [u8] );
impl Debug;
u32;
get_htype, _: 0, 1;
get_ptype, _: 2, 3;
get_hlen, _: 4;
get_plen, _: 5;
get_oper, _: 6, 7;
get_sha, _: 8,13;
get_tha, _: 18, 23;
u8, get_spa, _: 17, 14, 4;
u32, into Ipv4Addr, get_tpa, _: 28, 24;
}
impl<T: AsRef<[u8]> + AsMut<[u8]>> BitfieldArpHeader<T> {
fn get_spa_as_ip_addr(&self) -> Ipv4Addr{
let mut src = [0; 4];
for (i, src) in src.iter_mut().enumerate() {
*src = self.get_spa(i);
}
src.into()
}
}
pub fn arp_handler ( packet_data: &[u8] ) -> ArpHeader {
let (_head, tail) = packet_data.split_at(ETHER_HDRLEN);
let (raw_hdr, _) = tail.split_at(28);
let mut _tail: [u8; 28] = [0; 28];
_tail.copy_from_slice(raw_hdr);
let arp_header = BitfieldArpHeader(_tail);
let _sha: [u8;6] = [0;6]; let _tha: [u8;6] = [0;6];
_tail[8..13].copy_from_slice(&_sha);
_tail[18..23].copy_from_slice(&_tha);
ArpHeader{
htype: arp_header.get_htype(),
ptype: arp_header.get_ptype(),
hlen: arp_header.get_hlen().into(),
plen: arp_header.get_plen().into(),
oper: arp_header.get_oper(),
sha: MacAddress::new(_sha as Eui48).to_hex_string(),
//MacAddress::new(arp_header.get_sha() as Eui48).to_hex_string(),
spa: IpAddr::V4(arp_header.get_spa_as_ip_addr()),
tha: MacAddress::new(_tha as Eui48).to_hex_string(),
tpa: IpAddr::V4(arp_header.get_tpa()),
}
}
/* payload */
pub fn payload_handler ( ip_hlen: u32, data_offset: u32, packet_data : &[u8] ) -> Option<Vec<u8>> {
let (_head, tail)= packet_data.split_at(ETHER_HDRLEN+ip_hlen as usize * 4+data_offset as usize * 4);
Some(tail.to_vec())
}