rawsock/
lib.rs

1/**
2
3# Overview
4**rawsock** is a Rust library that highly simplifies use of packet capturing libraries
5such as **pcap**, **wpcap** or **pf_ring** and also libraries with a compatible API, such as **npcap**.
6It can help you to send and receive raw socket frames using one consistent API for all those libraries,
7so that the internal complexity is hidden.
8
9Main advantage: you write code using one simple API - rawsock. But when your application is run,
10the best available library on user's machine is chosen and used in the background.
11Platform-specific extensions (such as WinPcap ```pcap_sendqueue_transmit()```) are also used in optimal way.
12
13# Main features
14
15* One consistent API for all packet capturing libraries.
16* Support of pcap, wpcap (with Windows-specific optimizations), npcap and pfring
17* Supports all main platforms: tested on Windows, Linux, Mac. Many more should work too
18* Libraries are loaded in a dynamic manner, so that the library does not have any direct
19    dependency - it's going to work with whatever is available on the given platform.
20* Libraries are checked in the order of effectiveness. The best found library is loaded.
21* It is also possible to load a specific library when using the rawsock API
22or directly use the API of the given dynamically loaded library - for more advanced use cases.
23
24# Quick example
25
26```no_run
27extern crate rawsock;
28use rawsock::open_best_library;
29
30const ICMP_PACKET: [u8; 84] = [
310x45, 0x00, 0x00, 0x54, 0xee, 0x96, 0x40, 0x00, 0x40, 0x01, 0x79, 0xf0, 0xc0, 0xa8, 0x01, 0x6a,
320x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x2f, 0x08, 0x66, 0xc2, 0x00, 0x12, 0x82, 0xaa, 0xcc, 0x5c,
330x00, 0x00, 0x00, 0x00, 0x51, 0x49, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13,
340x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
350x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
360x34, 0x35, 0x36, 0x37];
37
38fn main() {
39
40    /*
41    This example shows automatic choosing of the best underlying library available on your system
42    and dynamic dispatch of calls to the right implementation.
43
44    For most applications this is the recommended approach.
45    */
46    println!("Opening packet capturing library");
47    let lib = open_best_library().expect("Could not open any packet capturing library");
48    println!("Library opened, version is {}", lib.version());
49    let interf_name = "eth0"; //replace with whatever is available on your platform
50    println!("Opening the {} interface", interf_name);
51    let mut interf = lib.open_interface(&interf_name).expect("Could not open network interface");
52    println!("Interface opened, data link: {}", interf.data_link());
53
54    //send some packets
55    println!("Sending 5 packets:");
56    for i in 0..5{
57        println!("Sending ICMP ping packet no {}",i);
58        interf.send(&ICMP_PACKET).expect("Could not send packet");
59    }
60
61    //receive some packets.
62    println!("Receiving 5 packets:");
63    for _ in 0..5 {
64        let packet = interf.receive().expect("Could not receive packet");
65        println!("Received packet: {}", packet);
66    }
67}
68```
69
70*/
71
72extern crate dlopen;
73#[macro_use]
74extern crate dlopen_derive;
75#[macro_use]
76extern crate bitflags;
77extern crate libc;
78extern crate time;
79
80pub mod pcap;
81pub mod wpcap;
82pub mod pfring;
83pub mod traits;
84mod pcap_common;
85mod utils;
86mod common;
87
88pub use self::common::{Packet, BorrowedPacket, OwnedPacket, DataLink, LibraryVersion, open_best_library, open_best_library_arc,InterfaceDescription, Error, Stats};