rwebtransport 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +666 -666
- package/Cargo.toml +31 -31
- package/LICENSE +202 -202
- package/NOTICE +41 -41
- package/README.md +550 -550
- package/crates/echo-server/Cargo.toml +20 -20
- package/crates/echo-server/src/main.rs +666 -666
- package/crates/native/Cargo.toml +23 -23
- package/crates/native/src/config.rs +129 -129
- package/crates/native/src/driver.rs +463 -463
- package/crates/native/src/lib.rs +821 -821
- package/crates/native/src/server.rs +718 -718
- package/crates/native/src/session.rs +1191 -1191
- package/crates/native/src/tls.rs +96 -96
- package/crates/wtcore/Cargo.toml +15 -15
- package/crates/wtcore/src/h3.rs +226 -226
- package/crates/wtcore/src/lib.rs +9 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +80 -79
- package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-x64/rwebtransport.node +0 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/bench.mjs +180 -180
- package/scripts/build.js +67 -67
- package/scripts/postinstall.js +67 -67
- package/scripts/setup-unix.sh +129 -129
- package/scripts/setup-windows.ps1 +229 -229
- package/vendor/quiche/AGENTS.md +75 -75
- package/vendor/quiche/COPYING +23 -23
- package/vendor/quiche/Cargo.lock +1212 -1212
- package/vendor/quiche/Cargo.toml +215 -215
- package/vendor/quiche/README.md +466 -466
- package/vendor/quiche/examples/Makefile +46 -46
- package/vendor/quiche/examples/README.md +20 -20
- package/vendor/quiche/examples/cert-big.crt +90 -90
- package/vendor/quiche/examples/cert.crt +18 -18
- package/vendor/quiche/examples/cert.key +28 -28
- package/vendor/quiche/examples/client.c +343 -343
- package/vendor/quiche/examples/client.rs +281 -281
- package/vendor/quiche/examples/gen-certs.sh +15 -15
- package/vendor/quiche/examples/http3-client.c +473 -473
- package/vendor/quiche/examples/http3-client.rs +349 -349
- package/vendor/quiche/examples/http3-server.c +614 -614
- package/vendor/quiche/examples/http3-server.rs +680 -680
- package/vendor/quiche/examples/qpack-decode.rs +87 -87
- package/vendor/quiche/examples/qpack-encode.rs +88 -88
- package/vendor/quiche/examples/rootca.crt +21 -21
- package/vendor/quiche/examples/server.c +529 -529
- package/vendor/quiche/examples/server.rs +529 -529
- package/vendor/quiche/include/quiche.h +1282 -1282
- package/vendor/quiche/src/buffers.rs +105 -105
- package/vendor/quiche/src/build.rs +66 -66
- package/vendor/quiche/src/cid.rs +1079 -1079
- package/vendor/quiche/src/crypto/boringssl.rs +373 -373
- package/vendor/quiche/src/crypto/mod.rs +671 -671
- package/vendor/quiche/src/dgram.rs +118 -118
- package/vendor/quiche/src/error.rs +263 -263
- package/vendor/quiche/src/ffi.rs +2290 -2290
- package/vendor/quiche/src/flowcontrol.rs +246 -246
- package/vendor/quiche/src/frame.rs +2172 -2172
- package/vendor/quiche/src/h3/AGENTS.md +54 -54
- package/vendor/quiche/src/h3/ffi.rs +442 -442
- package/vendor/quiche/src/h3/frame.rs +1404 -1404
- package/vendor/quiche/src/h3/mod.rs +8223 -8223
- package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
- package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
- package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
- package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
- package/vendor/quiche/src/h3/stream.rs +1595 -1595
- package/vendor/quiche/src/lib.rs +9556 -9556
- package/vendor/quiche/src/minmax.rs +446 -446
- package/vendor/quiche/src/packet.rs +2334 -2334
- package/vendor/quiche/src/path.rs +1374 -1374
- package/vendor/quiche/src/pmtud.rs +652 -652
- package/vendor/quiche/src/rand.rs +64 -64
- package/vendor/quiche/src/range_buf.rs +186 -186
- package/vendor/quiche/src/ranges.rs +755 -755
- package/vendor/quiche/src/recovery/AGENTS.md +80 -80
- package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
- package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
- package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
- package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
- package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
- package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
- package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
- package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
- package/vendor/quiche/src/recovery/mod.rs +3086 -3086
- package/vendor/quiche/src/recovery/rtt.rs +152 -152
- package/vendor/quiche/src/stream/mod.rs +2210 -2210
- package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
- package/vendor/quiche/src/stream/send_buf.rs +849 -849
- package/vendor/quiche/src/test_utils.rs +681 -681
- package/vendor/quiche/src/tests.rs +12922 -12922
- package/vendor/quiche/src/tls/boringssl.rs +375 -375
- package/vendor/quiche/src/tls/mod.rs +1245 -1245
- package/vendor/quiche/src/transport_params.rs +628 -628
package/vendor/quiche/src/ffi.rs
CHANGED
|
@@ -1,2290 +1,2290 @@
|
|
|
1
|
-
// Copyright (C) 2018-2019, Cloudflare, Inc.
|
|
2
|
-
// All rights reserved.
|
|
3
|
-
//
|
|
4
|
-
// Redistribution and use in source and binary forms, with or without
|
|
5
|
-
// modification, are permitted provided that the following conditions are
|
|
6
|
-
// met:
|
|
7
|
-
//
|
|
8
|
-
// * Redistributions of source code must retain the above copyright notice,
|
|
9
|
-
// this list of conditions and the following disclaimer.
|
|
10
|
-
//
|
|
11
|
-
// * Redistributions in binary form must reproduce the above copyright
|
|
12
|
-
// notice, this list of conditions and the following disclaimer in the
|
|
13
|
-
// documentation and/or other materials provided with the distribution.
|
|
14
|
-
//
|
|
15
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
16
|
-
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
17
|
-
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
18
|
-
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
19
|
-
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
20
|
-
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
21
|
-
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
22
|
-
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
23
|
-
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
24
|
-
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
25
|
-
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
-
|
|
27
|
-
use std::ffi;
|
|
28
|
-
use std::ptr;
|
|
29
|
-
use std::slice;
|
|
30
|
-
use std::sync::atomic;
|
|
31
|
-
|
|
32
|
-
use std::net::Ipv4Addr;
|
|
33
|
-
use std::net::Ipv6Addr;
|
|
34
|
-
use std::net::SocketAddrV4;
|
|
35
|
-
use std::net::SocketAddrV6;
|
|
36
|
-
|
|
37
|
-
#[cfg(unix)]
|
|
38
|
-
use std::os::unix::io::FromRawFd;
|
|
39
|
-
|
|
40
|
-
use libc::c_char;
|
|
41
|
-
use libc::c_int;
|
|
42
|
-
use libc::c_void;
|
|
43
|
-
use libc::size_t;
|
|
44
|
-
use libc::sockaddr;
|
|
45
|
-
use libc::ssize_t;
|
|
46
|
-
use libc::timespec;
|
|
47
|
-
|
|
48
|
-
#[cfg(not(windows))]
|
|
49
|
-
use libc::AF_INET;
|
|
50
|
-
#[cfg(windows)]
|
|
51
|
-
use windows_sys::Win32::Networking::WinSock::AF_INET;
|
|
52
|
-
|
|
53
|
-
#[cfg(not(windows))]
|
|
54
|
-
use libc::AF_INET6;
|
|
55
|
-
#[cfg(windows)]
|
|
56
|
-
use windows_sys::Win32::Networking::WinSock::AF_INET6;
|
|
57
|
-
|
|
58
|
-
#[cfg(not(windows))]
|
|
59
|
-
use libc::in_addr;
|
|
60
|
-
#[cfg(windows)]
|
|
61
|
-
use windows_sys::Win32::Networking::WinSock::IN_ADDR as in_addr;
|
|
62
|
-
|
|
63
|
-
#[cfg(not(windows))]
|
|
64
|
-
use libc::in6_addr;
|
|
65
|
-
#[cfg(windows)]
|
|
66
|
-
use windows_sys::Win32::Networking::WinSock::IN6_ADDR as in6_addr;
|
|
67
|
-
|
|
68
|
-
#[cfg(not(windows))]
|
|
69
|
-
use libc::sa_family_t;
|
|
70
|
-
#[cfg(windows)]
|
|
71
|
-
use windows_sys::Win32::Networking::WinSock::ADDRESS_FAMILY as sa_family_t;
|
|
72
|
-
|
|
73
|
-
#[cfg(not(windows))]
|
|
74
|
-
use libc::sockaddr_in;
|
|
75
|
-
#[cfg(windows)]
|
|
76
|
-
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN as sockaddr_in;
|
|
77
|
-
|
|
78
|
-
#[cfg(not(windows))]
|
|
79
|
-
use libc::sockaddr_in6;
|
|
80
|
-
#[cfg(windows)]
|
|
81
|
-
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6 as sockaddr_in6;
|
|
82
|
-
|
|
83
|
-
#[cfg(not(windows))]
|
|
84
|
-
use libc::sockaddr_storage;
|
|
85
|
-
#[cfg(windows)]
|
|
86
|
-
use windows_sys::Win32::Networking::WinSock::SOCKADDR_STORAGE as sockaddr_storage;
|
|
87
|
-
|
|
88
|
-
#[cfg(windows)]
|
|
89
|
-
use libc::c_int as socklen_t;
|
|
90
|
-
#[cfg(not(windows))]
|
|
91
|
-
use libc::socklen_t;
|
|
92
|
-
|
|
93
|
-
#[cfg(windows)]
|
|
94
|
-
use windows_sys::Win32::Networking::WinSock::IN6_ADDR_0;
|
|
95
|
-
#[cfg(windows)]
|
|
96
|
-
use windows_sys::Win32::Networking::WinSock::IN_ADDR_0;
|
|
97
|
-
#[cfg(windows)]
|
|
98
|
-
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6_0;
|
|
99
|
-
|
|
100
|
-
use crate::*;
|
|
101
|
-
|
|
102
|
-
#[no_mangle]
|
|
103
|
-
pub extern "C" fn quiche_version() -> *const u8 {
|
|
104
|
-
static VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
|
|
105
|
-
VERSION.as_ptr()
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
struct Logger {
|
|
109
|
-
cb: extern "C" fn(line: *const u8, argp: *mut c_void),
|
|
110
|
-
argp: atomic::AtomicPtr<c_void>,
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
impl log::Log for Logger {
|
|
114
|
-
fn enabled(&self, _metadata: &log::Metadata) -> bool {
|
|
115
|
-
true
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
fn log(&self, record: &log::Record) {
|
|
119
|
-
let line = format!("{}: {}\0", record.target(), record.args());
|
|
120
|
-
(self.cb)(line.as_ptr(), self.argp.load(atomic::Ordering::Relaxed));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
fn flush(&self) {}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
#[no_mangle]
|
|
127
|
-
pub extern "C" fn quiche_enable_debug_logging(
|
|
128
|
-
cb: extern "C" fn(line: *const u8, argp: *mut c_void), argp: *mut c_void,
|
|
129
|
-
) -> c_int {
|
|
130
|
-
let argp = atomic::AtomicPtr::new(argp);
|
|
131
|
-
let logger = Box::new(Logger { cb, argp });
|
|
132
|
-
|
|
133
|
-
if log::set_boxed_logger(logger).is_err() {
|
|
134
|
-
return -1;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
log::set_max_level(log::LevelFilter::Trace);
|
|
138
|
-
|
|
139
|
-
0
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
#[no_mangle]
|
|
143
|
-
pub extern "C" fn quiche_config_new(version: u32) -> *mut Config {
|
|
144
|
-
match Config::new(version) {
|
|
145
|
-
Ok(c) => Box::into_raw(Box::new(c)),
|
|
146
|
-
|
|
147
|
-
Err(_) => ptr::null_mut(),
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
#[no_mangle]
|
|
152
|
-
pub extern "C" fn quiche_config_load_cert_chain_from_pem_file(
|
|
153
|
-
config: &mut Config, path: *const c_char,
|
|
154
|
-
) -> c_int {
|
|
155
|
-
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
156
|
-
|
|
157
|
-
match config.load_cert_chain_from_pem_file(path) {
|
|
158
|
-
Ok(_) => 0,
|
|
159
|
-
|
|
160
|
-
Err(e) => e.to_c() as c_int,
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
#[no_mangle]
|
|
165
|
-
pub extern "C" fn quiche_config_load_priv_key_from_pem_file(
|
|
166
|
-
config: &mut Config, path: *const c_char,
|
|
167
|
-
) -> c_int {
|
|
168
|
-
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
169
|
-
|
|
170
|
-
match config.load_priv_key_from_pem_file(path) {
|
|
171
|
-
Ok(_) => 0,
|
|
172
|
-
|
|
173
|
-
Err(e) => e.to_c() as c_int,
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
#[no_mangle]
|
|
178
|
-
pub extern "C" fn quiche_config_load_verify_locations_from_file(
|
|
179
|
-
config: &mut Config, path: *const c_char,
|
|
180
|
-
) -> c_int {
|
|
181
|
-
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
182
|
-
|
|
183
|
-
match config.load_verify_locations_from_file(path) {
|
|
184
|
-
Ok(_) => 0,
|
|
185
|
-
|
|
186
|
-
Err(e) => e.to_c() as c_int,
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
#[no_mangle]
|
|
191
|
-
pub extern "C" fn quiche_config_load_verify_locations_from_directory(
|
|
192
|
-
config: &mut Config, path: *const c_char,
|
|
193
|
-
) -> c_int {
|
|
194
|
-
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
195
|
-
|
|
196
|
-
match config.load_verify_locations_from_directory(path) {
|
|
197
|
-
Ok(_) => 0,
|
|
198
|
-
|
|
199
|
-
Err(e) => e.to_c() as c_int,
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
#[no_mangle]
|
|
204
|
-
pub extern "C" fn quiche_config_verify_peer(config: &mut Config, v: bool) {
|
|
205
|
-
config.verify_peer(v);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
#[no_mangle]
|
|
209
|
-
pub extern "C" fn quiche_config_grease(config: &mut Config, v: bool) {
|
|
210
|
-
config.grease(v);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
#[no_mangle]
|
|
214
|
-
pub extern "C" fn quiche_config_discover_pmtu(config: &mut Config, v: bool) {
|
|
215
|
-
config.discover_pmtu(v);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
#[no_mangle]
|
|
219
|
-
pub extern "C" fn quiche_config_set_pmtud_max_probes(
|
|
220
|
-
config: &mut Config, max_probes: u8,
|
|
221
|
-
) {
|
|
222
|
-
config.set_pmtud_max_probes(max_probes);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
#[no_mangle]
|
|
226
|
-
pub extern "C" fn quiche_config_log_keys(config: &mut Config) {
|
|
227
|
-
config.log_keys();
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
#[no_mangle]
|
|
231
|
-
pub extern "C" fn quiche_config_enable_early_data(config: &mut Config) {
|
|
232
|
-
config.enable_early_data();
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
#[no_mangle]
|
|
236
|
-
/// Corresponds to the `Config::set_application_protos_wire_format` Rust
|
|
237
|
-
/// function.
|
|
238
|
-
pub extern "C" fn quiche_config_set_application_protos(
|
|
239
|
-
config: &mut Config, protos: *const u8, protos_len: size_t,
|
|
240
|
-
) -> c_int {
|
|
241
|
-
let protos = unsafe { slice::from_raw_parts(protos, protos_len) };
|
|
242
|
-
|
|
243
|
-
match config.set_application_protos_wire_format(protos) {
|
|
244
|
-
Ok(_) => 0,
|
|
245
|
-
|
|
246
|
-
Err(e) => e.to_c() as c_int,
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
#[no_mangle]
|
|
251
|
-
pub extern "C" fn quiche_config_set_max_amplification_factor(
|
|
252
|
-
config: &mut Config, v: usize,
|
|
253
|
-
) {
|
|
254
|
-
config.set_max_amplification_factor(v);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
#[no_mangle]
|
|
258
|
-
pub extern "C" fn quiche_config_set_max_idle_timeout(
|
|
259
|
-
config: &mut Config, v: u64,
|
|
260
|
-
) {
|
|
261
|
-
config.set_max_idle_timeout(v);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
#[no_mangle]
|
|
265
|
-
pub extern "C" fn quiche_config_set_max_recv_udp_payload_size(
|
|
266
|
-
config: &mut Config, v: size_t,
|
|
267
|
-
) {
|
|
268
|
-
config.set_max_recv_udp_payload_size(v);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
#[no_mangle]
|
|
272
|
-
pub extern "C" fn quiche_config_set_initial_max_data(
|
|
273
|
-
config: &mut Config, v: u64,
|
|
274
|
-
) {
|
|
275
|
-
config.set_initial_max_data(v);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
#[no_mangle]
|
|
279
|
-
pub extern "C" fn quiche_config_set_initial_max_stream_data_bidi_local(
|
|
280
|
-
config: &mut Config, v: u64,
|
|
281
|
-
) {
|
|
282
|
-
config.set_initial_max_stream_data_bidi_local(v);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
#[no_mangle]
|
|
286
|
-
pub extern "C" fn quiche_config_set_initial_max_stream_data_bidi_remote(
|
|
287
|
-
config: &mut Config, v: u64,
|
|
288
|
-
) {
|
|
289
|
-
config.set_initial_max_stream_data_bidi_remote(v);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
#[no_mangle]
|
|
293
|
-
pub extern "C" fn quiche_config_set_initial_max_stream_data_uni(
|
|
294
|
-
config: &mut Config, v: u64,
|
|
295
|
-
) {
|
|
296
|
-
config.set_initial_max_stream_data_uni(v);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
#[no_mangle]
|
|
300
|
-
pub extern "C" fn quiche_config_set_initial_max_streams_bidi(
|
|
301
|
-
config: &mut Config, v: u64,
|
|
302
|
-
) {
|
|
303
|
-
config.set_initial_max_streams_bidi(v);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
#[no_mangle]
|
|
307
|
-
pub extern "C" fn quiche_config_set_initial_max_streams_uni(
|
|
308
|
-
config: &mut Config, v: u64,
|
|
309
|
-
) {
|
|
310
|
-
config.set_initial_max_streams_uni(v);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
#[no_mangle]
|
|
314
|
-
pub extern "C" fn quiche_config_set_ack_delay_exponent(
|
|
315
|
-
config: &mut Config, v: u64,
|
|
316
|
-
) {
|
|
317
|
-
config.set_ack_delay_exponent(v);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
#[no_mangle]
|
|
321
|
-
pub extern "C" fn quiche_config_set_max_ack_delay(config: &mut Config, v: u64) {
|
|
322
|
-
config.set_max_ack_delay(v);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
#[no_mangle]
|
|
326
|
-
pub extern "C" fn quiche_config_set_disable_active_migration(
|
|
327
|
-
config: &mut Config, v: bool,
|
|
328
|
-
) {
|
|
329
|
-
config.set_disable_active_migration(v);
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
#[no_mangle]
|
|
333
|
-
pub extern "C" fn quiche_config_set_cc_algorithm_name(
|
|
334
|
-
config: &mut Config, name: *const c_char,
|
|
335
|
-
) -> c_int {
|
|
336
|
-
let name = unsafe { ffi::CStr::from_ptr(name).to_str().unwrap() };
|
|
337
|
-
match config.set_cc_algorithm_name(name) {
|
|
338
|
-
Ok(_) => 0,
|
|
339
|
-
|
|
340
|
-
Err(e) => e.to_c() as c_int,
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
#[no_mangle]
|
|
345
|
-
pub extern "C" fn quiche_config_set_cc_algorithm(
|
|
346
|
-
config: &mut Config, algo: CongestionControlAlgorithm,
|
|
347
|
-
) {
|
|
348
|
-
config.set_cc_algorithm(algo);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
#[no_mangle]
|
|
352
|
-
pub extern "C" fn quiche_config_set_initial_congestion_window_packets(
|
|
353
|
-
config: &mut Config, packets: size_t,
|
|
354
|
-
) {
|
|
355
|
-
config.set_initial_congestion_window_packets(packets);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
#[no_mangle]
|
|
359
|
-
pub extern "C" fn quiche_config_enable_hystart(config: &mut Config, v: bool) {
|
|
360
|
-
config.enable_hystart(v);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
#[no_mangle]
|
|
364
|
-
pub extern "C" fn quiche_config_enable_pacing(config: &mut Config, v: bool) {
|
|
365
|
-
config.enable_pacing(v);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
#[no_mangle]
|
|
369
|
-
pub extern "C" fn quiche_config_set_enable_cubic_idle_restart_fix(
|
|
370
|
-
config: &mut Config, v: bool,
|
|
371
|
-
) {
|
|
372
|
-
config.set_enable_cubic_idle_restart_fix(v);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
#[no_mangle]
|
|
376
|
-
pub extern "C" fn quiche_config_set_use_initial_max_data_as_flow_control_win(
|
|
377
|
-
config: &mut Config, v: bool,
|
|
378
|
-
) {
|
|
379
|
-
config.set_use_initial_max_data_as_flow_control_win(v);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
#[no_mangle]
|
|
383
|
-
pub extern "C" fn quiche_config_set_max_pacing_rate(config: &mut Config, v: u64) {
|
|
384
|
-
config.set_max_pacing_rate(v);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
#[no_mangle]
|
|
388
|
-
pub extern "C" fn quiche_config_enable_dgram(
|
|
389
|
-
config: &mut Config, enabled: bool, recv_queue_len: size_t,
|
|
390
|
-
send_queue_len: size_t,
|
|
391
|
-
) {
|
|
392
|
-
config.enable_dgram(enabled, recv_queue_len, send_queue_len);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
#[no_mangle]
|
|
396
|
-
pub extern "C" fn quiche_config_set_max_send_udp_payload_size(
|
|
397
|
-
config: &mut Config, v: size_t,
|
|
398
|
-
) {
|
|
399
|
-
config.set_max_send_udp_payload_size(v);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
#[no_mangle]
|
|
403
|
-
pub extern "C" fn quiche_config_set_max_connection_window(
|
|
404
|
-
config: &mut Config, v: u64,
|
|
405
|
-
) {
|
|
406
|
-
config.set_max_connection_window(v);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
#[no_mangle]
|
|
410
|
-
pub extern "C" fn quiche_config_set_max_stream_window(
|
|
411
|
-
config: &mut Config, v: u64,
|
|
412
|
-
) {
|
|
413
|
-
config.set_max_stream_window(v);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
#[no_mangle]
|
|
417
|
-
pub extern "C" fn quiche_config_set_active_connection_id_limit(
|
|
418
|
-
config: &mut Config, v: u64,
|
|
419
|
-
) {
|
|
420
|
-
config.set_active_connection_id_limit(v);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
#[no_mangle]
|
|
424
|
-
pub extern "C" fn quiche_config_set_stateless_reset_token(
|
|
425
|
-
config: &mut Config, v: *const u8,
|
|
426
|
-
) {
|
|
427
|
-
let reset_token = unsafe { slice::from_raw_parts(v, 16) };
|
|
428
|
-
let reset_token = match reset_token.try_into() {
|
|
429
|
-
Ok(rt) => rt,
|
|
430
|
-
Err(_) => unreachable!(),
|
|
431
|
-
};
|
|
432
|
-
let reset_token = u128::from_be_bytes(reset_token);
|
|
433
|
-
config.set_stateless_reset_token(Some(reset_token));
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
#[no_mangle]
|
|
437
|
-
pub extern "C" fn quiche_config_set_disable_dcid_reuse(
|
|
438
|
-
config: &mut Config, v: bool,
|
|
439
|
-
) {
|
|
440
|
-
config.set_disable_dcid_reuse(v);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
#[no_mangle]
|
|
444
|
-
pub extern "C" fn quiche_config_set_ticket_key(
|
|
445
|
-
config: &mut Config, key: *const u8, key_len: size_t,
|
|
446
|
-
) -> c_int {
|
|
447
|
-
let key = unsafe { slice::from_raw_parts(key, key_len) };
|
|
448
|
-
|
|
449
|
-
match config.set_ticket_key(key) {
|
|
450
|
-
Ok(_) => 0,
|
|
451
|
-
|
|
452
|
-
Err(e) => e.to_c() as c_int,
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
#[no_mangle]
|
|
457
|
-
pub extern "C" fn quiche_config_free(config: *mut Config) {
|
|
458
|
-
if !config.is_null() {
|
|
459
|
-
drop(unsafe { Box::from_raw(config) });
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
#[no_mangle]
|
|
464
|
-
pub extern "C" fn quiche_header_info(
|
|
465
|
-
buf: *mut u8, buf_len: size_t, dcil: size_t, version: *mut u32, ty: *mut u8,
|
|
466
|
-
scid: *mut u8, scid_len: *mut size_t, dcid: *mut u8, dcid_len: *mut size_t,
|
|
467
|
-
token: *mut u8, token_len: *mut size_t,
|
|
468
|
-
) -> c_int {
|
|
469
|
-
let buf = unsafe { slice::from_raw_parts_mut(buf, buf_len) };
|
|
470
|
-
let hdr = match Header::from_slice(buf, dcil) {
|
|
471
|
-
Ok(v) => v,
|
|
472
|
-
|
|
473
|
-
Err(e) => return e.to_c() as c_int,
|
|
474
|
-
};
|
|
475
|
-
|
|
476
|
-
unsafe {
|
|
477
|
-
*version = hdr.version;
|
|
478
|
-
|
|
479
|
-
*ty = match hdr.ty {
|
|
480
|
-
Type::Initial => 1,
|
|
481
|
-
Type::Retry => 2,
|
|
482
|
-
Type::Handshake => 3,
|
|
483
|
-
Type::ZeroRTT => 4,
|
|
484
|
-
Type::Short => 5,
|
|
485
|
-
Type::VersionNegotiation => 6,
|
|
486
|
-
};
|
|
487
|
-
|
|
488
|
-
if *scid_len < hdr.scid.len() {
|
|
489
|
-
return -1;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
let scid = slice::from_raw_parts_mut(scid, *scid_len);
|
|
493
|
-
let scid = &mut scid[..hdr.scid.len()];
|
|
494
|
-
scid.copy_from_slice(&hdr.scid);
|
|
495
|
-
|
|
496
|
-
*scid_len = hdr.scid.len();
|
|
497
|
-
|
|
498
|
-
if *dcid_len < hdr.dcid.len() {
|
|
499
|
-
return -1;
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
let dcid = slice::from_raw_parts_mut(dcid, *dcid_len);
|
|
503
|
-
let dcid = &mut dcid[..hdr.dcid.len()];
|
|
504
|
-
dcid.copy_from_slice(&hdr.dcid);
|
|
505
|
-
|
|
506
|
-
*dcid_len = hdr.dcid.len();
|
|
507
|
-
|
|
508
|
-
match hdr.token {
|
|
509
|
-
Some(tok) => {
|
|
510
|
-
if *token_len < tok.len() {
|
|
511
|
-
return -1;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
let token = slice::from_raw_parts_mut(token, *token_len);
|
|
515
|
-
let token = &mut token[..tok.len()];
|
|
516
|
-
token.copy_from_slice(&tok);
|
|
517
|
-
|
|
518
|
-
*token_len = tok.len();
|
|
519
|
-
},
|
|
520
|
-
|
|
521
|
-
None => *token_len = 0,
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
0
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
#[no_mangle]
|
|
529
|
-
pub extern "C" fn quiche_accept(
|
|
530
|
-
scid: *const u8, scid_len: size_t, odcid: *const u8, odcid_len: size_t,
|
|
531
|
-
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
532
|
-
config: &mut Config,
|
|
533
|
-
) -> *mut Connection {
|
|
534
|
-
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
535
|
-
let scid = ConnectionId::from_ref(scid);
|
|
536
|
-
|
|
537
|
-
let odcid = if !odcid.is_null() && odcid_len > 0 {
|
|
538
|
-
Some(ConnectionId::from_ref(unsafe {
|
|
539
|
-
slice::from_raw_parts(odcid, odcid_len)
|
|
540
|
-
}))
|
|
541
|
-
} else {
|
|
542
|
-
None
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
let local = std_addr_from_c(local, local_len);
|
|
546
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
547
|
-
|
|
548
|
-
match accept(&scid, odcid.as_ref(), local, peer, config) {
|
|
549
|
-
Ok(c) => Box::into_raw(Box::new(c)),
|
|
550
|
-
|
|
551
|
-
Err(_) => ptr::null_mut(),
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
#[no_mangle]
|
|
556
|
-
pub extern "C" fn quiche_connect(
|
|
557
|
-
server_name: *const c_char, scid: *const u8, scid_len: size_t,
|
|
558
|
-
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
559
|
-
config: &mut Config,
|
|
560
|
-
) -> *mut Connection {
|
|
561
|
-
let server_name = if server_name.is_null() {
|
|
562
|
-
None
|
|
563
|
-
} else {
|
|
564
|
-
Some(unsafe { ffi::CStr::from_ptr(server_name).to_str().unwrap() })
|
|
565
|
-
};
|
|
566
|
-
|
|
567
|
-
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
568
|
-
let scid = ConnectionId::from_ref(scid);
|
|
569
|
-
|
|
570
|
-
let local = std_addr_from_c(local, local_len);
|
|
571
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
572
|
-
|
|
573
|
-
match connect(server_name, &scid, local, peer, config) {
|
|
574
|
-
Ok(c) => Box::into_raw(Box::new(c)),
|
|
575
|
-
|
|
576
|
-
Err(_) => ptr::null_mut(),
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
#[no_mangle]
|
|
581
|
-
pub extern "C" fn quiche_negotiate_version(
|
|
582
|
-
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
583
|
-
out: *mut u8, out_len: size_t,
|
|
584
|
-
) -> ssize_t {
|
|
585
|
-
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
586
|
-
let scid = ConnectionId::from_ref(scid);
|
|
587
|
-
|
|
588
|
-
let dcid = unsafe { slice::from_raw_parts(dcid, dcid_len) };
|
|
589
|
-
let dcid = ConnectionId::from_ref(dcid);
|
|
590
|
-
|
|
591
|
-
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
592
|
-
|
|
593
|
-
match negotiate_version(&scid, &dcid, out) {
|
|
594
|
-
Ok(v) => v as ssize_t,
|
|
595
|
-
|
|
596
|
-
Err(e) => e.to_c(),
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
#[no_mangle]
|
|
601
|
-
pub extern "C" fn quiche_version_is_supported(version: u32) -> bool {
|
|
602
|
-
version_is_supported(version)
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
#[no_mangle]
|
|
606
|
-
pub extern "C" fn quiche_retry(
|
|
607
|
-
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
608
|
-
new_scid: *const u8, new_scid_len: size_t, token: *const u8,
|
|
609
|
-
token_len: size_t, version: u32, out: *mut u8, out_len: size_t,
|
|
610
|
-
) -> ssize_t {
|
|
611
|
-
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
612
|
-
let scid = ConnectionId::from_ref(scid);
|
|
613
|
-
|
|
614
|
-
let dcid = unsafe { slice::from_raw_parts(dcid, dcid_len) };
|
|
615
|
-
let dcid = ConnectionId::from_ref(dcid);
|
|
616
|
-
|
|
617
|
-
let new_scid = unsafe { slice::from_raw_parts(new_scid, new_scid_len) };
|
|
618
|
-
let new_scid = ConnectionId::from_ref(new_scid);
|
|
619
|
-
|
|
620
|
-
let token = unsafe { slice::from_raw_parts(token, token_len) };
|
|
621
|
-
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
622
|
-
|
|
623
|
-
match retry(&scid, &dcid, &new_scid, token, version, out) {
|
|
624
|
-
Ok(v) => v as ssize_t,
|
|
625
|
-
|
|
626
|
-
Err(e) => e.to_c(),
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
#[no_mangle]
|
|
631
|
-
#[cfg(feature = "custom-client-dcid")]
|
|
632
|
-
pub extern "C" fn quiche_conn_new_with_tls_and_client_dcid(
|
|
633
|
-
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
634
|
-
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
635
|
-
config: &Config, ssl: *mut c_void,
|
|
636
|
-
) -> *mut Connection {
|
|
637
|
-
{
|
|
638
|
-
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
639
|
-
let scid = ConnectionId::from_ref(scid);
|
|
640
|
-
|
|
641
|
-
let dcid = if !dcid.is_null() && dcid_len > 0 {
|
|
642
|
-
Some(ConnectionId::from_ref(unsafe {
|
|
643
|
-
slice::from_raw_parts(dcid, dcid_len)
|
|
644
|
-
}))
|
|
645
|
-
} else {
|
|
646
|
-
None
|
|
647
|
-
};
|
|
648
|
-
|
|
649
|
-
let local = std_addr_from_c(local, local_len);
|
|
650
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
651
|
-
|
|
652
|
-
let tls = unsafe { tls::Handshake::from_ptr(ssl) };
|
|
653
|
-
|
|
654
|
-
match Connection::with_tls(
|
|
655
|
-
&scid,
|
|
656
|
-
None, // retry_cids
|
|
657
|
-
dcid.as_ref(),
|
|
658
|
-
local,
|
|
659
|
-
peer,
|
|
660
|
-
config,
|
|
661
|
-
tls,
|
|
662
|
-
false,
|
|
663
|
-
) {
|
|
664
|
-
Ok(c) => Box::into_raw(Box::new(c)),
|
|
665
|
-
|
|
666
|
-
Err(_) => ptr::null_mut(),
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
#[no_mangle]
|
|
672
|
-
#[cfg(not(feature = "custom-client-dcid"))]
|
|
673
|
-
#[allow(unused_variables)]
|
|
674
|
-
pub extern "C" fn quiche_conn_new_with_tls_and_client_dcid(
|
|
675
|
-
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
676
|
-
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
677
|
-
config: &Config, ssl: *mut c_void,
|
|
678
|
-
) -> *mut Connection {
|
|
679
|
-
// It's always an error to call this function without the custom-client-dcid
|
|
680
|
-
// feature enabled.
|
|
681
|
-
ptr::null_mut()
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
#[no_mangle]
|
|
685
|
-
pub extern "C" fn quiche_conn_new_with_tls(
|
|
686
|
-
scid: *const u8, scid_len: size_t, odcid: *const u8, odcid_len: size_t,
|
|
687
|
-
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
688
|
-
config: &Config, ssl: *mut c_void, is_server: bool,
|
|
689
|
-
) -> *mut Connection {
|
|
690
|
-
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
691
|
-
let scid = ConnectionId::from_ref(scid);
|
|
692
|
-
|
|
693
|
-
let odcid = if !odcid.is_null() && odcid_len > 0 {
|
|
694
|
-
Some(ConnectionId::from_ref(unsafe {
|
|
695
|
-
slice::from_raw_parts(odcid, odcid_len)
|
|
696
|
-
}))
|
|
697
|
-
} else {
|
|
698
|
-
None
|
|
699
|
-
};
|
|
700
|
-
|
|
701
|
-
let retry_cids = odcid.as_ref().map(|odcid| RetryConnectionIds {
|
|
702
|
-
original_destination_cid: odcid,
|
|
703
|
-
retry_source_cid: &scid,
|
|
704
|
-
});
|
|
705
|
-
|
|
706
|
-
let local = std_addr_from_c(local, local_len);
|
|
707
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
708
|
-
|
|
709
|
-
let tls = unsafe { tls::Handshake::from_ptr(ssl) };
|
|
710
|
-
|
|
711
|
-
match Connection::with_tls(
|
|
712
|
-
&scid, retry_cids, None, local, peer, config, tls, is_server,
|
|
713
|
-
) {
|
|
714
|
-
Ok(c) => Box::into_raw(Box::new(c)),
|
|
715
|
-
|
|
716
|
-
Err(_) => ptr::null_mut(),
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
#[no_mangle]
|
|
721
|
-
pub extern "C" fn quiche_conn_set_keylog_path(
|
|
722
|
-
conn: &mut Connection, path: *const c_char,
|
|
723
|
-
) -> bool {
|
|
724
|
-
let filename = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
725
|
-
|
|
726
|
-
let file = std::fs::OpenOptions::new()
|
|
727
|
-
.create(true)
|
|
728
|
-
.append(true)
|
|
729
|
-
.open(filename);
|
|
730
|
-
|
|
731
|
-
let writer = match file {
|
|
732
|
-
Ok(f) => std::io::BufWriter::new(f),
|
|
733
|
-
|
|
734
|
-
Err(_) => return false,
|
|
735
|
-
};
|
|
736
|
-
|
|
737
|
-
conn.set_keylog(Box::new(writer));
|
|
738
|
-
|
|
739
|
-
true
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
#[no_mangle]
|
|
743
|
-
#[cfg(unix)]
|
|
744
|
-
pub extern "C" fn quiche_conn_set_keylog_fd(conn: &mut Connection, fd: c_int) {
|
|
745
|
-
let f = unsafe { std::fs::File::from_raw_fd(fd) };
|
|
746
|
-
let writer = std::io::BufWriter::new(f);
|
|
747
|
-
|
|
748
|
-
conn.set_keylog(Box::new(writer));
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
#[no_mangle]
|
|
752
|
-
#[cfg(feature = "qlog")]
|
|
753
|
-
pub extern "C" fn quiche_conn_set_qlog_path(
|
|
754
|
-
conn: &mut Connection, path: *const c_char, log_title: *const c_char,
|
|
755
|
-
log_desc: *const c_char,
|
|
756
|
-
) -> bool {
|
|
757
|
-
let filename = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
758
|
-
|
|
759
|
-
let file = std::fs::OpenOptions::new()
|
|
760
|
-
.write(true)
|
|
761
|
-
.create_new(true)
|
|
762
|
-
.open(filename);
|
|
763
|
-
|
|
764
|
-
let writer = match file {
|
|
765
|
-
Ok(f) => std::io::BufWriter::new(f),
|
|
766
|
-
|
|
767
|
-
Err(_) => return false,
|
|
768
|
-
};
|
|
769
|
-
|
|
770
|
-
let title = unsafe { ffi::CStr::from_ptr(log_title).to_str().unwrap() };
|
|
771
|
-
let description = unsafe { ffi::CStr::from_ptr(log_desc).to_str().unwrap() };
|
|
772
|
-
|
|
773
|
-
conn.set_qlog(
|
|
774
|
-
Box::new(writer),
|
|
775
|
-
title.to_string(),
|
|
776
|
-
format!("{} id={}", description, conn.trace_id),
|
|
777
|
-
);
|
|
778
|
-
|
|
779
|
-
true
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
#[no_mangle]
|
|
783
|
-
#[cfg(all(unix, feature = "qlog"))]
|
|
784
|
-
pub extern "C" fn quiche_conn_set_qlog_fd(
|
|
785
|
-
conn: &mut Connection, fd: c_int, log_title: *const c_char,
|
|
786
|
-
log_desc: *const c_char,
|
|
787
|
-
) {
|
|
788
|
-
let f = unsafe { std::fs::File::from_raw_fd(fd) };
|
|
789
|
-
let writer = std::io::BufWriter::new(f);
|
|
790
|
-
|
|
791
|
-
let title = unsafe { ffi::CStr::from_ptr(log_title).to_str().unwrap() };
|
|
792
|
-
let description = unsafe { ffi::CStr::from_ptr(log_desc).to_str().unwrap() };
|
|
793
|
-
|
|
794
|
-
conn.set_qlog(
|
|
795
|
-
Box::new(writer),
|
|
796
|
-
title.to_string(),
|
|
797
|
-
format!("{} id={}", description, conn.trace_id),
|
|
798
|
-
);
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
#[no_mangle]
|
|
802
|
-
pub extern "C" fn quiche_conn_set_session(
|
|
803
|
-
conn: &mut Connection, buf: *const u8, buf_len: size_t,
|
|
804
|
-
) -> c_int {
|
|
805
|
-
let buf = unsafe { slice::from_raw_parts(buf, buf_len) };
|
|
806
|
-
|
|
807
|
-
match conn.set_session(buf) {
|
|
808
|
-
Ok(_) => 0,
|
|
809
|
-
|
|
810
|
-
Err(e) => e.to_c() as c_int,
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
#[no_mangle]
|
|
815
|
-
pub extern "C" fn quiche_conn_set_max_idle_timeout(
|
|
816
|
-
conn: &mut Connection, v: u64,
|
|
817
|
-
) -> c_int {
|
|
818
|
-
match conn.set_max_idle_timeout(v) {
|
|
819
|
-
Ok(()) => 0,
|
|
820
|
-
|
|
821
|
-
Err(e) => e.to_c() as c_int,
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
#[repr(C)]
|
|
826
|
-
pub struct RecvInfo<'a> {
|
|
827
|
-
from: &'a sockaddr,
|
|
828
|
-
from_len: socklen_t,
|
|
829
|
-
to: &'a sockaddr,
|
|
830
|
-
to_len: socklen_t,
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
impl From<&RecvInfo<'_>> for crate::RecvInfo {
|
|
834
|
-
fn from(info: &RecvInfo) -> crate::RecvInfo {
|
|
835
|
-
crate::RecvInfo {
|
|
836
|
-
from: std_addr_from_c(info.from, info.from_len),
|
|
837
|
-
to: std_addr_from_c(info.to, info.to_len),
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
#[no_mangle]
|
|
843
|
-
pub extern "C" fn quiche_conn_recv(
|
|
844
|
-
conn: &mut Connection, buf: *mut u8, buf_len: size_t, info: &RecvInfo,
|
|
845
|
-
) -> ssize_t {
|
|
846
|
-
if buf_len > <ssize_t>::MAX as usize {
|
|
847
|
-
panic!("The provided buffer is too large");
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
let buf = unsafe { slice::from_raw_parts_mut(buf, buf_len) };
|
|
851
|
-
|
|
852
|
-
match conn.recv(buf, info.into()) {
|
|
853
|
-
Ok(v) => v as ssize_t,
|
|
854
|
-
|
|
855
|
-
Err(e) => e.to_c(),
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
#[repr(C)]
|
|
860
|
-
pub struct SendInfo {
|
|
861
|
-
from: sockaddr_storage,
|
|
862
|
-
from_len: socklen_t,
|
|
863
|
-
to: sockaddr_storage,
|
|
864
|
-
to_len: socklen_t,
|
|
865
|
-
|
|
866
|
-
at: timespec,
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
#[no_mangle]
|
|
870
|
-
pub extern "C" fn quiche_conn_send(
|
|
871
|
-
conn: &mut Connection, out: *mut u8, out_len: size_t, out_info: &mut SendInfo,
|
|
872
|
-
) -> ssize_t {
|
|
873
|
-
if out_len > <ssize_t>::MAX as usize {
|
|
874
|
-
panic!("The provided buffer is too large");
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
878
|
-
|
|
879
|
-
match conn.send(out) {
|
|
880
|
-
Ok((v, info)) => {
|
|
881
|
-
out_info.from_len = std_addr_to_c(&info.from, &mut out_info.from);
|
|
882
|
-
out_info.to_len = std_addr_to_c(&info.to, &mut out_info.to);
|
|
883
|
-
|
|
884
|
-
std_time_to_c(&info.at, &mut out_info.at);
|
|
885
|
-
|
|
886
|
-
v as ssize_t
|
|
887
|
-
},
|
|
888
|
-
|
|
889
|
-
Err(e) => e.to_c(),
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
#[no_mangle]
|
|
894
|
-
pub extern "C" fn quiche_conn_send_on_path(
|
|
895
|
-
conn: &mut Connection, out: *mut u8, out_len: size_t, from: *const sockaddr,
|
|
896
|
-
from_len: socklen_t, to: *const sockaddr, to_len: socklen_t,
|
|
897
|
-
out_info: &mut SendInfo,
|
|
898
|
-
) -> ssize_t {
|
|
899
|
-
if out_len > <ssize_t>::MAX as usize {
|
|
900
|
-
panic!("The provided buffer is too large");
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
let from = optional_std_addr_from_c(from, from_len);
|
|
904
|
-
let to = optional_std_addr_from_c(to, to_len);
|
|
905
|
-
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
906
|
-
|
|
907
|
-
match conn.send_on_path(out, from, to) {
|
|
908
|
-
Ok((v, info)) => {
|
|
909
|
-
out_info.from_len = std_addr_to_c(&info.from, &mut out_info.from);
|
|
910
|
-
out_info.to_len = std_addr_to_c(&info.to, &mut out_info.to);
|
|
911
|
-
|
|
912
|
-
std_time_to_c(&info.at, &mut out_info.at);
|
|
913
|
-
|
|
914
|
-
v as ssize_t
|
|
915
|
-
},
|
|
916
|
-
|
|
917
|
-
Err(e) => e.to_c(),
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
#[no_mangle]
|
|
922
|
-
pub extern "C" fn quiche_conn_stream_recv(
|
|
923
|
-
conn: &mut Connection, stream_id: u64, out: *mut u8, out_len: size_t,
|
|
924
|
-
fin: &mut bool, out_error_code: &mut u64,
|
|
925
|
-
) -> ssize_t {
|
|
926
|
-
if out_len > <ssize_t>::MAX as usize {
|
|
927
|
-
panic!("The provided buffer is too large");
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
931
|
-
|
|
932
|
-
let (out_len, out_fin) = match conn.stream_recv(stream_id, out) {
|
|
933
|
-
Ok(v) => v,
|
|
934
|
-
|
|
935
|
-
Err(e) => {
|
|
936
|
-
match e {
|
|
937
|
-
Error::StreamReset(error) => *out_error_code = error,
|
|
938
|
-
Error::StreamStopped(error) => *out_error_code = error,
|
|
939
|
-
_ => {},
|
|
940
|
-
}
|
|
941
|
-
return e.to_c();
|
|
942
|
-
},
|
|
943
|
-
};
|
|
944
|
-
|
|
945
|
-
*fin = out_fin;
|
|
946
|
-
|
|
947
|
-
out_len as ssize_t
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
#[no_mangle]
|
|
951
|
-
pub extern "C" fn quiche_conn_stream_send(
|
|
952
|
-
conn: &mut Connection, stream_id: u64, buf: *const u8, buf_len: size_t,
|
|
953
|
-
fin: bool, out_error_code: &mut u64,
|
|
954
|
-
) -> ssize_t {
|
|
955
|
-
if buf_len > <ssize_t>::MAX as usize {
|
|
956
|
-
panic!("The provided buffer is too large");
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
let buf = if buf.is_null() {
|
|
960
|
-
assert_eq!(buf_len, 0);
|
|
961
|
-
&[]
|
|
962
|
-
} else {
|
|
963
|
-
unsafe { slice::from_raw_parts(buf, buf_len) }
|
|
964
|
-
};
|
|
965
|
-
|
|
966
|
-
match conn.stream_send(stream_id, buf, fin) {
|
|
967
|
-
Ok(v) => v as ssize_t,
|
|
968
|
-
|
|
969
|
-
Err(e) => {
|
|
970
|
-
match e {
|
|
971
|
-
Error::StreamReset(error) => *out_error_code = error,
|
|
972
|
-
Error::StreamStopped(error) => *out_error_code = error,
|
|
973
|
-
_ => {},
|
|
974
|
-
}
|
|
975
|
-
e.to_c()
|
|
976
|
-
},
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
#[no_mangle]
|
|
981
|
-
pub extern "C" fn quiche_conn_stream_priority(
|
|
982
|
-
conn: &mut Connection, stream_id: u64, urgency: u8, incremental: bool,
|
|
983
|
-
) -> c_int {
|
|
984
|
-
match conn.stream_priority(stream_id, urgency, incremental) {
|
|
985
|
-
Ok(_) => 0,
|
|
986
|
-
|
|
987
|
-
Err(e) => e.to_c() as c_int,
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
#[no_mangle]
|
|
992
|
-
pub extern "C" fn quiche_conn_stream_shutdown(
|
|
993
|
-
conn: &mut Connection, stream_id: u64, direction: Shutdown, err: u64,
|
|
994
|
-
) -> c_int {
|
|
995
|
-
match conn.stream_shutdown(stream_id, direction, err) {
|
|
996
|
-
Ok(_) => 0,
|
|
997
|
-
|
|
998
|
-
Err(e) => e.to_c() as c_int,
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
#[no_mangle]
|
|
1003
|
-
pub extern "C" fn quiche_conn_stream_capacity(
|
|
1004
|
-
conn: &mut Connection, stream_id: u64,
|
|
1005
|
-
) -> ssize_t {
|
|
1006
|
-
match conn.stream_capacity(stream_id) {
|
|
1007
|
-
Ok(v) => v as ssize_t,
|
|
1008
|
-
|
|
1009
|
-
Err(e) => e.to_c(),
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
#[no_mangle]
|
|
1014
|
-
pub extern "C" fn quiche_conn_stream_readable(
|
|
1015
|
-
conn: &Connection, stream_id: u64,
|
|
1016
|
-
) -> bool {
|
|
1017
|
-
conn.stream_readable(stream_id)
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
#[no_mangle]
|
|
1021
|
-
pub extern "C" fn quiche_conn_stream_readable_next(conn: &mut Connection) -> i64 {
|
|
1022
|
-
conn.stream_readable_next().map(|v| v as i64).unwrap_or(-1)
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
#[no_mangle]
|
|
1026
|
-
pub extern "C" fn quiche_conn_stream_writable(
|
|
1027
|
-
conn: &mut Connection, stream_id: u64, len: usize,
|
|
1028
|
-
) -> c_int {
|
|
1029
|
-
match conn.stream_writable(stream_id, len) {
|
|
1030
|
-
Ok(true) => 1,
|
|
1031
|
-
|
|
1032
|
-
Ok(false) => 0,
|
|
1033
|
-
|
|
1034
|
-
Err(e) => e.to_c() as c_int,
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
#[no_mangle]
|
|
1039
|
-
pub extern "C" fn quiche_conn_stream_writable_next(conn: &mut Connection) -> i64 {
|
|
1040
|
-
conn.stream_writable_next().map(|v| v as i64).unwrap_or(-1)
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
#[no_mangle]
|
|
1044
|
-
pub extern "C" fn quiche_conn_stream_finished(
|
|
1045
|
-
conn: &Connection, stream_id: u64,
|
|
1046
|
-
) -> bool {
|
|
1047
|
-
conn.stream_finished(stream_id)
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
#[no_mangle]
|
|
1051
|
-
pub extern "C" fn quiche_conn_readable(conn: &Connection) -> *mut StreamIter {
|
|
1052
|
-
Box::into_raw(Box::new(conn.readable()))
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
#[no_mangle]
|
|
1056
|
-
pub extern "C" fn quiche_conn_writable(conn: &Connection) -> *mut StreamIter {
|
|
1057
|
-
Box::into_raw(Box::new(conn.writable()))
|
|
1058
|
-
}
|
|
1059
|
-
|
|
1060
|
-
#[no_mangle]
|
|
1061
|
-
pub extern "C" fn quiche_conn_max_send_udp_payload_size(
|
|
1062
|
-
conn: &Connection,
|
|
1063
|
-
) -> usize {
|
|
1064
|
-
conn.max_send_udp_payload_size()
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
#[no_mangle]
|
|
1068
|
-
pub extern "C" fn quiche_conn_is_readable(conn: &Connection) -> bool {
|
|
1069
|
-
conn.is_readable()
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
|
-
#[no_mangle]
|
|
1073
|
-
pub extern "C" fn quiche_conn_close(
|
|
1074
|
-
conn: &mut Connection, app: bool, err: u64, reason: *const u8,
|
|
1075
|
-
reason_len: size_t,
|
|
1076
|
-
) -> c_int {
|
|
1077
|
-
let reason = if reason.is_null() {
|
|
1078
|
-
assert_eq!(reason_len, 0);
|
|
1079
|
-
&[]
|
|
1080
|
-
} else {
|
|
1081
|
-
unsafe { slice::from_raw_parts(reason, reason_len) }
|
|
1082
|
-
};
|
|
1083
|
-
|
|
1084
|
-
match conn.close(app, err, reason) {
|
|
1085
|
-
Ok(_) => 0,
|
|
1086
|
-
|
|
1087
|
-
Err(e) => e.to_c() as c_int,
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
#[no_mangle]
|
|
1092
|
-
pub extern "C" fn quiche_conn_timeout_as_nanos(conn: &Connection) -> u64 {
|
|
1093
|
-
match conn.timeout() {
|
|
1094
|
-
Some(timeout) => timeout.as_nanos() as u64,
|
|
1095
|
-
|
|
1096
|
-
None => u64::MAX,
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
|
|
1100
|
-
#[no_mangle]
|
|
1101
|
-
pub extern "C" fn quiche_conn_timeout_as_millis(conn: &Connection) -> u64 {
|
|
1102
|
-
match conn.timeout() {
|
|
1103
|
-
Some(timeout) => timeout.as_millis() as u64,
|
|
1104
|
-
|
|
1105
|
-
None => u64::MAX,
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
#[no_mangle]
|
|
1110
|
-
pub extern "C" fn quiche_conn_on_timeout(conn: &mut Connection) {
|
|
1111
|
-
conn.on_timeout()
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
|
-
#[no_mangle]
|
|
1115
|
-
pub extern "C" fn quiche_conn_trace_id(
|
|
1116
|
-
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1117
|
-
) {
|
|
1118
|
-
let trace_id = conn.trace_id();
|
|
1119
|
-
|
|
1120
|
-
*out = trace_id.as_ptr();
|
|
1121
|
-
*out_len = trace_id.len();
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
|
-
/// An iterator over connection ids.
|
|
1125
|
-
#[derive(Default)]
|
|
1126
|
-
pub struct ConnectionIdIter<'a> {
|
|
1127
|
-
cids: Vec<ConnectionId<'a>>,
|
|
1128
|
-
index: usize,
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
impl<'a> Iterator for ConnectionIdIter<'a> {
|
|
1132
|
-
type Item = ConnectionId<'a>;
|
|
1133
|
-
|
|
1134
|
-
#[inline]
|
|
1135
|
-
fn next(&mut self) -> Option<Self::Item> {
|
|
1136
|
-
let v = self.cids.get(self.index)?;
|
|
1137
|
-
self.index += 1;
|
|
1138
|
-
Some(v.clone())
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
|
|
1142
|
-
#[no_mangle]
|
|
1143
|
-
pub extern "C" fn quiche_conn_source_ids(
|
|
1144
|
-
conn: &Connection,
|
|
1145
|
-
) -> *mut ConnectionIdIter<'_> {
|
|
1146
|
-
let vec = conn.source_ids().cloned().collect();
|
|
1147
|
-
Box::into_raw(Box::new(ConnectionIdIter {
|
|
1148
|
-
cids: vec,
|
|
1149
|
-
index: 0,
|
|
1150
|
-
}))
|
|
1151
|
-
}
|
|
1152
|
-
|
|
1153
|
-
#[no_mangle]
|
|
1154
|
-
pub extern "C" fn quiche_connection_id_iter_next(
|
|
1155
|
-
iter: &mut ConnectionIdIter, out: &mut *const u8, out_len: &mut size_t,
|
|
1156
|
-
) -> bool {
|
|
1157
|
-
if let Some(conn_id) = iter.next() {
|
|
1158
|
-
let id = conn_id.as_ref();
|
|
1159
|
-
*out = id.as_ptr();
|
|
1160
|
-
*out_len = id.len();
|
|
1161
|
-
return true;
|
|
1162
|
-
}
|
|
1163
|
-
|
|
1164
|
-
false
|
|
1165
|
-
}
|
|
1166
|
-
|
|
1167
|
-
#[no_mangle]
|
|
1168
|
-
pub extern "C" fn quiche_connection_id_iter_free(iter: *mut ConnectionIdIter) {
|
|
1169
|
-
if !iter.is_null() {
|
|
1170
|
-
drop(unsafe { Box::from_raw(iter) });
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
#[no_mangle]
|
|
1175
|
-
pub extern "C" fn quiche_conn_source_id(
|
|
1176
|
-
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1177
|
-
) {
|
|
1178
|
-
let conn_id = conn.source_id();
|
|
1179
|
-
let id = conn_id.as_ref();
|
|
1180
|
-
*out = id.as_ptr();
|
|
1181
|
-
*out_len = id.len();
|
|
1182
|
-
}
|
|
1183
|
-
|
|
1184
|
-
#[no_mangle]
|
|
1185
|
-
pub extern "C" fn quiche_conn_destination_id(
|
|
1186
|
-
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1187
|
-
) {
|
|
1188
|
-
let conn_id = conn.destination_id();
|
|
1189
|
-
let id = conn_id.as_ref();
|
|
1190
|
-
|
|
1191
|
-
*out = id.as_ptr();
|
|
1192
|
-
*out_len = id.len();
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
#[no_mangle]
|
|
1196
|
-
pub extern "C" fn quiche_conn_application_proto(
|
|
1197
|
-
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1198
|
-
) {
|
|
1199
|
-
let proto = conn.application_proto();
|
|
1200
|
-
|
|
1201
|
-
*out = proto.as_ptr();
|
|
1202
|
-
*out_len = proto.len();
|
|
1203
|
-
}
|
|
1204
|
-
|
|
1205
|
-
#[no_mangle]
|
|
1206
|
-
pub extern "C" fn quiche_conn_peer_cert(
|
|
1207
|
-
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1208
|
-
) {
|
|
1209
|
-
match conn.peer_cert() {
|
|
1210
|
-
Some(peer_cert) => {
|
|
1211
|
-
*out = peer_cert.as_ptr();
|
|
1212
|
-
*out_len = peer_cert.len();
|
|
1213
|
-
},
|
|
1214
|
-
|
|
1215
|
-
None => *out_len = 0,
|
|
1216
|
-
}
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
#[no_mangle]
|
|
1220
|
-
pub extern "C" fn quiche_conn_session(
|
|
1221
|
-
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1222
|
-
) {
|
|
1223
|
-
match conn.session() {
|
|
1224
|
-
Some(session) => {
|
|
1225
|
-
*out = session.as_ptr();
|
|
1226
|
-
*out_len = session.len();
|
|
1227
|
-
},
|
|
1228
|
-
|
|
1229
|
-
None => *out_len = 0,
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
#[no_mangle]
|
|
1234
|
-
pub extern "C" fn quiche_conn_server_name(
|
|
1235
|
-
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1236
|
-
) {
|
|
1237
|
-
match conn.server_name() {
|
|
1238
|
-
Some(server_name) => {
|
|
1239
|
-
*out = server_name.as_ptr();
|
|
1240
|
-
*out_len = server_name.len();
|
|
1241
|
-
},
|
|
1242
|
-
|
|
1243
|
-
None => *out_len = 0,
|
|
1244
|
-
}
|
|
1245
|
-
}
|
|
1246
|
-
|
|
1247
|
-
#[no_mangle]
|
|
1248
|
-
pub extern "C" fn quiche_conn_is_established(conn: &Connection) -> bool {
|
|
1249
|
-
conn.is_established()
|
|
1250
|
-
}
|
|
1251
|
-
|
|
1252
|
-
#[no_mangle]
|
|
1253
|
-
pub extern "C" fn quiche_conn_is_resumed(conn: &Connection) -> bool {
|
|
1254
|
-
conn.is_resumed()
|
|
1255
|
-
}
|
|
1256
|
-
|
|
1257
|
-
#[no_mangle]
|
|
1258
|
-
pub extern "C" fn quiche_conn_is_in_early_data(conn: &Connection) -> bool {
|
|
1259
|
-
conn.is_in_early_data()
|
|
1260
|
-
}
|
|
1261
|
-
|
|
1262
|
-
#[no_mangle]
|
|
1263
|
-
pub extern "C" fn quiche_conn_is_draining(conn: &Connection) -> bool {
|
|
1264
|
-
conn.is_draining()
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
#[no_mangle]
|
|
1268
|
-
pub extern "C" fn quiche_conn_is_closed(conn: &Connection) -> bool {
|
|
1269
|
-
conn.is_closed()
|
|
1270
|
-
}
|
|
1271
|
-
|
|
1272
|
-
#[no_mangle]
|
|
1273
|
-
pub extern "C" fn quiche_conn_is_timed_out(conn: &Connection) -> bool {
|
|
1274
|
-
conn.is_timed_out()
|
|
1275
|
-
}
|
|
1276
|
-
|
|
1277
|
-
#[no_mangle]
|
|
1278
|
-
pub extern "C" fn quiche_conn_peer_error(
|
|
1279
|
-
conn: &Connection, is_app: *mut bool, error_code: *mut u64,
|
|
1280
|
-
reason: &mut *const u8, reason_len: &mut size_t,
|
|
1281
|
-
) -> bool {
|
|
1282
|
-
match &conn.peer_error {
|
|
1283
|
-
Some(conn_err) => unsafe {
|
|
1284
|
-
*is_app = conn_err.is_app;
|
|
1285
|
-
*error_code = conn_err.error_code;
|
|
1286
|
-
*reason = conn_err.reason.as_ptr();
|
|
1287
|
-
*reason_len = conn_err.reason.len();
|
|
1288
|
-
|
|
1289
|
-
true
|
|
1290
|
-
},
|
|
1291
|
-
|
|
1292
|
-
None => false,
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
#[no_mangle]
|
|
1297
|
-
pub extern "C" fn quiche_conn_local_error(
|
|
1298
|
-
conn: &Connection, is_app: *mut bool, error_code: *mut u64,
|
|
1299
|
-
reason: &mut *const u8, reason_len: &mut size_t,
|
|
1300
|
-
) -> bool {
|
|
1301
|
-
match &conn.local_error {
|
|
1302
|
-
Some(conn_err) => unsafe {
|
|
1303
|
-
*is_app = conn_err.is_app;
|
|
1304
|
-
*error_code = conn_err.error_code;
|
|
1305
|
-
*reason = conn_err.reason.as_ptr();
|
|
1306
|
-
*reason_len = conn_err.reason.len();
|
|
1307
|
-
|
|
1308
|
-
true
|
|
1309
|
-
},
|
|
1310
|
-
|
|
1311
|
-
None => false,
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
|
|
1315
|
-
#[no_mangle]
|
|
1316
|
-
pub extern "C" fn quiche_stream_iter_next(
|
|
1317
|
-
iter: &mut StreamIter, stream_id: *mut u64,
|
|
1318
|
-
) -> bool {
|
|
1319
|
-
if let Some(v) = iter.next() {
|
|
1320
|
-
unsafe { *stream_id = v };
|
|
1321
|
-
return true;
|
|
1322
|
-
}
|
|
1323
|
-
|
|
1324
|
-
false
|
|
1325
|
-
}
|
|
1326
|
-
|
|
1327
|
-
#[no_mangle]
|
|
1328
|
-
pub extern "C" fn quiche_stream_iter_free(iter: *mut StreamIter) {
|
|
1329
|
-
if !iter.is_null() {
|
|
1330
|
-
drop(unsafe { Box::from_raw(iter) });
|
|
1331
|
-
}
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1334
|
-
#[repr(C)]
|
|
1335
|
-
pub struct Stats {
|
|
1336
|
-
recv: usize,
|
|
1337
|
-
sent: usize,
|
|
1338
|
-
lost: usize,
|
|
1339
|
-
spurious_lost: usize,
|
|
1340
|
-
retrans: usize,
|
|
1341
|
-
sent_bytes: u64,
|
|
1342
|
-
recv_bytes: u64,
|
|
1343
|
-
acked_bytes: u64,
|
|
1344
|
-
lost_bytes: u64,
|
|
1345
|
-
stream_retrans_bytes: u64,
|
|
1346
|
-
dgram_recv: usize,
|
|
1347
|
-
dgram_sent: usize,
|
|
1348
|
-
paths_count: usize,
|
|
1349
|
-
reset_stream_count_local: u64,
|
|
1350
|
-
stopped_stream_count_local: u64,
|
|
1351
|
-
reset_stream_count_remote: u64,
|
|
1352
|
-
stopped_stream_count_remote: u64,
|
|
1353
|
-
data_blocked_sent_count: u64,
|
|
1354
|
-
stream_data_blocked_sent_count: u64,
|
|
1355
|
-
data_blocked_recv_count: u64,
|
|
1356
|
-
stream_data_blocked_recv_count: u64,
|
|
1357
|
-
streams_blocked_bidi_recv_count: u64,
|
|
1358
|
-
streams_blocked_uni_recv_count: u64,
|
|
1359
|
-
path_challenge_rx_count: u64,
|
|
1360
|
-
bytes_in_flight_duration_msec: u64,
|
|
1361
|
-
tx_buffered_inconsistent: bool,
|
|
1362
|
-
}
|
|
1363
|
-
|
|
1364
|
-
pub struct TransportParams {
|
|
1365
|
-
max_idle_timeout: u64,
|
|
1366
|
-
max_udp_payload_size: u64,
|
|
1367
|
-
initial_max_data: u64,
|
|
1368
|
-
initial_max_stream_data_bidi_local: u64,
|
|
1369
|
-
initial_max_stream_data_bidi_remote: u64,
|
|
1370
|
-
initial_max_stream_data_uni: u64,
|
|
1371
|
-
initial_max_streams_bidi: u64,
|
|
1372
|
-
initial_max_streams_uni: u64,
|
|
1373
|
-
ack_delay_exponent: u64,
|
|
1374
|
-
max_ack_delay: u64,
|
|
1375
|
-
disable_active_migration: bool,
|
|
1376
|
-
active_conn_id_limit: u64,
|
|
1377
|
-
max_datagram_frame_size: ssize_t,
|
|
1378
|
-
}
|
|
1379
|
-
|
|
1380
|
-
#[no_mangle]
|
|
1381
|
-
pub extern "C" fn quiche_conn_stats(conn: &Connection, out: &mut Stats) {
|
|
1382
|
-
let stats = conn.stats();
|
|
1383
|
-
|
|
1384
|
-
out.recv = stats.recv;
|
|
1385
|
-
out.sent = stats.sent;
|
|
1386
|
-
out.lost = stats.lost;
|
|
1387
|
-
out.spurious_lost = stats.spurious_lost;
|
|
1388
|
-
out.retrans = stats.retrans;
|
|
1389
|
-
out.sent_bytes = stats.sent_bytes;
|
|
1390
|
-
out.recv_bytes = stats.recv_bytes;
|
|
1391
|
-
out.acked_bytes = stats.acked_bytes;
|
|
1392
|
-
out.lost_bytes = stats.lost_bytes;
|
|
1393
|
-
out.stream_retrans_bytes = stats.stream_retrans_bytes;
|
|
1394
|
-
out.dgram_recv = stats.dgram_recv;
|
|
1395
|
-
out.dgram_sent = stats.dgram_sent;
|
|
1396
|
-
out.paths_count = stats.paths_count;
|
|
1397
|
-
out.reset_stream_count_local = stats.reset_stream_count_local;
|
|
1398
|
-
out.stopped_stream_count_local = stats.stopped_stream_count_local;
|
|
1399
|
-
out.reset_stream_count_remote = stats.reset_stream_count_remote;
|
|
1400
|
-
out.stopped_stream_count_remote = stats.stopped_stream_count_remote;
|
|
1401
|
-
out.data_blocked_sent_count = stats.data_blocked_sent_count;
|
|
1402
|
-
out.stream_data_blocked_sent_count = stats.stream_data_blocked_sent_count;
|
|
1403
|
-
out.data_blocked_recv_count = stats.data_blocked_recv_count;
|
|
1404
|
-
out.stream_data_blocked_recv_count = stats.stream_data_blocked_recv_count;
|
|
1405
|
-
out.streams_blocked_bidi_recv_count = stats.streams_blocked_bidi_recv_count;
|
|
1406
|
-
out.streams_blocked_uni_recv_count = stats.streams_blocked_uni_recv_count;
|
|
1407
|
-
out.path_challenge_rx_count = stats.path_challenge_rx_count;
|
|
1408
|
-
out.bytes_in_flight_duration_msec =
|
|
1409
|
-
stats.bytes_in_flight_duration.as_millis() as u64;
|
|
1410
|
-
out.tx_buffered_inconsistent =
|
|
1411
|
-
stats.tx_buffered_state != TxBufferTrackingState::Ok;
|
|
1412
|
-
}
|
|
1413
|
-
|
|
1414
|
-
#[no_mangle]
|
|
1415
|
-
pub extern "C" fn quiche_conn_peer_transport_params(
|
|
1416
|
-
conn: &Connection, out: &mut TransportParams,
|
|
1417
|
-
) -> bool {
|
|
1418
|
-
let tps = match conn.peer_transport_params() {
|
|
1419
|
-
Some(v) => v,
|
|
1420
|
-
None => return false,
|
|
1421
|
-
};
|
|
1422
|
-
|
|
1423
|
-
out.max_idle_timeout = tps.max_idle_timeout;
|
|
1424
|
-
out.max_udp_payload_size = tps.max_udp_payload_size;
|
|
1425
|
-
out.initial_max_data = tps.initial_max_data;
|
|
1426
|
-
out.initial_max_stream_data_bidi_local =
|
|
1427
|
-
tps.initial_max_stream_data_bidi_local;
|
|
1428
|
-
out.initial_max_stream_data_bidi_remote =
|
|
1429
|
-
tps.initial_max_stream_data_bidi_remote;
|
|
1430
|
-
out.initial_max_stream_data_uni = tps.initial_max_stream_data_uni;
|
|
1431
|
-
out.initial_max_streams_bidi = tps.initial_max_streams_bidi;
|
|
1432
|
-
out.initial_max_streams_uni = tps.initial_max_streams_uni;
|
|
1433
|
-
out.ack_delay_exponent = tps.ack_delay_exponent;
|
|
1434
|
-
out.max_ack_delay = tps.max_ack_delay;
|
|
1435
|
-
out.disable_active_migration = tps.disable_active_migration;
|
|
1436
|
-
out.active_conn_id_limit = tps.active_conn_id_limit;
|
|
1437
|
-
out.max_datagram_frame_size = match tps.max_datagram_frame_size {
|
|
1438
|
-
None => Error::Done.to_c(),
|
|
1439
|
-
|
|
1440
|
-
Some(v) => v as ssize_t,
|
|
1441
|
-
};
|
|
1442
|
-
|
|
1443
|
-
true
|
|
1444
|
-
}
|
|
1445
|
-
|
|
1446
|
-
#[repr(C)]
|
|
1447
|
-
pub struct PathStats {
|
|
1448
|
-
local_addr: sockaddr_storage,
|
|
1449
|
-
local_addr_len: socklen_t,
|
|
1450
|
-
peer_addr: sockaddr_storage,
|
|
1451
|
-
peer_addr_len: socklen_t,
|
|
1452
|
-
validation_state: ssize_t,
|
|
1453
|
-
active: bool,
|
|
1454
|
-
recv: usize,
|
|
1455
|
-
sent: usize,
|
|
1456
|
-
lost: usize,
|
|
1457
|
-
retrans: usize,
|
|
1458
|
-
total_pto_count: usize,
|
|
1459
|
-
dgram_recv: usize,
|
|
1460
|
-
dgram_sent: usize,
|
|
1461
|
-
rtt: u64,
|
|
1462
|
-
min_rtt: u64,
|
|
1463
|
-
max_rtt: u64,
|
|
1464
|
-
rttvar: u64,
|
|
1465
|
-
cwnd: usize,
|
|
1466
|
-
sent_bytes: u64,
|
|
1467
|
-
recv_bytes: u64,
|
|
1468
|
-
lost_bytes: u64,
|
|
1469
|
-
stream_retrans_bytes: u64,
|
|
1470
|
-
pmtu: usize,
|
|
1471
|
-
delivery_rate: u64,
|
|
1472
|
-
max_bandwidth: u64,
|
|
1473
|
-
startup_exit_cwnd: u64,
|
|
1474
|
-
}
|
|
1475
|
-
|
|
1476
|
-
#[no_mangle]
|
|
1477
|
-
pub extern "C" fn quiche_conn_path_stats(
|
|
1478
|
-
conn: &Connection, idx: usize, out: &mut PathStats,
|
|
1479
|
-
) -> c_int {
|
|
1480
|
-
let stats = match conn.path_stats().nth(idx) {
|
|
1481
|
-
Some(p) => p,
|
|
1482
|
-
None => return Error::Done.to_c() as c_int,
|
|
1483
|
-
};
|
|
1484
|
-
|
|
1485
|
-
out.local_addr_len = std_addr_to_c(&stats.local_addr, &mut out.local_addr);
|
|
1486
|
-
out.peer_addr_len = std_addr_to_c(&stats.peer_addr, &mut out.peer_addr);
|
|
1487
|
-
out.validation_state = stats.validation_state.to_c();
|
|
1488
|
-
out.active = stats.active;
|
|
1489
|
-
out.recv = stats.recv;
|
|
1490
|
-
out.sent = stats.sent;
|
|
1491
|
-
out.lost = stats.lost;
|
|
1492
|
-
out.retrans = stats.retrans;
|
|
1493
|
-
out.total_pto_count = stats.total_pto_count;
|
|
1494
|
-
out.dgram_recv = stats.dgram_recv;
|
|
1495
|
-
out.dgram_sent = stats.dgram_sent;
|
|
1496
|
-
out.rtt = stats.rtt.as_nanos() as u64;
|
|
1497
|
-
out.min_rtt = stats.min_rtt.unwrap_or_default().as_nanos() as u64;
|
|
1498
|
-
out.rttvar = stats.rttvar.as_nanos() as u64;
|
|
1499
|
-
out.cwnd = stats.cwnd;
|
|
1500
|
-
out.sent_bytes = stats.sent_bytes;
|
|
1501
|
-
out.recv_bytes = stats.recv_bytes;
|
|
1502
|
-
out.lost_bytes = stats.lost_bytes;
|
|
1503
|
-
out.stream_retrans_bytes = stats.stream_retrans_bytes;
|
|
1504
|
-
out.pmtu = stats.pmtu;
|
|
1505
|
-
out.delivery_rate = stats.delivery_rate;
|
|
1506
|
-
out.max_bandwidth = stats.max_bandwidth.unwrap_or(0);
|
|
1507
|
-
out.startup_exit_cwnd =
|
|
1508
|
-
stats.startup_exit.map(|s| s.cwnd as u64).unwrap_or(0);
|
|
1509
|
-
|
|
1510
|
-
0
|
|
1511
|
-
}
|
|
1512
|
-
|
|
1513
|
-
#[no_mangle]
|
|
1514
|
-
pub extern "C" fn quiche_conn_is_server(conn: &Connection) -> bool {
|
|
1515
|
-
conn.is_server()
|
|
1516
|
-
}
|
|
1517
|
-
|
|
1518
|
-
#[no_mangle]
|
|
1519
|
-
pub extern "C" fn quiche_conn_dgram_max_writable_len(
|
|
1520
|
-
conn: &Connection,
|
|
1521
|
-
) -> ssize_t {
|
|
1522
|
-
match conn.dgram_max_writable_len() {
|
|
1523
|
-
None => Error::Done.to_c(),
|
|
1524
|
-
|
|
1525
|
-
Some(v) => v as ssize_t,
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
|
|
1529
|
-
#[no_mangle]
|
|
1530
|
-
pub extern "C" fn quiche_conn_dgram_recv_front_len(conn: &Connection) -> ssize_t {
|
|
1531
|
-
match conn.dgram_recv_front_len() {
|
|
1532
|
-
None => Error::Done.to_c(),
|
|
1533
|
-
|
|
1534
|
-
Some(v) => v as ssize_t,
|
|
1535
|
-
}
|
|
1536
|
-
}
|
|
1537
|
-
|
|
1538
|
-
#[no_mangle]
|
|
1539
|
-
pub extern "C" fn quiche_conn_dgram_recv_queue_len(conn: &Connection) -> ssize_t {
|
|
1540
|
-
conn.dgram_recv_queue_len() as ssize_t
|
|
1541
|
-
}
|
|
1542
|
-
|
|
1543
|
-
#[no_mangle]
|
|
1544
|
-
pub extern "C" fn quiche_conn_dgram_recv_queue_byte_size(
|
|
1545
|
-
conn: &Connection,
|
|
1546
|
-
) -> ssize_t {
|
|
1547
|
-
conn.dgram_recv_queue_byte_size() as ssize_t
|
|
1548
|
-
}
|
|
1549
|
-
|
|
1550
|
-
#[no_mangle]
|
|
1551
|
-
pub extern "C" fn quiche_conn_dgram_send_queue_len(conn: &Connection) -> ssize_t {
|
|
1552
|
-
conn.dgram_send_queue_len() as ssize_t
|
|
1553
|
-
}
|
|
1554
|
-
|
|
1555
|
-
#[no_mangle]
|
|
1556
|
-
pub extern "C" fn quiche_conn_dgram_send_queue_byte_size(
|
|
1557
|
-
conn: &Connection,
|
|
1558
|
-
) -> ssize_t {
|
|
1559
|
-
conn.dgram_send_queue_byte_size() as ssize_t
|
|
1560
|
-
}
|
|
1561
|
-
|
|
1562
|
-
#[no_mangle]
|
|
1563
|
-
pub extern "C" fn quiche_conn_dgram_send(
|
|
1564
|
-
conn: &mut Connection, buf: *const u8, buf_len: size_t,
|
|
1565
|
-
) -> ssize_t {
|
|
1566
|
-
if buf_len > <ssize_t>::MAX as usize {
|
|
1567
|
-
panic!("The provided buffer is too large");
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
|
-
let buf = unsafe { slice::from_raw_parts(buf, buf_len) };
|
|
1571
|
-
|
|
1572
|
-
match conn.dgram_send(buf) {
|
|
1573
|
-
Ok(_) => buf_len as ssize_t,
|
|
1574
|
-
|
|
1575
|
-
Err(e) => e.to_c(),
|
|
1576
|
-
}
|
|
1577
|
-
}
|
|
1578
|
-
|
|
1579
|
-
#[no_mangle]
|
|
1580
|
-
pub extern "C" fn quiche_conn_dgram_recv(
|
|
1581
|
-
conn: &mut Connection, out: *mut u8, out_len: size_t,
|
|
1582
|
-
) -> ssize_t {
|
|
1583
|
-
if out_len > <ssize_t>::MAX as usize {
|
|
1584
|
-
panic!("The provided buffer is too large");
|
|
1585
|
-
}
|
|
1586
|
-
|
|
1587
|
-
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
1588
|
-
|
|
1589
|
-
let out_len = match conn.dgram_recv(out) {
|
|
1590
|
-
Ok(v) => v,
|
|
1591
|
-
|
|
1592
|
-
Err(e) => return e.to_c(),
|
|
1593
|
-
};
|
|
1594
|
-
|
|
1595
|
-
out_len as ssize_t
|
|
1596
|
-
}
|
|
1597
|
-
|
|
1598
|
-
#[no_mangle]
|
|
1599
|
-
pub extern "C" fn quiche_conn_dgram_purge_outgoing(
|
|
1600
|
-
conn: &mut Connection, f: extern "C" fn(*const u8, size_t) -> bool,
|
|
1601
|
-
) {
|
|
1602
|
-
conn.dgram_purge_outgoing(|d: &[u8]| -> bool {
|
|
1603
|
-
let ptr: *const u8 = d.as_ptr();
|
|
1604
|
-
let len: size_t = d.len();
|
|
1605
|
-
|
|
1606
|
-
f(ptr, len)
|
|
1607
|
-
});
|
|
1608
|
-
}
|
|
1609
|
-
|
|
1610
|
-
#[no_mangle]
|
|
1611
|
-
pub extern "C" fn quiche_conn_is_dgram_send_queue_full(
|
|
1612
|
-
conn: &Connection,
|
|
1613
|
-
) -> bool {
|
|
1614
|
-
conn.is_dgram_send_queue_full()
|
|
1615
|
-
}
|
|
1616
|
-
|
|
1617
|
-
#[no_mangle]
|
|
1618
|
-
pub extern "C" fn quiche_conn_is_dgram_recv_queue_full(
|
|
1619
|
-
conn: &Connection,
|
|
1620
|
-
) -> bool {
|
|
1621
|
-
conn.is_dgram_recv_queue_full()
|
|
1622
|
-
}
|
|
1623
|
-
|
|
1624
|
-
#[no_mangle]
|
|
1625
|
-
pub extern "C" fn quiche_conn_send_ack_eliciting(
|
|
1626
|
-
conn: &mut Connection,
|
|
1627
|
-
) -> ssize_t {
|
|
1628
|
-
match conn.send_ack_eliciting() {
|
|
1629
|
-
Ok(()) => 0,
|
|
1630
|
-
Err(e) => e.to_c(),
|
|
1631
|
-
}
|
|
1632
|
-
}
|
|
1633
|
-
|
|
1634
|
-
#[no_mangle]
|
|
1635
|
-
pub extern "C" fn quiche_conn_send_ack_eliciting_on_path(
|
|
1636
|
-
conn: &mut Connection, local: &sockaddr, local_len: socklen_t,
|
|
1637
|
-
peer: &sockaddr, peer_len: socklen_t,
|
|
1638
|
-
) -> ssize_t {
|
|
1639
|
-
let local = std_addr_from_c(local, local_len);
|
|
1640
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
1641
|
-
match conn.send_ack_eliciting_on_path(local, peer) {
|
|
1642
|
-
Ok(()) => 0,
|
|
1643
|
-
Err(e) => e.to_c(),
|
|
1644
|
-
}
|
|
1645
|
-
}
|
|
1646
|
-
|
|
1647
|
-
#[no_mangle]
|
|
1648
|
-
pub extern "C" fn quiche_conn_free(conn: *mut Connection) {
|
|
1649
|
-
if !conn.is_null() {
|
|
1650
|
-
drop(unsafe { Box::from_raw(conn) });
|
|
1651
|
-
}
|
|
1652
|
-
}
|
|
1653
|
-
|
|
1654
|
-
#[no_mangle]
|
|
1655
|
-
pub extern "C" fn quiche_conn_peer_streams_left_bidi(conn: &Connection) -> u64 {
|
|
1656
|
-
conn.peer_streams_left_bidi()
|
|
1657
|
-
}
|
|
1658
|
-
|
|
1659
|
-
#[no_mangle]
|
|
1660
|
-
pub extern "C" fn quiche_conn_peer_streams_left_uni(conn: &Connection) -> u64 {
|
|
1661
|
-
conn.peer_streams_left_uni()
|
|
1662
|
-
}
|
|
1663
|
-
|
|
1664
|
-
#[no_mangle]
|
|
1665
|
-
pub extern "C" fn quiche_conn_send_quantum(conn: &Connection) -> size_t {
|
|
1666
|
-
conn.send_quantum() as size_t
|
|
1667
|
-
}
|
|
1668
|
-
|
|
1669
|
-
#[no_mangle]
|
|
1670
|
-
pub extern "C" fn quiche_conn_active_scids(conn: &Connection) -> size_t {
|
|
1671
|
-
conn.active_scids() as size_t
|
|
1672
|
-
}
|
|
1673
|
-
|
|
1674
|
-
#[no_mangle]
|
|
1675
|
-
pub extern "C" fn quiche_conn_scids_left(conn: &Connection) -> size_t {
|
|
1676
|
-
conn.scids_left() as size_t
|
|
1677
|
-
}
|
|
1678
|
-
|
|
1679
|
-
#[no_mangle]
|
|
1680
|
-
pub extern "C" fn quiche_conn_new_scid(
|
|
1681
|
-
conn: &mut Connection, scid: *const u8, scid_len: size_t,
|
|
1682
|
-
reset_token: *const u8, retire_if_needed: bool, scid_seq: *mut u64,
|
|
1683
|
-
) -> c_int {
|
|
1684
|
-
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
1685
|
-
let scid = ConnectionId::from_ref(scid);
|
|
1686
|
-
|
|
1687
|
-
let reset_token = unsafe { slice::from_raw_parts(reset_token, 16) };
|
|
1688
|
-
let reset_token = match reset_token.try_into() {
|
|
1689
|
-
Ok(rt) => rt,
|
|
1690
|
-
Err(_) => unreachable!(),
|
|
1691
|
-
};
|
|
1692
|
-
let reset_token = u128::from_be_bytes(reset_token);
|
|
1693
|
-
|
|
1694
|
-
match conn.new_scid(&scid, reset_token, retire_if_needed) {
|
|
1695
|
-
Ok(c) => {
|
|
1696
|
-
unsafe { *scid_seq = c }
|
|
1697
|
-
0
|
|
1698
|
-
},
|
|
1699
|
-
Err(e) => e.to_c() as c_int,
|
|
1700
|
-
}
|
|
1701
|
-
}
|
|
1702
|
-
|
|
1703
|
-
#[no_mangle]
|
|
1704
|
-
pub extern "C" fn quiche_conn_retire_dcid(
|
|
1705
|
-
conn: &mut Connection, dcid_seq: u64,
|
|
1706
|
-
) -> c_int {
|
|
1707
|
-
match conn.retire_dcid(dcid_seq) {
|
|
1708
|
-
Ok(_) => 0,
|
|
1709
|
-
Err(e) => e.to_c() as c_int,
|
|
1710
|
-
}
|
|
1711
|
-
}
|
|
1712
|
-
|
|
1713
|
-
#[no_mangle]
|
|
1714
|
-
pub extern "C" fn quiche_conn_available_dcids(conn: &Connection) -> size_t {
|
|
1715
|
-
conn.available_dcids() as size_t
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
|
-
#[no_mangle]
|
|
1719
|
-
pub extern "C" fn quiche_conn_retired_scids(conn: &Connection) -> size_t {
|
|
1720
|
-
conn.retired_scids() as size_t
|
|
1721
|
-
}
|
|
1722
|
-
|
|
1723
|
-
#[no_mangle]
|
|
1724
|
-
pub extern "C" fn quiche_conn_retired_scid_next(
|
|
1725
|
-
conn: &mut Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1726
|
-
) -> bool {
|
|
1727
|
-
match conn.retired_scid_next() {
|
|
1728
|
-
None => false,
|
|
1729
|
-
|
|
1730
|
-
Some(conn_id) => {
|
|
1731
|
-
let id = conn_id.as_ref();
|
|
1732
|
-
*out = id.as_ptr();
|
|
1733
|
-
*out_len = id.len();
|
|
1734
|
-
true
|
|
1735
|
-
},
|
|
1736
|
-
}
|
|
1737
|
-
}
|
|
1738
|
-
|
|
1739
|
-
#[no_mangle]
|
|
1740
|
-
pub extern "C" fn quiche_conn_send_quantum_on_path(
|
|
1741
|
-
conn: &Connection, local: &sockaddr, local_len: socklen_t, peer: &sockaddr,
|
|
1742
|
-
peer_len: socklen_t,
|
|
1743
|
-
) -> size_t {
|
|
1744
|
-
let local = std_addr_from_c(local, local_len);
|
|
1745
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
1746
|
-
|
|
1747
|
-
conn.send_quantum_on_path(local, peer) as size_t
|
|
1748
|
-
}
|
|
1749
|
-
|
|
1750
|
-
#[no_mangle]
|
|
1751
|
-
pub extern "C" fn quiche_conn_paths_iter(
|
|
1752
|
-
conn: &Connection, from: &sockaddr, from_len: socklen_t,
|
|
1753
|
-
) -> *mut SocketAddrIter {
|
|
1754
|
-
let addr = std_addr_from_c(from, from_len);
|
|
1755
|
-
|
|
1756
|
-
Box::into_raw(Box::new(conn.paths_iter(addr)))
|
|
1757
|
-
}
|
|
1758
|
-
|
|
1759
|
-
#[no_mangle]
|
|
1760
|
-
pub extern "C" fn quiche_socket_addr_iter_next(
|
|
1761
|
-
iter: &mut SocketAddrIter, peer: &mut sockaddr_storage,
|
|
1762
|
-
peer_len: *mut socklen_t,
|
|
1763
|
-
) -> bool {
|
|
1764
|
-
if let Some(v) = iter.next() {
|
|
1765
|
-
unsafe { *peer_len = std_addr_to_c(&v, peer) }
|
|
1766
|
-
return true;
|
|
1767
|
-
}
|
|
1768
|
-
|
|
1769
|
-
false
|
|
1770
|
-
}
|
|
1771
|
-
|
|
1772
|
-
#[no_mangle]
|
|
1773
|
-
pub extern "C" fn quiche_socket_addr_iter_free(iter: *mut SocketAddrIter) {
|
|
1774
|
-
if !iter.is_null() {
|
|
1775
|
-
drop(unsafe { Box::from_raw(iter) });
|
|
1776
|
-
}
|
|
1777
|
-
}
|
|
1778
|
-
|
|
1779
|
-
#[no_mangle]
|
|
1780
|
-
pub extern "C" fn quiche_conn_is_path_validated(
|
|
1781
|
-
conn: &Connection, from: &sockaddr, from_len: socklen_t, to: &sockaddr,
|
|
1782
|
-
to_len: socklen_t,
|
|
1783
|
-
) -> c_int {
|
|
1784
|
-
let from = std_addr_from_c(from, from_len);
|
|
1785
|
-
let to = std_addr_from_c(to, to_len);
|
|
1786
|
-
match conn.is_path_validated(from, to) {
|
|
1787
|
-
Ok(v) => v as c_int,
|
|
1788
|
-
Err(e) => e.to_c() as c_int,
|
|
1789
|
-
}
|
|
1790
|
-
}
|
|
1791
|
-
|
|
1792
|
-
#[no_mangle]
|
|
1793
|
-
pub extern "C" fn quiche_conn_probe_path(
|
|
1794
|
-
conn: &mut Connection, local: &sockaddr, local_len: socklen_t,
|
|
1795
|
-
peer: &sockaddr, peer_len: socklen_t, seq: *mut u64,
|
|
1796
|
-
) -> c_int {
|
|
1797
|
-
let local = std_addr_from_c(local, local_len);
|
|
1798
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
1799
|
-
match conn.probe_path(local, peer) {
|
|
1800
|
-
Ok(v) => {
|
|
1801
|
-
unsafe { *seq = v }
|
|
1802
|
-
0
|
|
1803
|
-
},
|
|
1804
|
-
Err(e) => e.to_c() as c_int,
|
|
1805
|
-
}
|
|
1806
|
-
}
|
|
1807
|
-
|
|
1808
|
-
#[no_mangle]
|
|
1809
|
-
pub extern "C" fn quiche_conn_migrate_source(
|
|
1810
|
-
conn: &mut Connection, local: &sockaddr, local_len: socklen_t, seq: *mut u64,
|
|
1811
|
-
) -> c_int {
|
|
1812
|
-
let local = std_addr_from_c(local, local_len);
|
|
1813
|
-
match conn.migrate_source(local) {
|
|
1814
|
-
Ok(v) => {
|
|
1815
|
-
unsafe { *seq = v }
|
|
1816
|
-
0
|
|
1817
|
-
},
|
|
1818
|
-
Err(e) => e.to_c() as c_int,
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
|
|
1822
|
-
#[no_mangle]
|
|
1823
|
-
pub extern "C" fn quiche_conn_migrate(
|
|
1824
|
-
conn: &mut Connection, local: &sockaddr, local_len: socklen_t,
|
|
1825
|
-
peer: &sockaddr, peer_len: socklen_t, seq: *mut u64,
|
|
1826
|
-
) -> c_int {
|
|
1827
|
-
let local = std_addr_from_c(local, local_len);
|
|
1828
|
-
let peer = std_addr_from_c(peer, peer_len);
|
|
1829
|
-
match conn.migrate(local, peer) {
|
|
1830
|
-
Ok(v) => {
|
|
1831
|
-
unsafe { *seq = v }
|
|
1832
|
-
0
|
|
1833
|
-
},
|
|
1834
|
-
Err(e) => e.to_c() as c_int,
|
|
1835
|
-
}
|
|
1836
|
-
}
|
|
1837
|
-
|
|
1838
|
-
#[no_mangle]
|
|
1839
|
-
pub extern "C" fn quiche_conn_path_event_next(
|
|
1840
|
-
conn: &mut Connection,
|
|
1841
|
-
) -> *mut PathEvent {
|
|
1842
|
-
match conn.path_event_next() {
|
|
1843
|
-
Some(v) => Box::into_raw(Box::new(v)),
|
|
1844
|
-
None => ptr::null_mut(),
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1847
|
-
|
|
1848
|
-
#[no_mangle]
|
|
1849
|
-
pub extern "C" fn quiche_path_event_type(ev: &PathEvent) -> u32 {
|
|
1850
|
-
match ev {
|
|
1851
|
-
PathEvent::New { .. } => 0,
|
|
1852
|
-
|
|
1853
|
-
PathEvent::Validated { .. } => 1,
|
|
1854
|
-
|
|
1855
|
-
PathEvent::FailedValidation { .. } => 2,
|
|
1856
|
-
|
|
1857
|
-
PathEvent::Closed { .. } => 3,
|
|
1858
|
-
|
|
1859
|
-
PathEvent::ReusedSourceConnectionId { .. } => 4,
|
|
1860
|
-
|
|
1861
|
-
PathEvent::PeerMigrated { .. } => 5,
|
|
1862
|
-
}
|
|
1863
|
-
}
|
|
1864
|
-
|
|
1865
|
-
#[no_mangle]
|
|
1866
|
-
pub extern "C" fn quiche_path_event_new(
|
|
1867
|
-
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1868
|
-
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1869
|
-
peer_addr_len: &mut socklen_t,
|
|
1870
|
-
) {
|
|
1871
|
-
match ev {
|
|
1872
|
-
PathEvent::New(local, peer) => {
|
|
1873
|
-
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1874
|
-
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1875
|
-
},
|
|
1876
|
-
|
|
1877
|
-
_ => unreachable!(),
|
|
1878
|
-
}
|
|
1879
|
-
}
|
|
1880
|
-
|
|
1881
|
-
#[no_mangle]
|
|
1882
|
-
pub extern "C" fn quiche_path_event_validated(
|
|
1883
|
-
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1884
|
-
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1885
|
-
peer_addr_len: &mut socklen_t,
|
|
1886
|
-
) {
|
|
1887
|
-
match ev {
|
|
1888
|
-
PathEvent::Validated(local, peer) => {
|
|
1889
|
-
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1890
|
-
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1891
|
-
},
|
|
1892
|
-
|
|
1893
|
-
_ => unreachable!(),
|
|
1894
|
-
}
|
|
1895
|
-
}
|
|
1896
|
-
|
|
1897
|
-
#[no_mangle]
|
|
1898
|
-
pub extern "C" fn quiche_path_event_failed_validation(
|
|
1899
|
-
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1900
|
-
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1901
|
-
peer_addr_len: &mut socklen_t,
|
|
1902
|
-
) {
|
|
1903
|
-
match ev {
|
|
1904
|
-
PathEvent::FailedValidation(local, peer) => {
|
|
1905
|
-
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1906
|
-
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1907
|
-
},
|
|
1908
|
-
|
|
1909
|
-
_ => unreachable!(),
|
|
1910
|
-
}
|
|
1911
|
-
}
|
|
1912
|
-
|
|
1913
|
-
#[no_mangle]
|
|
1914
|
-
pub extern "C" fn quiche_path_event_closed(
|
|
1915
|
-
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1916
|
-
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1917
|
-
peer_addr_len: &mut socklen_t,
|
|
1918
|
-
) {
|
|
1919
|
-
match ev {
|
|
1920
|
-
PathEvent::Closed(local, peer) => {
|
|
1921
|
-
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1922
|
-
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1923
|
-
},
|
|
1924
|
-
|
|
1925
|
-
_ => unreachable!(),
|
|
1926
|
-
}
|
|
1927
|
-
}
|
|
1928
|
-
|
|
1929
|
-
#[no_mangle]
|
|
1930
|
-
pub extern "C" fn quiche_path_event_reused_source_connection_id(
|
|
1931
|
-
ev: &PathEvent, cid_sequence_number: &mut u64,
|
|
1932
|
-
old_local_addr: &mut sockaddr_storage, old_local_addr_len: &mut socklen_t,
|
|
1933
|
-
old_peer_addr: &mut sockaddr_storage, old_peer_addr_len: &mut socklen_t,
|
|
1934
|
-
local_addr: &mut sockaddr_storage, local_addr_len: &mut socklen_t,
|
|
1935
|
-
peer_addr: &mut sockaddr_storage, peer_addr_len: &mut socklen_t,
|
|
1936
|
-
) {
|
|
1937
|
-
match ev {
|
|
1938
|
-
PathEvent::ReusedSourceConnectionId(id, old, new) => {
|
|
1939
|
-
*cid_sequence_number = *id;
|
|
1940
|
-
*old_local_addr_len = std_addr_to_c(&old.0, old_local_addr);
|
|
1941
|
-
*old_peer_addr_len = std_addr_to_c(&old.1, old_peer_addr);
|
|
1942
|
-
|
|
1943
|
-
*local_addr_len = std_addr_to_c(&new.0, local_addr);
|
|
1944
|
-
*peer_addr_len = std_addr_to_c(&new.1, peer_addr)
|
|
1945
|
-
},
|
|
1946
|
-
|
|
1947
|
-
_ => unreachable!(),
|
|
1948
|
-
}
|
|
1949
|
-
}
|
|
1950
|
-
|
|
1951
|
-
#[no_mangle]
|
|
1952
|
-
pub extern "C" fn quiche_path_event_peer_migrated(
|
|
1953
|
-
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1954
|
-
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1955
|
-
peer_addr_len: &mut socklen_t,
|
|
1956
|
-
) {
|
|
1957
|
-
match ev {
|
|
1958
|
-
PathEvent::PeerMigrated(local, peer) => {
|
|
1959
|
-
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1960
|
-
*peer_addr_len = std_addr_to_c(peer, peer_addr);
|
|
1961
|
-
},
|
|
1962
|
-
|
|
1963
|
-
_ => unreachable!(),
|
|
1964
|
-
}
|
|
1965
|
-
}
|
|
1966
|
-
|
|
1967
|
-
#[no_mangle]
|
|
1968
|
-
pub extern "C" fn quiche_path_event_free(ev: *mut PathEvent) {
|
|
1969
|
-
if !ev.is_null() {
|
|
1970
|
-
drop(unsafe { Box::from_raw(ev) });
|
|
1971
|
-
}
|
|
1972
|
-
}
|
|
1973
|
-
|
|
1974
|
-
#[no_mangle]
|
|
1975
|
-
pub extern "C" fn quiche_put_varint(
|
|
1976
|
-
buf: *mut u8, buf_len: size_t, val: u64,
|
|
1977
|
-
) -> c_int {
|
|
1978
|
-
let buf = unsafe { slice::from_raw_parts_mut(buf, buf_len) };
|
|
1979
|
-
|
|
1980
|
-
let mut b = octets::OctetsMut::with_slice(buf);
|
|
1981
|
-
match b.put_varint(val) {
|
|
1982
|
-
Ok(_) => 0,
|
|
1983
|
-
|
|
1984
|
-
Err(e) => {
|
|
1985
|
-
let err: Error = e.into();
|
|
1986
|
-
err.to_c() as c_int
|
|
1987
|
-
},
|
|
1988
|
-
}
|
|
1989
|
-
}
|
|
1990
|
-
|
|
1991
|
-
#[no_mangle]
|
|
1992
|
-
pub extern "C" fn quiche_get_varint(
|
|
1993
|
-
buf: *const u8, buf_len: size_t, val: *mut u64,
|
|
1994
|
-
) -> ssize_t {
|
|
1995
|
-
let buf = unsafe { slice::from_raw_parts(buf, buf_len) };
|
|
1996
|
-
|
|
1997
|
-
let mut b = octets::Octets::with_slice(buf);
|
|
1998
|
-
match b.get_varint() {
|
|
1999
|
-
Ok(v) => unsafe { *val = v },
|
|
2000
|
-
|
|
2001
|
-
Err(e) => {
|
|
2002
|
-
let err: Error = e.into();
|
|
2003
|
-
return err.to_c();
|
|
2004
|
-
},
|
|
2005
|
-
};
|
|
2006
|
-
|
|
2007
|
-
b.off() as ssize_t
|
|
2008
|
-
}
|
|
2009
|
-
|
|
2010
|
-
fn optional_std_addr_from_c(
|
|
2011
|
-
addr: *const sockaddr, addr_len: socklen_t,
|
|
2012
|
-
) -> Option<SocketAddr> {
|
|
2013
|
-
if addr.is_null() || addr_len == 0 {
|
|
2014
|
-
return None;
|
|
2015
|
-
}
|
|
2016
|
-
|
|
2017
|
-
Some(std_addr_from_c(unsafe { &*addr }, addr_len))
|
|
2018
|
-
}
|
|
2019
|
-
|
|
2020
|
-
fn std_addr_from_c(addr: &sockaddr, addr_len: socklen_t) -> SocketAddr {
|
|
2021
|
-
match addr.sa_family as _ {
|
|
2022
|
-
AF_INET => {
|
|
2023
|
-
assert!(addr_len as usize == size_of::<sockaddr_in>());
|
|
2024
|
-
|
|
2025
|
-
let in4 = unsafe { *(addr as *const _ as *const sockaddr_in) };
|
|
2026
|
-
|
|
2027
|
-
#[cfg(not(windows))]
|
|
2028
|
-
let ip_addr = Ipv4Addr::from(u32::from_be(in4.sin_addr.s_addr));
|
|
2029
|
-
#[cfg(windows)]
|
|
2030
|
-
let ip_addr = {
|
|
2031
|
-
let ip_bytes = unsafe { in4.sin_addr.S_un.S_un_b };
|
|
2032
|
-
|
|
2033
|
-
Ipv4Addr::from([
|
|
2034
|
-
ip_bytes.s_b1,
|
|
2035
|
-
ip_bytes.s_b2,
|
|
2036
|
-
ip_bytes.s_b3,
|
|
2037
|
-
ip_bytes.s_b4,
|
|
2038
|
-
])
|
|
2039
|
-
};
|
|
2040
|
-
|
|
2041
|
-
let port = u16::from_be(in4.sin_port);
|
|
2042
|
-
|
|
2043
|
-
let out = SocketAddrV4::new(ip_addr, port);
|
|
2044
|
-
|
|
2045
|
-
out.into()
|
|
2046
|
-
},
|
|
2047
|
-
|
|
2048
|
-
AF_INET6 => {
|
|
2049
|
-
assert!(addr_len as usize == size_of::<sockaddr_in6>());
|
|
2050
|
-
|
|
2051
|
-
let in6 = unsafe { *(addr as *const _ as *const sockaddr_in6) };
|
|
2052
|
-
|
|
2053
|
-
let ip_addr = Ipv6Addr::from(
|
|
2054
|
-
#[cfg(not(windows))]
|
|
2055
|
-
in6.sin6_addr.s6_addr,
|
|
2056
|
-
#[cfg(windows)]
|
|
2057
|
-
unsafe {
|
|
2058
|
-
in6.sin6_addr.u.Byte
|
|
2059
|
-
},
|
|
2060
|
-
);
|
|
2061
|
-
|
|
2062
|
-
let port = u16::from_be(in6.sin6_port);
|
|
2063
|
-
|
|
2064
|
-
#[cfg(not(windows))]
|
|
2065
|
-
let scope_id = in6.sin6_scope_id;
|
|
2066
|
-
#[cfg(windows)]
|
|
2067
|
-
let scope_id = unsafe { in6.Anonymous.sin6_scope_id };
|
|
2068
|
-
|
|
2069
|
-
let out =
|
|
2070
|
-
SocketAddrV6::new(ip_addr, port, in6.sin6_flowinfo, scope_id);
|
|
2071
|
-
|
|
2072
|
-
out.into()
|
|
2073
|
-
},
|
|
2074
|
-
|
|
2075
|
-
_ => unimplemented!("unsupported address type"),
|
|
2076
|
-
}
|
|
2077
|
-
}
|
|
2078
|
-
|
|
2079
|
-
fn std_addr_to_c(addr: &SocketAddr, out: &mut sockaddr_storage) -> socklen_t {
|
|
2080
|
-
let sin_port = addr.port().to_be();
|
|
2081
|
-
|
|
2082
|
-
match addr {
|
|
2083
|
-
SocketAddr::V4(addr) => unsafe {
|
|
2084
|
-
let sa_len = size_of::<sockaddr_in>();
|
|
2085
|
-
let out_in = out as *mut _ as *mut sockaddr_in;
|
|
2086
|
-
|
|
2087
|
-
let s_addr = u32::from_ne_bytes(addr.ip().octets());
|
|
2088
|
-
|
|
2089
|
-
#[cfg(not(windows))]
|
|
2090
|
-
let sin_addr = in_addr { s_addr };
|
|
2091
|
-
#[cfg(windows)]
|
|
2092
|
-
let sin_addr = in_addr {
|
|
2093
|
-
S_un: IN_ADDR_0 { S_addr: s_addr },
|
|
2094
|
-
};
|
|
2095
|
-
|
|
2096
|
-
*out_in = sockaddr_in {
|
|
2097
|
-
sin_family: AF_INET as sa_family_t,
|
|
2098
|
-
|
|
2099
|
-
sin_addr,
|
|
2100
|
-
|
|
2101
|
-
#[cfg(any(
|
|
2102
|
-
target_os = "macos",
|
|
2103
|
-
target_os = "ios",
|
|
2104
|
-
target_os = "watchos",
|
|
2105
|
-
target_os = "freebsd",
|
|
2106
|
-
target_os = "dragonfly",
|
|
2107
|
-
target_os = "openbsd",
|
|
2108
|
-
target_os = "netbsd"
|
|
2109
|
-
))]
|
|
2110
|
-
sin_len: sa_len as u8,
|
|
2111
|
-
|
|
2112
|
-
sin_port,
|
|
2113
|
-
|
|
2114
|
-
sin_zero: std::mem::zeroed(),
|
|
2115
|
-
};
|
|
2116
|
-
|
|
2117
|
-
sa_len as socklen_t
|
|
2118
|
-
},
|
|
2119
|
-
|
|
2120
|
-
SocketAddr::V6(addr) => unsafe {
|
|
2121
|
-
let sa_len = size_of::<sockaddr_in6>();
|
|
2122
|
-
let out_in6 = out as *mut _ as *mut sockaddr_in6;
|
|
2123
|
-
|
|
2124
|
-
#[cfg(not(windows))]
|
|
2125
|
-
let sin6_addr = in6_addr {
|
|
2126
|
-
s6_addr: addr.ip().octets(),
|
|
2127
|
-
};
|
|
2128
|
-
#[cfg(windows)]
|
|
2129
|
-
let sin6_addr = in6_addr {
|
|
2130
|
-
u: IN6_ADDR_0 {
|
|
2131
|
-
Byte: addr.ip().octets(),
|
|
2132
|
-
},
|
|
2133
|
-
};
|
|
2134
|
-
|
|
2135
|
-
*out_in6 = sockaddr_in6 {
|
|
2136
|
-
sin6_family: AF_INET6 as sa_family_t,
|
|
2137
|
-
|
|
2138
|
-
sin6_addr,
|
|
2139
|
-
|
|
2140
|
-
#[cfg(any(
|
|
2141
|
-
target_os = "macos",
|
|
2142
|
-
target_os = "ios",
|
|
2143
|
-
target_os = "watchos",
|
|
2144
|
-
target_os = "freebsd",
|
|
2145
|
-
target_os = "dragonfly",
|
|
2146
|
-
target_os = "openbsd",
|
|
2147
|
-
target_os = "netbsd"
|
|
2148
|
-
))]
|
|
2149
|
-
sin6_len: sa_len as u8,
|
|
2150
|
-
|
|
2151
|
-
sin6_port: sin_port,
|
|
2152
|
-
|
|
2153
|
-
sin6_flowinfo: addr.flowinfo(),
|
|
2154
|
-
|
|
2155
|
-
#[cfg(not(windows))]
|
|
2156
|
-
sin6_scope_id: addr.scope_id(),
|
|
2157
|
-
#[cfg(windows)]
|
|
2158
|
-
Anonymous: SOCKADDR_IN6_0 {
|
|
2159
|
-
sin6_scope_id: addr.scope_id(),
|
|
2160
|
-
},
|
|
2161
|
-
};
|
|
2162
|
-
|
|
2163
|
-
sa_len as socklen_t
|
|
2164
|
-
},
|
|
2165
|
-
}
|
|
2166
|
-
}
|
|
2167
|
-
|
|
2168
|
-
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "windows")))]
|
|
2169
|
-
fn std_time_to_c(time: &Instant, out: &mut timespec) {
|
|
2170
|
-
const INSTANT_ZERO: Instant =
|
|
2171
|
-
unsafe { std::mem::transmute(std::time::UNIX_EPOCH) };
|
|
2172
|
-
|
|
2173
|
-
let raw_time = time.duration_since(INSTANT_ZERO);
|
|
2174
|
-
|
|
2175
|
-
out.tv_sec = raw_time.as_secs() as libc::time_t;
|
|
2176
|
-
out.tv_nsec = raw_time.subsec_nanos() as libc::c_long;
|
|
2177
|
-
}
|
|
2178
|
-
|
|
2179
|
-
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "windows"))]
|
|
2180
|
-
fn std_time_to_c(_time: &Instant, out: &mut timespec) {
|
|
2181
|
-
// TODO: implement Instant conversion for systems that don't use timespec.
|
|
2182
|
-
out.tv_sec = 0;
|
|
2183
|
-
out.tv_nsec = 0;
|
|
2184
|
-
}
|
|
2185
|
-
|
|
2186
|
-
#[cfg(test)]
|
|
2187
|
-
mod tests {
|
|
2188
|
-
use super::*;
|
|
2189
|
-
|
|
2190
|
-
use libc::c_void;
|
|
2191
|
-
#[cfg(windows)]
|
|
2192
|
-
use windows_sys::Win32::Networking::WinSock::inet_ntop;
|
|
2193
|
-
|
|
2194
|
-
#[test]
|
|
2195
|
-
fn addr_v4() {
|
|
2196
|
-
let addr = "127.0.0.1:8080".parse().unwrap();
|
|
2197
|
-
|
|
2198
|
-
let mut out: sockaddr_storage = unsafe { std::mem::zeroed() };
|
|
2199
|
-
|
|
2200
|
-
assert_eq!(
|
|
2201
|
-
std_addr_to_c(&addr, &mut out),
|
|
2202
|
-
size_of::<sockaddr_in>() as socklen_t
|
|
2203
|
-
);
|
|
2204
|
-
|
|
2205
|
-
let s = ffi::CString::new("ddd.ddd.ddd.ddd").unwrap();
|
|
2206
|
-
|
|
2207
|
-
let s = unsafe {
|
|
2208
|
-
let in_addr = &out as *const _ as *const sockaddr_in;
|
|
2209
|
-
assert_eq!(u16::from_be((*in_addr).sin_port), addr.port());
|
|
2210
|
-
|
|
2211
|
-
let dst = s.into_raw();
|
|
2212
|
-
|
|
2213
|
-
inet_ntop(
|
|
2214
|
-
AF_INET as _,
|
|
2215
|
-
&((*in_addr).sin_addr) as *const _ as *const c_void,
|
|
2216
|
-
dst as _,
|
|
2217
|
-
16,
|
|
2218
|
-
);
|
|
2219
|
-
|
|
2220
|
-
ffi::CString::from_raw(dst).into_string().unwrap()
|
|
2221
|
-
};
|
|
2222
|
-
|
|
2223
|
-
assert_eq!(s, "127.0.0.1");
|
|
2224
|
-
|
|
2225
|
-
let addr = unsafe {
|
|
2226
|
-
std_addr_from_c(
|
|
2227
|
-
&*(&out as *const _ as *const sockaddr),
|
|
2228
|
-
size_of::<sockaddr_in>() as socklen_t,
|
|
2229
|
-
)
|
|
2230
|
-
};
|
|
2231
|
-
|
|
2232
|
-
assert_eq!(addr, "127.0.0.1:8080".parse().unwrap());
|
|
2233
|
-
}
|
|
2234
|
-
|
|
2235
|
-
#[test]
|
|
2236
|
-
fn addr_v6() {
|
|
2237
|
-
let addr = "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"
|
|
2238
|
-
.parse()
|
|
2239
|
-
.unwrap();
|
|
2240
|
-
|
|
2241
|
-
let mut out: sockaddr_storage = unsafe { std::mem::zeroed() };
|
|
2242
|
-
|
|
2243
|
-
assert_eq!(
|
|
2244
|
-
std_addr_to_c(&addr, &mut out),
|
|
2245
|
-
size_of::<sockaddr_in6>() as socklen_t
|
|
2246
|
-
);
|
|
2247
|
-
|
|
2248
|
-
let s =
|
|
2249
|
-
ffi::CString::new("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd").unwrap();
|
|
2250
|
-
|
|
2251
|
-
let s = unsafe {
|
|
2252
|
-
let in6_addr = &out as *const _ as *const sockaddr_in6;
|
|
2253
|
-
assert_eq!(u16::from_be((*in6_addr).sin6_port), addr.port());
|
|
2254
|
-
|
|
2255
|
-
let dst = s.into_raw();
|
|
2256
|
-
|
|
2257
|
-
inet_ntop(
|
|
2258
|
-
AF_INET6 as _,
|
|
2259
|
-
&((*in6_addr).sin6_addr) as *const _ as *const c_void,
|
|
2260
|
-
dst as _,
|
|
2261
|
-
45,
|
|
2262
|
-
);
|
|
2263
|
-
|
|
2264
|
-
ffi::CString::from_raw(dst).into_string().unwrap()
|
|
2265
|
-
};
|
|
2266
|
-
|
|
2267
|
-
assert_eq!(s, "2001:db8:85a3::8a2e:370:7334");
|
|
2268
|
-
|
|
2269
|
-
let addr = unsafe {
|
|
2270
|
-
std_addr_from_c(
|
|
2271
|
-
&*(&out as *const _ as *const sockaddr),
|
|
2272
|
-
size_of::<sockaddr_in6>() as socklen_t,
|
|
2273
|
-
)
|
|
2274
|
-
};
|
|
2275
|
-
|
|
2276
|
-
assert_eq!(
|
|
2277
|
-
addr,
|
|
2278
|
-
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"
|
|
2279
|
-
.parse()
|
|
2280
|
-
.unwrap()
|
|
2281
|
-
);
|
|
2282
|
-
}
|
|
2283
|
-
|
|
2284
|
-
#[cfg(not(windows))]
|
|
2285
|
-
extern "C" {
|
|
2286
|
-
fn inet_ntop(
|
|
2287
|
-
af: c_int, src: *const c_void, dst: *mut c_char, size: socklen_t,
|
|
2288
|
-
) -> *mut c_char;
|
|
2289
|
-
}
|
|
2290
|
-
}
|
|
1
|
+
// Copyright (C) 2018-2019, Cloudflare, Inc.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// Redistribution and use in source and binary forms, with or without
|
|
5
|
+
// modification, are permitted provided that the following conditions are
|
|
6
|
+
// met:
|
|
7
|
+
//
|
|
8
|
+
// * Redistributions of source code must retain the above copyright notice,
|
|
9
|
+
// this list of conditions and the following disclaimer.
|
|
10
|
+
//
|
|
11
|
+
// * Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
// notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
// documentation and/or other materials provided with the distribution.
|
|
14
|
+
//
|
|
15
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
16
|
+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
17
|
+
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
18
|
+
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
19
|
+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
20
|
+
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
21
|
+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
22
|
+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
23
|
+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
24
|
+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
25
|
+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
+
|
|
27
|
+
use std::ffi;
|
|
28
|
+
use std::ptr;
|
|
29
|
+
use std::slice;
|
|
30
|
+
use std::sync::atomic;
|
|
31
|
+
|
|
32
|
+
use std::net::Ipv4Addr;
|
|
33
|
+
use std::net::Ipv6Addr;
|
|
34
|
+
use std::net::SocketAddrV4;
|
|
35
|
+
use std::net::SocketAddrV6;
|
|
36
|
+
|
|
37
|
+
#[cfg(unix)]
|
|
38
|
+
use std::os::unix::io::FromRawFd;
|
|
39
|
+
|
|
40
|
+
use libc::c_char;
|
|
41
|
+
use libc::c_int;
|
|
42
|
+
use libc::c_void;
|
|
43
|
+
use libc::size_t;
|
|
44
|
+
use libc::sockaddr;
|
|
45
|
+
use libc::ssize_t;
|
|
46
|
+
use libc::timespec;
|
|
47
|
+
|
|
48
|
+
#[cfg(not(windows))]
|
|
49
|
+
use libc::AF_INET;
|
|
50
|
+
#[cfg(windows)]
|
|
51
|
+
use windows_sys::Win32::Networking::WinSock::AF_INET;
|
|
52
|
+
|
|
53
|
+
#[cfg(not(windows))]
|
|
54
|
+
use libc::AF_INET6;
|
|
55
|
+
#[cfg(windows)]
|
|
56
|
+
use windows_sys::Win32::Networking::WinSock::AF_INET6;
|
|
57
|
+
|
|
58
|
+
#[cfg(not(windows))]
|
|
59
|
+
use libc::in_addr;
|
|
60
|
+
#[cfg(windows)]
|
|
61
|
+
use windows_sys::Win32::Networking::WinSock::IN_ADDR as in_addr;
|
|
62
|
+
|
|
63
|
+
#[cfg(not(windows))]
|
|
64
|
+
use libc::in6_addr;
|
|
65
|
+
#[cfg(windows)]
|
|
66
|
+
use windows_sys::Win32::Networking::WinSock::IN6_ADDR as in6_addr;
|
|
67
|
+
|
|
68
|
+
#[cfg(not(windows))]
|
|
69
|
+
use libc::sa_family_t;
|
|
70
|
+
#[cfg(windows)]
|
|
71
|
+
use windows_sys::Win32::Networking::WinSock::ADDRESS_FAMILY as sa_family_t;
|
|
72
|
+
|
|
73
|
+
#[cfg(not(windows))]
|
|
74
|
+
use libc::sockaddr_in;
|
|
75
|
+
#[cfg(windows)]
|
|
76
|
+
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN as sockaddr_in;
|
|
77
|
+
|
|
78
|
+
#[cfg(not(windows))]
|
|
79
|
+
use libc::sockaddr_in6;
|
|
80
|
+
#[cfg(windows)]
|
|
81
|
+
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6 as sockaddr_in6;
|
|
82
|
+
|
|
83
|
+
#[cfg(not(windows))]
|
|
84
|
+
use libc::sockaddr_storage;
|
|
85
|
+
#[cfg(windows)]
|
|
86
|
+
use windows_sys::Win32::Networking::WinSock::SOCKADDR_STORAGE as sockaddr_storage;
|
|
87
|
+
|
|
88
|
+
#[cfg(windows)]
|
|
89
|
+
use libc::c_int as socklen_t;
|
|
90
|
+
#[cfg(not(windows))]
|
|
91
|
+
use libc::socklen_t;
|
|
92
|
+
|
|
93
|
+
#[cfg(windows)]
|
|
94
|
+
use windows_sys::Win32::Networking::WinSock::IN6_ADDR_0;
|
|
95
|
+
#[cfg(windows)]
|
|
96
|
+
use windows_sys::Win32::Networking::WinSock::IN_ADDR_0;
|
|
97
|
+
#[cfg(windows)]
|
|
98
|
+
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6_0;
|
|
99
|
+
|
|
100
|
+
use crate::*;
|
|
101
|
+
|
|
102
|
+
#[no_mangle]
|
|
103
|
+
pub extern "C" fn quiche_version() -> *const u8 {
|
|
104
|
+
static VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
|
|
105
|
+
VERSION.as_ptr()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
struct Logger {
|
|
109
|
+
cb: extern "C" fn(line: *const u8, argp: *mut c_void),
|
|
110
|
+
argp: atomic::AtomicPtr<c_void>,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
impl log::Log for Logger {
|
|
114
|
+
fn enabled(&self, _metadata: &log::Metadata) -> bool {
|
|
115
|
+
true
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
fn log(&self, record: &log::Record) {
|
|
119
|
+
let line = format!("{}: {}\0", record.target(), record.args());
|
|
120
|
+
(self.cb)(line.as_ptr(), self.argp.load(atomic::Ordering::Relaxed));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
fn flush(&self) {}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#[no_mangle]
|
|
127
|
+
pub extern "C" fn quiche_enable_debug_logging(
|
|
128
|
+
cb: extern "C" fn(line: *const u8, argp: *mut c_void), argp: *mut c_void,
|
|
129
|
+
) -> c_int {
|
|
130
|
+
let argp = atomic::AtomicPtr::new(argp);
|
|
131
|
+
let logger = Box::new(Logger { cb, argp });
|
|
132
|
+
|
|
133
|
+
if log::set_boxed_logger(logger).is_err() {
|
|
134
|
+
return -1;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
log::set_max_level(log::LevelFilter::Trace);
|
|
138
|
+
|
|
139
|
+
0
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
#[no_mangle]
|
|
143
|
+
pub extern "C" fn quiche_config_new(version: u32) -> *mut Config {
|
|
144
|
+
match Config::new(version) {
|
|
145
|
+
Ok(c) => Box::into_raw(Box::new(c)),
|
|
146
|
+
|
|
147
|
+
Err(_) => ptr::null_mut(),
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
#[no_mangle]
|
|
152
|
+
pub extern "C" fn quiche_config_load_cert_chain_from_pem_file(
|
|
153
|
+
config: &mut Config, path: *const c_char,
|
|
154
|
+
) -> c_int {
|
|
155
|
+
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
156
|
+
|
|
157
|
+
match config.load_cert_chain_from_pem_file(path) {
|
|
158
|
+
Ok(_) => 0,
|
|
159
|
+
|
|
160
|
+
Err(e) => e.to_c() as c_int,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#[no_mangle]
|
|
165
|
+
pub extern "C" fn quiche_config_load_priv_key_from_pem_file(
|
|
166
|
+
config: &mut Config, path: *const c_char,
|
|
167
|
+
) -> c_int {
|
|
168
|
+
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
169
|
+
|
|
170
|
+
match config.load_priv_key_from_pem_file(path) {
|
|
171
|
+
Ok(_) => 0,
|
|
172
|
+
|
|
173
|
+
Err(e) => e.to_c() as c_int,
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#[no_mangle]
|
|
178
|
+
pub extern "C" fn quiche_config_load_verify_locations_from_file(
|
|
179
|
+
config: &mut Config, path: *const c_char,
|
|
180
|
+
) -> c_int {
|
|
181
|
+
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
182
|
+
|
|
183
|
+
match config.load_verify_locations_from_file(path) {
|
|
184
|
+
Ok(_) => 0,
|
|
185
|
+
|
|
186
|
+
Err(e) => e.to_c() as c_int,
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#[no_mangle]
|
|
191
|
+
pub extern "C" fn quiche_config_load_verify_locations_from_directory(
|
|
192
|
+
config: &mut Config, path: *const c_char,
|
|
193
|
+
) -> c_int {
|
|
194
|
+
let path = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
195
|
+
|
|
196
|
+
match config.load_verify_locations_from_directory(path) {
|
|
197
|
+
Ok(_) => 0,
|
|
198
|
+
|
|
199
|
+
Err(e) => e.to_c() as c_int,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#[no_mangle]
|
|
204
|
+
pub extern "C" fn quiche_config_verify_peer(config: &mut Config, v: bool) {
|
|
205
|
+
config.verify_peer(v);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#[no_mangle]
|
|
209
|
+
pub extern "C" fn quiche_config_grease(config: &mut Config, v: bool) {
|
|
210
|
+
config.grease(v);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#[no_mangle]
|
|
214
|
+
pub extern "C" fn quiche_config_discover_pmtu(config: &mut Config, v: bool) {
|
|
215
|
+
config.discover_pmtu(v);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
#[no_mangle]
|
|
219
|
+
pub extern "C" fn quiche_config_set_pmtud_max_probes(
|
|
220
|
+
config: &mut Config, max_probes: u8,
|
|
221
|
+
) {
|
|
222
|
+
config.set_pmtud_max_probes(max_probes);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#[no_mangle]
|
|
226
|
+
pub extern "C" fn quiche_config_log_keys(config: &mut Config) {
|
|
227
|
+
config.log_keys();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#[no_mangle]
|
|
231
|
+
pub extern "C" fn quiche_config_enable_early_data(config: &mut Config) {
|
|
232
|
+
config.enable_early_data();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
#[no_mangle]
|
|
236
|
+
/// Corresponds to the `Config::set_application_protos_wire_format` Rust
|
|
237
|
+
/// function.
|
|
238
|
+
pub extern "C" fn quiche_config_set_application_protos(
|
|
239
|
+
config: &mut Config, protos: *const u8, protos_len: size_t,
|
|
240
|
+
) -> c_int {
|
|
241
|
+
let protos = unsafe { slice::from_raw_parts(protos, protos_len) };
|
|
242
|
+
|
|
243
|
+
match config.set_application_protos_wire_format(protos) {
|
|
244
|
+
Ok(_) => 0,
|
|
245
|
+
|
|
246
|
+
Err(e) => e.to_c() as c_int,
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
#[no_mangle]
|
|
251
|
+
pub extern "C" fn quiche_config_set_max_amplification_factor(
|
|
252
|
+
config: &mut Config, v: usize,
|
|
253
|
+
) {
|
|
254
|
+
config.set_max_amplification_factor(v);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#[no_mangle]
|
|
258
|
+
pub extern "C" fn quiche_config_set_max_idle_timeout(
|
|
259
|
+
config: &mut Config, v: u64,
|
|
260
|
+
) {
|
|
261
|
+
config.set_max_idle_timeout(v);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#[no_mangle]
|
|
265
|
+
pub extern "C" fn quiche_config_set_max_recv_udp_payload_size(
|
|
266
|
+
config: &mut Config, v: size_t,
|
|
267
|
+
) {
|
|
268
|
+
config.set_max_recv_udp_payload_size(v);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#[no_mangle]
|
|
272
|
+
pub extern "C" fn quiche_config_set_initial_max_data(
|
|
273
|
+
config: &mut Config, v: u64,
|
|
274
|
+
) {
|
|
275
|
+
config.set_initial_max_data(v);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#[no_mangle]
|
|
279
|
+
pub extern "C" fn quiche_config_set_initial_max_stream_data_bidi_local(
|
|
280
|
+
config: &mut Config, v: u64,
|
|
281
|
+
) {
|
|
282
|
+
config.set_initial_max_stream_data_bidi_local(v);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
#[no_mangle]
|
|
286
|
+
pub extern "C" fn quiche_config_set_initial_max_stream_data_bidi_remote(
|
|
287
|
+
config: &mut Config, v: u64,
|
|
288
|
+
) {
|
|
289
|
+
config.set_initial_max_stream_data_bidi_remote(v);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#[no_mangle]
|
|
293
|
+
pub extern "C" fn quiche_config_set_initial_max_stream_data_uni(
|
|
294
|
+
config: &mut Config, v: u64,
|
|
295
|
+
) {
|
|
296
|
+
config.set_initial_max_stream_data_uni(v);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
#[no_mangle]
|
|
300
|
+
pub extern "C" fn quiche_config_set_initial_max_streams_bidi(
|
|
301
|
+
config: &mut Config, v: u64,
|
|
302
|
+
) {
|
|
303
|
+
config.set_initial_max_streams_bidi(v);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
#[no_mangle]
|
|
307
|
+
pub extern "C" fn quiche_config_set_initial_max_streams_uni(
|
|
308
|
+
config: &mut Config, v: u64,
|
|
309
|
+
) {
|
|
310
|
+
config.set_initial_max_streams_uni(v);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
#[no_mangle]
|
|
314
|
+
pub extern "C" fn quiche_config_set_ack_delay_exponent(
|
|
315
|
+
config: &mut Config, v: u64,
|
|
316
|
+
) {
|
|
317
|
+
config.set_ack_delay_exponent(v);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
#[no_mangle]
|
|
321
|
+
pub extern "C" fn quiche_config_set_max_ack_delay(config: &mut Config, v: u64) {
|
|
322
|
+
config.set_max_ack_delay(v);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
#[no_mangle]
|
|
326
|
+
pub extern "C" fn quiche_config_set_disable_active_migration(
|
|
327
|
+
config: &mut Config, v: bool,
|
|
328
|
+
) {
|
|
329
|
+
config.set_disable_active_migration(v);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
#[no_mangle]
|
|
333
|
+
pub extern "C" fn quiche_config_set_cc_algorithm_name(
|
|
334
|
+
config: &mut Config, name: *const c_char,
|
|
335
|
+
) -> c_int {
|
|
336
|
+
let name = unsafe { ffi::CStr::from_ptr(name).to_str().unwrap() };
|
|
337
|
+
match config.set_cc_algorithm_name(name) {
|
|
338
|
+
Ok(_) => 0,
|
|
339
|
+
|
|
340
|
+
Err(e) => e.to_c() as c_int,
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
#[no_mangle]
|
|
345
|
+
pub extern "C" fn quiche_config_set_cc_algorithm(
|
|
346
|
+
config: &mut Config, algo: CongestionControlAlgorithm,
|
|
347
|
+
) {
|
|
348
|
+
config.set_cc_algorithm(algo);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
#[no_mangle]
|
|
352
|
+
pub extern "C" fn quiche_config_set_initial_congestion_window_packets(
|
|
353
|
+
config: &mut Config, packets: size_t,
|
|
354
|
+
) {
|
|
355
|
+
config.set_initial_congestion_window_packets(packets);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
#[no_mangle]
|
|
359
|
+
pub extern "C" fn quiche_config_enable_hystart(config: &mut Config, v: bool) {
|
|
360
|
+
config.enable_hystart(v);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
#[no_mangle]
|
|
364
|
+
pub extern "C" fn quiche_config_enable_pacing(config: &mut Config, v: bool) {
|
|
365
|
+
config.enable_pacing(v);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#[no_mangle]
|
|
369
|
+
pub extern "C" fn quiche_config_set_enable_cubic_idle_restart_fix(
|
|
370
|
+
config: &mut Config, v: bool,
|
|
371
|
+
) {
|
|
372
|
+
config.set_enable_cubic_idle_restart_fix(v);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
#[no_mangle]
|
|
376
|
+
pub extern "C" fn quiche_config_set_use_initial_max_data_as_flow_control_win(
|
|
377
|
+
config: &mut Config, v: bool,
|
|
378
|
+
) {
|
|
379
|
+
config.set_use_initial_max_data_as_flow_control_win(v);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
#[no_mangle]
|
|
383
|
+
pub extern "C" fn quiche_config_set_max_pacing_rate(config: &mut Config, v: u64) {
|
|
384
|
+
config.set_max_pacing_rate(v);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
#[no_mangle]
|
|
388
|
+
pub extern "C" fn quiche_config_enable_dgram(
|
|
389
|
+
config: &mut Config, enabled: bool, recv_queue_len: size_t,
|
|
390
|
+
send_queue_len: size_t,
|
|
391
|
+
) {
|
|
392
|
+
config.enable_dgram(enabled, recv_queue_len, send_queue_len);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
#[no_mangle]
|
|
396
|
+
pub extern "C" fn quiche_config_set_max_send_udp_payload_size(
|
|
397
|
+
config: &mut Config, v: size_t,
|
|
398
|
+
) {
|
|
399
|
+
config.set_max_send_udp_payload_size(v);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
#[no_mangle]
|
|
403
|
+
pub extern "C" fn quiche_config_set_max_connection_window(
|
|
404
|
+
config: &mut Config, v: u64,
|
|
405
|
+
) {
|
|
406
|
+
config.set_max_connection_window(v);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
#[no_mangle]
|
|
410
|
+
pub extern "C" fn quiche_config_set_max_stream_window(
|
|
411
|
+
config: &mut Config, v: u64,
|
|
412
|
+
) {
|
|
413
|
+
config.set_max_stream_window(v);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
#[no_mangle]
|
|
417
|
+
pub extern "C" fn quiche_config_set_active_connection_id_limit(
|
|
418
|
+
config: &mut Config, v: u64,
|
|
419
|
+
) {
|
|
420
|
+
config.set_active_connection_id_limit(v);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
#[no_mangle]
|
|
424
|
+
pub extern "C" fn quiche_config_set_stateless_reset_token(
|
|
425
|
+
config: &mut Config, v: *const u8,
|
|
426
|
+
) {
|
|
427
|
+
let reset_token = unsafe { slice::from_raw_parts(v, 16) };
|
|
428
|
+
let reset_token = match reset_token.try_into() {
|
|
429
|
+
Ok(rt) => rt,
|
|
430
|
+
Err(_) => unreachable!(),
|
|
431
|
+
};
|
|
432
|
+
let reset_token = u128::from_be_bytes(reset_token);
|
|
433
|
+
config.set_stateless_reset_token(Some(reset_token));
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
#[no_mangle]
|
|
437
|
+
pub extern "C" fn quiche_config_set_disable_dcid_reuse(
|
|
438
|
+
config: &mut Config, v: bool,
|
|
439
|
+
) {
|
|
440
|
+
config.set_disable_dcid_reuse(v);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
#[no_mangle]
|
|
444
|
+
pub extern "C" fn quiche_config_set_ticket_key(
|
|
445
|
+
config: &mut Config, key: *const u8, key_len: size_t,
|
|
446
|
+
) -> c_int {
|
|
447
|
+
let key = unsafe { slice::from_raw_parts(key, key_len) };
|
|
448
|
+
|
|
449
|
+
match config.set_ticket_key(key) {
|
|
450
|
+
Ok(_) => 0,
|
|
451
|
+
|
|
452
|
+
Err(e) => e.to_c() as c_int,
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
#[no_mangle]
|
|
457
|
+
pub extern "C" fn quiche_config_free(config: *mut Config) {
|
|
458
|
+
if !config.is_null() {
|
|
459
|
+
drop(unsafe { Box::from_raw(config) });
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
#[no_mangle]
|
|
464
|
+
pub extern "C" fn quiche_header_info(
|
|
465
|
+
buf: *mut u8, buf_len: size_t, dcil: size_t, version: *mut u32, ty: *mut u8,
|
|
466
|
+
scid: *mut u8, scid_len: *mut size_t, dcid: *mut u8, dcid_len: *mut size_t,
|
|
467
|
+
token: *mut u8, token_len: *mut size_t,
|
|
468
|
+
) -> c_int {
|
|
469
|
+
let buf = unsafe { slice::from_raw_parts_mut(buf, buf_len) };
|
|
470
|
+
let hdr = match Header::from_slice(buf, dcil) {
|
|
471
|
+
Ok(v) => v,
|
|
472
|
+
|
|
473
|
+
Err(e) => return e.to_c() as c_int,
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
unsafe {
|
|
477
|
+
*version = hdr.version;
|
|
478
|
+
|
|
479
|
+
*ty = match hdr.ty {
|
|
480
|
+
Type::Initial => 1,
|
|
481
|
+
Type::Retry => 2,
|
|
482
|
+
Type::Handshake => 3,
|
|
483
|
+
Type::ZeroRTT => 4,
|
|
484
|
+
Type::Short => 5,
|
|
485
|
+
Type::VersionNegotiation => 6,
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
if *scid_len < hdr.scid.len() {
|
|
489
|
+
return -1;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
let scid = slice::from_raw_parts_mut(scid, *scid_len);
|
|
493
|
+
let scid = &mut scid[..hdr.scid.len()];
|
|
494
|
+
scid.copy_from_slice(&hdr.scid);
|
|
495
|
+
|
|
496
|
+
*scid_len = hdr.scid.len();
|
|
497
|
+
|
|
498
|
+
if *dcid_len < hdr.dcid.len() {
|
|
499
|
+
return -1;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
let dcid = slice::from_raw_parts_mut(dcid, *dcid_len);
|
|
503
|
+
let dcid = &mut dcid[..hdr.dcid.len()];
|
|
504
|
+
dcid.copy_from_slice(&hdr.dcid);
|
|
505
|
+
|
|
506
|
+
*dcid_len = hdr.dcid.len();
|
|
507
|
+
|
|
508
|
+
match hdr.token {
|
|
509
|
+
Some(tok) => {
|
|
510
|
+
if *token_len < tok.len() {
|
|
511
|
+
return -1;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
let token = slice::from_raw_parts_mut(token, *token_len);
|
|
515
|
+
let token = &mut token[..tok.len()];
|
|
516
|
+
token.copy_from_slice(&tok);
|
|
517
|
+
|
|
518
|
+
*token_len = tok.len();
|
|
519
|
+
},
|
|
520
|
+
|
|
521
|
+
None => *token_len = 0,
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
0
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
#[no_mangle]
|
|
529
|
+
pub extern "C" fn quiche_accept(
|
|
530
|
+
scid: *const u8, scid_len: size_t, odcid: *const u8, odcid_len: size_t,
|
|
531
|
+
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
532
|
+
config: &mut Config,
|
|
533
|
+
) -> *mut Connection {
|
|
534
|
+
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
535
|
+
let scid = ConnectionId::from_ref(scid);
|
|
536
|
+
|
|
537
|
+
let odcid = if !odcid.is_null() && odcid_len > 0 {
|
|
538
|
+
Some(ConnectionId::from_ref(unsafe {
|
|
539
|
+
slice::from_raw_parts(odcid, odcid_len)
|
|
540
|
+
}))
|
|
541
|
+
} else {
|
|
542
|
+
None
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
let local = std_addr_from_c(local, local_len);
|
|
546
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
547
|
+
|
|
548
|
+
match accept(&scid, odcid.as_ref(), local, peer, config) {
|
|
549
|
+
Ok(c) => Box::into_raw(Box::new(c)),
|
|
550
|
+
|
|
551
|
+
Err(_) => ptr::null_mut(),
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
#[no_mangle]
|
|
556
|
+
pub extern "C" fn quiche_connect(
|
|
557
|
+
server_name: *const c_char, scid: *const u8, scid_len: size_t,
|
|
558
|
+
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
559
|
+
config: &mut Config,
|
|
560
|
+
) -> *mut Connection {
|
|
561
|
+
let server_name = if server_name.is_null() {
|
|
562
|
+
None
|
|
563
|
+
} else {
|
|
564
|
+
Some(unsafe { ffi::CStr::from_ptr(server_name).to_str().unwrap() })
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
568
|
+
let scid = ConnectionId::from_ref(scid);
|
|
569
|
+
|
|
570
|
+
let local = std_addr_from_c(local, local_len);
|
|
571
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
572
|
+
|
|
573
|
+
match connect(server_name, &scid, local, peer, config) {
|
|
574
|
+
Ok(c) => Box::into_raw(Box::new(c)),
|
|
575
|
+
|
|
576
|
+
Err(_) => ptr::null_mut(),
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
#[no_mangle]
|
|
581
|
+
pub extern "C" fn quiche_negotiate_version(
|
|
582
|
+
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
583
|
+
out: *mut u8, out_len: size_t,
|
|
584
|
+
) -> ssize_t {
|
|
585
|
+
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
586
|
+
let scid = ConnectionId::from_ref(scid);
|
|
587
|
+
|
|
588
|
+
let dcid = unsafe { slice::from_raw_parts(dcid, dcid_len) };
|
|
589
|
+
let dcid = ConnectionId::from_ref(dcid);
|
|
590
|
+
|
|
591
|
+
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
592
|
+
|
|
593
|
+
match negotiate_version(&scid, &dcid, out) {
|
|
594
|
+
Ok(v) => v as ssize_t,
|
|
595
|
+
|
|
596
|
+
Err(e) => e.to_c(),
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
#[no_mangle]
|
|
601
|
+
pub extern "C" fn quiche_version_is_supported(version: u32) -> bool {
|
|
602
|
+
version_is_supported(version)
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
#[no_mangle]
|
|
606
|
+
pub extern "C" fn quiche_retry(
|
|
607
|
+
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
608
|
+
new_scid: *const u8, new_scid_len: size_t, token: *const u8,
|
|
609
|
+
token_len: size_t, version: u32, out: *mut u8, out_len: size_t,
|
|
610
|
+
) -> ssize_t {
|
|
611
|
+
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
612
|
+
let scid = ConnectionId::from_ref(scid);
|
|
613
|
+
|
|
614
|
+
let dcid = unsafe { slice::from_raw_parts(dcid, dcid_len) };
|
|
615
|
+
let dcid = ConnectionId::from_ref(dcid);
|
|
616
|
+
|
|
617
|
+
let new_scid = unsafe { slice::from_raw_parts(new_scid, new_scid_len) };
|
|
618
|
+
let new_scid = ConnectionId::from_ref(new_scid);
|
|
619
|
+
|
|
620
|
+
let token = unsafe { slice::from_raw_parts(token, token_len) };
|
|
621
|
+
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
622
|
+
|
|
623
|
+
match retry(&scid, &dcid, &new_scid, token, version, out) {
|
|
624
|
+
Ok(v) => v as ssize_t,
|
|
625
|
+
|
|
626
|
+
Err(e) => e.to_c(),
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
#[no_mangle]
|
|
631
|
+
#[cfg(feature = "custom-client-dcid")]
|
|
632
|
+
pub extern "C" fn quiche_conn_new_with_tls_and_client_dcid(
|
|
633
|
+
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
634
|
+
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
635
|
+
config: &Config, ssl: *mut c_void,
|
|
636
|
+
) -> *mut Connection {
|
|
637
|
+
{
|
|
638
|
+
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
639
|
+
let scid = ConnectionId::from_ref(scid);
|
|
640
|
+
|
|
641
|
+
let dcid = if !dcid.is_null() && dcid_len > 0 {
|
|
642
|
+
Some(ConnectionId::from_ref(unsafe {
|
|
643
|
+
slice::from_raw_parts(dcid, dcid_len)
|
|
644
|
+
}))
|
|
645
|
+
} else {
|
|
646
|
+
None
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
let local = std_addr_from_c(local, local_len);
|
|
650
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
651
|
+
|
|
652
|
+
let tls = unsafe { tls::Handshake::from_ptr(ssl) };
|
|
653
|
+
|
|
654
|
+
match Connection::with_tls(
|
|
655
|
+
&scid,
|
|
656
|
+
None, // retry_cids
|
|
657
|
+
dcid.as_ref(),
|
|
658
|
+
local,
|
|
659
|
+
peer,
|
|
660
|
+
config,
|
|
661
|
+
tls,
|
|
662
|
+
false,
|
|
663
|
+
) {
|
|
664
|
+
Ok(c) => Box::into_raw(Box::new(c)),
|
|
665
|
+
|
|
666
|
+
Err(_) => ptr::null_mut(),
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
#[no_mangle]
|
|
672
|
+
#[cfg(not(feature = "custom-client-dcid"))]
|
|
673
|
+
#[allow(unused_variables)]
|
|
674
|
+
pub extern "C" fn quiche_conn_new_with_tls_and_client_dcid(
|
|
675
|
+
scid: *const u8, scid_len: size_t, dcid: *const u8, dcid_len: size_t,
|
|
676
|
+
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
677
|
+
config: &Config, ssl: *mut c_void,
|
|
678
|
+
) -> *mut Connection {
|
|
679
|
+
// It's always an error to call this function without the custom-client-dcid
|
|
680
|
+
// feature enabled.
|
|
681
|
+
ptr::null_mut()
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
#[no_mangle]
|
|
685
|
+
pub extern "C" fn quiche_conn_new_with_tls(
|
|
686
|
+
scid: *const u8, scid_len: size_t, odcid: *const u8, odcid_len: size_t,
|
|
687
|
+
local: &sockaddr, local_len: socklen_t, peer: &sockaddr, peer_len: socklen_t,
|
|
688
|
+
config: &Config, ssl: *mut c_void, is_server: bool,
|
|
689
|
+
) -> *mut Connection {
|
|
690
|
+
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
691
|
+
let scid = ConnectionId::from_ref(scid);
|
|
692
|
+
|
|
693
|
+
let odcid = if !odcid.is_null() && odcid_len > 0 {
|
|
694
|
+
Some(ConnectionId::from_ref(unsafe {
|
|
695
|
+
slice::from_raw_parts(odcid, odcid_len)
|
|
696
|
+
}))
|
|
697
|
+
} else {
|
|
698
|
+
None
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
let retry_cids = odcid.as_ref().map(|odcid| RetryConnectionIds {
|
|
702
|
+
original_destination_cid: odcid,
|
|
703
|
+
retry_source_cid: &scid,
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
let local = std_addr_from_c(local, local_len);
|
|
707
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
708
|
+
|
|
709
|
+
let tls = unsafe { tls::Handshake::from_ptr(ssl) };
|
|
710
|
+
|
|
711
|
+
match Connection::with_tls(
|
|
712
|
+
&scid, retry_cids, None, local, peer, config, tls, is_server,
|
|
713
|
+
) {
|
|
714
|
+
Ok(c) => Box::into_raw(Box::new(c)),
|
|
715
|
+
|
|
716
|
+
Err(_) => ptr::null_mut(),
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
#[no_mangle]
|
|
721
|
+
pub extern "C" fn quiche_conn_set_keylog_path(
|
|
722
|
+
conn: &mut Connection, path: *const c_char,
|
|
723
|
+
) -> bool {
|
|
724
|
+
let filename = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
725
|
+
|
|
726
|
+
let file = std::fs::OpenOptions::new()
|
|
727
|
+
.create(true)
|
|
728
|
+
.append(true)
|
|
729
|
+
.open(filename);
|
|
730
|
+
|
|
731
|
+
let writer = match file {
|
|
732
|
+
Ok(f) => std::io::BufWriter::new(f),
|
|
733
|
+
|
|
734
|
+
Err(_) => return false,
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
conn.set_keylog(Box::new(writer));
|
|
738
|
+
|
|
739
|
+
true
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
#[no_mangle]
|
|
743
|
+
#[cfg(unix)]
|
|
744
|
+
pub extern "C" fn quiche_conn_set_keylog_fd(conn: &mut Connection, fd: c_int) {
|
|
745
|
+
let f = unsafe { std::fs::File::from_raw_fd(fd) };
|
|
746
|
+
let writer = std::io::BufWriter::new(f);
|
|
747
|
+
|
|
748
|
+
conn.set_keylog(Box::new(writer));
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
#[no_mangle]
|
|
752
|
+
#[cfg(feature = "qlog")]
|
|
753
|
+
pub extern "C" fn quiche_conn_set_qlog_path(
|
|
754
|
+
conn: &mut Connection, path: *const c_char, log_title: *const c_char,
|
|
755
|
+
log_desc: *const c_char,
|
|
756
|
+
) -> bool {
|
|
757
|
+
let filename = unsafe { ffi::CStr::from_ptr(path).to_str().unwrap() };
|
|
758
|
+
|
|
759
|
+
let file = std::fs::OpenOptions::new()
|
|
760
|
+
.write(true)
|
|
761
|
+
.create_new(true)
|
|
762
|
+
.open(filename);
|
|
763
|
+
|
|
764
|
+
let writer = match file {
|
|
765
|
+
Ok(f) => std::io::BufWriter::new(f),
|
|
766
|
+
|
|
767
|
+
Err(_) => return false,
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
let title = unsafe { ffi::CStr::from_ptr(log_title).to_str().unwrap() };
|
|
771
|
+
let description = unsafe { ffi::CStr::from_ptr(log_desc).to_str().unwrap() };
|
|
772
|
+
|
|
773
|
+
conn.set_qlog(
|
|
774
|
+
Box::new(writer),
|
|
775
|
+
title.to_string(),
|
|
776
|
+
format!("{} id={}", description, conn.trace_id),
|
|
777
|
+
);
|
|
778
|
+
|
|
779
|
+
true
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
#[no_mangle]
|
|
783
|
+
#[cfg(all(unix, feature = "qlog"))]
|
|
784
|
+
pub extern "C" fn quiche_conn_set_qlog_fd(
|
|
785
|
+
conn: &mut Connection, fd: c_int, log_title: *const c_char,
|
|
786
|
+
log_desc: *const c_char,
|
|
787
|
+
) {
|
|
788
|
+
let f = unsafe { std::fs::File::from_raw_fd(fd) };
|
|
789
|
+
let writer = std::io::BufWriter::new(f);
|
|
790
|
+
|
|
791
|
+
let title = unsafe { ffi::CStr::from_ptr(log_title).to_str().unwrap() };
|
|
792
|
+
let description = unsafe { ffi::CStr::from_ptr(log_desc).to_str().unwrap() };
|
|
793
|
+
|
|
794
|
+
conn.set_qlog(
|
|
795
|
+
Box::new(writer),
|
|
796
|
+
title.to_string(),
|
|
797
|
+
format!("{} id={}", description, conn.trace_id),
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
#[no_mangle]
|
|
802
|
+
pub extern "C" fn quiche_conn_set_session(
|
|
803
|
+
conn: &mut Connection, buf: *const u8, buf_len: size_t,
|
|
804
|
+
) -> c_int {
|
|
805
|
+
let buf = unsafe { slice::from_raw_parts(buf, buf_len) };
|
|
806
|
+
|
|
807
|
+
match conn.set_session(buf) {
|
|
808
|
+
Ok(_) => 0,
|
|
809
|
+
|
|
810
|
+
Err(e) => e.to_c() as c_int,
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
#[no_mangle]
|
|
815
|
+
pub extern "C" fn quiche_conn_set_max_idle_timeout(
|
|
816
|
+
conn: &mut Connection, v: u64,
|
|
817
|
+
) -> c_int {
|
|
818
|
+
match conn.set_max_idle_timeout(v) {
|
|
819
|
+
Ok(()) => 0,
|
|
820
|
+
|
|
821
|
+
Err(e) => e.to_c() as c_int,
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
#[repr(C)]
|
|
826
|
+
pub struct RecvInfo<'a> {
|
|
827
|
+
from: &'a sockaddr,
|
|
828
|
+
from_len: socklen_t,
|
|
829
|
+
to: &'a sockaddr,
|
|
830
|
+
to_len: socklen_t,
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
impl From<&RecvInfo<'_>> for crate::RecvInfo {
|
|
834
|
+
fn from(info: &RecvInfo) -> crate::RecvInfo {
|
|
835
|
+
crate::RecvInfo {
|
|
836
|
+
from: std_addr_from_c(info.from, info.from_len),
|
|
837
|
+
to: std_addr_from_c(info.to, info.to_len),
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
#[no_mangle]
|
|
843
|
+
pub extern "C" fn quiche_conn_recv(
|
|
844
|
+
conn: &mut Connection, buf: *mut u8, buf_len: size_t, info: &RecvInfo,
|
|
845
|
+
) -> ssize_t {
|
|
846
|
+
if buf_len > <ssize_t>::MAX as usize {
|
|
847
|
+
panic!("The provided buffer is too large");
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
let buf = unsafe { slice::from_raw_parts_mut(buf, buf_len) };
|
|
851
|
+
|
|
852
|
+
match conn.recv(buf, info.into()) {
|
|
853
|
+
Ok(v) => v as ssize_t,
|
|
854
|
+
|
|
855
|
+
Err(e) => e.to_c(),
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
#[repr(C)]
|
|
860
|
+
pub struct SendInfo {
|
|
861
|
+
from: sockaddr_storage,
|
|
862
|
+
from_len: socklen_t,
|
|
863
|
+
to: sockaddr_storage,
|
|
864
|
+
to_len: socklen_t,
|
|
865
|
+
|
|
866
|
+
at: timespec,
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
#[no_mangle]
|
|
870
|
+
pub extern "C" fn quiche_conn_send(
|
|
871
|
+
conn: &mut Connection, out: *mut u8, out_len: size_t, out_info: &mut SendInfo,
|
|
872
|
+
) -> ssize_t {
|
|
873
|
+
if out_len > <ssize_t>::MAX as usize {
|
|
874
|
+
panic!("The provided buffer is too large");
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
878
|
+
|
|
879
|
+
match conn.send(out) {
|
|
880
|
+
Ok((v, info)) => {
|
|
881
|
+
out_info.from_len = std_addr_to_c(&info.from, &mut out_info.from);
|
|
882
|
+
out_info.to_len = std_addr_to_c(&info.to, &mut out_info.to);
|
|
883
|
+
|
|
884
|
+
std_time_to_c(&info.at, &mut out_info.at);
|
|
885
|
+
|
|
886
|
+
v as ssize_t
|
|
887
|
+
},
|
|
888
|
+
|
|
889
|
+
Err(e) => e.to_c(),
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
#[no_mangle]
|
|
894
|
+
pub extern "C" fn quiche_conn_send_on_path(
|
|
895
|
+
conn: &mut Connection, out: *mut u8, out_len: size_t, from: *const sockaddr,
|
|
896
|
+
from_len: socklen_t, to: *const sockaddr, to_len: socklen_t,
|
|
897
|
+
out_info: &mut SendInfo,
|
|
898
|
+
) -> ssize_t {
|
|
899
|
+
if out_len > <ssize_t>::MAX as usize {
|
|
900
|
+
panic!("The provided buffer is too large");
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
let from = optional_std_addr_from_c(from, from_len);
|
|
904
|
+
let to = optional_std_addr_from_c(to, to_len);
|
|
905
|
+
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
906
|
+
|
|
907
|
+
match conn.send_on_path(out, from, to) {
|
|
908
|
+
Ok((v, info)) => {
|
|
909
|
+
out_info.from_len = std_addr_to_c(&info.from, &mut out_info.from);
|
|
910
|
+
out_info.to_len = std_addr_to_c(&info.to, &mut out_info.to);
|
|
911
|
+
|
|
912
|
+
std_time_to_c(&info.at, &mut out_info.at);
|
|
913
|
+
|
|
914
|
+
v as ssize_t
|
|
915
|
+
},
|
|
916
|
+
|
|
917
|
+
Err(e) => e.to_c(),
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
#[no_mangle]
|
|
922
|
+
pub extern "C" fn quiche_conn_stream_recv(
|
|
923
|
+
conn: &mut Connection, stream_id: u64, out: *mut u8, out_len: size_t,
|
|
924
|
+
fin: &mut bool, out_error_code: &mut u64,
|
|
925
|
+
) -> ssize_t {
|
|
926
|
+
if out_len > <ssize_t>::MAX as usize {
|
|
927
|
+
panic!("The provided buffer is too large");
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
931
|
+
|
|
932
|
+
let (out_len, out_fin) = match conn.stream_recv(stream_id, out) {
|
|
933
|
+
Ok(v) => v,
|
|
934
|
+
|
|
935
|
+
Err(e) => {
|
|
936
|
+
match e {
|
|
937
|
+
Error::StreamReset(error) => *out_error_code = error,
|
|
938
|
+
Error::StreamStopped(error) => *out_error_code = error,
|
|
939
|
+
_ => {},
|
|
940
|
+
}
|
|
941
|
+
return e.to_c();
|
|
942
|
+
},
|
|
943
|
+
};
|
|
944
|
+
|
|
945
|
+
*fin = out_fin;
|
|
946
|
+
|
|
947
|
+
out_len as ssize_t
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
#[no_mangle]
|
|
951
|
+
pub extern "C" fn quiche_conn_stream_send(
|
|
952
|
+
conn: &mut Connection, stream_id: u64, buf: *const u8, buf_len: size_t,
|
|
953
|
+
fin: bool, out_error_code: &mut u64,
|
|
954
|
+
) -> ssize_t {
|
|
955
|
+
if buf_len > <ssize_t>::MAX as usize {
|
|
956
|
+
panic!("The provided buffer is too large");
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
let buf = if buf.is_null() {
|
|
960
|
+
assert_eq!(buf_len, 0);
|
|
961
|
+
&[]
|
|
962
|
+
} else {
|
|
963
|
+
unsafe { slice::from_raw_parts(buf, buf_len) }
|
|
964
|
+
};
|
|
965
|
+
|
|
966
|
+
match conn.stream_send(stream_id, buf, fin) {
|
|
967
|
+
Ok(v) => v as ssize_t,
|
|
968
|
+
|
|
969
|
+
Err(e) => {
|
|
970
|
+
match e {
|
|
971
|
+
Error::StreamReset(error) => *out_error_code = error,
|
|
972
|
+
Error::StreamStopped(error) => *out_error_code = error,
|
|
973
|
+
_ => {},
|
|
974
|
+
}
|
|
975
|
+
e.to_c()
|
|
976
|
+
},
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
#[no_mangle]
|
|
981
|
+
pub extern "C" fn quiche_conn_stream_priority(
|
|
982
|
+
conn: &mut Connection, stream_id: u64, urgency: u8, incremental: bool,
|
|
983
|
+
) -> c_int {
|
|
984
|
+
match conn.stream_priority(stream_id, urgency, incremental) {
|
|
985
|
+
Ok(_) => 0,
|
|
986
|
+
|
|
987
|
+
Err(e) => e.to_c() as c_int,
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
#[no_mangle]
|
|
992
|
+
pub extern "C" fn quiche_conn_stream_shutdown(
|
|
993
|
+
conn: &mut Connection, stream_id: u64, direction: Shutdown, err: u64,
|
|
994
|
+
) -> c_int {
|
|
995
|
+
match conn.stream_shutdown(stream_id, direction, err) {
|
|
996
|
+
Ok(_) => 0,
|
|
997
|
+
|
|
998
|
+
Err(e) => e.to_c() as c_int,
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
#[no_mangle]
|
|
1003
|
+
pub extern "C" fn quiche_conn_stream_capacity(
|
|
1004
|
+
conn: &mut Connection, stream_id: u64,
|
|
1005
|
+
) -> ssize_t {
|
|
1006
|
+
match conn.stream_capacity(stream_id) {
|
|
1007
|
+
Ok(v) => v as ssize_t,
|
|
1008
|
+
|
|
1009
|
+
Err(e) => e.to_c(),
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
#[no_mangle]
|
|
1014
|
+
pub extern "C" fn quiche_conn_stream_readable(
|
|
1015
|
+
conn: &Connection, stream_id: u64,
|
|
1016
|
+
) -> bool {
|
|
1017
|
+
conn.stream_readable(stream_id)
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
#[no_mangle]
|
|
1021
|
+
pub extern "C" fn quiche_conn_stream_readable_next(conn: &mut Connection) -> i64 {
|
|
1022
|
+
conn.stream_readable_next().map(|v| v as i64).unwrap_or(-1)
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
#[no_mangle]
|
|
1026
|
+
pub extern "C" fn quiche_conn_stream_writable(
|
|
1027
|
+
conn: &mut Connection, stream_id: u64, len: usize,
|
|
1028
|
+
) -> c_int {
|
|
1029
|
+
match conn.stream_writable(stream_id, len) {
|
|
1030
|
+
Ok(true) => 1,
|
|
1031
|
+
|
|
1032
|
+
Ok(false) => 0,
|
|
1033
|
+
|
|
1034
|
+
Err(e) => e.to_c() as c_int,
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
#[no_mangle]
|
|
1039
|
+
pub extern "C" fn quiche_conn_stream_writable_next(conn: &mut Connection) -> i64 {
|
|
1040
|
+
conn.stream_writable_next().map(|v| v as i64).unwrap_or(-1)
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
#[no_mangle]
|
|
1044
|
+
pub extern "C" fn quiche_conn_stream_finished(
|
|
1045
|
+
conn: &Connection, stream_id: u64,
|
|
1046
|
+
) -> bool {
|
|
1047
|
+
conn.stream_finished(stream_id)
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
#[no_mangle]
|
|
1051
|
+
pub extern "C" fn quiche_conn_readable(conn: &Connection) -> *mut StreamIter {
|
|
1052
|
+
Box::into_raw(Box::new(conn.readable()))
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
#[no_mangle]
|
|
1056
|
+
pub extern "C" fn quiche_conn_writable(conn: &Connection) -> *mut StreamIter {
|
|
1057
|
+
Box::into_raw(Box::new(conn.writable()))
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
#[no_mangle]
|
|
1061
|
+
pub extern "C" fn quiche_conn_max_send_udp_payload_size(
|
|
1062
|
+
conn: &Connection,
|
|
1063
|
+
) -> usize {
|
|
1064
|
+
conn.max_send_udp_payload_size()
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
#[no_mangle]
|
|
1068
|
+
pub extern "C" fn quiche_conn_is_readable(conn: &Connection) -> bool {
|
|
1069
|
+
conn.is_readable()
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
#[no_mangle]
|
|
1073
|
+
pub extern "C" fn quiche_conn_close(
|
|
1074
|
+
conn: &mut Connection, app: bool, err: u64, reason: *const u8,
|
|
1075
|
+
reason_len: size_t,
|
|
1076
|
+
) -> c_int {
|
|
1077
|
+
let reason = if reason.is_null() {
|
|
1078
|
+
assert_eq!(reason_len, 0);
|
|
1079
|
+
&[]
|
|
1080
|
+
} else {
|
|
1081
|
+
unsafe { slice::from_raw_parts(reason, reason_len) }
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
match conn.close(app, err, reason) {
|
|
1085
|
+
Ok(_) => 0,
|
|
1086
|
+
|
|
1087
|
+
Err(e) => e.to_c() as c_int,
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
#[no_mangle]
|
|
1092
|
+
pub extern "C" fn quiche_conn_timeout_as_nanos(conn: &Connection) -> u64 {
|
|
1093
|
+
match conn.timeout() {
|
|
1094
|
+
Some(timeout) => timeout.as_nanos() as u64,
|
|
1095
|
+
|
|
1096
|
+
None => u64::MAX,
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
#[no_mangle]
|
|
1101
|
+
pub extern "C" fn quiche_conn_timeout_as_millis(conn: &Connection) -> u64 {
|
|
1102
|
+
match conn.timeout() {
|
|
1103
|
+
Some(timeout) => timeout.as_millis() as u64,
|
|
1104
|
+
|
|
1105
|
+
None => u64::MAX,
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
#[no_mangle]
|
|
1110
|
+
pub extern "C" fn quiche_conn_on_timeout(conn: &mut Connection) {
|
|
1111
|
+
conn.on_timeout()
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
#[no_mangle]
|
|
1115
|
+
pub extern "C" fn quiche_conn_trace_id(
|
|
1116
|
+
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1117
|
+
) {
|
|
1118
|
+
let trace_id = conn.trace_id();
|
|
1119
|
+
|
|
1120
|
+
*out = trace_id.as_ptr();
|
|
1121
|
+
*out_len = trace_id.len();
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/// An iterator over connection ids.
|
|
1125
|
+
#[derive(Default)]
|
|
1126
|
+
pub struct ConnectionIdIter<'a> {
|
|
1127
|
+
cids: Vec<ConnectionId<'a>>,
|
|
1128
|
+
index: usize,
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
impl<'a> Iterator for ConnectionIdIter<'a> {
|
|
1132
|
+
type Item = ConnectionId<'a>;
|
|
1133
|
+
|
|
1134
|
+
#[inline]
|
|
1135
|
+
fn next(&mut self) -> Option<Self::Item> {
|
|
1136
|
+
let v = self.cids.get(self.index)?;
|
|
1137
|
+
self.index += 1;
|
|
1138
|
+
Some(v.clone())
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
#[no_mangle]
|
|
1143
|
+
pub extern "C" fn quiche_conn_source_ids(
|
|
1144
|
+
conn: &Connection,
|
|
1145
|
+
) -> *mut ConnectionIdIter<'_> {
|
|
1146
|
+
let vec = conn.source_ids().cloned().collect();
|
|
1147
|
+
Box::into_raw(Box::new(ConnectionIdIter {
|
|
1148
|
+
cids: vec,
|
|
1149
|
+
index: 0,
|
|
1150
|
+
}))
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
#[no_mangle]
|
|
1154
|
+
pub extern "C" fn quiche_connection_id_iter_next(
|
|
1155
|
+
iter: &mut ConnectionIdIter, out: &mut *const u8, out_len: &mut size_t,
|
|
1156
|
+
) -> bool {
|
|
1157
|
+
if let Some(conn_id) = iter.next() {
|
|
1158
|
+
let id = conn_id.as_ref();
|
|
1159
|
+
*out = id.as_ptr();
|
|
1160
|
+
*out_len = id.len();
|
|
1161
|
+
return true;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
false
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
#[no_mangle]
|
|
1168
|
+
pub extern "C" fn quiche_connection_id_iter_free(iter: *mut ConnectionIdIter) {
|
|
1169
|
+
if !iter.is_null() {
|
|
1170
|
+
drop(unsafe { Box::from_raw(iter) });
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
#[no_mangle]
|
|
1175
|
+
pub extern "C" fn quiche_conn_source_id(
|
|
1176
|
+
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1177
|
+
) {
|
|
1178
|
+
let conn_id = conn.source_id();
|
|
1179
|
+
let id = conn_id.as_ref();
|
|
1180
|
+
*out = id.as_ptr();
|
|
1181
|
+
*out_len = id.len();
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
#[no_mangle]
|
|
1185
|
+
pub extern "C" fn quiche_conn_destination_id(
|
|
1186
|
+
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1187
|
+
) {
|
|
1188
|
+
let conn_id = conn.destination_id();
|
|
1189
|
+
let id = conn_id.as_ref();
|
|
1190
|
+
|
|
1191
|
+
*out = id.as_ptr();
|
|
1192
|
+
*out_len = id.len();
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
#[no_mangle]
|
|
1196
|
+
pub extern "C" fn quiche_conn_application_proto(
|
|
1197
|
+
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1198
|
+
) {
|
|
1199
|
+
let proto = conn.application_proto();
|
|
1200
|
+
|
|
1201
|
+
*out = proto.as_ptr();
|
|
1202
|
+
*out_len = proto.len();
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
#[no_mangle]
|
|
1206
|
+
pub extern "C" fn quiche_conn_peer_cert(
|
|
1207
|
+
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1208
|
+
) {
|
|
1209
|
+
match conn.peer_cert() {
|
|
1210
|
+
Some(peer_cert) => {
|
|
1211
|
+
*out = peer_cert.as_ptr();
|
|
1212
|
+
*out_len = peer_cert.len();
|
|
1213
|
+
},
|
|
1214
|
+
|
|
1215
|
+
None => *out_len = 0,
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
#[no_mangle]
|
|
1220
|
+
pub extern "C" fn quiche_conn_session(
|
|
1221
|
+
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1222
|
+
) {
|
|
1223
|
+
match conn.session() {
|
|
1224
|
+
Some(session) => {
|
|
1225
|
+
*out = session.as_ptr();
|
|
1226
|
+
*out_len = session.len();
|
|
1227
|
+
},
|
|
1228
|
+
|
|
1229
|
+
None => *out_len = 0,
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
#[no_mangle]
|
|
1234
|
+
pub extern "C" fn quiche_conn_server_name(
|
|
1235
|
+
conn: &Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1236
|
+
) {
|
|
1237
|
+
match conn.server_name() {
|
|
1238
|
+
Some(server_name) => {
|
|
1239
|
+
*out = server_name.as_ptr();
|
|
1240
|
+
*out_len = server_name.len();
|
|
1241
|
+
},
|
|
1242
|
+
|
|
1243
|
+
None => *out_len = 0,
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
#[no_mangle]
|
|
1248
|
+
pub extern "C" fn quiche_conn_is_established(conn: &Connection) -> bool {
|
|
1249
|
+
conn.is_established()
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
#[no_mangle]
|
|
1253
|
+
pub extern "C" fn quiche_conn_is_resumed(conn: &Connection) -> bool {
|
|
1254
|
+
conn.is_resumed()
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
#[no_mangle]
|
|
1258
|
+
pub extern "C" fn quiche_conn_is_in_early_data(conn: &Connection) -> bool {
|
|
1259
|
+
conn.is_in_early_data()
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
#[no_mangle]
|
|
1263
|
+
pub extern "C" fn quiche_conn_is_draining(conn: &Connection) -> bool {
|
|
1264
|
+
conn.is_draining()
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
#[no_mangle]
|
|
1268
|
+
pub extern "C" fn quiche_conn_is_closed(conn: &Connection) -> bool {
|
|
1269
|
+
conn.is_closed()
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
#[no_mangle]
|
|
1273
|
+
pub extern "C" fn quiche_conn_is_timed_out(conn: &Connection) -> bool {
|
|
1274
|
+
conn.is_timed_out()
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
#[no_mangle]
|
|
1278
|
+
pub extern "C" fn quiche_conn_peer_error(
|
|
1279
|
+
conn: &Connection, is_app: *mut bool, error_code: *mut u64,
|
|
1280
|
+
reason: &mut *const u8, reason_len: &mut size_t,
|
|
1281
|
+
) -> bool {
|
|
1282
|
+
match &conn.peer_error {
|
|
1283
|
+
Some(conn_err) => unsafe {
|
|
1284
|
+
*is_app = conn_err.is_app;
|
|
1285
|
+
*error_code = conn_err.error_code;
|
|
1286
|
+
*reason = conn_err.reason.as_ptr();
|
|
1287
|
+
*reason_len = conn_err.reason.len();
|
|
1288
|
+
|
|
1289
|
+
true
|
|
1290
|
+
},
|
|
1291
|
+
|
|
1292
|
+
None => false,
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
#[no_mangle]
|
|
1297
|
+
pub extern "C" fn quiche_conn_local_error(
|
|
1298
|
+
conn: &Connection, is_app: *mut bool, error_code: *mut u64,
|
|
1299
|
+
reason: &mut *const u8, reason_len: &mut size_t,
|
|
1300
|
+
) -> bool {
|
|
1301
|
+
match &conn.local_error {
|
|
1302
|
+
Some(conn_err) => unsafe {
|
|
1303
|
+
*is_app = conn_err.is_app;
|
|
1304
|
+
*error_code = conn_err.error_code;
|
|
1305
|
+
*reason = conn_err.reason.as_ptr();
|
|
1306
|
+
*reason_len = conn_err.reason.len();
|
|
1307
|
+
|
|
1308
|
+
true
|
|
1309
|
+
},
|
|
1310
|
+
|
|
1311
|
+
None => false,
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
#[no_mangle]
|
|
1316
|
+
pub extern "C" fn quiche_stream_iter_next(
|
|
1317
|
+
iter: &mut StreamIter, stream_id: *mut u64,
|
|
1318
|
+
) -> bool {
|
|
1319
|
+
if let Some(v) = iter.next() {
|
|
1320
|
+
unsafe { *stream_id = v };
|
|
1321
|
+
return true;
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
false
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
#[no_mangle]
|
|
1328
|
+
pub extern "C" fn quiche_stream_iter_free(iter: *mut StreamIter) {
|
|
1329
|
+
if !iter.is_null() {
|
|
1330
|
+
drop(unsafe { Box::from_raw(iter) });
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
#[repr(C)]
|
|
1335
|
+
pub struct Stats {
|
|
1336
|
+
recv: usize,
|
|
1337
|
+
sent: usize,
|
|
1338
|
+
lost: usize,
|
|
1339
|
+
spurious_lost: usize,
|
|
1340
|
+
retrans: usize,
|
|
1341
|
+
sent_bytes: u64,
|
|
1342
|
+
recv_bytes: u64,
|
|
1343
|
+
acked_bytes: u64,
|
|
1344
|
+
lost_bytes: u64,
|
|
1345
|
+
stream_retrans_bytes: u64,
|
|
1346
|
+
dgram_recv: usize,
|
|
1347
|
+
dgram_sent: usize,
|
|
1348
|
+
paths_count: usize,
|
|
1349
|
+
reset_stream_count_local: u64,
|
|
1350
|
+
stopped_stream_count_local: u64,
|
|
1351
|
+
reset_stream_count_remote: u64,
|
|
1352
|
+
stopped_stream_count_remote: u64,
|
|
1353
|
+
data_blocked_sent_count: u64,
|
|
1354
|
+
stream_data_blocked_sent_count: u64,
|
|
1355
|
+
data_blocked_recv_count: u64,
|
|
1356
|
+
stream_data_blocked_recv_count: u64,
|
|
1357
|
+
streams_blocked_bidi_recv_count: u64,
|
|
1358
|
+
streams_blocked_uni_recv_count: u64,
|
|
1359
|
+
path_challenge_rx_count: u64,
|
|
1360
|
+
bytes_in_flight_duration_msec: u64,
|
|
1361
|
+
tx_buffered_inconsistent: bool,
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
pub struct TransportParams {
|
|
1365
|
+
max_idle_timeout: u64,
|
|
1366
|
+
max_udp_payload_size: u64,
|
|
1367
|
+
initial_max_data: u64,
|
|
1368
|
+
initial_max_stream_data_bidi_local: u64,
|
|
1369
|
+
initial_max_stream_data_bidi_remote: u64,
|
|
1370
|
+
initial_max_stream_data_uni: u64,
|
|
1371
|
+
initial_max_streams_bidi: u64,
|
|
1372
|
+
initial_max_streams_uni: u64,
|
|
1373
|
+
ack_delay_exponent: u64,
|
|
1374
|
+
max_ack_delay: u64,
|
|
1375
|
+
disable_active_migration: bool,
|
|
1376
|
+
active_conn_id_limit: u64,
|
|
1377
|
+
max_datagram_frame_size: ssize_t,
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
#[no_mangle]
|
|
1381
|
+
pub extern "C" fn quiche_conn_stats(conn: &Connection, out: &mut Stats) {
|
|
1382
|
+
let stats = conn.stats();
|
|
1383
|
+
|
|
1384
|
+
out.recv = stats.recv;
|
|
1385
|
+
out.sent = stats.sent;
|
|
1386
|
+
out.lost = stats.lost;
|
|
1387
|
+
out.spurious_lost = stats.spurious_lost;
|
|
1388
|
+
out.retrans = stats.retrans;
|
|
1389
|
+
out.sent_bytes = stats.sent_bytes;
|
|
1390
|
+
out.recv_bytes = stats.recv_bytes;
|
|
1391
|
+
out.acked_bytes = stats.acked_bytes;
|
|
1392
|
+
out.lost_bytes = stats.lost_bytes;
|
|
1393
|
+
out.stream_retrans_bytes = stats.stream_retrans_bytes;
|
|
1394
|
+
out.dgram_recv = stats.dgram_recv;
|
|
1395
|
+
out.dgram_sent = stats.dgram_sent;
|
|
1396
|
+
out.paths_count = stats.paths_count;
|
|
1397
|
+
out.reset_stream_count_local = stats.reset_stream_count_local;
|
|
1398
|
+
out.stopped_stream_count_local = stats.stopped_stream_count_local;
|
|
1399
|
+
out.reset_stream_count_remote = stats.reset_stream_count_remote;
|
|
1400
|
+
out.stopped_stream_count_remote = stats.stopped_stream_count_remote;
|
|
1401
|
+
out.data_blocked_sent_count = stats.data_blocked_sent_count;
|
|
1402
|
+
out.stream_data_blocked_sent_count = stats.stream_data_blocked_sent_count;
|
|
1403
|
+
out.data_blocked_recv_count = stats.data_blocked_recv_count;
|
|
1404
|
+
out.stream_data_blocked_recv_count = stats.stream_data_blocked_recv_count;
|
|
1405
|
+
out.streams_blocked_bidi_recv_count = stats.streams_blocked_bidi_recv_count;
|
|
1406
|
+
out.streams_blocked_uni_recv_count = stats.streams_blocked_uni_recv_count;
|
|
1407
|
+
out.path_challenge_rx_count = stats.path_challenge_rx_count;
|
|
1408
|
+
out.bytes_in_flight_duration_msec =
|
|
1409
|
+
stats.bytes_in_flight_duration.as_millis() as u64;
|
|
1410
|
+
out.tx_buffered_inconsistent =
|
|
1411
|
+
stats.tx_buffered_state != TxBufferTrackingState::Ok;
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
#[no_mangle]
|
|
1415
|
+
pub extern "C" fn quiche_conn_peer_transport_params(
|
|
1416
|
+
conn: &Connection, out: &mut TransportParams,
|
|
1417
|
+
) -> bool {
|
|
1418
|
+
let tps = match conn.peer_transport_params() {
|
|
1419
|
+
Some(v) => v,
|
|
1420
|
+
None => return false,
|
|
1421
|
+
};
|
|
1422
|
+
|
|
1423
|
+
out.max_idle_timeout = tps.max_idle_timeout;
|
|
1424
|
+
out.max_udp_payload_size = tps.max_udp_payload_size;
|
|
1425
|
+
out.initial_max_data = tps.initial_max_data;
|
|
1426
|
+
out.initial_max_stream_data_bidi_local =
|
|
1427
|
+
tps.initial_max_stream_data_bidi_local;
|
|
1428
|
+
out.initial_max_stream_data_bidi_remote =
|
|
1429
|
+
tps.initial_max_stream_data_bidi_remote;
|
|
1430
|
+
out.initial_max_stream_data_uni = tps.initial_max_stream_data_uni;
|
|
1431
|
+
out.initial_max_streams_bidi = tps.initial_max_streams_bidi;
|
|
1432
|
+
out.initial_max_streams_uni = tps.initial_max_streams_uni;
|
|
1433
|
+
out.ack_delay_exponent = tps.ack_delay_exponent;
|
|
1434
|
+
out.max_ack_delay = tps.max_ack_delay;
|
|
1435
|
+
out.disable_active_migration = tps.disable_active_migration;
|
|
1436
|
+
out.active_conn_id_limit = tps.active_conn_id_limit;
|
|
1437
|
+
out.max_datagram_frame_size = match tps.max_datagram_frame_size {
|
|
1438
|
+
None => Error::Done.to_c(),
|
|
1439
|
+
|
|
1440
|
+
Some(v) => v as ssize_t,
|
|
1441
|
+
};
|
|
1442
|
+
|
|
1443
|
+
true
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
#[repr(C)]
|
|
1447
|
+
pub struct PathStats {
|
|
1448
|
+
local_addr: sockaddr_storage,
|
|
1449
|
+
local_addr_len: socklen_t,
|
|
1450
|
+
peer_addr: sockaddr_storage,
|
|
1451
|
+
peer_addr_len: socklen_t,
|
|
1452
|
+
validation_state: ssize_t,
|
|
1453
|
+
active: bool,
|
|
1454
|
+
recv: usize,
|
|
1455
|
+
sent: usize,
|
|
1456
|
+
lost: usize,
|
|
1457
|
+
retrans: usize,
|
|
1458
|
+
total_pto_count: usize,
|
|
1459
|
+
dgram_recv: usize,
|
|
1460
|
+
dgram_sent: usize,
|
|
1461
|
+
rtt: u64,
|
|
1462
|
+
min_rtt: u64,
|
|
1463
|
+
max_rtt: u64,
|
|
1464
|
+
rttvar: u64,
|
|
1465
|
+
cwnd: usize,
|
|
1466
|
+
sent_bytes: u64,
|
|
1467
|
+
recv_bytes: u64,
|
|
1468
|
+
lost_bytes: u64,
|
|
1469
|
+
stream_retrans_bytes: u64,
|
|
1470
|
+
pmtu: usize,
|
|
1471
|
+
delivery_rate: u64,
|
|
1472
|
+
max_bandwidth: u64,
|
|
1473
|
+
startup_exit_cwnd: u64,
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
#[no_mangle]
|
|
1477
|
+
pub extern "C" fn quiche_conn_path_stats(
|
|
1478
|
+
conn: &Connection, idx: usize, out: &mut PathStats,
|
|
1479
|
+
) -> c_int {
|
|
1480
|
+
let stats = match conn.path_stats().nth(idx) {
|
|
1481
|
+
Some(p) => p,
|
|
1482
|
+
None => return Error::Done.to_c() as c_int,
|
|
1483
|
+
};
|
|
1484
|
+
|
|
1485
|
+
out.local_addr_len = std_addr_to_c(&stats.local_addr, &mut out.local_addr);
|
|
1486
|
+
out.peer_addr_len = std_addr_to_c(&stats.peer_addr, &mut out.peer_addr);
|
|
1487
|
+
out.validation_state = stats.validation_state.to_c();
|
|
1488
|
+
out.active = stats.active;
|
|
1489
|
+
out.recv = stats.recv;
|
|
1490
|
+
out.sent = stats.sent;
|
|
1491
|
+
out.lost = stats.lost;
|
|
1492
|
+
out.retrans = stats.retrans;
|
|
1493
|
+
out.total_pto_count = stats.total_pto_count;
|
|
1494
|
+
out.dgram_recv = stats.dgram_recv;
|
|
1495
|
+
out.dgram_sent = stats.dgram_sent;
|
|
1496
|
+
out.rtt = stats.rtt.as_nanos() as u64;
|
|
1497
|
+
out.min_rtt = stats.min_rtt.unwrap_or_default().as_nanos() as u64;
|
|
1498
|
+
out.rttvar = stats.rttvar.as_nanos() as u64;
|
|
1499
|
+
out.cwnd = stats.cwnd;
|
|
1500
|
+
out.sent_bytes = stats.sent_bytes;
|
|
1501
|
+
out.recv_bytes = stats.recv_bytes;
|
|
1502
|
+
out.lost_bytes = stats.lost_bytes;
|
|
1503
|
+
out.stream_retrans_bytes = stats.stream_retrans_bytes;
|
|
1504
|
+
out.pmtu = stats.pmtu;
|
|
1505
|
+
out.delivery_rate = stats.delivery_rate;
|
|
1506
|
+
out.max_bandwidth = stats.max_bandwidth.unwrap_or(0);
|
|
1507
|
+
out.startup_exit_cwnd =
|
|
1508
|
+
stats.startup_exit.map(|s| s.cwnd as u64).unwrap_or(0);
|
|
1509
|
+
|
|
1510
|
+
0
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
#[no_mangle]
|
|
1514
|
+
pub extern "C" fn quiche_conn_is_server(conn: &Connection) -> bool {
|
|
1515
|
+
conn.is_server()
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
#[no_mangle]
|
|
1519
|
+
pub extern "C" fn quiche_conn_dgram_max_writable_len(
|
|
1520
|
+
conn: &Connection,
|
|
1521
|
+
) -> ssize_t {
|
|
1522
|
+
match conn.dgram_max_writable_len() {
|
|
1523
|
+
None => Error::Done.to_c(),
|
|
1524
|
+
|
|
1525
|
+
Some(v) => v as ssize_t,
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
#[no_mangle]
|
|
1530
|
+
pub extern "C" fn quiche_conn_dgram_recv_front_len(conn: &Connection) -> ssize_t {
|
|
1531
|
+
match conn.dgram_recv_front_len() {
|
|
1532
|
+
None => Error::Done.to_c(),
|
|
1533
|
+
|
|
1534
|
+
Some(v) => v as ssize_t,
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
#[no_mangle]
|
|
1539
|
+
pub extern "C" fn quiche_conn_dgram_recv_queue_len(conn: &Connection) -> ssize_t {
|
|
1540
|
+
conn.dgram_recv_queue_len() as ssize_t
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
#[no_mangle]
|
|
1544
|
+
pub extern "C" fn quiche_conn_dgram_recv_queue_byte_size(
|
|
1545
|
+
conn: &Connection,
|
|
1546
|
+
) -> ssize_t {
|
|
1547
|
+
conn.dgram_recv_queue_byte_size() as ssize_t
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
#[no_mangle]
|
|
1551
|
+
pub extern "C" fn quiche_conn_dgram_send_queue_len(conn: &Connection) -> ssize_t {
|
|
1552
|
+
conn.dgram_send_queue_len() as ssize_t
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
#[no_mangle]
|
|
1556
|
+
pub extern "C" fn quiche_conn_dgram_send_queue_byte_size(
|
|
1557
|
+
conn: &Connection,
|
|
1558
|
+
) -> ssize_t {
|
|
1559
|
+
conn.dgram_send_queue_byte_size() as ssize_t
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
#[no_mangle]
|
|
1563
|
+
pub extern "C" fn quiche_conn_dgram_send(
|
|
1564
|
+
conn: &mut Connection, buf: *const u8, buf_len: size_t,
|
|
1565
|
+
) -> ssize_t {
|
|
1566
|
+
if buf_len > <ssize_t>::MAX as usize {
|
|
1567
|
+
panic!("The provided buffer is too large");
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
let buf = unsafe { slice::from_raw_parts(buf, buf_len) };
|
|
1571
|
+
|
|
1572
|
+
match conn.dgram_send(buf) {
|
|
1573
|
+
Ok(_) => buf_len as ssize_t,
|
|
1574
|
+
|
|
1575
|
+
Err(e) => e.to_c(),
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
#[no_mangle]
|
|
1580
|
+
pub extern "C" fn quiche_conn_dgram_recv(
|
|
1581
|
+
conn: &mut Connection, out: *mut u8, out_len: size_t,
|
|
1582
|
+
) -> ssize_t {
|
|
1583
|
+
if out_len > <ssize_t>::MAX as usize {
|
|
1584
|
+
panic!("The provided buffer is too large");
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
let out = unsafe { slice::from_raw_parts_mut(out, out_len) };
|
|
1588
|
+
|
|
1589
|
+
let out_len = match conn.dgram_recv(out) {
|
|
1590
|
+
Ok(v) => v,
|
|
1591
|
+
|
|
1592
|
+
Err(e) => return e.to_c(),
|
|
1593
|
+
};
|
|
1594
|
+
|
|
1595
|
+
out_len as ssize_t
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
#[no_mangle]
|
|
1599
|
+
pub extern "C" fn quiche_conn_dgram_purge_outgoing(
|
|
1600
|
+
conn: &mut Connection, f: extern "C" fn(*const u8, size_t) -> bool,
|
|
1601
|
+
) {
|
|
1602
|
+
conn.dgram_purge_outgoing(|d: &[u8]| -> bool {
|
|
1603
|
+
let ptr: *const u8 = d.as_ptr();
|
|
1604
|
+
let len: size_t = d.len();
|
|
1605
|
+
|
|
1606
|
+
f(ptr, len)
|
|
1607
|
+
});
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
#[no_mangle]
|
|
1611
|
+
pub extern "C" fn quiche_conn_is_dgram_send_queue_full(
|
|
1612
|
+
conn: &Connection,
|
|
1613
|
+
) -> bool {
|
|
1614
|
+
conn.is_dgram_send_queue_full()
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
#[no_mangle]
|
|
1618
|
+
pub extern "C" fn quiche_conn_is_dgram_recv_queue_full(
|
|
1619
|
+
conn: &Connection,
|
|
1620
|
+
) -> bool {
|
|
1621
|
+
conn.is_dgram_recv_queue_full()
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
#[no_mangle]
|
|
1625
|
+
pub extern "C" fn quiche_conn_send_ack_eliciting(
|
|
1626
|
+
conn: &mut Connection,
|
|
1627
|
+
) -> ssize_t {
|
|
1628
|
+
match conn.send_ack_eliciting() {
|
|
1629
|
+
Ok(()) => 0,
|
|
1630
|
+
Err(e) => e.to_c(),
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
#[no_mangle]
|
|
1635
|
+
pub extern "C" fn quiche_conn_send_ack_eliciting_on_path(
|
|
1636
|
+
conn: &mut Connection, local: &sockaddr, local_len: socklen_t,
|
|
1637
|
+
peer: &sockaddr, peer_len: socklen_t,
|
|
1638
|
+
) -> ssize_t {
|
|
1639
|
+
let local = std_addr_from_c(local, local_len);
|
|
1640
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
1641
|
+
match conn.send_ack_eliciting_on_path(local, peer) {
|
|
1642
|
+
Ok(()) => 0,
|
|
1643
|
+
Err(e) => e.to_c(),
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
#[no_mangle]
|
|
1648
|
+
pub extern "C" fn quiche_conn_free(conn: *mut Connection) {
|
|
1649
|
+
if !conn.is_null() {
|
|
1650
|
+
drop(unsafe { Box::from_raw(conn) });
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
#[no_mangle]
|
|
1655
|
+
pub extern "C" fn quiche_conn_peer_streams_left_bidi(conn: &Connection) -> u64 {
|
|
1656
|
+
conn.peer_streams_left_bidi()
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
#[no_mangle]
|
|
1660
|
+
pub extern "C" fn quiche_conn_peer_streams_left_uni(conn: &Connection) -> u64 {
|
|
1661
|
+
conn.peer_streams_left_uni()
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
#[no_mangle]
|
|
1665
|
+
pub extern "C" fn quiche_conn_send_quantum(conn: &Connection) -> size_t {
|
|
1666
|
+
conn.send_quantum() as size_t
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
#[no_mangle]
|
|
1670
|
+
pub extern "C" fn quiche_conn_active_scids(conn: &Connection) -> size_t {
|
|
1671
|
+
conn.active_scids() as size_t
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
#[no_mangle]
|
|
1675
|
+
pub extern "C" fn quiche_conn_scids_left(conn: &Connection) -> size_t {
|
|
1676
|
+
conn.scids_left() as size_t
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
#[no_mangle]
|
|
1680
|
+
pub extern "C" fn quiche_conn_new_scid(
|
|
1681
|
+
conn: &mut Connection, scid: *const u8, scid_len: size_t,
|
|
1682
|
+
reset_token: *const u8, retire_if_needed: bool, scid_seq: *mut u64,
|
|
1683
|
+
) -> c_int {
|
|
1684
|
+
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };
|
|
1685
|
+
let scid = ConnectionId::from_ref(scid);
|
|
1686
|
+
|
|
1687
|
+
let reset_token = unsafe { slice::from_raw_parts(reset_token, 16) };
|
|
1688
|
+
let reset_token = match reset_token.try_into() {
|
|
1689
|
+
Ok(rt) => rt,
|
|
1690
|
+
Err(_) => unreachable!(),
|
|
1691
|
+
};
|
|
1692
|
+
let reset_token = u128::from_be_bytes(reset_token);
|
|
1693
|
+
|
|
1694
|
+
match conn.new_scid(&scid, reset_token, retire_if_needed) {
|
|
1695
|
+
Ok(c) => {
|
|
1696
|
+
unsafe { *scid_seq = c }
|
|
1697
|
+
0
|
|
1698
|
+
},
|
|
1699
|
+
Err(e) => e.to_c() as c_int,
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
#[no_mangle]
|
|
1704
|
+
pub extern "C" fn quiche_conn_retire_dcid(
|
|
1705
|
+
conn: &mut Connection, dcid_seq: u64,
|
|
1706
|
+
) -> c_int {
|
|
1707
|
+
match conn.retire_dcid(dcid_seq) {
|
|
1708
|
+
Ok(_) => 0,
|
|
1709
|
+
Err(e) => e.to_c() as c_int,
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
#[no_mangle]
|
|
1714
|
+
pub extern "C" fn quiche_conn_available_dcids(conn: &Connection) -> size_t {
|
|
1715
|
+
conn.available_dcids() as size_t
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
#[no_mangle]
|
|
1719
|
+
pub extern "C" fn quiche_conn_retired_scids(conn: &Connection) -> size_t {
|
|
1720
|
+
conn.retired_scids() as size_t
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
#[no_mangle]
|
|
1724
|
+
pub extern "C" fn quiche_conn_retired_scid_next(
|
|
1725
|
+
conn: &mut Connection, out: &mut *const u8, out_len: &mut size_t,
|
|
1726
|
+
) -> bool {
|
|
1727
|
+
match conn.retired_scid_next() {
|
|
1728
|
+
None => false,
|
|
1729
|
+
|
|
1730
|
+
Some(conn_id) => {
|
|
1731
|
+
let id = conn_id.as_ref();
|
|
1732
|
+
*out = id.as_ptr();
|
|
1733
|
+
*out_len = id.len();
|
|
1734
|
+
true
|
|
1735
|
+
},
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
#[no_mangle]
|
|
1740
|
+
pub extern "C" fn quiche_conn_send_quantum_on_path(
|
|
1741
|
+
conn: &Connection, local: &sockaddr, local_len: socklen_t, peer: &sockaddr,
|
|
1742
|
+
peer_len: socklen_t,
|
|
1743
|
+
) -> size_t {
|
|
1744
|
+
let local = std_addr_from_c(local, local_len);
|
|
1745
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
1746
|
+
|
|
1747
|
+
conn.send_quantum_on_path(local, peer) as size_t
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
#[no_mangle]
|
|
1751
|
+
pub extern "C" fn quiche_conn_paths_iter(
|
|
1752
|
+
conn: &Connection, from: &sockaddr, from_len: socklen_t,
|
|
1753
|
+
) -> *mut SocketAddrIter {
|
|
1754
|
+
let addr = std_addr_from_c(from, from_len);
|
|
1755
|
+
|
|
1756
|
+
Box::into_raw(Box::new(conn.paths_iter(addr)))
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
#[no_mangle]
|
|
1760
|
+
pub extern "C" fn quiche_socket_addr_iter_next(
|
|
1761
|
+
iter: &mut SocketAddrIter, peer: &mut sockaddr_storage,
|
|
1762
|
+
peer_len: *mut socklen_t,
|
|
1763
|
+
) -> bool {
|
|
1764
|
+
if let Some(v) = iter.next() {
|
|
1765
|
+
unsafe { *peer_len = std_addr_to_c(&v, peer) }
|
|
1766
|
+
return true;
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
false
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
#[no_mangle]
|
|
1773
|
+
pub extern "C" fn quiche_socket_addr_iter_free(iter: *mut SocketAddrIter) {
|
|
1774
|
+
if !iter.is_null() {
|
|
1775
|
+
drop(unsafe { Box::from_raw(iter) });
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
#[no_mangle]
|
|
1780
|
+
pub extern "C" fn quiche_conn_is_path_validated(
|
|
1781
|
+
conn: &Connection, from: &sockaddr, from_len: socklen_t, to: &sockaddr,
|
|
1782
|
+
to_len: socklen_t,
|
|
1783
|
+
) -> c_int {
|
|
1784
|
+
let from = std_addr_from_c(from, from_len);
|
|
1785
|
+
let to = std_addr_from_c(to, to_len);
|
|
1786
|
+
match conn.is_path_validated(from, to) {
|
|
1787
|
+
Ok(v) => v as c_int,
|
|
1788
|
+
Err(e) => e.to_c() as c_int,
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
#[no_mangle]
|
|
1793
|
+
pub extern "C" fn quiche_conn_probe_path(
|
|
1794
|
+
conn: &mut Connection, local: &sockaddr, local_len: socklen_t,
|
|
1795
|
+
peer: &sockaddr, peer_len: socklen_t, seq: *mut u64,
|
|
1796
|
+
) -> c_int {
|
|
1797
|
+
let local = std_addr_from_c(local, local_len);
|
|
1798
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
1799
|
+
match conn.probe_path(local, peer) {
|
|
1800
|
+
Ok(v) => {
|
|
1801
|
+
unsafe { *seq = v }
|
|
1802
|
+
0
|
|
1803
|
+
},
|
|
1804
|
+
Err(e) => e.to_c() as c_int,
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
#[no_mangle]
|
|
1809
|
+
pub extern "C" fn quiche_conn_migrate_source(
|
|
1810
|
+
conn: &mut Connection, local: &sockaddr, local_len: socklen_t, seq: *mut u64,
|
|
1811
|
+
) -> c_int {
|
|
1812
|
+
let local = std_addr_from_c(local, local_len);
|
|
1813
|
+
match conn.migrate_source(local) {
|
|
1814
|
+
Ok(v) => {
|
|
1815
|
+
unsafe { *seq = v }
|
|
1816
|
+
0
|
|
1817
|
+
},
|
|
1818
|
+
Err(e) => e.to_c() as c_int,
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
#[no_mangle]
|
|
1823
|
+
pub extern "C" fn quiche_conn_migrate(
|
|
1824
|
+
conn: &mut Connection, local: &sockaddr, local_len: socklen_t,
|
|
1825
|
+
peer: &sockaddr, peer_len: socklen_t, seq: *mut u64,
|
|
1826
|
+
) -> c_int {
|
|
1827
|
+
let local = std_addr_from_c(local, local_len);
|
|
1828
|
+
let peer = std_addr_from_c(peer, peer_len);
|
|
1829
|
+
match conn.migrate(local, peer) {
|
|
1830
|
+
Ok(v) => {
|
|
1831
|
+
unsafe { *seq = v }
|
|
1832
|
+
0
|
|
1833
|
+
},
|
|
1834
|
+
Err(e) => e.to_c() as c_int,
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
#[no_mangle]
|
|
1839
|
+
pub extern "C" fn quiche_conn_path_event_next(
|
|
1840
|
+
conn: &mut Connection,
|
|
1841
|
+
) -> *mut PathEvent {
|
|
1842
|
+
match conn.path_event_next() {
|
|
1843
|
+
Some(v) => Box::into_raw(Box::new(v)),
|
|
1844
|
+
None => ptr::null_mut(),
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
#[no_mangle]
|
|
1849
|
+
pub extern "C" fn quiche_path_event_type(ev: &PathEvent) -> u32 {
|
|
1850
|
+
match ev {
|
|
1851
|
+
PathEvent::New { .. } => 0,
|
|
1852
|
+
|
|
1853
|
+
PathEvent::Validated { .. } => 1,
|
|
1854
|
+
|
|
1855
|
+
PathEvent::FailedValidation { .. } => 2,
|
|
1856
|
+
|
|
1857
|
+
PathEvent::Closed { .. } => 3,
|
|
1858
|
+
|
|
1859
|
+
PathEvent::ReusedSourceConnectionId { .. } => 4,
|
|
1860
|
+
|
|
1861
|
+
PathEvent::PeerMigrated { .. } => 5,
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
#[no_mangle]
|
|
1866
|
+
pub extern "C" fn quiche_path_event_new(
|
|
1867
|
+
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1868
|
+
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1869
|
+
peer_addr_len: &mut socklen_t,
|
|
1870
|
+
) {
|
|
1871
|
+
match ev {
|
|
1872
|
+
PathEvent::New(local, peer) => {
|
|
1873
|
+
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1874
|
+
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1875
|
+
},
|
|
1876
|
+
|
|
1877
|
+
_ => unreachable!(),
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
#[no_mangle]
|
|
1882
|
+
pub extern "C" fn quiche_path_event_validated(
|
|
1883
|
+
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1884
|
+
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1885
|
+
peer_addr_len: &mut socklen_t,
|
|
1886
|
+
) {
|
|
1887
|
+
match ev {
|
|
1888
|
+
PathEvent::Validated(local, peer) => {
|
|
1889
|
+
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1890
|
+
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1891
|
+
},
|
|
1892
|
+
|
|
1893
|
+
_ => unreachable!(),
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
#[no_mangle]
|
|
1898
|
+
pub extern "C" fn quiche_path_event_failed_validation(
|
|
1899
|
+
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1900
|
+
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1901
|
+
peer_addr_len: &mut socklen_t,
|
|
1902
|
+
) {
|
|
1903
|
+
match ev {
|
|
1904
|
+
PathEvent::FailedValidation(local, peer) => {
|
|
1905
|
+
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1906
|
+
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1907
|
+
},
|
|
1908
|
+
|
|
1909
|
+
_ => unreachable!(),
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
|
|
1913
|
+
#[no_mangle]
|
|
1914
|
+
pub extern "C" fn quiche_path_event_closed(
|
|
1915
|
+
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1916
|
+
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1917
|
+
peer_addr_len: &mut socklen_t,
|
|
1918
|
+
) {
|
|
1919
|
+
match ev {
|
|
1920
|
+
PathEvent::Closed(local, peer) => {
|
|
1921
|
+
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1922
|
+
*peer_addr_len = std_addr_to_c(peer, peer_addr)
|
|
1923
|
+
},
|
|
1924
|
+
|
|
1925
|
+
_ => unreachable!(),
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
#[no_mangle]
|
|
1930
|
+
pub extern "C" fn quiche_path_event_reused_source_connection_id(
|
|
1931
|
+
ev: &PathEvent, cid_sequence_number: &mut u64,
|
|
1932
|
+
old_local_addr: &mut sockaddr_storage, old_local_addr_len: &mut socklen_t,
|
|
1933
|
+
old_peer_addr: &mut sockaddr_storage, old_peer_addr_len: &mut socklen_t,
|
|
1934
|
+
local_addr: &mut sockaddr_storage, local_addr_len: &mut socklen_t,
|
|
1935
|
+
peer_addr: &mut sockaddr_storage, peer_addr_len: &mut socklen_t,
|
|
1936
|
+
) {
|
|
1937
|
+
match ev {
|
|
1938
|
+
PathEvent::ReusedSourceConnectionId(id, old, new) => {
|
|
1939
|
+
*cid_sequence_number = *id;
|
|
1940
|
+
*old_local_addr_len = std_addr_to_c(&old.0, old_local_addr);
|
|
1941
|
+
*old_peer_addr_len = std_addr_to_c(&old.1, old_peer_addr);
|
|
1942
|
+
|
|
1943
|
+
*local_addr_len = std_addr_to_c(&new.0, local_addr);
|
|
1944
|
+
*peer_addr_len = std_addr_to_c(&new.1, peer_addr)
|
|
1945
|
+
},
|
|
1946
|
+
|
|
1947
|
+
_ => unreachable!(),
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
#[no_mangle]
|
|
1952
|
+
pub extern "C" fn quiche_path_event_peer_migrated(
|
|
1953
|
+
ev: &PathEvent, local_addr: &mut sockaddr_storage,
|
|
1954
|
+
local_addr_len: &mut socklen_t, peer_addr: &mut sockaddr_storage,
|
|
1955
|
+
peer_addr_len: &mut socklen_t,
|
|
1956
|
+
) {
|
|
1957
|
+
match ev {
|
|
1958
|
+
PathEvent::PeerMigrated(local, peer) => {
|
|
1959
|
+
*local_addr_len = std_addr_to_c(local, local_addr);
|
|
1960
|
+
*peer_addr_len = std_addr_to_c(peer, peer_addr);
|
|
1961
|
+
},
|
|
1962
|
+
|
|
1963
|
+
_ => unreachable!(),
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
#[no_mangle]
|
|
1968
|
+
pub extern "C" fn quiche_path_event_free(ev: *mut PathEvent) {
|
|
1969
|
+
if !ev.is_null() {
|
|
1970
|
+
drop(unsafe { Box::from_raw(ev) });
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
#[no_mangle]
|
|
1975
|
+
pub extern "C" fn quiche_put_varint(
|
|
1976
|
+
buf: *mut u8, buf_len: size_t, val: u64,
|
|
1977
|
+
) -> c_int {
|
|
1978
|
+
let buf = unsafe { slice::from_raw_parts_mut(buf, buf_len) };
|
|
1979
|
+
|
|
1980
|
+
let mut b = octets::OctetsMut::with_slice(buf);
|
|
1981
|
+
match b.put_varint(val) {
|
|
1982
|
+
Ok(_) => 0,
|
|
1983
|
+
|
|
1984
|
+
Err(e) => {
|
|
1985
|
+
let err: Error = e.into();
|
|
1986
|
+
err.to_c() as c_int
|
|
1987
|
+
},
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
#[no_mangle]
|
|
1992
|
+
pub extern "C" fn quiche_get_varint(
|
|
1993
|
+
buf: *const u8, buf_len: size_t, val: *mut u64,
|
|
1994
|
+
) -> ssize_t {
|
|
1995
|
+
let buf = unsafe { slice::from_raw_parts(buf, buf_len) };
|
|
1996
|
+
|
|
1997
|
+
let mut b = octets::Octets::with_slice(buf);
|
|
1998
|
+
match b.get_varint() {
|
|
1999
|
+
Ok(v) => unsafe { *val = v },
|
|
2000
|
+
|
|
2001
|
+
Err(e) => {
|
|
2002
|
+
let err: Error = e.into();
|
|
2003
|
+
return err.to_c();
|
|
2004
|
+
},
|
|
2005
|
+
};
|
|
2006
|
+
|
|
2007
|
+
b.off() as ssize_t
|
|
2008
|
+
}
|
|
2009
|
+
|
|
2010
|
+
fn optional_std_addr_from_c(
|
|
2011
|
+
addr: *const sockaddr, addr_len: socklen_t,
|
|
2012
|
+
) -> Option<SocketAddr> {
|
|
2013
|
+
if addr.is_null() || addr_len == 0 {
|
|
2014
|
+
return None;
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
Some(std_addr_from_c(unsafe { &*addr }, addr_len))
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
fn std_addr_from_c(addr: &sockaddr, addr_len: socklen_t) -> SocketAddr {
|
|
2021
|
+
match addr.sa_family as _ {
|
|
2022
|
+
AF_INET => {
|
|
2023
|
+
assert!(addr_len as usize == size_of::<sockaddr_in>());
|
|
2024
|
+
|
|
2025
|
+
let in4 = unsafe { *(addr as *const _ as *const sockaddr_in) };
|
|
2026
|
+
|
|
2027
|
+
#[cfg(not(windows))]
|
|
2028
|
+
let ip_addr = Ipv4Addr::from(u32::from_be(in4.sin_addr.s_addr));
|
|
2029
|
+
#[cfg(windows)]
|
|
2030
|
+
let ip_addr = {
|
|
2031
|
+
let ip_bytes = unsafe { in4.sin_addr.S_un.S_un_b };
|
|
2032
|
+
|
|
2033
|
+
Ipv4Addr::from([
|
|
2034
|
+
ip_bytes.s_b1,
|
|
2035
|
+
ip_bytes.s_b2,
|
|
2036
|
+
ip_bytes.s_b3,
|
|
2037
|
+
ip_bytes.s_b4,
|
|
2038
|
+
])
|
|
2039
|
+
};
|
|
2040
|
+
|
|
2041
|
+
let port = u16::from_be(in4.sin_port);
|
|
2042
|
+
|
|
2043
|
+
let out = SocketAddrV4::new(ip_addr, port);
|
|
2044
|
+
|
|
2045
|
+
out.into()
|
|
2046
|
+
},
|
|
2047
|
+
|
|
2048
|
+
AF_INET6 => {
|
|
2049
|
+
assert!(addr_len as usize == size_of::<sockaddr_in6>());
|
|
2050
|
+
|
|
2051
|
+
let in6 = unsafe { *(addr as *const _ as *const sockaddr_in6) };
|
|
2052
|
+
|
|
2053
|
+
let ip_addr = Ipv6Addr::from(
|
|
2054
|
+
#[cfg(not(windows))]
|
|
2055
|
+
in6.sin6_addr.s6_addr,
|
|
2056
|
+
#[cfg(windows)]
|
|
2057
|
+
unsafe {
|
|
2058
|
+
in6.sin6_addr.u.Byte
|
|
2059
|
+
},
|
|
2060
|
+
);
|
|
2061
|
+
|
|
2062
|
+
let port = u16::from_be(in6.sin6_port);
|
|
2063
|
+
|
|
2064
|
+
#[cfg(not(windows))]
|
|
2065
|
+
let scope_id = in6.sin6_scope_id;
|
|
2066
|
+
#[cfg(windows)]
|
|
2067
|
+
let scope_id = unsafe { in6.Anonymous.sin6_scope_id };
|
|
2068
|
+
|
|
2069
|
+
let out =
|
|
2070
|
+
SocketAddrV6::new(ip_addr, port, in6.sin6_flowinfo, scope_id);
|
|
2071
|
+
|
|
2072
|
+
out.into()
|
|
2073
|
+
},
|
|
2074
|
+
|
|
2075
|
+
_ => unimplemented!("unsupported address type"),
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
fn std_addr_to_c(addr: &SocketAddr, out: &mut sockaddr_storage) -> socklen_t {
|
|
2080
|
+
let sin_port = addr.port().to_be();
|
|
2081
|
+
|
|
2082
|
+
match addr {
|
|
2083
|
+
SocketAddr::V4(addr) => unsafe {
|
|
2084
|
+
let sa_len = size_of::<sockaddr_in>();
|
|
2085
|
+
let out_in = out as *mut _ as *mut sockaddr_in;
|
|
2086
|
+
|
|
2087
|
+
let s_addr = u32::from_ne_bytes(addr.ip().octets());
|
|
2088
|
+
|
|
2089
|
+
#[cfg(not(windows))]
|
|
2090
|
+
let sin_addr = in_addr { s_addr };
|
|
2091
|
+
#[cfg(windows)]
|
|
2092
|
+
let sin_addr = in_addr {
|
|
2093
|
+
S_un: IN_ADDR_0 { S_addr: s_addr },
|
|
2094
|
+
};
|
|
2095
|
+
|
|
2096
|
+
*out_in = sockaddr_in {
|
|
2097
|
+
sin_family: AF_INET as sa_family_t,
|
|
2098
|
+
|
|
2099
|
+
sin_addr,
|
|
2100
|
+
|
|
2101
|
+
#[cfg(any(
|
|
2102
|
+
target_os = "macos",
|
|
2103
|
+
target_os = "ios",
|
|
2104
|
+
target_os = "watchos",
|
|
2105
|
+
target_os = "freebsd",
|
|
2106
|
+
target_os = "dragonfly",
|
|
2107
|
+
target_os = "openbsd",
|
|
2108
|
+
target_os = "netbsd"
|
|
2109
|
+
))]
|
|
2110
|
+
sin_len: sa_len as u8,
|
|
2111
|
+
|
|
2112
|
+
sin_port,
|
|
2113
|
+
|
|
2114
|
+
sin_zero: std::mem::zeroed(),
|
|
2115
|
+
};
|
|
2116
|
+
|
|
2117
|
+
sa_len as socklen_t
|
|
2118
|
+
},
|
|
2119
|
+
|
|
2120
|
+
SocketAddr::V6(addr) => unsafe {
|
|
2121
|
+
let sa_len = size_of::<sockaddr_in6>();
|
|
2122
|
+
let out_in6 = out as *mut _ as *mut sockaddr_in6;
|
|
2123
|
+
|
|
2124
|
+
#[cfg(not(windows))]
|
|
2125
|
+
let sin6_addr = in6_addr {
|
|
2126
|
+
s6_addr: addr.ip().octets(),
|
|
2127
|
+
};
|
|
2128
|
+
#[cfg(windows)]
|
|
2129
|
+
let sin6_addr = in6_addr {
|
|
2130
|
+
u: IN6_ADDR_0 {
|
|
2131
|
+
Byte: addr.ip().octets(),
|
|
2132
|
+
},
|
|
2133
|
+
};
|
|
2134
|
+
|
|
2135
|
+
*out_in6 = sockaddr_in6 {
|
|
2136
|
+
sin6_family: AF_INET6 as sa_family_t,
|
|
2137
|
+
|
|
2138
|
+
sin6_addr,
|
|
2139
|
+
|
|
2140
|
+
#[cfg(any(
|
|
2141
|
+
target_os = "macos",
|
|
2142
|
+
target_os = "ios",
|
|
2143
|
+
target_os = "watchos",
|
|
2144
|
+
target_os = "freebsd",
|
|
2145
|
+
target_os = "dragonfly",
|
|
2146
|
+
target_os = "openbsd",
|
|
2147
|
+
target_os = "netbsd"
|
|
2148
|
+
))]
|
|
2149
|
+
sin6_len: sa_len as u8,
|
|
2150
|
+
|
|
2151
|
+
sin6_port: sin_port,
|
|
2152
|
+
|
|
2153
|
+
sin6_flowinfo: addr.flowinfo(),
|
|
2154
|
+
|
|
2155
|
+
#[cfg(not(windows))]
|
|
2156
|
+
sin6_scope_id: addr.scope_id(),
|
|
2157
|
+
#[cfg(windows)]
|
|
2158
|
+
Anonymous: SOCKADDR_IN6_0 {
|
|
2159
|
+
sin6_scope_id: addr.scope_id(),
|
|
2160
|
+
},
|
|
2161
|
+
};
|
|
2162
|
+
|
|
2163
|
+
sa_len as socklen_t
|
|
2164
|
+
},
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
|
|
2168
|
+
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "windows")))]
|
|
2169
|
+
fn std_time_to_c(time: &Instant, out: &mut timespec) {
|
|
2170
|
+
const INSTANT_ZERO: Instant =
|
|
2171
|
+
unsafe { std::mem::transmute(std::time::UNIX_EPOCH) };
|
|
2172
|
+
|
|
2173
|
+
let raw_time = time.duration_since(INSTANT_ZERO);
|
|
2174
|
+
|
|
2175
|
+
out.tv_sec = raw_time.as_secs() as libc::time_t;
|
|
2176
|
+
out.tv_nsec = raw_time.subsec_nanos() as libc::c_long;
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "windows"))]
|
|
2180
|
+
fn std_time_to_c(_time: &Instant, out: &mut timespec) {
|
|
2181
|
+
// TODO: implement Instant conversion for systems that don't use timespec.
|
|
2182
|
+
out.tv_sec = 0;
|
|
2183
|
+
out.tv_nsec = 0;
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
#[cfg(test)]
|
|
2187
|
+
mod tests {
|
|
2188
|
+
use super::*;
|
|
2189
|
+
|
|
2190
|
+
use libc::c_void;
|
|
2191
|
+
#[cfg(windows)]
|
|
2192
|
+
use windows_sys::Win32::Networking::WinSock::inet_ntop;
|
|
2193
|
+
|
|
2194
|
+
#[test]
|
|
2195
|
+
fn addr_v4() {
|
|
2196
|
+
let addr = "127.0.0.1:8080".parse().unwrap();
|
|
2197
|
+
|
|
2198
|
+
let mut out: sockaddr_storage = unsafe { std::mem::zeroed() };
|
|
2199
|
+
|
|
2200
|
+
assert_eq!(
|
|
2201
|
+
std_addr_to_c(&addr, &mut out),
|
|
2202
|
+
size_of::<sockaddr_in>() as socklen_t
|
|
2203
|
+
);
|
|
2204
|
+
|
|
2205
|
+
let s = ffi::CString::new("ddd.ddd.ddd.ddd").unwrap();
|
|
2206
|
+
|
|
2207
|
+
let s = unsafe {
|
|
2208
|
+
let in_addr = &out as *const _ as *const sockaddr_in;
|
|
2209
|
+
assert_eq!(u16::from_be((*in_addr).sin_port), addr.port());
|
|
2210
|
+
|
|
2211
|
+
let dst = s.into_raw();
|
|
2212
|
+
|
|
2213
|
+
inet_ntop(
|
|
2214
|
+
AF_INET as _,
|
|
2215
|
+
&((*in_addr).sin_addr) as *const _ as *const c_void,
|
|
2216
|
+
dst as _,
|
|
2217
|
+
16,
|
|
2218
|
+
);
|
|
2219
|
+
|
|
2220
|
+
ffi::CString::from_raw(dst).into_string().unwrap()
|
|
2221
|
+
};
|
|
2222
|
+
|
|
2223
|
+
assert_eq!(s, "127.0.0.1");
|
|
2224
|
+
|
|
2225
|
+
let addr = unsafe {
|
|
2226
|
+
std_addr_from_c(
|
|
2227
|
+
&*(&out as *const _ as *const sockaddr),
|
|
2228
|
+
size_of::<sockaddr_in>() as socklen_t,
|
|
2229
|
+
)
|
|
2230
|
+
};
|
|
2231
|
+
|
|
2232
|
+
assert_eq!(addr, "127.0.0.1:8080".parse().unwrap());
|
|
2233
|
+
}
|
|
2234
|
+
|
|
2235
|
+
#[test]
|
|
2236
|
+
fn addr_v6() {
|
|
2237
|
+
let addr = "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"
|
|
2238
|
+
.parse()
|
|
2239
|
+
.unwrap();
|
|
2240
|
+
|
|
2241
|
+
let mut out: sockaddr_storage = unsafe { std::mem::zeroed() };
|
|
2242
|
+
|
|
2243
|
+
assert_eq!(
|
|
2244
|
+
std_addr_to_c(&addr, &mut out),
|
|
2245
|
+
size_of::<sockaddr_in6>() as socklen_t
|
|
2246
|
+
);
|
|
2247
|
+
|
|
2248
|
+
let s =
|
|
2249
|
+
ffi::CString::new("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd").unwrap();
|
|
2250
|
+
|
|
2251
|
+
let s = unsafe {
|
|
2252
|
+
let in6_addr = &out as *const _ as *const sockaddr_in6;
|
|
2253
|
+
assert_eq!(u16::from_be((*in6_addr).sin6_port), addr.port());
|
|
2254
|
+
|
|
2255
|
+
let dst = s.into_raw();
|
|
2256
|
+
|
|
2257
|
+
inet_ntop(
|
|
2258
|
+
AF_INET6 as _,
|
|
2259
|
+
&((*in6_addr).sin6_addr) as *const _ as *const c_void,
|
|
2260
|
+
dst as _,
|
|
2261
|
+
45,
|
|
2262
|
+
);
|
|
2263
|
+
|
|
2264
|
+
ffi::CString::from_raw(dst).into_string().unwrap()
|
|
2265
|
+
};
|
|
2266
|
+
|
|
2267
|
+
assert_eq!(s, "2001:db8:85a3::8a2e:370:7334");
|
|
2268
|
+
|
|
2269
|
+
let addr = unsafe {
|
|
2270
|
+
std_addr_from_c(
|
|
2271
|
+
&*(&out as *const _ as *const sockaddr),
|
|
2272
|
+
size_of::<sockaddr_in6>() as socklen_t,
|
|
2273
|
+
)
|
|
2274
|
+
};
|
|
2275
|
+
|
|
2276
|
+
assert_eq!(
|
|
2277
|
+
addr,
|
|
2278
|
+
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"
|
|
2279
|
+
.parse()
|
|
2280
|
+
.unwrap()
|
|
2281
|
+
);
|
|
2282
|
+
}
|
|
2283
|
+
|
|
2284
|
+
#[cfg(not(windows))]
|
|
2285
|
+
extern "C" {
|
|
2286
|
+
fn inet_ntop(
|
|
2287
|
+
af: c_int, src: *const c_void, dst: *mut c_char, size: socklen_t,
|
|
2288
|
+
) -> *mut c_char;
|
|
2289
|
+
}
|
|
2290
|
+
}
|