Example and some debugging

This commit is contained in:
Jeremy Wall 2021-01-16 13:45:12 -05:00
parent 07d8ee676a
commit f49a16c2f6
3 changed files with 33 additions and 3 deletions

25
examples/ping6.rs Normal file
View File

@ -0,0 +1,25 @@
// Copyright 2021 Jeremy Wall
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
use std::net::Ipv6Addr;
use icmp_socket::*;
pub fn main() {
let mut socket6 = IcmpSocket6::new().unwrap();
socket6.bind("::1".parse::<Ipv6Addr>().unwrap()).unwrap();
let mut echo_socket = echo::EchoSocket6::new(socket6);
echo_socket.send_ping("::1".parse::<Ipv6Addr>().unwrap(), 42, &[]).unwrap();
let resp = echo_socket.recv_ping().unwrap();
println!("seq: {}, identifier: {} payload: {}", resp.sequence, resp.identifier, resp.payload.len());
}

View File

@ -17,13 +17,14 @@ use std::net::{Ipv4Addr, Ipv6Addr};
use packet::{Builder, Packet as P}; use packet::{Builder, Packet as P};
use packet::icmp::echo::Packet; use packet::icmp::echo::Packet;
use crate::packet::{Icmpv6Packet, Icmpv6Message::{EchoReply, EchoRequest}}; use crate::packet::{Icmpv6Packet, Icmpv6Message::EchoReply};
// TODO(jwall): It turns out that the ICMPv6 packets are sufficiently // TODO(jwall): It turns out that the ICMPv6 packets are sufficiently
// different from the ICMPv4 packets. In order to handle them appropriately // different from the ICMPv4 packets. In order to handle them appropriately
// It is going to take some consideration. // It is going to take some consideration.
use crate::{IcmpSocket4, IcmpSocket6}; use crate::{IcmpSocket4, IcmpSocket6};
#[derive(Debug)]
pub struct EchoResponse { pub struct EchoResponse {
pub identifier: u16, pub identifier: u16,
pub sequence: u16, pub sequence: u16,
@ -45,7 +46,7 @@ impl TryFrom<Icmpv6Packet> for EchoResponse {
payload, payload,
}) })
} else { } else {
Err(std::io::Error::new(std::io::ErrorKind::Other, "Incorrect icmpv6 message")) Err(std::io::Error::new(std::io::ErrorKind::Other, format!("Incorrect icmpv6 message: {:?}, code: {}", pkt.message, pkt.code)))
} }
} }
} }
@ -119,6 +120,7 @@ impl EchoSocket6 {
} }
pub fn recv_ping(&mut self) -> std::io::Result<EchoResponse> { pub fn recv_ping(&mut self) -> std::io::Result<EchoResponse> {
self.buf.resize(512, 0);
let bytes_read = self.inner.rcv_from(&mut self.buf)?; let bytes_read = self.inner.rcv_from(&mut self.buf)?;
match Icmpv6Packet::parse(&self.buf[0..bytes_read]) { match Icmpv6Packet::parse(&self.buf[0..bytes_read]) {
Ok(p) => return Ok(p.try_into()?), Ok(p) => return Ok(p.try_into()?),

View File

@ -369,7 +369,7 @@ mod checksum_tests {
0x6b, 0x6e, 0x69, 0x67, 0x68, 0x74, 0x73, 0x20, 0x6b, 0x6e, 0x69, 0x67, 0x68, 0x74, 0x73, 0x20,
0x6f, 0x66, 0x20, 0x6e, 0x69, 0x20, 0x20, 0x20 0x6f, 0x66, 0x20, 0x6e, 0x69, 0x20, 0x20, 0x20
]; ];
let pkt = Icmpv6Packet::parse(&data).unwrap(); let mut pkt = Icmpv6Packet::parse(&data).unwrap();
assert_eq!(pkt.typ, 128); assert_eq!(pkt.typ, 128);
assert_eq!(pkt.code, 0x00); assert_eq!(pkt.code, 0x00);
if let EchoRequest{ if let EchoRequest{
@ -393,6 +393,8 @@ mod checksum_tests {
} }
assert_eq!(pkt.get_bytes(true), data); assert_eq!(pkt.get_bytes(true), data);
assert_eq!(pkt.calculate_checksum(lo, lo), 0x1d2e); assert_eq!(pkt.calculate_checksum(lo, lo), 0x1d2e);
pkt = pkt.with_checksum(lo, lo);
assert_eq!(pkt.checksum, 0x1d2e);
// Check echo response as well // Check echo response as well
data[0] = 0x81; data[0] = 0x81;
@ -418,6 +420,7 @@ mod checksum_tests {
} else { } else {
assert!(false, "Packet did not parse as an EchoReply {:?}", pkt.message); assert!(false, "Packet did not parse as an EchoReply {:?}", pkt.message);
} }
assert_eq!(pkt.get_bytes(true), data);
assert_eq!(pkt.calculate_checksum(lo, lo), 0x1c2e); assert_eq!(pkt.calculate_checksum(lo, lo), 0x1c2e);
} }
} }