Skip to main content

Crate openvpn_mgmt_frame

Crate openvpn_mgmt_frame 

Source
Expand description

Low-level line framing for the OpenVPN management protocol.

This crate provides:

The decoder does not track which command was sent — it classifies each line purely from its content. Multi-line response accumulation (grouping Line/End sequences) is left to higher layers.

>CLIENT:ENV accumulation is handled here because the protocol guarantees atomicity for that block and the individual ENV lines are not meaningful on their own.

§Decoding frames

use bytes::BytesMut;
use tokio_util::codec::Decoder;
use openvpn_mgmt_frame::{Frame, FrameDecoder};

let mut decoder = FrameDecoder::new();
let mut buf = BytesMut::from("SUCCESS: pid=1234\n>HOLD:Waiting for hold release:0\n");

assert_eq!(
    decoder.decode(&mut buf).unwrap(),
    Some(Frame::Success("pid=1234".to_string())),
);
assert_eq!(
    decoder.decode(&mut buf).unwrap(),
    Some(Frame::Notification {
        kind: "HOLD".to_string(),
        payload: "Waiting for hold release:0".to_string(),
    }),
);
assert_eq!(decoder.decode(&mut buf).unwrap(), None); // buffer drained

§Encoding commands

use bytes::BytesMut;
use openvpn_mgmt_frame::{write_line, write_block, escape, quote, EncoderMode};

let mut buf = BytesMut::new();
write_line(&mut buf, "status 3");
assert_eq!(&buf[..], b"status 3\n");

buf.clear();
write_block(&mut buf, "client-auth 1 2", &["push-reply".to_string()], EncoderMode::Sanitize).unwrap();
assert_eq!(&buf[..], b"client-auth 1 2\npush-reply\nEND\n");

Structs§

FrameDecoder
A low-level decoder that splits the byte stream into Frame values.

Enums§

AccumulationLimit
Controls how many items the decoder will accumulate before returning an error.
EncodeError
Structured error for encoder-side validation failures.
EncoderMode
Controls how the encoder handles characters that are unsafe for the line-oriented management protocol (\n, \r, \0).
Frame
A classified line (or accumulated block) from the OpenVPN management interface.

Functions§

escape
Backslash-escape \ and " per the OpenVPN config-file lexer rules.
quote
Wrap an already-escaped string in double quotes.
wire_safe
Ensure a string is safe for the wire protocol.
write_block
Write a multi-line block: header line, body lines, and a terminating END.
write_line
Write a single line followed by \n.