use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::env; use cross_socket::datalink::interface::Interface; use cross_socket::packet::ethernet::EtherType; use cross_socket::packet::ip::IpNextLevelProtocol; use cross_socket::packet::tcp::{TcpFlag, TcpOption, TcpPacketBuilder}; use cross_socket::socket::{DataLinkSocket, IpVersion, Socket, SocketOption, SocketType}; // Send TCP SYN packets to 1.1.1.1:80 and check reply // This example is for Unix(Linux, macOS ...) only. // For Windows, use examples/datalink_socket/tcp_ping.rs instead. // (Due to Winsock2 limitation.) fn main() { let interface: Interface = match env::args().nth(1) { Some(n) => { // Use interface specified by user let interfaces: Vec = default_net::get_interfaces(); let interface: Interface = interfaces .into_iter() .find(|interface| interface.name == n) .expect("Failed to get interface information"); interface }, None => { // Use default interface default_net::get_default_interface().expect("Failed to get default interface information") } }; let use_tun: bool = interface.is_tun(); let src_ip: IpAddr = IpAddr::V4(interface.ipv4[0].addr); let dst_ipv4: Ipv4Addr = Ipv4Addr::new(1, 1, 1, 1); let dst_ip: IpAddr = IpAddr::V4(dst_ipv4); 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(); // Packet builder for TCP SYN let mut tcp_packet_builder = TcpPacketBuilder::new( SocketAddr::new(src_ip, 53443), SocketAddr::new(dst_ip, 80), ); tcp_packet_builder.flags = vec![TcpFlag::Syn]; tcp_packet_builder.options = vec![ TcpOption::mss(1460), TcpOption::sack_perm(), TcpOption::nop(), TcpOption::nop(), TcpOption::wscale(7), ]; // Send TCP SYN packet to 1.1.1.1 match socket.send_to(&tcp_packet_builder.build(), 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) => { if use_tun { // Logic for TUN interface let ip_packet = cross_socket::packet::ipv4::Ipv4Packet::from_bytes(&packet); if ip_packet.next_protocol != IpNextLevelProtocol::Tcp || ip_packet.source != dst_ipv4 { 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; }else { 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_protocol != IpNextLevelProtocol::Tcp || ip_packet.source != dst_ipv4 { 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); } } } }