Home / Blog / Porting mosh to Rust
Porting mosh to Rust, and how I know it matches
mosh-rs is a Rust port of mosh. Its client talks to the mosh-server already installed on your machines, its server talks to the stock mosh client, and on top of that it has a native Windows client and scrollback, which upstream has refused since 2012. It was built with AI coding agents working from a written plan, and the full record is in the repository. This post skips the features and covers the part that took most of the time: proving the port actually behaves like the C++.
Why port file by file
Mosh's wire format has been frozen since 1.4. There are mosh-servers on hosts I
do not control and will never upgrade, and a "better mosh" that only talks to
itself would be useless to me. So the rule from day one was to port file by
file and keep the names, the constants, and the order of operations.
terminalframebuffer.cc became terminalframebuffer.rs.
Every Rust file names the C++ file it came from. If you put them side by side
you can follow along.
The cost is that the code is not idiomatic in places. Some functions take eight parameters because the C++ versions take eight. I accepted that, because being able to review the code against upstream mattered more than style.
Oracles instead of unit tests
A port has one correct answer: whatever the original does. So in place of unit
tests for individual functions, there is a directory of small C++ programs
linked against upstream's own build. One dumps the emulator screen
after feeding it bytes. One dumps the new_frame output, the escape
sequences mosh writes to your real terminal. One encrypts and decrypts packets.
The Rust side has to produce byte-identical output for the same input, and a
test fails on the first byte that differs.
The inputs are 27 recorded sessions (bash, vim, less, htop, tmux, a TUI, wide characters, emoji, a resize) plus the inputs from upstream's own emulation tests, replayed at 80x24 and 132x43. By the end, 27 files matched cell for cell and 3024 screen redraws matched byte for byte, 756 of them across a resize.
Then differential fuzzing. A generator produces random escape sequences, both
emulators process them, and any mismatch is minimized to the shortest input that
reproduces it. There was one run of 1,000,000 cases, four more of 200,000, and
200,000 cases of random screen pairs through new_frame. By the end
there were zero mismatches. There were plenty before that, which is what the
tool is for.
Crypto: do not write crypto
Every mosh packet is AES-128 in OCB3 mode. Upstream ships its own OCB
implementation, and the port deliberately leaves it out. The Rust side uses the RustCrypto
aes and ocb3 crates, with zeroize so the
key schedule is wiped on drop like upstream's ae_clear().
The check is 2000 random packets encrypted by Rust and decrypted by the C++
harness, and the same in the other direction. Both sides reject tampered
packets and 14 malformed keys, and the RFC 7253 vectors pass. This is also
where the minimum Rust version came from: aes 0.9.3 refuses to
build on 1.88, so the workspace says 1.89 and CI checks it.
Where the port fought Rust
Three crates have #![forbid(unsafe_code)]: the terminal emulator,
the network layer, the client and server logic. All the unsafe lives in one
platform crate (tty, pty, fork, sockets, the select loop), and every block has
a comment stating the invariant. This split was the single best decision in the
project. When something crashed, I knew which crate to look in.
Signed and unsigned casts were the most annoying part. The C++ mixes
int, size_t and uint64_t freely, and in
a few places the value can be negative. The rule I settled on was to mirror the
C++ cast exactly and leave a comment wherever the value could be negative or
out of range. It is boring, and it is also the only way to be sure the
behaviour at the edges matches.
The worst bug was in easy code. In 0.1.0 the macOS client could not connect at
all: "Did not find remote IP address". The launcher reads the address family
out of a C sockaddr, and the port read it at the Linux offset, but
on Darwin the struct starts with a length byte. Linux CI was green the whole
time. A Mac test machine found it in five minutes.
The interop matrix
Byte-exact oracles show that the pieces match. They cannot show whether two
implementations hold a session together on a bad network. For that there
is a UDP proxy that drops, delays and reorders packets, and a matrix: C++
client and Rust client, against C++ server and Rust server, under clean, 5%
loss, 30% loss with 200 ms delay and reordering, a two-minute blackout, and a
client address change, each with six screen scenarios. 120 cells, 120 pass.
Upstream's own make check runs with the Rust binaries swapped in:
30 pass, 2 expected failures, same as the C++.
A ported quirk I did not understand
Last week someone on Hacker News complained that mosh does not play well with
ssh ControlMaster. I replied that mosh just runs your ssh, so your
config applies. That was wrong, and the port had copied the reason without my
noticing.
Mosh needs to know which IP address ssh actually connected to. It learns it by
passing ssh a fake ProxyCommand that sits in the middle of the
connection and reports the address. A reused ControlMaster socket never opens
a new connection, so the proxy never runs, so mosh gets nothing. Upstream's fix
in 2012 was to pass -S none and turn ControlMaster off. The mosh-rs
launcher does the same, line for line.
I tested it against my own box with a master socket open and
ssh -v -E log. The default mode opens a full new connection and
authenticates. With --experimental-remote-ip=remote, which asks the
server for the address instead and drops the -S none, the log shows
mux_client_request_session: the socket is reused and there is no
auth. So the answer is one flag plus a ControlMaster entry, and now I know why
the flag exists.
Host myhost
ControlMaster auto
ControlPersist 10m
mosh --experimental-remote-ip=remote myhost
Scrollback, briefly
Upstream's position is that scrollback is impossible in mosh's model, because
the screen is synchronised as a diff and rows that scroll off are gone. The
server-side emulator does know when a row leaves the top of the primary
screen, though. So the mosh-rs server records those rows and sends them in a
new protobuf field that old clients ignore, a few packets per round trip so it
stays behind the live screen under loss. The client writes them into your real
terminal's history, so the mouse wheel and your terminal's search work.
Full-screen programs run in your terminal's own alternate screen and never
touch history. seq 1 50000 arrives exactly, through 30% loss.
Height resizes can lose or repeat a few rows. That is documented but not
fixed.
Numbers
Measured side by side with C++ mosh 1.4.0 in the same harness, idle CPU is 0.00%
for both. After an hour the client is at 4.1 MiB and the server at 2.5 MiB,
against 14.3 and 9.9 for C++ mosh. Static binaries are 0.8 to 1.2 MB, and
seq 1 100000 renders in 110 ms versus 130.
What I would tell someone porting something else
Build the oracle before the port. Fuzz the two implementations against each other. Put all the unsafe in one place. Get a machine with the other OS early. And when a user tells you a ported quirk is a problem, test it before you answer.
The code and test logs are at github.com/Arunjay4213/mosh-rs. Every number above is in the repository's STATUS.md, next to the command that produced it.