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
// SPDX-License-Identifier: MIT

use crate::{
    nlas::route::Nla,
    traits::{Emitable, Parseable},
    DecodeError,
    RouteHeader,
    RouteMessageBuffer,
};
use anyhow::Context;
use std::net::IpAddr;

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct RouteMessage {
    pub header: RouteHeader,
    pub nlas: Vec<Nla>,
}

impl Emitable for RouteMessage {
    fn buffer_len(&self) -> usize {
        self.header.buffer_len() + self.nlas.as_slice().buffer_len()
    }

    fn emit(&self, buffer: &mut [u8]) {
        self.header.emit(buffer);
        self.nlas
            .as_slice()
            .emit(&mut buffer[self.header.buffer_len()..]);
    }
}

impl<'a, T: AsRef<[u8]> + 'a> Parseable<RouteMessageBuffer<&'a T>> for RouteMessage {
    fn parse(buf: &RouteMessageBuffer<&'a T>) -> Result<Self, DecodeError> {
        Ok(RouteMessage {
            header: RouteHeader::parse(buf).context("failed to parse route message header")?,
            nlas: Vec::<Nla>::parse(buf).context("failed to parse route message NLAs")?,
        })
    }
}

impl<'a, T: AsRef<[u8]> + 'a> Parseable<RouteMessageBuffer<&'a T>> for Vec<Nla> {
    fn parse(buf: &RouteMessageBuffer<&'a T>) -> Result<Self, DecodeError> {
        let mut nlas = vec![];
        for nla_buf in buf.nlas() {
            nlas.push(Nla::parse(&nla_buf?)?);
        }
        Ok(nlas)
    }
}

fn octets_to_addr(octets: &[u8]) -> Result<IpAddr, DecodeError> {
    if octets.len() == 4 {
        let mut ary: [u8; 4] = Default::default();
        ary.copy_from_slice(octets);
        Ok(IpAddr::from(ary))
    } else if octets.len() == 16 {
        let mut ary: [u8; 16] = Default::default();
        ary.copy_from_slice(octets);
        Ok(IpAddr::from(ary))
    } else {
        Err(DecodeError::from("Cannot decode IP address"))
    }
}

impl RouteMessage {
    /// Returns the input interface index, if present.
    pub fn input_interface(&self) -> Option<u32> {
        self.nlas.iter().find_map(|nla| {
            if let Nla::Iif(v) = nla {
                Some(*v)
            } else {
                None
            }
        })
    }

    /// Returns the output interface index, if present.
    pub fn output_interface(&self) -> Option<u32> {
        self.nlas.iter().find_map(|nla| {
            if let Nla::Oif(v) = nla {
                Some(*v)
            } else {
                None
            }
        })
    }

    /// Returns the source address prefix, if present.
    pub fn source_prefix(&self) -> Option<(IpAddr, u8)> {
        self.nlas.iter().find_map(|nla| {
            if let Nla::Source(v) = nla {
                octets_to_addr(v)
                    .ok()
                    .map(|addr| (addr, self.header.source_prefix_length))
            } else {
                None
            }
        })
    }

    /// Returns the destination subnet prefix, if present.
    pub fn destination_prefix(&self) -> Option<(IpAddr, u8)> {
        self.nlas.iter().find_map(|nla| {
            if let Nla::Destination(v) = nla {
                octets_to_addr(v)
                    .ok()
                    .map(|addr| (addr, self.header.destination_prefix_length))
            } else {
                None
            }
        })
    }

    /// Returns the gateway address, if present.
    pub fn gateway(&self) -> Option<IpAddr> {
        self.nlas.iter().find_map(|nla| {
            if let Nla::Gateway(v) = nla {
                octets_to_addr(v).ok()
            } else {
                None
            }
        })
    }
}