sixlowpan_udp_header.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #![no_main]
  2. use libfuzzer_sys::fuzz_target;
  3. use smoltcp::wire::{Ipv6Address, SixlowpanUdpNhcPacket, SixlowpanUdpNhcRepr};
  4. #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, arbitrary::Arbitrary)]
  5. pub struct AddressFuzzer(pub [u8; 16]);
  6. impl From<AddressFuzzer> for Ipv6Address {
  7. fn from(val: AddressFuzzer) -> Self {
  8. Ipv6Address(val.0)
  9. }
  10. }
  11. #[derive(Debug, arbitrary::Arbitrary)]
  12. struct SixlowpanUdpPacketFuzzer<'a> {
  13. data: &'a [u8],
  14. src_addr: AddressFuzzer,
  15. dst_addr: AddressFuzzer,
  16. }
  17. fuzz_target!(|fuzz: SixlowpanUdpPacketFuzzer| {
  18. if let Ok(ref frame) = SixlowpanUdpNhcPacket::new_checked(fuzz.data) {
  19. if let Ok(repr) = SixlowpanUdpNhcRepr::parse(
  20. frame,
  21. &fuzz.src_addr.into(),
  22. &fuzz.dst_addr.into(),
  23. ) {
  24. let payload = frame.payload();
  25. let mut buffer = vec![0; repr.header_len() + payload.len()];
  26. let mut frame = SixlowpanUdpNhcPacket::new_unchecked(&mut buffer[..]);
  27. repr.emit(
  28. &mut frame,
  29. &fuzz.src_addr.into(),
  30. &fuzz.dst_addr.into(),
  31. payload.len(),
  32. |b| b.copy_from_slice(payload),
  33. );
  34. }
  35. };
  36. });