Giter VIP home page Giter VIP logo

rust-packet's Introduction

packet Crates.io packet WTFPL Build Status

This crate allows the parsing and creation of various network packets with an ergonomic API.

Usage

First, add the following to your Cargo.toml:

[dependencies]
packet = "0.1"

Next, add this to your crate root:

extern crate packet;

Packet

Packets take any type implementing AsRef<[u8]>, this means they can borrow an &[u8] or own a Vec<u8> or your own buffer type without needing different types or annoying lifetime parameters.

If the type also implements AsMut<[u8]> some fields are modifiable in place instead of going through a builder, not all fields are settable this way, for instance any fields that have a dynamic size.

Fields in any packet are parsed lazily directly from the buffer, the only exception are fields that are required to verify the packet is correct.

The correctness check doesn't do any checksum validation, this is because some protocols require additional information to calculate the checksum, for instance TCP and UDP require the IP packet to validate.

Buffer

Buffers are abstractions over growable or static slices, they implement a layered setup where each builder creates its own layer and any accesses to the buffer start from where the layer starts.

The buffer can be grown, but the operation may fail if the underlying buffer is not growable, static buffers will only fail if they can't accomodate the requested size.

Builder

Builders are structures that take a Buffer (or create one internally) and incrementally define a new packet.

Builders of upper layer protocols usually provide a way to create a specific sub-protocol, for example ip::v4::Builder allows creating an udp::Builder or tcp::Builder and deal with checksuming and length delimiting as needed.

Examples

Creating an ICMP packet echo request packet.

extern crate packet;
use packet::builder::Builder;
use packet::icmp;

fn main() {
	let packet = icmp::Builder::default()
	  .echo().unwrap().request().unwrap()
	  .identifier(42).unwrap()
			.sequence(2).unwrap()
			.payload(b"test").unwrap()
			.build().unwrap();
}

rust-packet's People

Contributors

fdubois1 avatar meh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.