Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payload): add support for arp #8

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(payload): add support for arp
  • Loading branch information
fu050409 committed Feb 1, 2025
commit 96116937e5ea80039de5f5b8aca13379ab83b285
5 changes: 5 additions & 0 deletions .changes/arp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tyr": patch:feat
---

Add support for ARP payload.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub enum Error {
#[error("Interface not found")]
InterfaceNotFound,
#[error("Buffer too small")]
BufferTooSmall,
InsufficientBuffer,
#[error("Invalid interface: {0}")]
InvalidInterface(String),
}
50 changes: 50 additions & 0 deletions src/payload/arp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::net::Ipv4Addr;

use pnet::packet::{arp, ethernet};
use pnet::{datalink, util::MacAddr};

pub struct ArpPayload {
pub src_ip: Ipv4Addr,
pub dst_ip: Ipv4Addr,
pub interface: datalink::NetworkInterface,
}

impl super::Payload for ArpPayload {
fn build(&mut self, packet: &mut [u8]) -> crate::Result<()> {
{
let mut ethernet_header =
ethernet::MutableEthernetPacket::new(&mut packet[..super::PKT_ETH_SIZE])
.ok_or(crate::error::Error::InsufficientBuffer)?;
ethernet_header.set_destination(MacAddr::broadcast());
ethernet_header.set_source(self.interface.mac.ok_or(
crate::error::Error::InvalidInterface(format!(
"Failed to get MAC address for interface {}",
self.interface.name,
)),
)?);
ethernet_header.set_ethertype(ethernet::EtherTypes::Arp);
}

{
let mut arp_header = arp::MutableArpPacket::new(&mut packet[super::PKT_ETH_SIZE..])
.ok_or(crate::error::Error::InsufficientBuffer)?;

arp_header.set_hardware_type(arp::ArpHardwareTypes::Ethernet);
arp_header.set_protocol_type(ethernet::EtherTypes::Ipv4);
arp_header.set_hw_addr_len(6);
arp_header.set_proto_addr_len(4);
arp_header.set_operation(arp::ArpOperations::Request);
arp_header.set_sender_hw_addr(self.interface.mac.ok_or(
crate::error::Error::InvalidInterface(format!(
"Failed to get MAC address for interface {}",
self.interface.name,
)),
)?);
arp_header.set_sender_proto_addr(self.src_ip);
arp_header.set_target_hw_addr(MacAddr::zero());
arp_header.set_target_proto_addr(self.dst_ip);
}

Ok(())
}
}
1 change: 1 addition & 0 deletions src/payload/mod.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ pub trait Payload {
fn build(&mut self, packet: &mut [u8]) -> crate::Result<()>;
}

pub mod arp;
pub mod syn;

mod constant;
7 changes: 4 additions & 3 deletions src/payload/syn.rs
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ impl super::Payload for SynPayload {
{
let mut ethernet_header =
ethernet::MutableEthernetPacket::new(&mut packet[..super::PKT_ETH_SIZE])
.ok_or(crate::error::Error::BufferTooSmall)?;
.ok_or(crate::error::Error::InsufficientBuffer)?;
ethernet_header.set_destination(MacAddr::broadcast());
ethernet_header.set_source(self.interface.mac.ok_or(
crate::error::Error::InvalidInterface(format!(
@@ -78,7 +78,7 @@ impl super::Payload for SynPayload {
let mut ipv4_header = ipv4::MutableIpv4Packet::new(
&mut packet[super::PKT_ETH_SIZE..(super::PKT_ETH_SIZE + super::PKT_IPV4_SIZE)],
)
.ok_or(crate::error::Error::BufferTooSmall)?;
.ok_or(crate::error::Error::InsufficientBuffer)?;
ipv4_header.set_header_length(69);
ipv4_header.set_total_length(52);
ipv4_header.set_next_level_protocol(ip::IpNextHeaderProtocols::Tcp);
@@ -97,7 +97,8 @@ impl super::Payload for SynPayload {
let mut tcp_header = tcp::MutableTcpPacket::new(
&mut packet[(super::PKT_ETH_SIZE + super::PKT_IPV4_SIZE)..],
)
.ok_or(crate::error::Error::BufferTooSmall)?;
.ok_or(crate::error::Error::InsufficientBuffer)?;

tcp_header.set_source(self.src_port);
tcp_header.set_destination(self.dst_port);