Convert Figma logo to code with AI

mfontanini logolibtins

High-level, multiplatform C++ network packet sniffing and crafting library.

1,906
376
1,906
156

Top Related Projects

10,525

Scapy: the Python-based interactive packet manipulation program & library.

9,874

Nmap - the Network Mapper. Github mirror of official SVN repository.

Read-only mirror of Wireshark's Git repository at https://gitlab.com/wireshark/wireshark. ⚠️ GitHub won't let us disable pull requests. ⚠️ THEY WILL BE IGNORED HERE ⚠️ Upload them at GitLab instead.

Provides packet processing capabilities for Go

2,642

the LIBpcap interface to various kernel packet capture mechanism

Quick Overview

libtins is a high-level, multiplatform C++ network packet sniffing and crafting library. It provides a clean and easy-to-use API for creating, sending, receiving, and analyzing network packets for various protocols, making it suitable for network programming, security research, and packet manipulation tasks.

Pros

  • Easy-to-use API with high-level abstractions for packet handling
  • Support for a wide range of network protocols
  • Cross-platform compatibility (Linux, macOS, Windows)
  • Actively maintained with regular updates and bug fixes

Cons

  • Limited documentation compared to some other networking libraries
  • Steeper learning curve for beginners in network programming
  • May have performance overhead compared to lower-level libraries for certain use cases
  • Requires C++11 or later, which might be a limitation for some older projects

Code Examples

  1. Sniffing network packets:
#include <tins/tins.h>
using namespace Tins;

bool callback(const PDU &pdu) {
    const IP &ip = pdu.rfind_pdu<IP>();
    const TCP &tcp = pdu.rfind_pdu<TCP>();
    std::cout << ip.src_addr() << ":" << tcp.sport() << " -> "
              << ip.dst_addr() << ":" << tcp.dport() << std::endl;
    return true;
}

int main() {
    SnifferConfiguration config;
    config.set_filter("tcp");
    Sniffer sniffer("eth0", config);
    sniffer.sniff_loop(callback);
    return 0;
}
  1. Crafting and sending a custom TCP packet:
#include <tins/tins.h>
using namespace Tins;

int main() {
    IP ip = IP("192.168.0.1") / TCP(22, 12345);
    ip.ttl(64);
    TCP& tcp = ip.rfind_pdu<TCP>();
    tcp.set_flag(TCP::SYN, 1);
    tcp.mss(1460);

    NetworkInterface iface = NetworkInterface::default_interface();
    PacketSender sender;
    sender.send(ip, iface);
    return 0;
}
  1. Parsing a PCAP file:
#include <tins/tins.h>
using namespace Tins;

int main() {
    FileSniffer sniffer("capture.pcap");
    for (const auto &packet : sniffer) {
        const IP &ip = packet.pdu()->rfind_pdu<IP>();
        std::cout << "Source IP: " << ip.src_addr() << std::endl;
    }
    return 0;
}

Getting Started

To get started with libtins, follow these steps:

  1. Install the library:

    • On Ubuntu/Debian: sudo apt-get install libtins-dev
    • On macOS with Homebrew: brew install libtins
    • For Windows or manual installation, follow the instructions in the GitHub repository.
  2. Include the library in your C++ project:

    #include <tins/tins.h>
    
  3. Compile your program with the appropriate flags:

    g++ -std=c++11 your_program.cpp -ltins -o your_program
    
  4. Run your program with the necessary permissions (e.g., root for packet sniffing on most systems).

Competitor Comparisons

10,525

Scapy: the Python-based interactive packet manipulation program & library.

Pros of Scapy

  • Written in Python, offering easier scripting and rapid prototyping
  • Extensive protocol support, including many application-layer protocols
  • Interactive mode for quick testing and exploration

Cons of Scapy

  • Generally slower performance compared to C++ based solutions
  • Larger memory footprint, especially for processing large packet captures
  • Less suitable for high-performance production environments

Code Comparison

Scapy (Python):

from scapy.all import *
packet = IP(dst="8.8.8.8")/TCP(dport=80)
send(packet)

Libtins (C++):

#include <tins/tins.h>
using namespace Tins;
EthernetII pkt = IP("8.8.8.8") / TCP(80);
PacketSender sender;
sender.send(pkt);

Both libraries offer intuitive syntax for packet crafting and sending. Scapy's Python-based approach allows for more concise code, while Libtins provides the performance benefits of C++. Scapy excels in rapid prototyping and scripting scenarios, whereas Libtins is better suited for high-performance applications requiring low-level control and efficiency.

9,874

Nmap - the Network Mapper. Github mirror of official SVN repository.

Pros of Nmap

  • Comprehensive network scanning and discovery tool with a wide range of features
  • Large, active community and extensive documentation
  • Includes a powerful scripting engine for custom functionality

Cons of Nmap

  • Larger codebase and more complex to contribute to or modify
  • Primarily focused on network scanning rather than packet manipulation
  • Steeper learning curve for beginners

Code Comparison

Nmap (C++):

void scan_host(const char *host) {
    struct sockaddr_in addr;
    addr.sin_addr.s_addr = inet_addr(host);
    // Perform scan operations
}

Libtins (C++):

void analyze_packet(const Tins::PDU &pdu) {
    const Tins::IP &ip = pdu.rfind_pdu<Tins::IP>();
    std::cout << "Source IP: " << ip.src_addr() << std::endl;
}

Summary

Nmap is a comprehensive network scanning tool with a wide range of features and a large community, while Libtins is a lightweight packet crafting and sniffing library. Nmap excels in network discovery and security auditing, whereas Libtins provides more flexibility for custom packet manipulation and analysis. The choice between them depends on the specific requirements of your project and your familiarity with network programming concepts.

Read-only mirror of Wireshark's Git repository at https://gitlab.com/wireshark/wireshark. ⚠️ GitHub won't let us disable pull requests. ⚠️ THEY WILL BE IGNORED HERE ⚠️ Upload them at GitLab instead.

Pros of Wireshark

  • Comprehensive GUI for packet analysis and visualization
  • Extensive protocol support and decoding capabilities
  • Large community and frequent updates

Cons of Wireshark

  • Heavier resource usage due to full-featured GUI
  • Steeper learning curve for beginners
  • Less suitable for embedding in custom applications

Code Comparison

Wireshark (C):

dissector_handle_t handle;
handle = create_dissector_handle(dissect_example, proto_example);
dissector_add_uint("udp.port", UDP_PORT_EXAMPLE, handle);

Libtins (C++):

Sniffer sniffer("eth0");
sniffer.sniff_loop([](PDU& pdu) {
    // Process packet
    return true;
});

Summary

Wireshark is a full-featured network protocol analyzer with a comprehensive GUI, making it ideal for interactive packet analysis and debugging. Libtins, on the other hand, is a lightweight C++ library focused on packet crafting and sniffing, better suited for integration into custom applications. While Wireshark offers extensive protocol support and visualization tools, Libtins provides a simpler API for programmatic packet manipulation and analysis.

Provides packet processing capabilities for Go

Pros of gopacket

  • Written in Go, offering better concurrency and easier deployment
  • More actively maintained with frequent updates
  • Extensive documentation and examples

Cons of gopacket

  • Potentially slower performance compared to C++ implementation
  • Limited cross-platform support (mainly focused on Linux)

Code Comparison

libtins

#include <tins/tins.h>
using namespace Tins;

EthernetII pkt = EthernetII() / IP() / TCP() / RawPDU("Hello");
PacketSender sender;
sender.send(pkt);

gopacket

import (
    "github.com/google/gopacket"
    "github.com/google/gopacket/layers"
)

packet := gopacket.NewPacket(
    layers.IPv4{} / layers.TCP{} / gopacket.Payload([]byte("Hello")),
    layers.LayerTypeEthernet,
    gopacket.Default,
)

Both libraries provide similar functionality for packet crafting and manipulation, but with syntax differences reflecting their respective languages. libtins uses operator overloading for a more concise syntax, while gopacket relies on Go's struct composition.

2,642

the LIBpcap interface to various kernel packet capture mechanism

Pros of libpcap

  • Widely adopted and supported across multiple platforms
  • Provides low-level packet capture and filtering capabilities
  • Extensive documentation and community support

Cons of libpcap

  • Limited high-level protocol analysis features
  • Requires more manual work for packet parsing and interpretation
  • Less user-friendly API for complex network analysis tasks

Code Comparison

libpcap:

pcap_t *handle;
handle = pcap_open_live("eth0", BUFSIZ, 1, 1000, errbuf);
pcap_loop(handle, -1, packet_handler, NULL);

libtins:

SnifferConfiguration config;
config.set_promisc_mode(true);
Sniffer sniffer("eth0", config);
sniffer.sniff_loop(packet_handler);

The code comparison shows that libtins provides a more intuitive and object-oriented approach to packet sniffing, while libpcap requires more low-level configuration. libtins abstracts away some of the complexity, making it easier to set up and use for higher-level network analysis tasks. However, libpcap offers more fine-grained control over the packet capture process, which can be beneficial for certain use cases.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

libtins

Build status Build status

libtins is a high-level, multiplatform C++ network packet sniffing and crafting library.

Its main purpose is to provide the C++ developer an easy, efficient, platform and endianess-independent way to create tools which need to send, receive and manipulate specially crafted packets.

In order to read tutorials, examples and checkout some benchmarks of the library, please visit:

http://libtins.github.io/

Compiling

libtins depends on libpcap and openssl, although the latter is not necessary if some features of the library are disabled.

In order to compile, execute:

# Create the build directory
mkdir build
cd build

# Configure the project. Add any relevant configuration flags
cmake ../

# Compile!
make

Static/shared build

Note that by default, only the shared object is compiled. If you would like to generate a static library file, run:

cmake ../ -DLIBTINS_BUILD_SHARED=0

The generated static/shared library files will be located in the build/lib directory.

C++11 support

libtins is noticeably faster if you enable C++11 support. Therefore, if your compiler supports this standard, then you should enable it. In order to do so, use the LIBTINS_ENABLE_CXX11 switch:

cmake ../ -DLIBTINS_ENABLE_CXX11=1

TCP ACK tracker

The TCP ACK tracker feature requires the boost.icl library (header only). This feature is enabled by default but will be disabled if the boost headers are not found. You can disable this feature by using:

cmake ../ -DLIBTINS_ENABLE_ACK_TRACKER=0

If your boost installation is on some non-standard path, use the parameters shown on the CMake FindBoost help

WPA2 decryption

If you want to disable WPA2 decryption support, which will remove openssl as a dependency for compilation, use the LIBTINS_ENABLE_WPA2 switch:

cmake ../ -DLIBTINS_ENABLE_WPA2=0

IEEE 802.11 support

If you want to disable IEEE 802.11 support(this will also disable RadioTap and WPA2 decryption), which will reduce the size of the resulting library in around 20%, use the LIBTINS_ENABLE_DOT11 switch:

cmake ../ -DLIBTINS_ENABLE_DOT11=0

Installing

Once you're done, if you want to install the header files and the shared object, execute as root:

make install

This will install the shared object typically in /usr/local/lib. Note that you might have to update ldconfig's cache before using it, so in order to invalidate it, you should run(as root):

ldconfig

Running tests

You may want to run the unit tests on your system so you make sure everything works. In order to do so, you need to follow these steps:

# This will fetch the googletest submodule, needed for tests
git submodule init
git submodule update

mkdir build
cd build

# Use any options you want
cmake .. 

# Compile tests
make tests

# Run them
make test

If you find that any tests fail, please create an ticket in the issue tracker indicating the platform and architecture you're using.

Examples

You might want to have a look at the examples located in the "examples" directory. The same samples can be found online at:

http://libtins.github.io/examples/

Contributing

If you want to report a bug or make a pull request, please have a look at the contributing file before doing so.