1
2
3
4
5
6
7
8
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::NdpPacket;
use crate::packets::icmp::v6::{Icmpv6, Icmpv6Message, Icmpv6Packet, Icmpv6Type, Icmpv6Types};
use crate::packets::ip::v6::Ipv6Packet;
use crate::packets::types::u32be;
use crate::packets::{Internal, Packet};
use crate::SizeOf;
use failure::Fallible;
use std::fmt;
use std::ptr::NonNull;
#[derive(Icmpv6Packet)]
pub struct RouterSolicitation<E: Ipv6Packet> {
icmp: Icmpv6<E>,
body: NonNull<RouterSolicitationBody>,
}
impl<E: Ipv6Packet> fmt::Debug for RouterSolicitation<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RouterSolicitation")
.field("type", &format!("{}", self.msg_type()))
.field("code", &self.code())
.field("checksum", &format!("0x{:04x}", self.checksum()))
.finish()
}
}
impl<E: Ipv6Packet> Icmpv6Message for RouterSolicitation<E> {
type Envelope = E;
#[inline]
fn msg_type() -> Icmpv6Type {
Icmpv6Types::RouterSolicitation
}
#[inline]
fn icmp(&self) -> &Icmpv6<Self::Envelope> {
&self.icmp
}
#[inline]
fn icmp_mut(&mut self) -> &mut Icmpv6<Self::Envelope> {
&mut self.icmp
}
#[inline]
fn into_icmp(self) -> Icmpv6<Self::Envelope> {
self.icmp
}
#[inline]
unsafe fn clone(&self, internal: Internal) -> Self {
RouterSolicitation {
icmp: self.icmp.clone(internal),
body: self.body,
}
}
#[inline]
fn try_parse(icmp: Icmpv6<Self::Envelope>, _internal: Internal) -> Fallible<Self> {
let mbuf = icmp.mbuf();
let offset = icmp.payload_offset();
let body = mbuf.read_data(offset)?;
Ok(RouterSolicitation { icmp, body })
}
#[inline]
fn try_push(mut icmp: Icmpv6<Self::Envelope>, _internal: Internal) -> Fallible<Self> {
let offset = icmp.payload_offset();
let mbuf = icmp.mbuf_mut();
mbuf.extend(offset, RouterSolicitationBody::size_of())?;
let body = mbuf.write_data(offset, &RouterSolicitationBody::default())?;
Ok(RouterSolicitation { icmp, body })
}
}
impl<E: Ipv6Packet> NdpPacket for RouterSolicitation<E> {
fn options_offset(&self) -> usize {
self.payload_offset() + RouterSolicitationBody::size_of()
}
}
#[derive(Clone, Copy, Debug, Default, SizeOf)]
#[repr(C, packed)]
struct RouterSolicitationBody {
_reserved: u32be,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::packets::ip::v6::Ipv6;
use crate::packets::Ethernet;
use crate::Mbuf;
#[test]
fn size_of_router_solicitation_body() {
assert_eq!(4, RouterSolicitationBody::size_of());
}
#[capsule::test]
fn push_and_set_router_solicitation() {
let packet = Mbuf::new().unwrap();
let ethernet = packet.push::<Ethernet>().unwrap();
let ipv6 = ethernet.push::<Ipv6>().unwrap();
let mut solicit = ipv6.push::<RouterSolicitation<Ipv6>>().unwrap();
assert_eq!(4, solicit.header_len());
assert_eq!(RouterSolicitationBody::size_of(), solicit.payload_len());
assert_eq!(Icmpv6Types::RouterSolicitation, solicit.msg_type());
assert_eq!(0, solicit.code());
solicit.reconcile_all();
assert!(solicit.checksum() != 0);
}
}