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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use core::convert::TryFrom;
use core::ops::Deref;

use byteorder::{ByteOrder, NetworkEndian};
use slice_ext::SplitBefore;

use crate::base::{Base, BaseBuilder, Body};
use crate::options::Options;
use crate::page::Page;
use crate::types::*;

use super::Common;
use super::BUFF_SIZE;

/// Generic Response message
#[derive(Clone, Debug)]
pub struct Response {
    pub common: Common,
    pub data: ResponseKind,
}

/// Response message kinds
#[derive(Clone, PartialEq, Debug)]
pub enum ResponseKind {
    Status(Status),
    NodesFound(Id, Vec<(Id, Address, PublicKey)>),
    ValuesFound(Id, Vec<Page>),
    NoResult,
    PullData(Id, Vec<Page>),
}

mod status {
    pub const OK: u32 = 0x0000_0000;
    pub const INVALID_REQUEST: u32 = 0x0000_0001;
}

/// Status response codes
#[derive(Clone, PartialEq, Debug)]
pub enum Status {
    Ok,
    InvalidRequest,
    Unknown(u32),
}

impl From<u32> for Status {
    fn from(v: u32) -> Self {
        match v {
            status::OK => Status::Ok,
            status::INVALID_REQUEST => Status::InvalidRequest,
            _ => Status::Unknown(v),
        }
    }
}

impl Into<u32> for Status {
    fn into(self) -> u32 {
        match self {
            Status::Ok => status::OK,
            Status::InvalidRequest => status::INVALID_REQUEST,
            Status::Unknown(v) => v,
        }
    }
}

impl Deref for Response {
    type Target = Common;

    fn deref(&self) -> &Common {
        &self.common
    }
}

impl Response {
    pub fn new(from: Id, id: RequestId, data: ResponseKind, flags: Flags) -> Response {
        let common = Common {
            from,
            id,
            flags,
            public_key: None,
            remote_address: None,
        };
        Response { common, data }
    }

    pub fn flags(&mut self) -> &mut Flags {
        &mut self.common.flags
    }

    pub fn with_remote_address(mut self, addr: Address) -> Self {
        self.common.remote_address = Some(addr);
        self
    }

    pub fn set_public_key(&mut self, pk: PublicKey) {
        self.common.public_key = Some(pk);
    }

    pub fn with_public_key(mut self, pk: PublicKey) -> Self {
        self.common.public_key = Some(pk);
        self
    }
}

#[cfg(nope)]
impl fmt::Debug for ResponseKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ResponseKind::Status => write!(f, "status"),
            ResponseKind::NodesFound(id, nodes) => {
                write!(f, "NodesFound({:?}): [", id)?;
                for n in nodes {
                    write!(f, "\n    - {:?}", n)?;
                }
                writeln!(f, "]")
            }
            ResponseKind::ValuesFound(id, values) => {
                write!(f, "ValuesFound({:?}): [", id)?;
                for v in values {
                    write!(f, "\n    - {:?}", v)?;
                }
                writeln!(f, "]")
            }
            ResponseKind::NoResult => write!(f, "NoResult"),
            ResponseKind::PullData(id, values) => {
                write!(f, "PullData({:?}): [", id)?;
                for v in values {
                    write!(f, "\n    - {:?}", v)?;
                }
                writeln!(f, "]")
            }
        }
    }
}

impl PartialEq for Response {
    fn eq(&self, b: &Self) -> bool {
        self.from == b.from && self.flags == b.flags && self.data == b.data
    }
}

impl Response {
    pub fn convert<V>(base: Base, key_source: V) -> Result<Response, Error>
    where
        V: Fn(&Id) -> Option<PublicKey>,
    {
        let header = base.header();

        let empty_body = vec![];
        let body = match base.body() {
            Body::Cleartext(d) => d,
            Body::None => &empty_body,
            Body::Encrypted(_e) => {
                panic!("Attempting to convert encrypted object to response message")
            }
        };

        let remote_address = None;

        let _public_options = base.public_options().to_vec();
        //let _private_options = base.private_options().to_vec();

        let kind = match MessageKind::try_from(header.kind()) {
            Ok(k) => k,
            Err(_) => return Err(Error::InvalidMessageKind),
        };

        let data = match kind {
            MessageKind::Status => {
                let status = NetworkEndian::read_u32(&body);
                ResponseKind::Status(status.into())
            }
            MessageKind::NoResult => ResponseKind::NoResult,
            MessageKind::NodesFound => {
                let mut id = Id::default();
                id.copy_from_slice(&body[..ID_LEN]);

                // Build options array from body
                let (options, _n) = Options::parse_vec(&body[ID_LEN..]).unwrap();

                let nodes: Vec<_> = (&options[..])
                    .split_before(|o| match o {
                        Options::PeerId(_) => true,
                        _ => false,
                    })
                    .filter_map(|opts| {
                        let id = Base::peer_id_option(&opts);
                        let addr = Base::address_option(&opts);
                        let key = Base::pub_key_option(&opts);

                        match (id, addr, key) {
                            (Some(id), Some(addr), Some(key)) => Some((id, addr, key)),
                            // TODO: warn here
                            _ => None,
                        }
                    })
                    .collect();

                ResponseKind::NodesFound(id, nodes)
            }
            MessageKind::ValuesFound => {
                let mut id = Id::default();
                id.copy_from_slice(&body[0..ID_LEN]);

                let pages = Page::decode_pages(&body[ID_LEN..], key_source).unwrap();

                ResponseKind::ValuesFound(id, pages)
            }
            MessageKind::PullData => {
                let mut id = Id::default();
                id.copy_from_slice(&body[0..ID_LEN]);

                let pages = Page::decode_pages(&body[ID_LEN..], key_source).unwrap();

                ResponseKind::PullData(id, pages)
            }
            _ => {
                error!(
                    "Error converting base object of kind {:?} to response message",
                    header.kind()
                );
                return Err(Error::InvalidMessageKind);
            }
        };

        // Fetch other key options
        let public_key = base.public_key.clone();

        //let remote_address = Base::filter_address_option(&mut public_options);

        let common = Common {
            from: base.id().clone(),
            id: header.index(),
            flags: header.flags(),
            public_key,
            remote_address,
        };
        Ok(Response { common, data })
    }
}

impl Into<Base> for Response {
    fn into(self) -> Base {
        let kind: MessageKind;
        let body: Vec<u8>;

        let mut buff = vec![0; BUFF_SIZE];

        let mut builder = BaseBuilder::default();

        match &self.data {
            ResponseKind::Status(code) => {
                kind = MessageKind::Status;
                NetworkEndian::write_u32(&mut buff, code.clone().into());
                body = (&buff[0..4]).to_vec();
            }
            ResponseKind::NoResult => {
                kind = MessageKind::NoResult;
                //TODO?: (&mut body[..ID_LEN]).copy_from_slice(&id);
                body = vec![];
            }
            ResponseKind::NodesFound(id, nodes) => {
                kind = MessageKind::NodesFound;
                (&mut buff[..ID_LEN]).copy_from_slice(&id);

                // Build options list from nodes
                let mut options = Vec::with_capacity(nodes.len() * 3);
                for n in nodes {
                    options.push(Options::peer_id(n.0.clone()));
                    options.push(Options::address(n.1));
                    options.push(Options::pub_key(n.2.clone()));
                }

                // Encode options list to body
                let n = Options::encode_vec(&options, &mut buff[ID_LEN..]).unwrap();

                body = buff[..ID_LEN + n].to_vec();
            }
            ResponseKind::ValuesFound(id, pages) => {
                kind = MessageKind::ValuesFound;
                (&mut buff[..ID_LEN]).copy_from_slice(&id);

                let n = Page::encode_pages(&pages, &mut buff[ID_LEN..]).unwrap();

                body = buff[..ID_LEN + n].to_vec();
            }
            ResponseKind::PullData(id, pages) => {
                kind = MessageKind::PullData;
                (&mut buff[..ID_LEN]).copy_from_slice(&id);

                let n = Page::encode_pages(&pages, &mut buff[ID_LEN..]).unwrap();

                body = buff[..ID_LEN + n].to_vec();
            }
        }

        let builder = builder
            .base(
                self.common.from.clone(),
                0,
                kind.into(),
                self.common.id.clone(),
                self.common.flags,
            )
            .body(Body::from(body));

        builder.public_key(self.common.public_key.clone());

        if let Some(a) = self.remote_address {
            builder.append_public_option(Options::address(a));
        }

        builder.build().unwrap()
    }
}