| 1 | # libpnet [](https://crates.io/crates/pnet)  [](https://docs.rs/pnet/) |
| 2 | |
| 3 | Build Status: [](https://github.com/libpnet/libpnet/actions/workflows/ci.yml) |
| 4 | |
| 5 | Discussion and support: |
| 6 | |
| 7 | * Live chat on IRC - [#libpnet on irc.libera.chat](https://kiwiirc.com/nextclient/irc.libera.chat/libpnet?nick=pnet-user42) |
| 8 | * [GitHub Discussions](https://github.com/libpnet/libpnet/discussions) |
| 9 | |
| 10 | `libpnet` provides a cross-platform API for low level networking using Rust. |
| 11 | |
| 12 | There are four key components: |
| 13 | |
| 14 | * The `packet` module, allowing safe construction and manipulation of packets; |
| 15 | * The `pnet_macros` crate, providing infrastructure for the packet module; |
| 16 | * The `transport` module, which allows implementation of transport protocols; |
| 17 | * The `datalink` module, which allows sending and receiving data link packets directly. |
| 18 | |
| 19 | ## Why? |
| 20 | |
| 21 | There are lots of reasons to use low level networking, and many more to do it using Rust. A few are |
| 22 | outlined here: |
| 23 | |
| 24 | ### Developing Transport Protocols |
| 25 | |
| 26 | There are usually two ways to go about developing a new transport layer protocol: |
| 27 | |
| 28 | * Write it in a scripting language such as Python; |
| 29 | * Write it using C. |
| 30 | |
| 31 | The former is great for trying out new ideas and rapid prototyping, however not so great as a |
| 32 | real-world implementation. While you can usually get reasonable performance out of these |
| 33 | implementations, they're generally significantly slower than an implementation in C, and not |
| 34 | suitable for any "heavy lifting". |
| 35 | |
| 36 | The next option is to write it in C - this will give you great performance, but comes with a number |
| 37 | of other issues: |
| 38 | |
| 39 | * Lack of memory safety - this is a huge source of security vulnerabilities and other bugs in |
| 40 | C-based network stacks. It is far too easy to forget a bounds check or use a pointer after it is |
| 41 | freed. |
| 42 | * Lack of thread safety - you have to be very careful to make sure the correct locks are used, and |
| 43 | used correctly. |
| 44 | * Lack of high level abstractions - part of the appeal of scripting languages such as Python is |
| 45 | the higher level of abstraction which enables simpler APIs and ease of programming. |
| 46 | |
| 47 | Using `libpnet` and Rust, you get the best of both worlds. The higher level abstractions, memory |
| 48 | and thread safety, alongside the performance of C. |
| 49 | |
| 50 | ### Network Utilities |
| 51 | |
| 52 | Many networking utilities such as ping and traceroute rely on being able to manipulate network and |
| 53 | transport headers, which isn't possible with standard networking stacks such as those provided by |
| 54 | `std::io::net`. |
| 55 | |
| 56 | ### Data Link Layer |
| 57 | |
| 58 | It can be useful to work directly at the data link layer, to see packets as they are "on the wire". |
| 59 | There are lots of uses for this, including network diagnostics, packet capture and traffic shaping. |
| 60 | |
| 61 | ## Documentation |
| 62 | |
| 63 | API documentation for the latest build can be found here: https://docs.rs/pnet/ |
| 64 | |
| 65 | ## Usage |
| 66 | |
| 67 | To use `libpnet` in your project, add the following to your Cargo.toml: |
| 68 | |
| 69 | ``` |
| 70 | [dependencies.pnet] |
| 71 | version = "0.35.0" |
| 72 | ``` |
| 73 | |
| 74 | `libpnet` should work with the latest stable version of Rust. |
| 75 | |
| 76 | When running the test suite, there are a number of networking tests which will |
| 77 | likely fail - the easiest way to workaround this is to run `cargo test` as a |
| 78 | root or administrative user. This can often be avoided, however it is more |
| 79 | involved. |
| 80 | |
| 81 | ### Windows |
| 82 | |
| 83 | There are three requirements for building on Windows: |
| 84 | |
| 85 | * You must use a version of Rust which uses the MSVC toolchain |
| 86 | * You must have [WinPcap](https://www.winpcap.org/) or [npcap](https://nmap.org/npcap/) installed |
| 87 | (tested with version WinPcap 4.1.3) (If using npcap, make sure to install with the "Install Npcap in WinPcap API-compatible Mode") |
| 88 | * You must place `Packet.lib` from the [WinPcap Developers pack](https://www.winpcap.org/devel.htm) |
| 89 | in a directory named `lib`, in the root of this repository. Alternatively, you can use any of the |
| 90 | locations listed in the `%LIB%`/`$Env:LIB` environment variables. For the 64 bit toolchain it is |
| 91 | in `WpdPack/Lib/x64/Packet.lib`, for the 32 bit toolchain, it is in `WpdPack/Lib/Packet.lib`. |