ipv6routing.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. use super::{Error, Result};
  2. use core::fmt;
  3. use crate::wire::IpProtocol as Protocol;
  4. use crate::wire::Ipv6Address as Address;
  5. enum_with_unknown! {
  6. /// IPv6 Extension Routing Header Routing Type
  7. pub enum Type(u8) {
  8. /// Source Route (DEPRECATED)
  9. ///
  10. /// See https://tools.ietf.org/html/rfc5095 for details.
  11. Type0 = 0,
  12. /// Nimrod (DEPRECATED 2009-05-06)
  13. Nimrod = 1,
  14. /// Type 2 Routing Header for Mobile IPv6
  15. ///
  16. /// See https://tools.ietf.org/html/rfc6275#section-6.4 for details.
  17. Type2 = 2,
  18. /// RPL Source Routing Header
  19. ///
  20. /// See https://tools.ietf.org/html/rfc6554 for details.
  21. Rpl = 3,
  22. /// RFC3692-style Experiment 1
  23. ///
  24. /// See https://tools.ietf.org/html/rfc4727 for details.
  25. Experiment1 = 253,
  26. /// RFC3692-style Experiment 2
  27. ///
  28. /// See https://tools.ietf.org/html/rfc4727 for details.
  29. Experiment2 = 254,
  30. /// Reserved for future use
  31. Reserved = 252
  32. }
  33. }
  34. impl fmt::Display for Type {
  35. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  36. match *self {
  37. Type::Type0 => write!(f, "Type0"),
  38. Type::Nimrod => write!(f, "Nimrod"),
  39. Type::Type2 => write!(f, "Type2"),
  40. Type::Rpl => write!(f, "Rpl"),
  41. Type::Experiment1 => write!(f, "Experiment1"),
  42. Type::Experiment2 => write!(f, "Experiment2"),
  43. Type::Reserved => write!(f, "Reserved"),
  44. Type::Unknown(id) => write!(f, "{}", id),
  45. }
  46. }
  47. }
  48. /// A read/write wrapper around an IPv6 Routing Header buffer.
  49. #[derive(Debug, PartialEq, Eq)]
  50. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  51. pub struct Header<T: AsRef<[u8]>> {
  52. buffer: T,
  53. }
  54. // Format of the Routing Header
  55. //
  56. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  57. // | Next Header | Hdr Ext Len | Routing Type | Segments Left |
  58. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  59. // | |
  60. // . .
  61. // . type-specific data .
  62. // . .
  63. // | |
  64. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  65. //
  66. //
  67. // See https://tools.ietf.org/html/rfc8200#section-4.4 for details.
  68. mod field {
  69. #![allow(non_snake_case)]
  70. use crate::wire::field::*;
  71. // Minimum size of the header.
  72. pub const MIN_HEADER_SIZE: usize = 4;
  73. // 8-bit identifier of the header immediately following this header.
  74. pub const NXT_HDR: usize = 0;
  75. // 8-bit unsigned integer. Length of the DATA field in 8-octet units,
  76. // not including the first 8 octets.
  77. pub const LENGTH: usize = 1;
  78. // 8-bit identifier of a particular Routing header variant.
  79. pub const TYPE: usize = 2;
  80. // 8-bit unsigned integer. The number of route segments remaining.
  81. pub const SEG_LEFT: usize = 3;
  82. // Variable-length field. Routing-Type-specific data.
  83. //
  84. // Length of the header is in 8-octet units, not including the first 8 octets. The first four
  85. // octets are the next header type, the header length, routing type and segments left.
  86. pub fn DATA(length_field: u8) -> Field {
  87. let bytes = length_field as usize * 8 + 8;
  88. 4..bytes
  89. }
  90. // The Type 2 Routing Header has the following format:
  91. //
  92. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  93. // | Next Header | Hdr Ext Len=2 | Routing Type=2|Segments Left=1|
  94. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  95. // | Reserved |
  96. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  97. // | |
  98. // + +
  99. // | |
  100. // + Home Address +
  101. // | |
  102. // + +
  103. // | |
  104. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  105. // 16-byte field containing the home address of the destination mobile node.
  106. pub const HOME_ADDRESS: Field = 8..24;
  107. // The RPL Source Routing Header has the following format:
  108. //
  109. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  110. // | Next Header | Hdr Ext Len | Routing Type | Segments Left |
  111. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  112. // | CmprI | CmprE | Pad | Reserved |
  113. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  114. // | |
  115. // . .
  116. // . Addresses[1..n] .
  117. // . .
  118. // | |
  119. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  120. // 8-bit field containing the CmprI and CmprE values.
  121. pub const CMPR: usize = 4;
  122. // 8-bit field containing the Pad value.
  123. pub const PAD: usize = 5;
  124. // Variable length field containing addresses
  125. pub fn ADDRESSES(length_field: u8) -> Field {
  126. let data = DATA(length_field);
  127. 8..data.end
  128. }
  129. }
  130. /// Core getter methods relevant to any routing type.
  131. impl<T: AsRef<[u8]>> Header<T> {
  132. /// Create a raw octet buffer with an IPv6 Routing Header structure.
  133. pub fn new(buffer: T) -> Header<T> {
  134. Header { buffer }
  135. }
  136. /// Shorthand for a combination of [new_unchecked] and [check_len].
  137. ///
  138. /// [new_unchecked]: #method.new_unchecked
  139. /// [check_len]: #method.check_len
  140. pub fn new_checked(buffer: T) -> Result<Header<T>> {
  141. let header = Self::new(buffer);
  142. header.check_len()?;
  143. Ok(header)
  144. }
  145. /// Ensure that no accessor method will panic if called.
  146. /// Returns `Err(Error)` if the buffer is too short.
  147. ///
  148. /// The result of this check is invalidated by calling [set_header_len].
  149. ///
  150. /// [set_header_len]: #method.set_header_len
  151. pub fn check_len(&self) -> Result<()> {
  152. let len = self.buffer.as_ref().len();
  153. if len < field::MIN_HEADER_SIZE {
  154. return Err(Error);
  155. }
  156. if len < field::DATA(self.header_len()).end as usize {
  157. return Err(Error);
  158. }
  159. // The header lenght field could be wrong and thus we need to check this as well:
  160. if matches!(self.routing_type(), Type::Type2)
  161. && field::DATA(self.header_len()).end != field::HOME_ADDRESS.end
  162. {
  163. return Err(Error);
  164. }
  165. Ok(())
  166. }
  167. /// Consume the header, returning the underlying buffer.
  168. pub fn into_inner(self) -> T {
  169. self.buffer
  170. }
  171. /// Return the next header field.
  172. #[inline]
  173. pub fn next_header(&self) -> Protocol {
  174. let data = self.buffer.as_ref();
  175. Protocol::from(data[field::NXT_HDR])
  176. }
  177. /// Return the header length field. Length of the Routing header in 8-octet units,
  178. /// not including the first 8 octets.
  179. #[inline]
  180. pub fn header_len(&self) -> u8 {
  181. let data = self.buffer.as_ref();
  182. data[field::LENGTH]
  183. }
  184. /// Return the routing type field.
  185. #[inline]
  186. pub fn routing_type(&self) -> Type {
  187. let data = self.buffer.as_ref();
  188. Type::from(data[field::TYPE])
  189. }
  190. /// Return the segments left field.
  191. #[inline]
  192. pub fn segments_left(&self) -> u8 {
  193. let data = self.buffer.as_ref();
  194. data[field::SEG_LEFT]
  195. }
  196. }
  197. /// Getter methods for the Type 2 Routing Header routing type.
  198. impl<T: AsRef<[u8]>> Header<T> {
  199. /// Return the IPv6 Home Address
  200. ///
  201. /// # Panics
  202. /// This function may panic if this header is not the Type2 Routing Header routing type.
  203. pub fn home_address(&self) -> Address {
  204. let data = self.buffer.as_ref();
  205. Address::from_bytes(&data[field::HOME_ADDRESS])
  206. }
  207. }
  208. /// Getter methods for the RPL Source Routing Header routing type.
  209. impl<T: AsRef<[u8]>> Header<T> {
  210. /// Return the number of prefix octets elided from addresses[1..n-1].
  211. ///
  212. /// # Panics
  213. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  214. pub fn cmpr_i(&self) -> u8 {
  215. let data = self.buffer.as_ref();
  216. data[field::CMPR] >> 4
  217. }
  218. /// Return the number of prefix octets elided from the last address (`addresses[n]`).
  219. ///
  220. /// # Panics
  221. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  222. pub fn cmpr_e(&self) -> u8 {
  223. let data = self.buffer.as_ref();
  224. data[field::CMPR] & 0xf
  225. }
  226. /// Return the number of octets used for padding after `addresses[n]`.
  227. ///
  228. /// # Panics
  229. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  230. pub fn pad(&self) -> u8 {
  231. let data = self.buffer.as_ref();
  232. data[field::PAD] >> 4
  233. }
  234. /// Return the address vector in bytes
  235. ///
  236. /// # Panics
  237. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  238. pub fn addresses(&self) -> &[u8] {
  239. let data = self.buffer.as_ref();
  240. &data[field::ADDRESSES(data[field::LENGTH])]
  241. }
  242. }
  243. /// Core setter methods relevant to any routing type.
  244. impl<T: AsRef<[u8]> + AsMut<[u8]>> Header<T> {
  245. /// Set the next header field.
  246. #[inline]
  247. pub fn set_next_header(&mut self, value: Protocol) {
  248. let data = self.buffer.as_mut();
  249. data[field::NXT_HDR] = value.into();
  250. }
  251. /// Set the option data length. Length of the Routing header in 8-octet units.
  252. #[inline]
  253. pub fn set_header_len(&mut self, value: u8) {
  254. let data = self.buffer.as_mut();
  255. data[field::LENGTH] = value;
  256. }
  257. /// Set the routing type.
  258. #[inline]
  259. pub fn set_routing_type(&mut self, value: Type) {
  260. let data = self.buffer.as_mut();
  261. data[field::TYPE] = value.into();
  262. }
  263. /// Set the segments left field.
  264. #[inline]
  265. pub fn set_segments_left(&mut self, value: u8) {
  266. let data = self.buffer.as_mut();
  267. data[field::SEG_LEFT] = value;
  268. }
  269. /// Initialize reserved fields to 0.
  270. ///
  271. /// # Panics
  272. /// This function may panic if the routing type is not set.
  273. #[inline]
  274. pub fn clear_reserved(&mut self) {
  275. let routing_type = self.routing_type();
  276. let data = self.buffer.as_mut();
  277. match routing_type {
  278. Type::Type2 => {
  279. data[4] = 0;
  280. data[5] = 0;
  281. data[6] = 0;
  282. data[7] = 0;
  283. }
  284. Type::Rpl => {
  285. // Retain the higher order 4 bits of the padding field
  286. data[field::PAD] &= 0xF0;
  287. data[6] = 0;
  288. data[7] = 0;
  289. }
  290. _ => panic!("Unrecognized routing type when clearing reserved fields."),
  291. }
  292. }
  293. }
  294. /// Setter methods for the RPL Source Routing Header routing type.
  295. impl<T: AsRef<[u8]> + AsMut<[u8]>> Header<T> {
  296. /// Set the Ipv6 Home Address
  297. ///
  298. /// # Panics
  299. /// This function may panic if this header is not the Type 2 Routing Header routing type.
  300. pub fn set_home_address(&mut self, value: Address) {
  301. let data = self.buffer.as_mut();
  302. data[field::HOME_ADDRESS].copy_from_slice(value.as_bytes());
  303. }
  304. }
  305. /// Setter methods for the RPL Source Routing Header routing type.
  306. impl<T: AsRef<[u8]> + AsMut<[u8]>> Header<T> {
  307. /// Set the number of prefix octets elided from addresses[1..n-1].
  308. ///
  309. /// # Panics
  310. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  311. pub fn set_cmpr_i(&mut self, value: u8) {
  312. let data = self.buffer.as_mut();
  313. let raw = (value << 4) | (data[field::CMPR] & 0xF);
  314. data[field::CMPR] = raw;
  315. }
  316. /// Set the number of prefix octets elided from the last address (`addresses[n]`).
  317. ///
  318. /// # Panics
  319. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  320. pub fn set_cmpr_e(&mut self, value: u8) {
  321. let data = self.buffer.as_mut();
  322. let raw = (value & 0xF) | (data[field::CMPR] & 0xF0);
  323. data[field::CMPR] = raw;
  324. }
  325. /// Set the number of octets used for padding after `addresses[n]`.
  326. ///
  327. /// # Panics
  328. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  329. pub fn set_pad(&mut self, value: u8) {
  330. let data = self.buffer.as_mut();
  331. data[field::PAD] = value << 4;
  332. }
  333. /// Set address data
  334. ///
  335. /// # Panics
  336. /// This function may panic if this header is not the RPL Source Routing Header routing type.
  337. pub fn set_addresses(&mut self, value: &[u8]) {
  338. let data = self.buffer.as_mut();
  339. let len = data[field::LENGTH];
  340. let addresses = &mut data[field::ADDRESSES(len)];
  341. addresses.copy_from_slice(value);
  342. }
  343. }
  344. impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Header<&'a T> {
  345. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  346. match Repr::parse(self) {
  347. Ok(repr) => write!(f, "{}", repr),
  348. Err(err) => {
  349. write!(f, "IPv6 Routing ({})", err)?;
  350. Ok(())
  351. }
  352. }
  353. }
  354. }
  355. /// A high-level representation of an IPv6 Routing Header.
  356. #[derive(Debug, PartialEq, Eq, Clone, Copy)]
  357. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  358. #[non_exhaustive]
  359. pub enum Repr<'a> {
  360. Type2 {
  361. /// The type of header immediately following the Routing header.
  362. next_header: Protocol,
  363. /// Length of the Routing header in 8-octet units, not including the first 8 octets.
  364. length: u8,
  365. /// Number of route segments remaining.
  366. segments_left: u8,
  367. /// The home address of the destination mobile node.
  368. home_address: Address,
  369. },
  370. Rpl {
  371. /// The type of header immediately following the Routing header.
  372. next_header: Protocol,
  373. /// Length of the Routing header in 8-octet units, not including the first 8 octets.
  374. length: u8,
  375. /// Number of route segments remaining.
  376. segments_left: u8,
  377. /// Number of prefix octets from each segment, except the last segment, that are elided.
  378. cmpr_i: u8,
  379. /// Number of prefix octets from the last segment that are elided.
  380. cmpr_e: u8,
  381. /// Number of octets that are used for padding after `address[n]` at the end of the
  382. /// RPL Source Route Header.
  383. pad: u8,
  384. /// Vector of addresses, numbered 1 to `n`.
  385. addresses: &'a [u8],
  386. },
  387. }
  388. impl<'a> Repr<'a> {
  389. /// Parse an IPv6 Routing Header and return a high-level representation.
  390. pub fn parse<T>(header: &'a Header<&'a T>) -> Result<Repr<'a>>
  391. where
  392. T: AsRef<[u8]> + ?Sized,
  393. {
  394. match header.routing_type() {
  395. Type::Type2 => Ok(Repr::Type2 {
  396. next_header: header.next_header(),
  397. length: header.header_len(),
  398. segments_left: header.segments_left(),
  399. home_address: header.home_address(),
  400. }),
  401. Type::Rpl => Ok(Repr::Rpl {
  402. next_header: header.next_header(),
  403. length: header.header_len(),
  404. segments_left: header.segments_left(),
  405. cmpr_i: header.cmpr_i(),
  406. cmpr_e: header.cmpr_e(),
  407. pad: header.pad(),
  408. addresses: header.addresses(),
  409. }),
  410. _ => Err(Error),
  411. }
  412. }
  413. /// Return the length, in bytes, of a header that will be emitted from this high-level
  414. /// representation.
  415. pub fn buffer_len(&self) -> usize {
  416. match self {
  417. &Repr::Rpl { length, .. } | &Repr::Type2 { length, .. } => field::DATA(length).end,
  418. }
  419. }
  420. /// Emit a high-level representation into an IPv6 Routing Header.
  421. pub fn emit<T: AsRef<[u8]> + AsMut<[u8]> + ?Sized>(&self, header: &mut Header<&mut T>) {
  422. match *self {
  423. Repr::Type2 {
  424. next_header,
  425. length,
  426. segments_left,
  427. home_address,
  428. } => {
  429. header.set_next_header(next_header);
  430. header.set_header_len(length);
  431. header.set_routing_type(Type::Type2);
  432. header.set_segments_left(segments_left);
  433. header.clear_reserved();
  434. header.set_home_address(home_address);
  435. }
  436. Repr::Rpl {
  437. next_header,
  438. length,
  439. segments_left,
  440. cmpr_i,
  441. cmpr_e,
  442. pad,
  443. addresses,
  444. } => {
  445. header.set_next_header(next_header);
  446. header.set_header_len(length);
  447. header.set_routing_type(Type::Rpl);
  448. header.set_segments_left(segments_left);
  449. header.set_cmpr_i(cmpr_i);
  450. header.set_cmpr_e(cmpr_e);
  451. header.set_pad(pad);
  452. header.clear_reserved();
  453. header.set_addresses(addresses);
  454. }
  455. }
  456. }
  457. }
  458. impl<'a> fmt::Display for Repr<'a> {
  459. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  460. match *self {
  461. Repr::Type2 {
  462. next_header,
  463. length,
  464. segments_left,
  465. home_address,
  466. } => {
  467. write!(
  468. f,
  469. "IPv6 Routing next_hdr={} length={} type={} seg_left={} home_address={}",
  470. next_header,
  471. length,
  472. Type::Type2,
  473. segments_left,
  474. home_address
  475. )
  476. }
  477. Repr::Rpl {
  478. next_header,
  479. length,
  480. segments_left,
  481. cmpr_i,
  482. cmpr_e,
  483. pad,
  484. ..
  485. } => {
  486. write!(f, "IPv6 Routing next_hdr={} length={} type={} seg_left={} cmpr_i={} cmpr_e={} pad={}",
  487. next_header, length, Type::Rpl, segments_left, cmpr_i, cmpr_e, pad)
  488. }
  489. }
  490. }
  491. }
  492. #[cfg(test)]
  493. mod test {
  494. use super::*;
  495. // A Type 2 Routing Header
  496. static BYTES_TYPE2: [u8; 24] = [
  497. 0x6, 0x2, 0x2, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  498. 0x0, 0x0, 0x0, 0x0, 0x0, 0x1,
  499. ];
  500. // A representation of a Type 2 Routing header
  501. static REPR_TYPE2: Repr = Repr::Type2 {
  502. next_header: Protocol::Tcp,
  503. length: 2,
  504. segments_left: 1,
  505. home_address: Address::LOOPBACK,
  506. };
  507. // A Source Routing Header with full IPv6 addresses in bytes
  508. static BYTES_SRH_FULL: [u8; 40] = [
  509. 0x6, 0x4, 0x3, 0x2, 0x0, 0x0, 0x0, 0x0, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  510. 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  511. 0x0, 0x0, 0x3, 0x1,
  512. ];
  513. // A representation of a Source Routing Header with full IPv6 addresses
  514. static REPR_SRH_FULL: Repr = Repr::Rpl {
  515. next_header: Protocol::Tcp,
  516. length: 4,
  517. segments_left: 2,
  518. cmpr_i: 0,
  519. cmpr_e: 0,
  520. pad: 0,
  521. addresses: &[
  522. 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0xfd,
  523. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x1,
  524. ],
  525. };
  526. // A Source Routing Header with elided IPv6 addresses in bytes
  527. static BYTES_SRH_ELIDED: [u8; 16] = [
  528. 0x6, 0x1, 0x3, 0x2, 0xfe, 0x50, 0x0, 0x0, 0x2, 0x3, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
  529. ];
  530. // A representation of a Source Routing Header with elided IPv6 addresses
  531. static REPR_SRH_ELIDED: Repr = Repr::Rpl {
  532. next_header: Protocol::Tcp,
  533. length: 1,
  534. segments_left: 2,
  535. cmpr_i: 15,
  536. cmpr_e: 14,
  537. pad: 5,
  538. addresses: &[0x2, 0x3, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0],
  539. };
  540. #[test]
  541. fn test_check_len() {
  542. // less than min header size
  543. assert_eq!(Err(Error), Header::new(&BYTES_TYPE2[..3]).check_len());
  544. assert_eq!(Err(Error), Header::new(&BYTES_SRH_FULL[..3]).check_len());
  545. assert_eq!(Err(Error), Header::new(&BYTES_SRH_ELIDED[..3]).check_len());
  546. // less than specified length field
  547. assert_eq!(Err(Error), Header::new(&BYTES_TYPE2[..23]).check_len());
  548. assert_eq!(Err(Error), Header::new(&BYTES_SRH_FULL[..39]).check_len());
  549. assert_eq!(Err(Error), Header::new(&BYTES_SRH_ELIDED[..11]).check_len());
  550. // valid
  551. assert_eq!(Ok(()), Header::new(&BYTES_TYPE2[..]).check_len());
  552. assert_eq!(Ok(()), Header::new(&BYTES_SRH_FULL[..]).check_len());
  553. assert_eq!(Ok(()), Header::new(&BYTES_SRH_ELIDED[..]).check_len());
  554. }
  555. #[test]
  556. fn test_header_deconstruct() {
  557. let header = Header::new(&BYTES_TYPE2[..]);
  558. assert_eq!(header.next_header(), Protocol::Tcp);
  559. assert_eq!(header.header_len(), 2);
  560. assert_eq!(header.routing_type(), Type::Type2);
  561. assert_eq!(header.segments_left(), 1);
  562. assert_eq!(header.home_address(), Address::LOOPBACK);
  563. let header = Header::new(&BYTES_SRH_FULL[..]);
  564. assert_eq!(header.next_header(), Protocol::Tcp);
  565. assert_eq!(header.header_len(), 4);
  566. assert_eq!(header.routing_type(), Type::Rpl);
  567. assert_eq!(header.segments_left(), 2);
  568. assert_eq!(header.addresses(), &BYTES_SRH_FULL[8..]);
  569. let header = Header::new(&BYTES_SRH_ELIDED[..]);
  570. assert_eq!(header.next_header(), Protocol::Tcp);
  571. assert_eq!(header.header_len(), 1);
  572. assert_eq!(header.routing_type(), Type::Rpl);
  573. assert_eq!(header.segments_left(), 2);
  574. assert_eq!(header.addresses(), &BYTES_SRH_ELIDED[8..]);
  575. }
  576. #[test]
  577. fn test_repr_parse_valid() {
  578. let header = Header::new_checked(&BYTES_TYPE2[..]).unwrap();
  579. let repr = Repr::parse(&header).unwrap();
  580. assert_eq!(repr, REPR_TYPE2);
  581. let header = Header::new_checked(&BYTES_SRH_FULL[..]).unwrap();
  582. let repr = Repr::parse(&header).unwrap();
  583. assert_eq!(repr, REPR_SRH_FULL);
  584. let header = Header::new_checked(&BYTES_SRH_ELIDED[..]).unwrap();
  585. let repr = Repr::parse(&header).unwrap();
  586. assert_eq!(repr, REPR_SRH_ELIDED);
  587. }
  588. #[test]
  589. fn test_repr_emit() {
  590. let mut bytes = [0u8; 24];
  591. let mut header = Header::new(&mut bytes[..]);
  592. REPR_TYPE2.emit(&mut header);
  593. assert_eq!(header.into_inner(), &BYTES_TYPE2[..]);
  594. let mut bytes = [0u8; 40];
  595. let mut header = Header::new(&mut bytes[..]);
  596. REPR_SRH_FULL.emit(&mut header);
  597. assert_eq!(header.into_inner(), &BYTES_SRH_FULL[..]);
  598. let mut bytes = [0u8; 16];
  599. let mut header = Header::new(&mut bytes[..]);
  600. REPR_SRH_ELIDED.emit(&mut header);
  601. assert_eq!(header.into_inner(), &BYTES_SRH_ELIDED[..]);
  602. }
  603. #[test]
  604. fn test_buffer_len() {
  605. assert_eq!(REPR_TYPE2.buffer_len(), 24);
  606. assert_eq!(REPR_SRH_FULL.buffer_len(), 40);
  607. assert_eq!(REPR_SRH_ELIDED.buffer_len(), 16);
  608. }
  609. }