feat: add non-blocking NTCP2 sends and Firefox settings sync
check / check (macos-latest) (push) Failing after 26s
check / check (ubuntu-latest) (push) Failing after 27s
check / check (windows-latest) (push) Failing after 30s

- NTCP2: queue outbound data frames and flush them without blocking (send_blocks_nb, send_i2np_nb, flush_nonblocking)
- NTCP2: bound write_all with a deadline and set TCP keepalive and user timeout on peer sockets
- Firefox extension 0.1.3: save settings as a patch, keep unsaved form fields from being overwritten on refresh, add timeouts to router API calls, and honor the interceptTorrents toggle in the content script
This commit is contained in:
nandi committed 2026-11-28 18:39:32 -08:00
1 parent 82aaa9f11e
commit 9aa15f9ae8
18 files changed
+1323 -239

No files matched your search

+135 -6
View File
@@ -45,6 +45,12 @@ extern "C" fn c_tcp_check_connected(fd : Int32) -> Int32 = "moon_i2p_net_tcp_che
///|
extern "C" fn c_tcp_set_nonblocking(fd : Int32, nonblocking : Int32) -> Int32 = "moon_i2p_net_tcp_set_nonblocking"
///|
extern "C" fn c_tcp_set_peer_timeouts(
fd : Int32,
user_timeout_ms : Int32,
) -> Int32 = "moon_i2p_net_tcp_set_peer_timeouts"
///|
#borrow(host)
extern "C" fn c_tcp_listen(
@@ -384,20 +390,32 @@ pub fn TcpSocket::poll_readable(
pub fn TcpSocket::write_all(
self : TcpSocket,
data : Bytes,
timeout_ms~ : Int32 = 5000,
) -> Unit raise Ntcp2NetError {
if self.fd < 0 {
raise Closed
}
let total = data.length()
if total == 0 {
return
}
let budget = if timeout_ms > 0 { timeout_ms.to_uint64() } else { 1UL }
let deadline_ms = unix_time_ms() + budget
let mut sent = 0
while sent < total {
let n = c_tcp_send(self.fd, data, sent, total - sent)
if n == -2 {
let rc = c_tcp_poll_writable(self.fd, 5000)
let now_ms = unix_time_ms()
if now_ms >= deadline_ms {
raise Timeout
}
let rem64 = deadline_ms - now_ms
let rem_ms = if rem64 > 5000UL { 5000 } else { rem64.to_int() }
let rc = c_tcp_poll_writable(self.fd, if rem_ms > 0 { rem_ms } else { 1 })
if rc < 0 {
raise IoError("poll writable failed: \{last_os_error()}")
}
if rc == 0 {
if rc == 0 && unix_time_ms() >= deadline_ms {
raise Timeout
}
continue
@@ -745,6 +763,10 @@ pub struct Ntcp2Conn {
remote_router_info : @common.RouterInfo?
priv frame_buf : Array[Byte]
priv mut expected_frame_len : Int32
priv send_queue : Array[Bytes]
priv mut send_head_off : Int32
priv mut send_queued_bytes : Int32
priv mut send_blocked_since_ms : UInt64
}
///|
@@ -759,6 +781,10 @@ fn make_ntcp2_conn(
remote_router_info,
frame_buf: [],
expected_frame_len: -1,
send_queue: [],
send_head_off: 0,
send_queued_bytes: 0,
send_blocked_since_ms: 0UL,
}
}
@@ -877,6 +903,7 @@ pub fn PendingNtcp2Dial::start(
"connect_nb(\{peer.host}:\{peer.port}) failed: \{last_os_error()}",
)
}
let _ = c_tcp_set_peer_timeouts(fd, 15000)
let sock : TcpSocket = {
fd,
buf: FixedArray::make(4096, b'\x00'),
@@ -933,8 +960,7 @@ pub fn PendingNtcp2Dial::step(
self.sock.close()
raise IoError("non-blocking connect failed: \{msg}")
}
let _ = c_tcp_set_nonblocking(self.sock.fd, 0)
self.sock.write_all(self.m1_bytes) catch {
self.sock.write_all(self.m1_bytes, timeout_ms=250) catch {
e => {
self.sock.close()
raise e
@@ -1002,7 +1028,7 @@ pub fn PendingNtcp2Dial::step(
raise Protocol(e)
}
}
self.sock.write_all(msg3) catch {
self.sock.write_all(msg3, timeout_ms=250) catch {
e => {
self.sock.close()
raise e
@@ -1068,6 +1094,8 @@ pub fn PendingNtcp2Accept::start(
}
}
hs.set_ephemeral(ephem)
let _ = c_tcp_set_nonblocking(sock.fd, 1)
let _ = c_tcp_set_peer_timeouts(sock.fd, 15000)
{
sock,
hs,
@@ -1182,7 +1210,7 @@ pub fn PendingNtcp2Accept::step(
raise Protocol(e)
}
}
self.sock.write_all(msg2) catch {
self.sock.write_all(msg2, timeout_ms=250) catch {
e => {
self.sock.close()
raise e
@@ -1416,12 +1444,113 @@ pub fn Ntcp2Conn::try_recv_blocks(
Some(blocks)
}
///|
/// Flushes queued outbound NTCP2 frames without blocking. Returns `true` if any bytes were written.
/// Raises `Timeout` if outbound socket stays un-writable with queued data for >10s.
pub fn Ntcp2Conn::flush_nonblocking(
self : Ntcp2Conn,
) -> Bool raise Ntcp2NetError {
if self.send_queue.length() == 0 {
self.send_head_off = 0
self.send_queued_bytes = 0
self.send_blocked_since_ms = 0UL
return false
}
if self.socket.fd < 0 {
raise Closed
}
let mut wrote_any = false
while self.send_queue.length() > 0 {
let head = self.send_queue[0]
let rem = head.length() - self.send_head_off
if rem <= 0 {
let _ = self.send_queue.remove(0)
self.send_head_off = 0
continue
}
let n = c_tcp_send(self.socket.fd, head, self.send_head_off, rem)
if n == -2 {
break
}
if n < 0 {
raise IoError("non-blocking send failed: \{last_os_error()}")
}
if n == 0 {
raise Closed
}
wrote_any = true
self.send_head_off += n
self.send_queued_bytes -= n
if self.send_head_off >= head.length() {
let _ = self.send_queue.remove(0)
self.send_head_off = 0
}
}
let now_ms = unix_time_ms()
if self.send_queue.length() == 0 {
self.send_blocked_since_ms = 0UL
} else if wrote_any {
self.send_blocked_since_ms = now_ms
} else if self.send_blocked_since_ms == 0UL {
self.send_blocked_since_ms = now_ms
} else if now_ms > self.send_blocked_since_ms + 10000UL {
raise Timeout
}
wrote_any
}
///|
/// Encrypts and queues a data-phase frame for non-blocking transmission.
pub fn Ntcp2Conn::send_blocks_nb(
self : Ntcp2Conn,
blocks : Array[@ntcp2.Ntcp2Block],
) -> Unit raise Ntcp2NetError {
if self.send_queued_bytes > 262144 {
raise IoError("NTCP2 outbound send queue full")
}
let frame = self.session.encrypt_frame(blocks) catch {
e => raise Protocol(e)
}
self.send_queue.push(frame)
self.send_queued_bytes += frame.length()
let _ = self.flush_nonblocking()
}
///|
/// Encrypts and queues a single I2NP message for non-blocking NTCP2 transmission.
pub fn Ntcp2Conn::send_i2np_nb(
self : Ntcp2Conn,
msg_id : UInt32,
expiration_sec : UInt32,
payload : @i2np.I2npPayload,
) -> Unit raise Ntcp2NetError {
let blk = @ntcp2.i2np_block(msg_id, expiration_sec, payload) catch {
e => raise Protocol(e)
}
self.send_blocks_nb([blk])
}
///|
/// Encrypts and sends a data-phase frame containing `blocks`.
pub fn Ntcp2Conn::send_blocks(
self : Ntcp2Conn,
blocks : Array[@ntcp2.Ntcp2Block],
) -> Unit raise Ntcp2NetError {
if self.send_queue.length() > 0 {
let head = self.send_queue[0]
if self.send_head_off > 0 && self.send_head_off < head.length() {
self.socket.write_all(head[self.send_head_off:].to_owned())
} else if self.send_head_off == 0 {
self.socket.write_all(head)
}
let _ = self.send_queue.remove(0)
self.send_head_off = 0
while self.send_queue.length() > 0 {
self.socket.write_all(self.send_queue.remove(0))
}
self.send_queued_bytes = 0
self.send_blocked_since_ms = 0UL
}
let frame = self.session.encrypt_frame(blocks) catch {
e => raise Protocol(e)
}