Struct cross_socket::socket::DataLinkSocket
source · pub struct DataLinkSocket {
pub interface: Interface,
/* private fields */
}
Expand description
Cross-platform raw socket. Enables to send and receive packets with custom headers. from datalink layer to application layer.
Fields§
§interface: Interface
Implementations§
source§impl DataLinkSocket
impl DataLinkSocket
sourcepub fn new(interface: Interface, promiscuous: bool) -> Result<DataLinkSocket>
pub fn new(interface: Interface, promiscuous: bool) -> Result<DataLinkSocket>
Examples found in repository?
examples/datalink_socket/arp.rs (line 12)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info for ARP request
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = MacAddr::zero();
packet_info.ether_type = cross_socket::packet::ethernet::EtherType::Arp;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = socket.interface.gateway.clone().unwrap().ip_addr;
// Build ARP packet
let arp_packet = builder::build_arp_packet(packet_info);
// Send ARP request to default gateway
match socket.send_to(&arp_packet) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
let src_mac = socket.interface.mac_addr.clone().unwrap();
// Receive packets
println!("Waiting for ARP reply... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != cross_socket::packet::ethernet::EtherType::Arp {
continue;
}
let arp_packet = cross_socket::packet::arp::ArpPacket::from_bytes(ðernet_packet.payload);
if arp_packet.sender_hw_addr.address() != src_mac.address() {
println!("Received {} bytes from {}", packet.len(), arp_packet.sender_hw_addr.address());
println!("Packet: {:?}", arp_packet);
break;
}
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
More examples
examples/datalink_socket/tcp_ping.rs (line 12)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = Some(53443);
packet_info.dst_port = Some(80);
packet_info.ip_protocol = Some(IpNextLevelProtocol::Tcp);
packet_info.payload = vec![0; 0];
// Send TCP SYN packets to 1.1.1.1:80
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for TCP SYN+ACK... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Tcp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let tcp_packet = cross_socket::packet::tcp::TcpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", tcp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
examples/datalink_socket/icmp_ping.rs (line 12)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = None;
packet_info.dst_port = None;
packet_info.ip_protocol = Some(IpNextLevelProtocol::Icmp);
packet_info.payload = vec![0; 0];
// Send ICMP Echo Request packets to 1.1.1.1
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for ICMP Echo Reply... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Icmp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let icmp_packet = cross_socket::packet::icmp::IcmpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", icmp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
examples/datalink_socket/udp_ping.rs (line 12)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = Some(53443);
packet_info.dst_port = Some(33435);
packet_info.ip_protocol = Some(IpNextLevelProtocol::Udp);
packet_info.payload = vec![0; 0];
// Send UDP packets to 1.1.1.1:33435
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for ICMP Destination (Port) Unreachable...");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Icmp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let icmp_packet = cross_socket::packet::icmp::IcmpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", icmp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
examples/socket/tcp.rs (line 31)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
let src_ip: IpAddr = IpAddr::V4(interface.ipv4[0].addr);
let src_socket_addr: SocketAddr = SocketAddr::new(src_ip, 53443);
let dst_ip: IpAddr = IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1));
let dst_socket_addr: SocketAddr = SocketAddr::new(dst_ip, 80);
let socket_option = SocketOption {
ip_version: IpVersion::V4,
socket_type: SocketType::Raw,
protocol: Some(IpNextLevelProtocol::Tcp),
timeout: None,
ttl: None,
non_blocking: false,
};
// Sender socket
let socket: Socket = Socket::new(socket_option).unwrap();
// Receiver socket
// RAW SOCKET recvfrom not working for TCP. So we use DataLinkSocket instead.
let mut listener_socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_ip = src_socket_addr.ip();
packet_info.dst_ip = dst_socket_addr.ip();
packet_info.src_port = Some(src_socket_addr.port());
packet_info.dst_port = Some(dst_socket_addr.port());
packet_info.ip_protocol = Some(IpNextLevelProtocol::Tcp);
packet_info.payload = vec![0; 0];
// Build TCP SYN packet
let tcp_packet = cross_socket::packet::builder::build_tcp_syn_packet(packet_info);
// Send TCP SYN packet to 1.1.1.1
match socket.send_to(&tcp_packet, dst_socket_addr) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for TCP SYN+ACK... ");
loop {
match listener_socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Tcp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let tcp_packet = cross_socket::packet::tcp::TcpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", tcp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
sourcepub fn send(&mut self, packet_info: PacketInfo) -> Result<usize>
pub fn send(&mut self, packet_info: PacketInfo) -> Result<usize>
Examples found in repository?
examples/datalink_socket/tcp_ping.rs (line 26)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = Some(53443);
packet_info.dst_port = Some(80);
packet_info.ip_protocol = Some(IpNextLevelProtocol::Tcp);
packet_info.payload = vec![0; 0];
// Send TCP SYN packets to 1.1.1.1:80
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for TCP SYN+ACK... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Tcp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let tcp_packet = cross_socket::packet::tcp::TcpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", tcp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
More examples
examples/datalink_socket/icmp_ping.rs (line 26)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = None;
packet_info.dst_port = None;
packet_info.ip_protocol = Some(IpNextLevelProtocol::Icmp);
packet_info.payload = vec![0; 0];
// Send ICMP Echo Request packets to 1.1.1.1
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for ICMP Echo Reply... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Icmp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let icmp_packet = cross_socket::packet::icmp::IcmpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", icmp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
examples/datalink_socket/udp_ping.rs (line 26)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = Some(53443);
packet_info.dst_port = Some(33435);
packet_info.ip_protocol = Some(IpNextLevelProtocol::Udp);
packet_info.payload = vec![0; 0];
// Send UDP packets to 1.1.1.1:33435
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for ICMP Destination (Port) Unreachable...");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Icmp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let icmp_packet = cross_socket::packet::icmp::IcmpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", icmp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
sourcepub fn send_to(&mut self, buf: &[u8]) -> Result<usize>
pub fn send_to(&mut self, buf: &[u8]) -> Result<usize>
Examples found in repository?
examples/datalink_socket/arp.rs (line 25)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info for ARP request
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = MacAddr::zero();
packet_info.ether_type = cross_socket::packet::ethernet::EtherType::Arp;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = socket.interface.gateway.clone().unwrap().ip_addr;
// Build ARP packet
let arp_packet = builder::build_arp_packet(packet_info);
// Send ARP request to default gateway
match socket.send_to(&arp_packet) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
let src_mac = socket.interface.mac_addr.clone().unwrap();
// Receive packets
println!("Waiting for ARP reply... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != cross_socket::packet::ethernet::EtherType::Arp {
continue;
}
let arp_packet = cross_socket::packet::arp::ArpPacket::from_bytes(ðernet_packet.payload);
if arp_packet.sender_hw_addr.address() != src_mac.address() {
println!("Received {} bytes from {}", packet.len(), arp_packet.sender_hw_addr.address());
println!("Packet: {:?}", arp_packet);
break;
}
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
pub fn build_and_send( &mut self, num_packets: usize, packet_size: usize, func: &mut dyn FnMut(&mut [u8]) ) -> Result<()>
sourcepub fn receive(&mut self) -> Result<&[u8]>
pub fn receive(&mut self) -> Result<&[u8]>
Examples found in repository?
examples/datalink_socket/arp.rs (line 37)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info for ARP request
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = MacAddr::zero();
packet_info.ether_type = cross_socket::packet::ethernet::EtherType::Arp;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = socket.interface.gateway.clone().unwrap().ip_addr;
// Build ARP packet
let arp_packet = builder::build_arp_packet(packet_info);
// Send ARP request to default gateway
match socket.send_to(&arp_packet) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
let src_mac = socket.interface.mac_addr.clone().unwrap();
// Receive packets
println!("Waiting for ARP reply... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != cross_socket::packet::ethernet::EtherType::Arp {
continue;
}
let arp_packet = cross_socket::packet::arp::ArpPacket::from_bytes(ðernet_packet.payload);
if arp_packet.sender_hw_addr.address() != src_mac.address() {
println!("Received {} bytes from {}", packet.len(), arp_packet.sender_hw_addr.address());
println!("Packet: {:?}", arp_packet);
break;
}
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
More examples
examples/datalink_socket/tcp_ping.rs (line 38)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = Some(53443);
packet_info.dst_port = Some(80);
packet_info.ip_protocol = Some(IpNextLevelProtocol::Tcp);
packet_info.payload = vec![0; 0];
// Send TCP SYN packets to 1.1.1.1:80
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for TCP SYN+ACK... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Tcp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let tcp_packet = cross_socket::packet::tcp::TcpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", tcp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
examples/datalink_socket/icmp_ping.rs (line 38)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = None;
packet_info.dst_port = None;
packet_info.ip_protocol = Some(IpNextLevelProtocol::Icmp);
packet_info.payload = vec![0; 0];
// Send ICMP Echo Request packets to 1.1.1.1
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for ICMP Echo Reply... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Icmp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let icmp_packet = cross_socket::packet::icmp::IcmpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", icmp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
examples/datalink_socket/udp_ping.rs (line 38)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = socket.interface.gateway.clone().unwrap().mac_addr;
packet_info.ether_type = EtherType::Ipv4;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1));
packet_info.src_port = Some(53443);
packet_info.dst_port = Some(33435);
packet_info.ip_protocol = Some(IpNextLevelProtocol::Udp);
packet_info.payload = vec![0; 0];
// Send UDP packets to 1.1.1.1:33435
match socket.send(packet_info) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for ICMP Destination (Port) Unreachable...");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Icmp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let icmp_packet = cross_socket::packet::icmp::IcmpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", icmp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
examples/socket/tcp.rs (line 58)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
let src_ip: IpAddr = IpAddr::V4(interface.ipv4[0].addr);
let src_socket_addr: SocketAddr = SocketAddr::new(src_ip, 53443);
let dst_ip: IpAddr = IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1));
let dst_socket_addr: SocketAddr = SocketAddr::new(dst_ip, 80);
let socket_option = SocketOption {
ip_version: IpVersion::V4,
socket_type: SocketType::Raw,
protocol: Some(IpNextLevelProtocol::Tcp),
timeout: None,
ttl: None,
non_blocking: false,
};
// Sender socket
let socket: Socket = Socket::new(socket_option).unwrap();
// Receiver socket
// RAW SOCKET recvfrom not working for TCP. So we use DataLinkSocket instead.
let mut listener_socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info
let mut packet_info = PacketInfo::new();
packet_info.src_ip = src_socket_addr.ip();
packet_info.dst_ip = dst_socket_addr.ip();
packet_info.src_port = Some(src_socket_addr.port());
packet_info.dst_port = Some(dst_socket_addr.port());
packet_info.ip_protocol = Some(IpNextLevelProtocol::Tcp);
packet_info.payload = vec![0; 0];
// Build TCP SYN packet
let tcp_packet = cross_socket::packet::builder::build_tcp_syn_packet(packet_info);
// Send TCP SYN packet to 1.1.1.1
match socket.send_to(&tcp_packet, dst_socket_addr) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
// Receive packets
println!("Waiting for TCP SYN+ACK... ");
loop {
match listener_socket.receive() {
Ok(packet) => {
let ethernet_packet = cross_socket::packet::ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != EtherType::Ipv4 {
continue;
}
let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(ðernet_packet.payload);
if ip_packet.next_level_protocol != IpNextLevelProtocol::Tcp || ip_packet.source != std::net::Ipv4Addr::new(1, 1, 1, 1) {
continue;
}
println!("Received {} bytes from {}", packet.len(), ip_packet.source);
let tcp_packet = cross_socket::packet::tcp::TcpPacket::from_bytes(&ip_packet.payload);
println!("Packet: {:?}", tcp_packet);
break;
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
Auto Trait Implementations§
impl !RefUnwindSafe for DataLinkSocket
impl Send for DataLinkSocket
impl !Sync for DataLinkSocket
impl Unpin for DataLinkSocket
impl !UnwindSafe for DataLinkSocket
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more