All Projects → alexforster → pdu

alexforster / pdu

Licence: Apache-2.0 license
Small, fast, and correct L2/L3/L4 packet parser.

Programming Languages

rust
11053 projects
shell
77523 projects
RenderScript
48 projects

Projects that are alternatives of or similar to pdu

rust android ios
Android / iOS app with shared Rust logic
Stars: ✭ 182 (+435.29%)
Mutual labels:  rustlang
flowd
An inter-language runtime for flow-based programming (FBP)
Stars: ✭ 18 (-47.06%)
Mutual labels:  rustlang
headlines
[WIP] A simple news reading GUI app built in Rust
Stars: ✭ 92 (+170.59%)
Mutual labels:  rustlang
WorkingGroup
Issues tracker for ideas, ongoing work, looking for mentors, mentors available. Join here!
Stars: ✭ 37 (+8.82%)
Mutual labels:  rustlang
taileater
A puzzle game where you eat your own tail to win!
Stars: ✭ 19 (-44.12%)
Mutual labels:  rustlang
Rustup
The Rust toolchain installer
Stars: ✭ 4,394 (+12823.53%)
Mutual labels:  rustlang
enum-display-derive
Derive Display for simple enums automagically
Stars: ✭ 22 (-35.29%)
Mutual labels:  rustlang
rust-advent
Learning Rust by solving advent of code challenges (Streaming live on Twitch every Monday)
Stars: ✭ 20 (-41.18%)
Mutual labels:  rustlang
craftql
A CLI tool to visualize GraphQL schemas and to output a graph data structure as a graphviz .dot format
Stars: ✭ 75 (+120.59%)
Mutual labels:  rustlang
Node
MASQ combines the benefits of VPN and Tor technology to create a superior next-generation privacy software, where users are rewarded for supporting an uncensored global web. Users gain privacy and anonymity online, while helping promote Internet Freedom.
Stars: ✭ 123 (+261.76%)
Mutual labels:  rustlang
hypergraph
Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.
Stars: ✭ 205 (+502.94%)
Mutual labels:  rustlang
image-hub
Image Hub is a sample application for exploring WebAssembly modules used as Envoy filters.
Stars: ✭ 56 (+64.71%)
Mutual labels:  rustlang
rust-for-backend-development
SWITCH TO RUST AND STOP WASTING YOUR TIME WITH JAVASCRIPT RUNTIME EXCEPTIONS
Stars: ✭ 30 (-11.76%)
Mutual labels:  rustlang
rust-wasm-webpack
A simple boilerplate to get WebAssembly (WASM) code generated by Rust and bundled by Webpack!
Stars: ✭ 88 (+158.82%)
Mutual labels:  rustlang
nativeshell
Experimental embedder for Flutter
Stars: ✭ 553 (+1526.47%)
Mutual labels:  rustlang
mqtt rs
MQTT broker in Rust
Stars: ✭ 23 (-32.35%)
Mutual labels:  rustlang
goprisma
A Go wrapper for prisma to turn databases into GraphQL APIs using Go.
Stars: ✭ 54 (+58.82%)
Mutual labels:  rustlang
wasmbin
A self-generating WebAssembly parser & serializer in Rust.
Stars: ✭ 40 (+17.65%)
Mutual labels:  rustlang
coding-challenge
Algorithms and Data-structures, problems and solutions in Rust language using cargo-workspaces
Stars: ✭ 17 (-50%)
Mutual labels:  rustlang
easy reader
⏮ ⏯ ⏭ A Rust library for easily navigating forward, backward or randomly through the lines of huge files.
Stars: ✭ 83 (+144.12%)
Mutual labels:  rustlang

pdu

Small, fast, and correct L2/L3/L4 packet parser.

Author: Alex Forster <[email protected]>
License: Apache-2.0

build status crates.io version docs.rs

Small

  • Fully-featured no_std support
  • No Crate dependencies and no macros
  • Internet protocols only: application-layer protocols are out of scope

Fast

  • Lazy parsing: only the fields that you access are parsed
  • Zero-copy construction: no heap allocations are performed

Correct

  • Tested against Wireshark to ensure all packet fields are parsed correctly
  • Fuzzed using Honggfuzz to ensure invalid input does not cause panics
  • Does not use any unsafe code

Supported Protocols

The following protocol hierarchy can be parsed with this library:

  • Ethernet (including vlan)
    • ARP
    • IPv4 (including options)
      • TCP (including options)
      • UDP
      • ICMP
      • GREv0
        • ...Ethernet, IPv4, IPv6...
    • IPv6 (including extension headers)
      • TCP (including options)
      • UDP
      • ICMPv6
      • GREv0
        • ...Ethernet, IPv4, IPv6...

In addition, unrecognized upper protocols are accessible as bytes via Raw enum variants.

Getting Started

Cargo.toml

[dependencies]
pdu = "1.1"

Examples

use pdu::*;

// parse a layer 2 (Ethernet) packet using EthernetPdu::new()

fn main() {
    let packet: &[u8] = &[
        0x68, 0x5b, 0x35, 0xc0, 0x61, 0xb6, 0x00, 0x1d, 0x09, 0x94, 0x65, 0x38, 0x08, 0x00, 0x45, 0x00, 0x00,
        0x3b, 0x2d, 0xfd, 0x00, 0x00, 0x40, 0x11, 0xbc, 0x43, 0x83, 0xb3, 0xc4, 0x2e, 0x83, 0xb3, 0xc4, 0xdc,
        0x18, 0xdb, 0x18, 0xdb, 0x00, 0x27, 0xe0, 0x3e, 0x05, 0x1d, 0x07, 0x15, 0x08, 0x07, 0x65, 0x78, 0x61,
        0x6d, 0x70, 0x6c, 0x65, 0x08, 0x07, 0x74, 0x65, 0x73, 0x74, 0x41, 0x70, 0x70, 0x08, 0x01, 0x31, 0x0a,
        0x04, 0x1e, 0xcc, 0xe2, 0x51,
    ];
    
    match EthernetPdu::new(&packet) {
        Ok(ethernet_pdu) => {
            println!("[ethernet] destination_address: {:x?}", ethernet_pdu.destination_address().as_ref());
            println!("[ethernet] source_address: {:x?}", ethernet_pdu.source_address().as_ref());
            println!("[ethernet] ethertype: 0x{:04x}", ethernet_pdu.ethertype());
            if let Some(vlan) = ethernet_pdu.vlan() {
                println!("[ethernet] vlan: 0x{:04x}", vlan);
            }
            // upper-layer protocols can be accessed via the inner() method
            match ethernet_pdu.inner() {
                Ok(Ethernet::Ipv4(ipv4_pdu)) => {
                    println!("[ipv4] source_address: {:x?}", ipv4_pdu.source_address().as_ref());
                    println!("[ipv4] destination_address: {:x?}", ipv4_pdu.destination_address().as_ref());
                    println!("[ipv4] protocol: 0x{:02x}", ipv4_pdu.protocol());
                    // upper-layer protocols can be accessed via the inner() method (not shown)
                }
                Ok(Ethernet::Ipv6(ipv6_pdu)) => {
                    println!("[ipv6] source_address: {:x?}", ipv6_pdu.source_address().as_ref());
                    println!("[ipv6] destination_address: {:x?}", ipv6_pdu.destination_address().as_ref());
                    println!("[ipv6] protocol: 0x{:02x}", ipv6_pdu.computed_protocol());
                    // upper-layer protocols can be accessed via the inner() method (not shown)
                }
                Ok(other) => {
                    panic!("Unexpected protocol {:?}", other);
                }
                Err(e) => {
                    panic!("EthernetPdu::inner() parser failure: {:?}", e);
                }
            }
        }
        Err(e) => {
            panic!("EthernetPdu::new() parser failure: {:?}", e);
        }
    }
}
use pdu::*;

// parse a layer 3 (IP) packet using Ip::new()

fn main() {
    let packet: &[u8] = &[
        0x45, 0x00, 0x00, 0x3b, 0x2d, 0xfd, 0x00, 0x00, 0x40, 0x11, 0xbc, 0x43, 0x83, 0xb3, 0xc4, 0x2e, 0x83, 0xb3,
        0xc4, 0xdc, 0x18, 0xdb, 0x18, 0xdb, 0x00, 0x27, 0xe0, 0x3e, 0x05, 0x1d, 0x07, 0x15, 0x08, 0x07, 0x65, 0x78,
        0x61, 0x6d, 0x70, 0x6c, 0x65, 0x08, 0x07, 0x74, 0x65, 0x73, 0x74, 0x41, 0x70, 0x70, 0x08, 0x01, 0x31, 0x0a,
        0x04, 0x1e, 0xcc, 0xe2, 0x51,
    ];

    match Ip::new(&packet) {
        Ok(Ip::Ipv4(ipv4_pdu)) => {
            println!("[ipv4] source_address: {:x?}", ipv4_pdu.source_address().as_ref());
            println!("[ipv4] destination_address: {:x?}", ipv4_pdu.destination_address().as_ref());
            println!("[ipv4] protocol: 0x{:02x}", ipv4_pdu.protocol());
            // upper-layer protocols can be accessed via the inner() method (not shown)
        }
        Ok(Ip::Ipv6(ipv6_pdu)) => {
            println!("[ipv6] source_address: {:x?}", ipv6_pdu.source_address().as_ref());
            println!("[ipv6] destination_address: {:x?}", ipv6_pdu.destination_address().as_ref());
            println!("[ipv6] protocol: 0x{:02x}", ipv6_pdu.computed_protocol());
            // upper-layer protocols can be accessed via the inner() method (not shown)
        }
        Err(e) => {
            panic!("Ip::new() parser failure: {:?}", e);
        }
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].