refactor(tvix/nix-compat/wire): move magic bytes to worker_protocol
`primitive.rs` implements reading and writing primitive (fixed-length) types in the wire format, used in the the nix daemon protocol and NAR format. Move worker-protocol specific magic bytes to worker_protocol.rs (and possibly further split there once needed) Change-Id: If681c01e9460294619f1d000229b81f0ac745810 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11377 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: picnoir picnoir <picnoir@alternativebit.fr>
This commit is contained in:
parent
71a3855f09
commit
fd749070e2
3 changed files with 18 additions and 16 deletions
|
@ -4,13 +4,6 @@
|
||||||
|
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||||
|
|
||||||
// LE-encoded nixc on 64 bits. Because why not.
|
|
||||||
pub static MAGIC_HELLO: [u8; 8] = *b"cxin\0\0\0\0";
|
|
||||||
// LE-encoded dxio on 64 bits. What's dxio? I have no clue.
|
|
||||||
pub static MAGIC_HELLO_RESPONSE: [u8; 8] = *b"oixd\0\0\0\0";
|
|
||||||
// LE-encoded protocol version.
|
|
||||||
pub static PROTOCOL_VERSION: [u8; 8] = [0x23, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
/// Read a u64 from the AsyncRead (little endian).
|
/// Read a u64 from the AsyncRead (little endian).
|
||||||
pub async fn read_u64<R: AsyncReadExt + Unpin>(r: &mut R) -> std::io::Result<u64> {
|
pub async fn read_u64<R: AsyncReadExt + Unpin>(r: &mut R) -> std::io::Result<u64> {
|
||||||
|
|
|
@ -11,6 +11,13 @@ use crate::wire::primitive;
|
||||||
|
|
||||||
use super::bytes::read_string;
|
use super::bytes::read_string;
|
||||||
|
|
||||||
|
// LE-encoded nixc on 64 bits. Because why not.
|
||||||
|
pub static MAGIC_HELLO: [u8; 8] = *b"cxin\0\0\0\0";
|
||||||
|
// LE-encoded dxio on 64 bits. What's dxio? I have no clue.
|
||||||
|
pub static MAGIC_HELLO_RESPONSE: [u8; 8] = *b"oixd\0\0\0\0";
|
||||||
|
// LE-encoded protocol version.
|
||||||
|
pub static PROTOCOL_VERSION: [u8; 8] = [0x23, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
|
|
||||||
pub static STDERR_LAST: u64 = 0x616c7473;
|
pub static STDERR_LAST: u64 = 0x616c7473;
|
||||||
|
|
||||||
/// Max length of a Nix setting name/value. In bytes.
|
/// Max length of a Nix setting name/value. In bytes.
|
||||||
|
|
|
@ -127,15 +127,17 @@ where
|
||||||
let mut magic_hello = vec![0; 8];
|
let mut magic_hello = vec![0; 8];
|
||||||
conn.read_exact(&mut magic_hello).await?;
|
conn.read_exact(&mut magic_hello).await?;
|
||||||
debug!("Hello read");
|
debug!("Hello read");
|
||||||
if magic_hello != primitive::MAGIC_HELLO {
|
if magic_hello != worker_protocol::MAGIC_HELLO {
|
||||||
Err(anyhow!(
|
Err(anyhow!(
|
||||||
"Invalid client hello received: {:?}, expected {:?}",
|
"Invalid client hello received: {:?}, expected {:?}",
|
||||||
magic_hello,
|
magic_hello,
|
||||||
primitive::MAGIC_HELLO
|
worker_protocol::MAGIC_HELLO
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
conn.write_all(&primitive::MAGIC_HELLO_RESPONSE).await?;
|
conn.write_all(&worker_protocol::MAGIC_HELLO_RESPONSE[..])
|
||||||
conn.write_all(&primitive::PROTOCOL_VERSION).await?;
|
.await?;
|
||||||
|
conn.write_all(&worker_protocol::PROTOCOL_VERSION[..])
|
||||||
|
.await?;
|
||||||
conn.flush().await?;
|
conn.flush().await?;
|
||||||
debug!("Hello responded");
|
debug!("Hello responded");
|
||||||
let client_version = primitive::read_u64(&mut conn).await?;
|
let client_version = primitive::read_u64(&mut conn).await?;
|
||||||
|
@ -192,16 +194,16 @@ where
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod integration_tests {
|
mod integration_tests {
|
||||||
use nix_compat::wire::primitive;
|
use nix_compat::wire::worker_protocol;
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_init_handshake() {
|
async fn test_init_handshake() {
|
||||||
let mut test_conn = tokio_test::io::Builder::new()
|
let mut test_conn = tokio_test::io::Builder::new()
|
||||||
.read(&primitive::MAGIC_HELLO)
|
.read(&worker_protocol::MAGIC_HELLO)
|
||||||
.write(&primitive::MAGIC_HELLO_RESPONSE)
|
.write(&worker_protocol::MAGIC_HELLO_RESPONSE)
|
||||||
.write(&primitive::PROTOCOL_VERSION)
|
.write(&worker_protocol::PROTOCOL_VERSION)
|
||||||
// Let's say the client is in sync with the daemon
|
// Let's say the client is in sync with the daemon
|
||||||
// protocol-wise
|
// protocol-wise
|
||||||
.read(&primitive::PROTOCOL_VERSION)
|
.read(&worker_protocol::PROTOCOL_VERSION)
|
||||||
// cpu affinity
|
// cpu affinity
|
||||||
.read(&vec![0; 8])
|
.read(&vec![0; 8])
|
||||||
// reservespace
|
// reservespace
|
||||||
|
|
Loading…
Reference in a new issue