#![allow(dead_code)]
#![allow(non_camel_case_types)]
use std::io::{Read, Write};
use byteorder::{ReadBytesExt, WriteBytesExt};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::cast::FromPrimitive;
use crate::xdr::*;
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive)]
#[repr(u32)]
pub enum _msg_type {
CALL = 0,
REPLY = 1,
}
xdr_enum_serde!(_msg_type);
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive)]
#[repr(u32)]
pub enum _reply_stat {
MSG_ACCEPTED = 0,
MSG_DENIED = 1,
}
xdr_enum_serde!(_reply_stat);
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive)]
#[repr(u32)]
pub enum _accept_stat {
SUCCESS = 0,
PROG_UNAVAIL = 1,
PROG_MISMATCH = 2,
PROC_UNAVAIL = 3,
GARBAGE_ARGS = 4,
}
xdr_enum_serde!(_accept_stat);
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive)]
#[repr(u32)]
pub enum _reject_stat {
RPC_MISMATCH = 0,
AUTH_ERROR = 1,
}
xdr_enum_serde!(_reject_stat);
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, Default, FromPrimitive, ToPrimitive)]
#[repr(u32)]
pub enum auth_stat {
#[default]
AUTH_BADCRED = 1,
AUTH_REJECTEDCRED = 2,
AUTH_BADVERF = 3,
AUTH_REJECTEDVERF = 4,
AUTH_TOOWEAK = 5,
}
xdr_enum_serde!(auth_stat);
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive)]
#[repr(u32)]
#[non_exhaustive]
pub enum auth_flavor {
AUTH_NULL = 0,
AUTH_UNIX = 1,
AUTH_SHORT = 2,
AUTH_DES = 3,
}
xdr_enum_serde!(auth_flavor);
#[allow(non_camel_case_types)]
#[derive(Clone, Debug, Default)]
pub struct auth_unix {
stamp: u32,
machinename: Vec<u8>,
uid: u32,
gid: u32,
gids: Vec<u32>,
}
xdr_struct!(auth_unix, stamp, machinename, uid, gid, gids);
#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
pub struct opaque_auth {
pub flavor: auth_flavor,
pub body: Vec<u8>,
}
xdr_struct!(opaque_auth, flavor, body);
impl Default for opaque_auth {
fn default() -> opaque_auth {
opaque_auth {
flavor: auth_flavor::AUTH_NULL,
body: Vec::new(),
}
}
}
#[allow(non_camel_case_types)]
#[derive(Clone, Debug, Default)]
pub struct rpc_msg {
pub xid: u32,
pub body: rpc_body,
}
xdr_struct!(rpc_msg, xid, body);
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
#[derive(Clone, Debug)]
#[repr(u32)]
pub enum rpc_body {
CALL(call_body),
REPLY(reply_body),
}
impl Default for rpc_body {
fn default() -> rpc_body {
rpc_body::CALL(call_body::default())
}
}
impl XDR for rpc_body {
fn serialize<R: Write>(&self, dest: &mut R) -> std::io::Result<()> {
match self {
rpc_body::CALL(v) => {
0_u32.serialize(dest)?;
v.serialize(dest)?;
},
rpc_body::REPLY(v) => {
1_u32.serialize(dest)?;
v.serialize(dest)?;
},
}
Ok(())
}
fn deserialize<R: Read>(&mut self, src: &mut R) -> std::io::Result<()> {
let mut c: u32 = 0;
c.deserialize(src)?;
if c == 0 {
let mut r = call_body::default();
r.deserialize(src)?;
*self = rpc_body::CALL(r);
} else if c == 1 {
let mut r = reply_body::default();
r.deserialize(src)?;
*self = rpc_body::REPLY(r);
}
Ok(())
}
}
#[allow(non_camel_case_types)]
#[derive(Clone, Debug, Default)]
pub struct call_body {
pub rpcvers: u32,
pub prog: u32,
pub vers: u32,
pub proc: u32,
pub cred: opaque_auth,
pub verf: opaque_auth,
}
xdr_struct!(call_body, rpcvers, prog, vers, proc, cred, verf);
#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
#[repr(u32)]
pub enum reply_body {
MSG_ACCEPTED(accepted_reply),
MSG_DENIED(rejected_reply),
}
impl Default for reply_body {
fn default() -> reply_body {
reply_body::MSG_ACCEPTED(accepted_reply::default())
}
}
impl XDR for reply_body {
fn serialize<R: Write>(&self, dest: &mut R) -> std::io::Result<()> {
match self {
reply_body::MSG_ACCEPTED(v) => {
0_u32.serialize(dest)?;
v.serialize(dest)?;
},
reply_body::MSG_DENIED(v) => {
1_u32.serialize(dest)?;
v.serialize(dest)?;
},
}
Ok(())
}
fn deserialize<R: Read>(&mut self, src: &mut R) -> std::io::Result<()> {
let mut c: u32 = 0;
c.deserialize(src)?;
if c == 0 {
let mut r = accepted_reply::default();
r.deserialize(src)?;
*self = reply_body::MSG_ACCEPTED(r);
} else if c == 1 {
let mut r = rejected_reply::default();
r.deserialize(src)?;
*self = reply_body::MSG_DENIED(r);
}
Ok(())
}
}
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, Default)]
#[repr(C)]
pub struct mismatch_info {
pub low: u32,
pub high: u32,
}
xdr_struct!(mismatch_info, low, high);
#[allow(non_camel_case_types)]
#[derive(Clone, Debug, Default)]
pub struct accepted_reply {
pub verf: opaque_auth,
pub reply_data: accept_body,
}
xdr_struct!(accepted_reply, verf, reply_data);
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
#[derive(Copy, Clone, Debug, Default)]
#[repr(u32)]
pub enum accept_body {
#[default]
SUCCESS,
PROG_UNAVAIL,
PROG_MISMATCH(mismatch_info),
PROC_UNAVAIL,
GARBAGE_ARGS,
}
impl XDR for accept_body {
fn serialize<R: Write>(&self, dest: &mut R) -> std::io::Result<()> {
match self {
accept_body::SUCCESS => {
0_u32.serialize(dest)?;
},
accept_body::PROG_UNAVAIL => {
1_u32.serialize(dest)?;
},
accept_body::PROG_MISMATCH(v) => {
2_u32.serialize(dest)?;
v.serialize(dest)?;
},
accept_body::PROC_UNAVAIL => {
3_u32.serialize(dest)?;
},
accept_body::GARBAGE_ARGS => {
4_u32.serialize(dest)?;
},
}
Ok(())
}
fn deserialize<R: Read>(&mut self, src: &mut R) -> std::io::Result<()> {
let mut c: u32 = 0;
c.deserialize(src)?;
if c == 0 {
*self = accept_body::SUCCESS;
} else if c == 1 {
*self = accept_body::PROG_UNAVAIL;
} else if c == 2 {
let mut r = mismatch_info::default();
r.deserialize(src)?;
*self = accept_body::PROG_MISMATCH(r);
} else if c == 3 {
*self = accept_body::PROC_UNAVAIL;
} else {
*self = accept_body::GARBAGE_ARGS;
}
Ok(())
}
}
#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
#[repr(u32)]
pub enum rejected_reply {
RPC_MISMATCH(mismatch_info),
AUTH_ERROR(auth_stat),
}
impl Default for rejected_reply {
fn default() -> rejected_reply {
rejected_reply::RPC_MISMATCH(mismatch_info::default())
}
}
impl XDR for rejected_reply {
fn serialize<R: Write>(&self, dest: &mut R) -> std::io::Result<()> {
match self {
rejected_reply::RPC_MISMATCH(v) => {
0_u32.serialize(dest)?;
v.serialize(dest)?;
},
rejected_reply::AUTH_ERROR(v) => {
1_u32.serialize(dest)?;
v.serialize(dest)?;
},
}
Ok(())
}
fn deserialize<R: Read>(&mut self, src: &mut R) -> std::io::Result<()> {
let mut c: u32 = 0;
c.deserialize(src)?;
if c == 0 {
let mut r = mismatch_info::default();
r.deserialize(src)?;
*self = rejected_reply::RPC_MISMATCH(r);
} else if c == 1 {
let mut r = auth_stat::default();
r.deserialize(src)?;
*self = rejected_reply::AUTH_ERROR(r);
}
Ok(())
}
}
pub fn proc_unavail_reply_message(xid: u32) -> rpc_msg {
let reply = reply_body::MSG_ACCEPTED(accepted_reply {
verf: opaque_auth::default(),
reply_data: accept_body::PROC_UNAVAIL,
});
rpc_msg {
xid,
body: rpc_body::REPLY(reply),
}
}
pub fn prog_unavail_reply_message(xid: u32) -> rpc_msg {
let reply = reply_body::MSG_ACCEPTED(accepted_reply {
verf: opaque_auth::default(),
reply_data: accept_body::PROG_UNAVAIL,
});
rpc_msg {
xid,
body: rpc_body::REPLY(reply),
}
}
pub fn prog_mismatch_reply_message(xid: u32, accepted_ver: u32) -> rpc_msg {
let reply = reply_body::MSG_ACCEPTED(accepted_reply {
verf: opaque_auth::default(),
reply_data: accept_body::PROG_MISMATCH(mismatch_info {
low: accepted_ver,
high: accepted_ver,
}),
});
rpc_msg {
xid,
body: rpc_body::REPLY(reply),
}
}
pub fn garbage_args_reply_message(xid: u32) -> rpc_msg {
let reply = reply_body::MSG_ACCEPTED(accepted_reply {
verf: opaque_auth::default(),
reply_data: accept_body::GARBAGE_ARGS,
});
rpc_msg {
xid,
body: rpc_body::REPLY(reply),
}
}
pub fn rpc_vers_mismatch(xid: u32) -> rpc_msg {
let reply = reply_body::MSG_DENIED(rejected_reply::RPC_MISMATCH(mismatch_info::default()));
rpc_msg {
xid,
body: rpc_body::REPLY(reply),
}
}
pub fn make_success_reply(xid: u32) -> rpc_msg {
let reply = reply_body::MSG_ACCEPTED(accepted_reply {
verf: opaque_auth::default(),
reply_data: accept_body::SUCCESS,
});
rpc_msg {
xid,
body: rpc_body::REPLY(reply),
}
}