Giter Club home page Giter Club logo

bs's Introduction

bs

Build Status

Safe, sound, and all around better sockets for Rust

Examples

# use bs_system::Result;
# use bs_system::SystemError as Error;
# use bs_socket::socket::{BasicSocket, SetFilter};
# use std::net::IpAddr;
# use eui48::MacAddress;
use bs::{
    filter::{
        backend::Classic,
        idiom::{
            ip::ip_src,
            ethernet::ether_src,
        },
    },
    socket::{
        socket::Socket,
        packet::PacketLayer2Socket,
    },
};

# const IP_HEADER_LENGTH: usize = 20;
# const IP_SOURCE_START: usize = 12;
# const IP_SOURCE_END: usize = IP_SOURCE_START + 4;
# const PARSE_ERROR: i32 = 0;

fn raw_ethernet_only_loves_one_one_one_one(buffer: &mut [u8]) -> Result<()> {

    let vip = "1.1.1.1".parse().map_err(|_| Error(PARSE_ERROR))?;
    let my_gateway = "00:11:22:33:44:55".parse().map_err(|_| Error(PARSE_ERROR))?;

    let mut s: Socket<PacketLayer2Socket> = Socket::new()?;

    s.set_filter(
        // Generate kernel socket filters at runtime, just like that!
        ( ip_src::<Classic>(vip) & ether_src(my_gateway) )
            .compile()?
            .build()?
    )?;

    let got_this_many_bytes = s.receive(buffer, 0)?;

    assert!(got_this_many_bytes > IP_HEADER_LENGTH);
    assert_eq!([1, 1, 1, 1], buffer[IP_SOURCE_START..IP_SOURCE_END]);

    Ok(())
}

Contributing

bs is a work in progress, and assistance is welcome. Feel free to open pull requests or issues for changes and suggestions.

PR should include relevant tests and documentation, so please make sure you get those.

License

MIT

bs's People

Contributors

aviramha avatar oskardrums avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

bs's Issues

More idioms

Create more useful idiom's, e.g.:

  • ethernet_host

  • ip_host

  • tcp_port/udp_port

bs-ebpf: Make it work on rust's Stable channel

The only reason bs-ebpf is currently nighlty-only is because we use the syscall crate to call create a raw call to bpf(2), and the syscall crate uses #[feature(asm)], and that's marked as unstable for the foreseeable future...

We only use syscall in this line:

let fd = unsafe { syscall!(BPF, 5, ptr, size_of_val(&attr)) as i32 };

So we probably need some build.rs script to generate a .a static library that exposes just a bpf(2) wrapper ๐Ÿคทโ€โ™‚๏ธ

Complete the Socket API

Add missing bs_socket::Socket methods:

  • lock_filter

  • peek

  • send_to/recv_from

  • listen/accept

  • OOB

  • bind

  • connect

  • more...

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.