mirror of
https://github.com/zaphar/icmp-socket.git
synced 2025-07-23 19:39:49 -04:00
We need the sockaddr from rcv_from
This commit is contained in:
parent
b85822c95d
commit
37f3651995
@ -35,7 +35,8 @@ pub fn main() {
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let resp = echo_socket.recv_ping().unwrap();
|
let _ = echo_socket.recv_ping();
|
||||||
|
let (resp, _addr) = echo_socket.recv_ping().unwrap();
|
||||||
println!(
|
println!(
|
||||||
"seq: {}, identifier: {} payload: {}",
|
"seq: {}, identifier: {} payload: {}",
|
||||||
resp.sequence,
|
resp.sequence,
|
||||||
|
@ -36,7 +36,7 @@ pub fn main() {
|
|||||||
// TODO(jwall): The first packet we recieve will be the one we sent.
|
// TODO(jwall): The first packet we recieve will be the one we sent.
|
||||||
// We need to implement packet filtering for the socket.
|
// We need to implement packet filtering for the socket.
|
||||||
let _ = echo_socket.recv_ping();
|
let _ = echo_socket.recv_ping();
|
||||||
let resp = echo_socket.recv_ping().unwrap();
|
let (resp, _addr) = echo_socket.recv_ping().unwrap();
|
||||||
println!(
|
println!(
|
||||||
"seq: {}, identifier: {} payload: {}",
|
"seq: {}, identifier: {} payload: {}",
|
||||||
resp.sequence,
|
resp.sequence,
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
use std::convert::{From, TryFrom, TryInto};
|
use std::convert::{From, TryFrom, TryInto};
|
||||||
|
|
||||||
|
use socket2::SockAddr;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
packet::{Icmpv4Message, Icmpv4Packet, Icmpv6Message, Icmpv6Packet, WithEchoRequest},
|
packet::{Icmpv4Message, Icmpv4Packet, Icmpv6Message, Icmpv6Packet, WithEchoRequest},
|
||||||
socket::IcmpSocket,
|
socket::IcmpSocket,
|
||||||
@ -116,9 +118,9 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recv_ping(&mut self) -> std::io::Result<EchoResponse> {
|
pub fn recv_ping(&mut self) -> std::io::Result<(EchoResponse, SockAddr)> {
|
||||||
let packet = self.inner.rcv_from()?;
|
let (packet, addr) = self.inner.rcv_from()?;
|
||||||
Ok(packet.try_into()?)
|
Ok((packet.try_into()?, addr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
use std::convert::{Into, TryFrom, TryInto};
|
use std::convert::{Into, TryFrom, TryInto};
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||||
|
|
||||||
use socket2::{Domain, Protocol, Socket, Type};
|
use socket2::{Domain, Protocol, SockAddr, Socket, Type};
|
||||||
|
|
||||||
use crate::packet::{Icmpv4Packet, Icmpv6Packet};
|
use crate::packet::{Icmpv4Packet, Icmpv6Packet};
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ pub trait IcmpSocket {
|
|||||||
|
|
||||||
fn send_to(&mut self, dest: Self::AddrType, packet: Self::PacketType) -> std::io::Result<()>;
|
fn send_to(&mut self, dest: Self::AddrType, packet: Self::PacketType) -> std::io::Result<()>;
|
||||||
|
|
||||||
fn rcv_from(&mut self) -> std::io::Result<Self::PacketType>;
|
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Opts {
|
pub struct Opts {
|
||||||
@ -84,9 +84,9 @@ impl IcmpSocket for IcmpSocket4 {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rcv_from(&mut self) -> std::io::Result<Self::PacketType> {
|
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> {
|
||||||
let (read_count, _addr) = dbg!(self.inner.recv_from(&mut self.buf)?);
|
let (read_count, addr) = self.inner.recv_from(&mut self.buf)?;
|
||||||
Ok(self.buf[0..read_count].try_into()?)
|
Ok((self.buf[0..read_count].try_into()?, addr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,9 +148,9 @@ impl IcmpSocket for IcmpSocket6 {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rcv_from(&mut self) -> std::io::Result<Self::PacketType> {
|
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> {
|
||||||
let (read_count, _addr) = self.inner.recv_from(&mut self.buf)?;
|
let (read_count, addr) = self.inner.recv_from(&mut self.buf)?;
|
||||||
Ok(self.buf[0..read_count].try_into()?)
|
Ok((self.buf[0..read_count].try_into()?, addr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user