icmp-socket/src/socket.rs

176 lines
4.9 KiB
Rust
Raw Normal View History

2021-01-08 15:16:02 -05:00
// Copyright 2021 Jeremy Wall
2021-01-25 19:50:40 -05:00
//
2021-01-08 15:16:02 -05:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2021-01-25 19:50:40 -05:00
//
2021-01-08 15:16:02 -05:00
// http://www.apache.org/licenses/LICENSE-2.0
2021-01-25 19:50:40 -05:00
//
2021-01-08 15:16:02 -05:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-01-25 20:55:25 -05:00
use std::convert::{Into, TryFrom, TryInto};
2021-01-08 15:16:02 -05:00
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
2021-01-25 19:50:40 -05:00
use socket2::{Domain, Protocol, Socket, Type};
2021-01-08 15:16:02 -05:00
2021-01-25 20:55:25 -05:00
use crate::packet::{Icmpv4Packet, Icmpv6Packet};
2021-01-12 21:10:11 -05:00
2021-01-08 15:16:02 -05:00
fn ip_to_socket(ip: &IpAddr) -> SocketAddr {
SocketAddr::new(*ip, 0)
2021-01-08 15:16:02 -05:00
}
2021-01-25 20:55:25 -05:00
pub trait IcmpSocket {
type AddrType;
type PacketType;
fn set_max_hops(&mut self, hops: u32);
fn bind<A: Into<Self::AddrType>>(&mut self, addr: A) -> std::io::Result<()>;
fn send_to(&mut self, dest: Self::AddrType, packet: Self::PacketType) -> std::io::Result<()>;
2021-01-27 20:38:24 -05:00
fn rcv_from(&mut self) -> std::io::Result<Self::PacketType>;
2021-01-25 20:55:25 -05:00
}
2021-01-08 15:16:02 -05:00
pub struct Opts {
hops: u32,
}
pub struct IcmpSocket4 {
bound_to: Option<Ipv4Addr>,
2021-01-27 20:38:24 -05:00
buf: Vec<u8>,
2021-01-08 15:16:02 -05:00
inner: Socket,
opts: Opts,
}
impl IcmpSocket4 {
pub fn new() -> std::io::Result<Self> {
2021-01-27 20:38:24 -05:00
let socket = Socket::new(Domain::ipv4(), Type::raw(), Some(Protocol::icmpv4()))?;
socket.set_recv_buffer_size(512)?;
2021-01-25 19:50:40 -05:00
Ok(Self {
2021-01-08 15:16:02 -05:00
bound_to: None,
2021-01-27 20:38:24 -05:00
inner: socket,
buf: vec![0; 512],
2021-01-25 19:50:40 -05:00
opts: Opts { hops: 50 },
2021-01-08 15:16:02 -05:00
})
}
2021-01-25 20:55:25 -05:00
}
impl IcmpSocket for IcmpSocket4 {
type AddrType = Ipv4Addr;
type PacketType = Icmpv4Packet;
2021-01-08 15:16:02 -05:00
2021-01-25 20:55:25 -05:00
fn set_max_hops(&mut self, hops: u32) {
2021-01-08 15:16:02 -05:00
self.opts.hops = hops;
}
2021-01-25 19:50:40 -05:00
2021-01-25 20:55:25 -05:00
fn bind<A: Into<Self::AddrType>>(&mut self, addr: A) -> std::io::Result<()> {
2021-01-08 15:16:02 -05:00
let addr = addr.into();
self.bound_to = Some(addr.clone());
let sock = ip_to_socket(&IpAddr::V4(addr));
self.inner.bind(&(sock.into()))?;
Ok(())
}
2021-01-25 20:55:25 -05:00
fn send_to(&mut self, dest: Self::AddrType, packet: Self::PacketType) -> std::io::Result<()> {
2021-01-08 15:16:02 -05:00
let dest = ip_to_socket(&IpAddr::V4(dest));
self.inner.set_ttl(self.opts.hops)?;
2021-01-27 20:49:48 -05:00
self.inner
.send_to(&packet.with_checksum().get_bytes(true), &(dest.into()))?;
2021-01-08 15:16:02 -05:00
Ok(())
}
2021-01-25 19:50:40 -05:00
2021-01-27 20:38:24 -05:00
fn rcv_from(&mut self) -> std::io::Result<Self::PacketType> {
let (read_count, _addr) = dbg!(self.inner.recv_from(&mut self.buf)?);
Ok(self.buf[0..read_count].try_into()?)
2021-01-08 15:16:02 -05:00
}
}
pub struct IcmpSocket6 {
bound_to: Option<Ipv6Addr>,
inner: Socket,
2021-01-27 20:38:24 -05:00
buf: Vec<u8>,
2021-01-08 15:16:02 -05:00
opts: Opts,
}
impl IcmpSocket6 {
2021-01-25 19:50:40 -05:00
pub fn new() -> std::io::Result<Self> {
2021-01-27 20:38:24 -05:00
let socket = Socket::new(Domain::ipv6(), Type::raw(), Some(Protocol::icmpv6()))?;
socket.set_recv_buffer_size(512)?;
2021-01-25 19:50:40 -05:00
Ok(Self {
2021-01-08 15:16:02 -05:00
bound_to: None,
2021-01-27 20:38:24 -05:00
inner: socket,
buf: vec![0; 512],
2021-01-25 19:50:40 -05:00
opts: Opts { hops: 50 },
2021-01-08 15:16:02 -05:00
})
}
2021-01-25 20:55:25 -05:00
}
impl IcmpSocket for IcmpSocket6 {
type AddrType = Ipv6Addr;
type PacketType = Icmpv6Packet;
2021-01-08 15:16:02 -05:00
2021-01-25 20:55:25 -05:00
fn set_max_hops(&mut self, hops: u32) {
2021-01-08 15:16:02 -05:00
self.opts.hops = hops;
}
2021-01-25 19:50:40 -05:00
2021-01-25 20:55:25 -05:00
fn bind<A: Into<Self::AddrType>>(&mut self, addr: A) -> std::io::Result<()> {
2021-01-08 15:16:02 -05:00
let addr = addr.into();
self.bound_to = Some(addr.clone());
let sock = ip_to_socket(&IpAddr::V6(addr));
self.inner.bind(&(sock.into()))?;
Ok(())
}
2021-01-25 20:55:25 -05:00
fn send_to(
&mut self,
dest: Self::AddrType,
mut packet: Self::PacketType,
) -> std::io::Result<()> {
2021-01-12 21:10:11 -05:00
let source = match self.bound_to {
Some(ref addr) => addr,
2021-01-25 19:50:40 -05:00
None => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Socket not bound to an address",
))
}
2021-01-12 21:10:11 -05:00
};
packet = packet.with_checksum(source, &dest);
2021-01-08 15:16:02 -05:00
let dest = ip_to_socket(&IpAddr::V6(dest));
self.inner.set_unicast_hops_v6(self.opts.hops)?;
let pkt = packet.get_bytes(true);
self.inner.send_to(&pkt, &(dest.into()))?;
2021-01-08 15:16:02 -05:00
Ok(())
}
2021-01-25 19:50:40 -05:00
2021-01-27 20:38:24 -05:00
fn rcv_from(&mut self) -> std::io::Result<Self::PacketType> {
let (read_count, _addr) = self.inner.recv_from(&mut self.buf)?;
Ok(self.buf[0..read_count].try_into()?)
2021-01-08 15:16:02 -05:00
}
}
2021-01-12 21:10:11 -05:00
impl TryFrom<Ipv4Addr> for IcmpSocket4 {
2021-01-08 15:16:02 -05:00
type Error = std::io::Error;
fn try_from(addr: Ipv4Addr) -> Result<Self, Self::Error> {
let mut sock = IcmpSocket4::new()?;
sock.bind(addr)?;
2021-01-12 21:10:11 -05:00
Ok(sock)
2021-01-08 15:16:02 -05:00
}
}
2021-01-12 21:10:11 -05:00
impl TryFrom<Ipv6Addr> for IcmpSocket6 {
2021-01-08 15:16:02 -05:00
type Error = std::io::Error;
fn try_from(addr: Ipv6Addr) -> Result<Self, Self::Error> {
let mut sock = IcmpSocket6::new()?;
sock.bind(addr)?;
2021-01-25 19:50:40 -05:00
Ok(sock)
2021-01-08 15:16:02 -05:00
}
}