Skip to main content

netio/
lib.rs

1// netio -- network protocols on top of plat::net
2//
3//   dns        host names to addresses (Linux: /etc/hosts, resolv.conf, UDP / TCP queries;
4//              Windows: the system resolver); records of any type, a cache, DNS over TLS and
5//              HTTPS, DNSSEC validation (Config::dnssec, Lookup::security)
6//   dane       TLSA records (DNSSEC-secure) for a TLS server: lookup, trust -> Trust::Dane
7//              (RFC 6698, 7671)
8//   url        Url::parse("https://host:9443/api?x=1"), percent_encode / percent_decode,
9//              query_pairs
10//   http       Request, Response, Body (bytes or a stream sent chunked), Headers, status texts;
11//              the HTTP/1.1 wire format (bounded parsing, chunked transfer, keep-alive)
12//   server     Server::start(addr, ServerConfig, handler): accept thread + worker pool
13//   client     Client::request(...): blocking, timeouts, size limits; Content-Encoding decoded,
14//              redirects, cookies (netpkt::cookie), a connection pool, proxies
15//   proxy      HTTP proxies (absolute form, CONNECT tunnels), NO_PROXY, from the environment
16//   tls        TLS 1.3 servers and clients (ChaCha20-Poly1305, AES-GCM; X25519MLKEM768,
17//              X25519, P-256, P-384; Ed25519, ECDSA, RSA keys; SNI, ALPN, mutual auth;
18//              resumption with tickets, 0-RTT where the server allows it, OCSP stapling) and the TLS 1.2
19//              fallback for public servers: connect / accept -> TlsStream (a Stream)
20//   x509 / der the crate jflib-x509 (re-exported): certificates (parse, build with any key,
21//              verify chains) of a private CA and of the web PKI; PrivateKey (PKCS #8 ...);
22//              OCSP responses and certificate requests
23//   ocsp       a certificate's status from its CA's responder; the stapled responses of a TLS
24//              server kept current (tls::Staples)
25//   acme       certificates from an ACME CA (RFC 8555: Let's Encrypt, ...): accounts, orders,
26//              http-01 / tls-alpn-01 / dns-01, renewal information, revocation
27//   pem        PEM blocks
28//   roots      the system's certificate authorities (Windows: CryptoAPI; Linux: the bundle)
29//   ssh        SSH-2 client (curve25519, ssh-ed25519, chacha20-poly1305): run commands with
30//              stdin; the server side of the transport for tests
31//   trust      https to a server with its own CA: known CA, pinned fingerprint or trust on
32//              first use (the presented CA comes back for the user's decision); https to
33//              public servers with the system's CAs
34//   smtp       sending mail: STARTTLS / TLS, AUTH PLAIN / LOGIN, UTF-8 messages
35//   ws         WebSocket (RFC 6455): ws::Acceptor in a Server's handler (the upgrade, a thread
36//              per connection), ws::connect for clients, the frame codec for untrusted bytes
37//   Stream     what HTTP runs over (plat::net::TcpStream, tls::TlsStream)
38//
39// Everything is blocking with timeouts and size limits on every read; a malformed peer gets an
40// error answer or a closed connection, never a panic.
41#![no_std]
42#![forbid(unsafe_code)]
43
44extern crate alloc;
45
46pub mod acme;
47pub mod client;
48pub mod dane;
49pub mod dns;
50pub mod http;
51pub mod ocsp;
52pub mod pem;
53pub mod proxy;
54pub mod roots;
55pub mod server;
56pub mod smtp;
57pub mod ssh;
58pub mod tls;
59pub mod trust;
60pub mod url;
61pub mod ws;
62
63/// certificates and their DER: the crate jflib-x509, which netpkt uses as well (netio::x509 and
64/// netio::der stay the paths they were)
65pub use x509;
66pub use x509::der;
67
68pub use client::{Client, Fetched, HttpError, Part, Pool};
69pub use proxy::Proxy;
70pub use http::{Body, Headers, Request, Response};
71pub use server::{Server, ServerConfig};
72pub use url::Url;
73
74use plat::net::{NetResult, TcpStream};
75
76/// a byte stream HTTP runs over (shared between a reader and a writer: &self methods)
77pub trait Stream: Send + Sync {
78    /// up to buf.len() bytes; Ok(0) at the end
79    fn read(&self, buf: &mut [u8]) -> NetResult<usize>;
80    fn write_all(&self, buf: &[u8]) -> NetResult<()>;
81    /// timeouts for the following reads / writes (None: block)
82    fn set_timeouts(&self, read_ms: Option<u32>, write_ms: Option<u32>);
83    /// no more writes from this side
84    fn shutdown(&self) {}
85    /// whether bytes (or the end) can be read within `ms`
86    fn wait_readable(&self, _ms: u32) -> bool {
87        true
88    }
89    /// after read gave Ok(0): whether the peer ended the stream in order.  TLS: only after its
90    /// close_notify -- a TCP end without one may be an attacker cutting a body that runs to the
91    /// end of the connection (HTTP without a length).  Plain streams: always
92    fn end_is_clean(&self) -> bool {
93        true
94    }
95}
96
97impl Stream for TcpStream {
98    fn read(&self, buf: &mut [u8]) -> NetResult<usize> {
99        TcpStream::read(self, buf)
100    }
101    fn write_all(&self, buf: &[u8]) -> NetResult<()> {
102        TcpStream::write_all(self, buf)
103    }
104    fn set_timeouts(&self, read_ms: Option<u32>, write_ms: Option<u32>) {
105        self.set_read_timeout(read_ms);
106        self.set_write_timeout(write_ms);
107    }
108    fn shutdown(&self) {
109        let _ = self.shutdown_write();
110    }
111    fn wait_readable(&self, ms: u32) -> bool {
112        TcpStream::wait_readable(self, ms).unwrap_or(true)
113    }
114}