50 releases (29 breaking)

0.35.0 May 30, 2024
0.34.0 Jul 19, 2023
0.33.0 Feb 3, 2023
0.31.0 Jun 1, 2022
0.1.0 Jul 31, 2015

#1908 in Network programming

Download history 56800/week @ 2024-06-16 53651/week @ 2024-06-23 60152/week @ 2024-06-30 60571/week @ 2024-07-07 58941/week @ 2024-07-14 70113/week @ 2024-07-21 63029/week @ 2024-07-28 64953/week @ 2024-08-04 64917/week @ 2024-08-11 66942/week @ 2024-08-18 66783/week @ 2024-08-25 65730/week @ 2024-09-01 51183/week @ 2024-09-08 51874/week @ 2024-09-15 59777/week @ 2024-09-22 49393/week @ 2024-09-29

215,410 downloads per month
Used in 244 crates (7 directly)

MIT/Apache

110KB
2.5K SLoC

libpnet Crates.io License Documentation

Build Status: Build Status

Discussion and support:

libpnet provides a cross-platform API for low level networking using Rust.

There are four key components:

  • The packet module, allowing safe construction and manipulation of packets;
  • The pnet_macros crate, providing infrastructure for the packet module;
  • The transport module, which allows implementation of transport protocols;
  • The datalink module, which allows sending and receiving data link packets directly.

Why?

There are lots of reasons to use low level networking, and many more to do it using Rust. A few are outlined here:

Developing Transport Protocols

There are usually two ways to go about developing a new transport layer protocol:

  • Write it in a scripting language such as Python;
  • Write it using C.

The former is great for trying out new ideas and rapid prototyping, however not so great as a real-world implementation. While you can usually get reasonable performance out of these implementations, they're generally significantly slower than an implementation in C, and not suitable for any "heavy lifting".

The next option is to write it in C - this will give you great performance, but comes with a number of other issues:

  • Lack of memory safety - this is a huge source of security vulnerabilities and other bugs in C-based network stacks. It is far too easy to forget a bounds check or use a pointer after it is freed.
  • Lack of thread safety - you have to be very careful to make sure the correct locks are used, and used correctly.
  • Lack of high level abstractions - part of the appeal of scripting languages such as Python is the higher level of abstraction which enables simpler APIs and ease of programming.

Using libpnet and Rust, you get the best of both worlds. The higher level abstractions, memory and thread safety, alongside the performance of C.

Network Utilities

Many networking utilities such as ping and traceroute rely on being able to manipulate network and transport headers, which isn't possible with standard networking stacks such as those provided by std::io::net.

It can be useful to work directly at the data link layer, to see packets as they are "on the wire". There are lots of uses for this, including network diagnostics, packet capture and traffic shaping.

Documentation

API documentation for the latest build can be found here: https://docs.rs/pnet/

Usage

To use libpnet in your project, add the following to your Cargo.toml:

[dependencies.pnet]
version = "0.35.0"

libpnet should work with the latest stable version of Rust.

When running the test suite, there are a number of networking tests which will likely fail - the easiest way to workaround this is to run cargo test as a root or administrative user. This can often be avoided, however it is more involved.

Windows

There are three requirements for building on Windows:

  • You must use a version of Rust which uses the MSVC toolchain
  • You must have WinPcap or npcap installed (tested with version WinPcap 4.1.3) (If using npcap, make sure to install with the "Install Npcap in WinPcap API-compatible Mode")
  • You must place Packet.lib from the WinPcap Developers pack in a directory named lib, in the root of this repository. Alternatively, you can use any of the locations listed in the %LIB%/$Env:LIB environment variables. For the 64 bit toolchain it is in WpdPack/Lib/x64/Packet.lib, for the 32 bit toolchain, it is in WpdPack/Lib/Packet.lib.

Dependencies

~2.4–4MB
~71K SLoC