Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DHCP option order not being preserved #76

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
indexmap = { version = "2.6.0", features = ["serde"] }
thiserror = "1.0"
rand = "0.8"
serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
16 changes: 9 additions & 7 deletions src/v4/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{borrow::Cow, collections::HashMap, iter, net::Ipv4Addr};
use std::{borrow::Cow, iter, net::Ipv4Addr};

use indexmap::IndexMap;

use crate::{
decoder::{Decodable, Decoder},
Expand Down Expand Up @@ -155,7 +157,7 @@ dhcproto_macros::declare_codes!(
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DhcpOptions(HashMap<OptionCode, DhcpOption>);
pub struct DhcpOptions(IndexMap<OptionCode, DhcpOption>);

impl DhcpOptions {
/// Create new [`DhcpOptions`]
Expand All @@ -178,7 +180,7 @@ impl DhcpOptions {
}
/// remove option
pub fn remove(&mut self, code: OptionCode) -> Option<DhcpOption> {
self.0.remove(&code)
self.0.shift_remove(&code)
}
/// insert a new [`DhcpOption`]
///
Expand Down Expand Up @@ -276,7 +278,7 @@ impl DhcpOptions {

impl IntoIterator for DhcpOptions {
type Item = (OptionCode, DhcpOption);
type IntoIter = std::collections::hash_map::IntoIter<OptionCode, DhcpOption>;
type IntoIter = indexmap::map::IntoIter<OptionCode, DhcpOption>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand All @@ -288,21 +290,21 @@ impl FromIterator<DhcpOption> for DhcpOptions {
DhcpOptions(
iter.into_iter()
.map(|opt| ((&opt).into(), opt))
.collect::<HashMap<OptionCode, DhcpOption>>(),
.collect::<IndexMap<OptionCode, DhcpOption>>(),
)
}
}

impl FromIterator<(OptionCode, DhcpOption)> for DhcpOptions {
fn from_iter<T: IntoIterator<Item = (OptionCode, DhcpOption)>>(iter: T) -> Self {
DhcpOptions(iter.into_iter().collect::<HashMap<_, _>>())
DhcpOptions(iter.into_iter().collect::<IndexMap<_, _>>())
}
}

impl Decodable for DhcpOptions {
fn decode(decoder: &mut Decoder<'_>) -> DecodeResult<Self> {
// represented as a vector in the actual message
let mut opts = HashMap::new();
let mut opts = IndexMap::new();
// should we error the whole parser if we fail to parse an
// option or just stop parsing options? -- here we will just stop
while let Ok(opt) = DhcpOption::decode(decoder) {
Expand Down