Code Monkey home page Code Monkey logo

blatta-stream's Introduction

blatta-stream

blatta-stream is a TCP stream reassembly package for Rust, built on top of smoltcp. It allows us to read IP packets from a network interface and reassemble the TCP segments. It is similar to pynids in Python or libnids in C. Unlike the mentioned libraries, blatta-stream fully supports multithreading and is memory-safe.

Installation

To use blatta-stream in your Rust project, you need to add the following line to your Cargo.toml file:

[dependencies]
    smoltcp = "0.7.5"
    blatta_stream = { git = "https://github.com/bazz-066/blatta-stream/" }

Example usage

Once you have included blatta-stream to Cargo.toml file, you can start capturing the interface and reassembling TCP segments using the following code:

use blatta_stream::stream;
use smoltcp::phy::wait as phy_wait;
use smoltcp::phy::{Device, RawSocket, RxToken};
use smoltcp::time::Instant;
use smoltcp::wire::{EthernetFrame, PrettyPrinter, Ipv4Packet, EthernetProtocol, IpProtocol, TcpPacket};

fn main() {
    let ifname = env::args().nth(1).unwrap();
    let port_filter = Vec::from([80u16]);
    let mut srt_controller = stream::StreamReaderController::new(port_filter, false, ifname);
    
    let handle = thread::spawn(move || {
        loop {
            let ten_millis = std::time::Duration::from_millis(10);
            thread::sleep(ten_millis);

            let data_received = srt_controller.get_ready_conn();

            match data_received {
                Some(reconstructed_packets) => {
                    println!("{:?}", reconstructed_packets.get_init_tcp_message());
                },
                None => {}
            }
        }
    });

    handle.join();
}

To run this code, simply execute sudo cargo run <interface name>. Please note that the command has to be executed by root user.

Another example is also provided in the lib.rs file. The test can be executed by running sudo cargo test <interface name>.

blatta-stream's People

Contributors

bazz-066 avatar

Stargazers

Valeriy V. Vorotyntsev avatar Alex Lu avatar 种豆得豆 avatar

Watchers

 avatar

Forkers

ravivendra

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.