This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from rshearman/mpls
Looks great, thanks :)
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
netlink-packet-route/src/rtnl/route/nlas/mpls_ip_tunnel.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
use anyhow::Context; | ||
|
||
use crate::{ | ||
constants::*, | ||
nlas::{DefaultNla, Nla, NlaBuffer}, | ||
parsers::parse_u8, | ||
traits::Parseable, | ||
DecodeError, | ||
}; | ||
|
||
/// Netlink attributes for `RTA_ENCAP` with `RTA_ENCAP_TYPE` set to `LWTUNNEL_ENCAP_MPLS`. | ||
pub enum MplsIpTunnel { | ||
Destination(Vec<u8>), | ||
Ttl(u8), | ||
Other(DefaultNla), | ||
} | ||
|
||
impl Nla for MplsIpTunnel { | ||
fn value_len(&self) -> usize { | ||
use self::MplsIpTunnel::*; | ||
match self { | ||
Destination(bytes) => bytes.len(), | ||
Ttl(_) => 1, | ||
Other(attr) => attr.value_len(), | ||
} | ||
} | ||
|
||
fn kind(&self) -> u16 { | ||
use self::MplsIpTunnel::*; | ||
match self { | ||
Destination(_) => MPLS_IPTUNNEL_DST, | ||
Ttl(_) => MPLS_IPTUNNEL_TTL, | ||
Other(attr) => attr.kind(), | ||
} | ||
} | ||
|
||
fn emit_value(&self, buffer: &mut [u8]) { | ||
use self::MplsIpTunnel::*; | ||
match self { | ||
Destination(bytes) => buffer.copy_from_slice(bytes.as_slice()), | ||
Ttl(ttl) => buffer[0] = *ttl, | ||
Other(attr) => attr.emit_value(buffer), | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for MplsIpTunnel { | ||
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> { | ||
use self::MplsIpTunnel::*; | ||
let payload = buf.value(); | ||
Ok(match buf.kind() { | ||
MPLS_IPTUNNEL_DST => Destination(payload.to_vec()), | ||
MPLS_IPTUNNEL_TTL => Ttl(parse_u8(payload).context("invalid MPLS_IPTUNNEL_TTL value")?), | ||
_ => Other(DefaultNla::parse(buf).context("invalid NLA value (unknown type) value")?), | ||
}) | ||
} | ||
} |