ipv6routing.rs 24 KB

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