Giter Club home page Giter Club logo

ustcp's Introduction

ustunet

A pure Rust TCP library that can be simply used in place of TCP implementations provided by operating systems, enabling services to be built with uncommon flexibility.

Usage

At a high level, it provides APIs similar to any asynchronous TCP socket library.

To start listening, use TcpListener. But instead of binding to a single SocketAddr, give it the name of a TUN network interface:

use ustunet::TcpListener;

let mut listener = TcpListener::bind("tuna").unwrap();

And it will process all IP packets going to the interface, accepting incoming TCP connections destined to any SocketAddr. It's like binding to all possible IP and ports. (But without the complexity or the cost. Only one file descriptor is used.)

Accept TcpStreams:

while let Some(tcp_stream) = listener.next().await {
}

Use tokio::io::AsyncRead and AsyncWrite implementations:

let mut buf = [0u8; 1024];
let n = tcp_stream.read(&mut buf).await.unwrap();

tcp_stream.write("Aloha!".as_bytes()).await.unwrap();

Since the listener is not bound to an address, the SocketAddr actually used by a connection can be of more interest.

let server_addr = socket.local_addr();
println!("Provided service on port {}, IP: {}.", server_addr.port(), server_addr.ip());

Similarly, call peer_addr to get the address of the host that initiated the connection.

Adding the dependency

Add to Cargo.toml

[dependencies.ustunet]
git = "https://github.com/ustcp/ustcp.git"

Setting up a TUN network interface

TUN allows a user to send and receive IP packets on the interface with no permission requirements at runtime.

Create a TUN interface with name set to tuna, and ownership given to user yuki. And bring it up.

sudo ip tuntap add dev tuna mode tun user yuki
sudo ip link set up dev tuna

Give it an IP address (and a subnet mask) that's not currently in use:

sudo ip addr add 192.168.9.100/24 dev tuna

With the subnet mask /24, ustunet will be able to respond on any address in the range from 192.168.9.1 to 192.168.9.254.

The command can be repeated to add more addresses, but adding routes can be more flexible. Configure any address in the subnet as a gateway for a different range of IPs to instruct the operating systems to contact those addresses also via tuna.

sudo ip route add 10.33.0.0/16 via 192.168.9.4 dev tuna

ustcp's People

Contributors

wzhd avatar

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.