1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Web messaging
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::io::{Read, Write};

use crate::serdes::{DumpError, Dumpable, LoadError, Loadable};
use crate::{RemoteCall, RemoteCallResponse};

/// Host IP address for web browsers
pub const HOST_STR: &str = "localhost";
/// Host IP address
pub const HOST: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);

/// Standard max packet size
pub const PACKET_BUFFER_SIZE: usize = 1024;
/// Encryption nonce size
pub const NONCE_SIZE: usize = 12;

/// Address and port
#[inline]
pub fn socket_addr(port: u16) -> SocketAddr {
    SocketAddr::V4(SocketAddrV4::new(HOST, port))
}

/// Accepted Packet types and the data they contain
pub enum Packet {
    /// A remote call
    Call(RemoteCall),
    /// A reponse to a remote call
    CallResponse(RemoteCallResponse),
    /// Unused
    KeepAlive,
    /// Invalid
    Invalid,
    /// General message
    Message(String),
    /// Response to an unsupported packet
    Unsupported,
    /// Broken packet type, useful for testing
    Bad,
    /// Many packets merged into one
    Many(Vec<Packet>),
}

impl Packet {
    /// Byte representing the packet type -- the first byte of any packet in USDPL
    const fn discriminant(&self) -> u8 {
        match self {
            Self::Call(_) => 1,
            Self::CallResponse(_) => 2,
            Self::KeepAlive => 3,
            Self::Invalid => 4,
            Self::Message(_) => 5,
            Self::Unsupported => 6,
            Self::Bad => 7,
            Self::Many(_) => 8,
        }
    }
}

impl Loadable for Packet {
    fn load(buf: &mut dyn Read) -> Result<(Self, usize), LoadError> {
        let mut discriminant_buf = [u8::MAX; 1];
        buf.read_exact(&mut discriminant_buf).map_err(LoadError::Io)?;
        let mut result: (Self, usize) = match discriminant_buf[0] {
            //0 => (None, 0),
            1 => {
                let (obj, len) = RemoteCall::load(buf)?;
                (Self::Call(obj), len)
            }
            2 => {
                let (obj, len) = RemoteCallResponse::load(buf)?;
                (Self::CallResponse(obj), len)
            }
            3 => (Self::KeepAlive, 0),
            4 => (Self::Invalid, 0),
            5 => {
                let (obj, len) = String::load(buf)?;
                (Self::Message(obj), len)
            }
            6 => (Self::Unsupported, 0),
            7 => return Err(LoadError::InvalidData),
            8 => {
                let (obj, len) = <_>::load(buf)?;
                (Self::Many(obj), len)
            }
            _ => return Err(LoadError::InvalidData),
        };
        result.1 += 1;
        Ok(result)
    }
}

impl Dumpable for Packet {
    fn dump(&self, buf: &mut dyn Write) -> Result<usize, DumpError> {
        let size1 = buf.write(&[self.discriminant()]).map_err(DumpError::Io)?;
        let result = match self {
            Self::Call(c) => c.dump(buf),
            Self::CallResponse(c) => c.dump(buf),
            Self::KeepAlive => Ok(0),
            Self::Invalid => Ok(0),
            Self::Message(s) => s.dump(buf),
            Self::Unsupported => Ok(0),
            Self::Bad => return Err(DumpError::Unsupported),
            Self::Many(v) => v.dump(buf),
        }?;
        Ok(size1 + result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "encrypt")]
    #[test]
    fn encryption_integration_test() {
        let key = hex_literal::hex!("59C4E408F27250B3147E7724511824F1D28ED7BEF43CF7103ACE747F77A2B265");
        let nonce = [0u8; NONCE_SIZE];
        let packet = Packet::Call(RemoteCall{
            id: 42,
            function: "test".into(),
            parameters: Vec::new(),
        });
        let mut buffer = Vec::with_capacity(PACKET_BUFFER_SIZE);
        let len = packet.dump_encrypted(&mut buffer, &key, &nonce).unwrap();
        println!("buffer: {}", String::from_utf8(buffer.as_slice()[..len].to_vec()).unwrap());

        let (packet_out, _len) = Packet::load_encrypted(&buffer.as_slice()[..len], &key, &nonce).unwrap();

        if let Packet::Call(call_out) = packet_out {
            if let Packet::Call(call_in) = packet {
                assert_eq!(call_in.id, call_out.id, "Input and output packets do not match");
                assert_eq!(call_in.function, call_out.function, "Input and output packets do not match");
                assert_eq!(call_in.parameters.len(), call_out.parameters.len(), "Input and output packets do not match");
            } else {
                panic!("Packet in not a Call");
            }
        } else {
            panic!("Packet out not a Call!");
        }
    }
}