A rustic tcp + serialization abstraction.

Overview

Wire

An abstraction over TCP and Serialization

"put a struct in one side and it comes out the other end"

Wire is a library that makes writing applications that communicate via TCP easy. If you've ever wanted to conceptually put a struct into one end of a tcp stream and have it come out the other side, then Wire might be what you are looking for!

##Api docs

Example

Let's write a simple server that computes fibonacci numbers as a service.

These files can be found in the examples directory.

Server

extern crate wire;

use std::thread::spawn;
use wire::SizeLimit;

fn fib(n: u64) -> u64 {
    match n {
        0 => 0,
        1 => 1,
        n => fib(n - 1) + fib(n - 2)
    }
}

fn main() {
    // Make a listener on 0.0.0.0:8080
    let (listener, _) = wire::listen_tcp(("0.0.0.0", 8080)).unwrap();

    // Only allow incoming messages of at max 8 bytes, and verify that we aren't
    // writing anything over 16 bytes.
    let (read_limit, write_limit) = (SizeLimit::Bounded(8),
                                     SizeLimit::Bounded(16));

    // Turn the listener into an iterator of connections.
    for (connection, _) in listener.into_blocking_iter() {
        // Spawn a new thread for each connection that we get.
        spawn(move || {
            // Upgrade the connection to read `u64` and write `(u64, u64)`.
            let (i, mut o) = wire::upgrade_tcp(connection, read_limit, write_limit).unwrap();
            // For each `u64` that we read from the network...
            for x in i.into_blocking_iter() {
                // Send that number back with the computed value.
                o.send(&(x, fib(x))).ok();
            }
        });
    }
}

Client

extern crate wire;

use wire::SizeLimit;

fn main() {
    // Only allow incomming messages of at max 16 bytes, and verify that all of
    // our outgoing messages aren't over 8 bytes.
    let (read_limit, write_limit) = (SizeLimit::Bounded(16),
                                     SizeLimit::Bounded(8));

    // Connect to our running fib-server.
    // incoming: (u64, u64)
    // outgoing: u64
    let (i, mut o) = wire::connect_tcp(("localhost", 8080), read_limit, write_limit).unwrap();

    // Send all the numbers from 0 to 10.
    for x in 0u64 .. 10u64 {
        o.send(&x).ok();
    }

    // Close our outgoing pipe. This is necessary because otherwise,
    // the server will keep waiting for the client to send it data and
    // we will deadlock.
    o.close();

    // Print everything that we get back.
    for a in i.into_blocking_iter() {
        let (x, fx): (u64, u64) = a;
        println!("{} -> {}", x, fx);
    }
}
You might also like...
A simple tcp server that written in rustlang
A simple tcp server that written in rustlang

rust_tcp A simple tcp server that written in rustlang How to build In the root dir cargo run Then you can do a test by using telnet as a client telne

Tunnel TCP traffic through SOCKS5 or HTTP using a TUN interface.

tun2proxy Tunnel TCP traffic through SOCKS5 or HTTP on Linux. Authentication not yet supported. Error handling incomplete and too restrictive. Build C

A remote shell, TCP tunnel and HTTP proxy for Replit.
A remote shell, TCP tunnel and HTTP proxy for Replit.

Autobahn A remote shell, TCP tunnel and HTTP proxy for Replit. Hybrid SSH/HTTP server for Replit. Based on leon332157/replish. Autobahn runs a WebSock

Fast User-Space TCP/UDP Stack

Catnip Catnip is a TCP/IP stack that focuses on being an embeddable, low-latency solution for user-space networking. Building and Running 1. Clone Thi

A tcp proxy server/client which exchange the data in temp files

ftcp A tcp proxy server/client which exchange the data in temp files 通过在临时文件中交换数据来进行TCP代理的一个服务端/客户端 学校内网中有针对教学楼的防火墙导致教室电脑难以上网( 但学校内建有公共ftp服务器,因此就有了这个借

Send files over TCP. Quick and simple. Made in Rust.

SFT Multithreaded utility to send files over TCP. The sender writes a header containing the filename, and then the contents of the file, buffered, to

A modern, simple TCP tunnel in Rust that exposes local ports to a remote server, bypassing standard NAT connection firewalls
A modern, simple TCP tunnel in Rust that exposes local ports to a remote server, bypassing standard NAT connection firewalls

bore A modern, simple TCP tunnel in Rust that exposes local ports to a remote server, bypassing standard NAT connection firewalls. That's all it does:

A tcp port forwarding system like ngrok.

Pruxy A tcp port forwarding system like ngrok. Todo http request handler agent - server connection agent How to use Generate cert files mkdir ssl_ce

An app which reads data from a serial port and serves it on a TCP port.

serial-to-tcp An app which reads data from a serial port and serves it on a TCP port. How to use Clone this repo and build the app as outlined below (

Comments
  • v0.0.10 Compiles

    v0.0.10 Compiles

    I am unable to compile version 0.0.9 of wire because I am on a later version of rustc (1.0.0) after some RFC changes. I also could not compile 0.0.10 because of API changes in unreliable message. Now I have it compiling and it seems to work.

    I really want to use this crate for a project of mine, there doesn't seem to be anything else like it on crates.io. I'm wondering if 0.0.10 is in a usable state, and if not is there any easy fixes I can submit?

    opened by illegalprime 8
  • unable to clone tcp outstream

    unable to clone tcp outstream

    error: type `wire::tcp::OutTcpStream<_>` does not implement any method in scope named `clone`
    

    However, I do see that there is (used to be?) a clone method in the api docs

    Is this still the case? What might I be doing wrong?

    Thanks

    opened by viperscape 8
  • Cannot compile wire, old_io error

    Cannot compile wire, old_io error

       Compiling wire v0.0.8 (https://github.com/TyOverby/wire#aab3dc53)
    /home/chris/.cargo/git/checkouts/wire-b190f1fb73dd19f6/master/src/tcp.rs:45:9: 45:29 error: the trait `std::io::Write` is not implemented for the type `std::old_io::net::tcp::TcpStream` [E0277]
    /home/chris/.cargo/git/checkouts/wire-b190f1fb73dd19f6/master/src/tcp.rs:45         bincode::encode_into(m, &mut self.tcp_stream, self.write_limit)
                                                                                        ^~~~~~~~~~~~~~~~~~~~
    /home/chris/.cargo/git/checkouts/wire-b190f1fb73dd19f6/master/src/tcp.rs:145:19: 145:39 error: the trait `std::io::BufRead` is not implemented for the type `std::old_io::buffered::BufferedReader<std::old_io::net::tcp::TcpStream>` [E0277]
    /home/chris/.cargo/git/checkouts/wire-b190f1fb73dd19f6/master/src/tcp.rs:145             match bincode::decode_from(&mut buffer, read_limit) {
                                                                                                   ^~~~~~~~~~~~~~~~~~~~
    error: aborting due to 2 previous errors
    Build failed, waiting for other jobs to finish...
    Could not compile `wire`.
    

    I assume it's related to bincode getting pushed to new io: https://github.com/TyOverby/bincode/issues/20

    Thanks for your help!

    opened by viperscape 4
Owner
Ty Overby
compilers are cool
Ty Overby
a smol tcp/ip stack

smoltcp smoltcp is a standalone, event-driven TCP/IP stack that is designed for bare-metal, real-time systems. Its design goals are simplicity and rob

smoltcp 2.8k Jan 4, 2023
A high performance TCP SYN port scanner.

Armada A High-Performance TCP SYN scanner What is Armada? Armada is a high performance TCP SYN scanner. This is equivalent to the type of scanning tha

resync 259 Dec 19, 2022
A tcp over http2 + tls proxy

mtunnel A tcp over http2 + tls proxy. Usage 1. get certificates, by following steps. 2. make your config client config: { "local_addr": "127.0.0.1

cssivision 9 Sep 5, 2022
🤖 brwrs is a new protocol running over TCP/IP that is intended to be a suitable candidate for terminal-only servers

brwrs is a new protocol running over TCP/IP that is intended to be a suitable candidate for terminal-only servers (plain text data). That is, although it can be accessed from a browser, brwrs will not correctly interpret the browser's GET request.

daCoUSB 3 Jul 30, 2021
TCP is so widely used, however QUIC may have a better performance.

TCP is so widely used, however QUIC may have a better performance. For softwares which use protocols built on TCP, this program helps them take FULL advantage of QUIC.

zephyr 15 Jun 10, 2022
Library + CLI-Tool to measure the TTFB (time to first byte) of HTTP requests. Additionally, this crate measures the times of DNS lookup, TCP connect and TLS handshake.

TTFB: CLI + Lib to Measure the TTFB of HTTP/1.1 Requests Similar to the network tab in Google Chrome or Mozilla Firefox, this crate helps you find the

Philipp Schuster 24 Dec 1, 2022
Simple utility to ping a TCP port.

TcpPing Simple utility to ping a TCP port. Example > tcpping 1.1.1.1 53 -b en0 -i 1 -t 4 Connected to 1.1.1.1:53 in 21 ms Connected to 1.1.1.1:53 in 3

null 11 Nov 24, 2022
Transforms UDP stream into (fake) TCP streams that can go through Layer 3 & Layer 4 (NAPT) firewalls/NATs.

Phantun A lightweight and fast UDP to TCP obfuscator. Table of Contents Phantun Latest release Overview Usage 1. Enable Kernel IP forwarding 2. Add re

Datong Sun 782 Dec 30, 2022
RedLizard - A Rust TCP Reverse Shell with SSL

RedLizard - A Rust TCP Reverse Shell with SSL RedLizard Rust TCP Reverse Shell Server/Client This is a reverse shell in Rust called RedLizard, basical

Thanasis Tserpelis 105 Dec 24, 2022
Passive TCP/IP fingerprinting tool

This tool analyzes first stage of TCP handshake (SYN) and recognize operating system of client Build To build sp0ky, you need to install Rust git clon

Ivan Tyunin 19 Sep 12, 2022