Skip to content

Various commits, mostly fyi #296

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

Closed
wants to merge 7 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignore = [
"*",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is no longer correct, as the format has changed to match that of .gitignore.

The ignore list feature isn't stable yet anyway and is only available in nightly, so I suppose it could change again.

]
1 change: 1 addition & 0 deletions src/dhcp/clientv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ impl Client {
server_identifier: None,
parameter_request_list: None,
dns_servers: None,
ip_lease_time: None,
};
let mut send_packet = |iface, endpoint, dhcp_repr| {
send_packet(iface, raw_socket, &endpoint, &dhcp_repr, checksum_caps)
Expand Down
4 changes: 2 additions & 2 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,8 +1644,8 @@ impl<'a> TcpSocket<'a> {
}
}

impl<'a> Into<Socket<'a, 'static>> for TcpSocket<'a> {
fn into(self) -> Socket<'a, 'static> {
impl<'a, 'b> Into<Socket<'a, 'b>> for TcpSocket<'a> {
fn into(self) -> Socket<'a, 'b> {
Socket::Tcp(self)
}
}
Expand Down
69 changes: 68 additions & 1 deletion src/wire/dhcpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub enum DhcpOption<'a> {
ServerIdentifier(Ipv4Address),
Router(Ipv4Address),
SubnetMask(Ipv4Address),
IpLeaseTime(u32),
Other { kind: u8, data: &'a [u8] }
}

Expand Down Expand Up @@ -122,6 +123,7 @@ impl<'a> DhcpOption<'a> {
&DhcpOption::SubnetMask(ip) => {
2 + ip.as_bytes().len()
},
&DhcpOption::IpLeaseTime(_) => 2 + 4,
&DhcpOption::Other { data, .. } => 2 + data.len()
}
}
Expand Down Expand Up @@ -167,6 +169,10 @@ impl<'a> DhcpOption<'a> {
buffer[0] = field::OPT_SUBNET_MASK;
buffer[2..6].copy_from_slice(mask.as_bytes());
}
&DhcpOption::IpLeaseTime(value) => {
buffer[0] = field::OPT_IP_LEASE_TIME;
NetworkEndian::write_u32(&mut buffer[2..6], value);
}
&DhcpOption::Other { kind, data: provided } => {
buffer[0] = kind;
buffer[2..skip_length].copy_from_slice(provided);
Expand Down Expand Up @@ -661,8 +667,33 @@ pub struct Repr<'a> {
pub parameter_request_list: Option<&'a [u8]>,
/// DNS servers
pub dns_servers: Option<[Option<Ipv4Address>; 3]>,
/// IP Address Lease Time
pub ip_lease_time: Option<u32>,
}

impl<'a> Default for Repr<'a> {
fn default() -> Self {
Self {
message_type: MessageType::Unknown(0),
transaction_id: 0,
client_hardware_address: Default::default(),
client_ip: Ipv4Address::UNSPECIFIED,
your_ip: Ipv4Address::UNSPECIFIED,
server_ip: Ipv4Address::UNSPECIFIED,
router: None,
subnet_mask: None,
relay_agent_ip: Ipv4Address::UNSPECIFIED,
broadcast: false,
requested_ip: None,
client_identifier: None,
server_identifier: None,
parameter_request_list: None,
dns_servers: None,
ip_lease_time: None,
}
}
}

impl<'a> Repr<'a> {
/// Return the length of a packet that will be emitted from this high-level representation.
pub fn buffer_len(&self) -> usize {
Expand All @@ -672,7 +703,17 @@ impl<'a> Repr<'a> {
if self.requested_ip.is_some() { len += 6; }
if self.client_identifier.is_some() { len += 9; }
if self.server_identifier.is_some() { len += 6; }
if self.subnet_mask.is_some() { len += 6; }
if let Some(value) = self.router {
len += DhcpOption::Router(value).buffer_len();
}
if let Some(value) = self.ip_lease_time {
len += DhcpOption::IpLeaseTime(value).buffer_len();
}
if let Some(list) = self.parameter_request_list { len += list.len() + 2; }
if let Some(servers) = self.dns_servers {
len += servers.iter().filter(|s| s.is_some()).count() * 4 + 2;
}

len
}
Expand Down Expand Up @@ -710,6 +751,7 @@ impl<'a> Repr<'a> {
let mut subnet_mask = None;
let mut parameter_request_list = None;
let mut dns_servers = None;
let mut ip_lease_time = None;

let mut options = packet.options()?;
while options.len() > 0 {
Expand Down Expand Up @@ -737,6 +779,9 @@ impl<'a> Repr<'a> {
DhcpOption::SubnetMask(mask) => {
subnet_mask = Some(mask);
}
DhcpOption::IpLeaseTime(value) => {
ip_lease_time = Some(value);
}
DhcpOption::Other {kind: field::OPT_PARAMETER_REQUEST_LIST, data} => {
parameter_request_list = Some(data);
}
Expand All @@ -760,7 +805,7 @@ impl<'a> Repr<'a> {
Ok(Repr {
transaction_id, client_hardware_address, client_ip, your_ip, server_ip, relay_agent_ip,
broadcast, requested_ip, server_identifier, router,
subnet_mask, client_identifier, parameter_request_list, dns_servers,
subnet_mask, client_identifier, parameter_request_list, dns_servers, ip_lease_time,
message_type: message_type?,
})
}
Expand Down Expand Up @@ -806,6 +851,28 @@ impl<'a> Repr<'a> {
let option = DhcpOption::Other{ kind: field::OPT_PARAMETER_REQUEST_LIST, data: list };
let tmp = options; options = option.emit(tmp);
}
if let Some(dns_servers) = self.dns_servers {
let mut data: [u8; 3 * 4] = Default::default();
let mut i = 0;
while i < dns_servers.len() {
if let Some(dns_server) = dns_servers[i] {
data[i * 4..(i + 1) * 4].copy_from_slice(dns_server.as_bytes());
} else {
break;
}
i += 1;
}
let option = DhcpOption::Other {
kind: field::OPT_DOMAIN_NAME_SERVER,
data: &data[0..i * 4],
};
let tmp = options;
options = option.emit(tmp);
}
if let Some(value) = self.ip_lease_time {
let tmp = options;
options = DhcpOption::IpLeaseTime(value).emit(tmp);
}
DhcpOption::EndOfList.emit(options);
}

Expand Down
10 changes: 10 additions & 0 deletions src/wire/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ impl From<::std::net::IpAddr> for Address {
}
}

impl From<Address> for ::std::net::IpAddr {
fn from(x: Address) -> ::std::net::IpAddr {
match x {
Address::Ipv4(ipv4) => ::std::net::IpAddr::V4(ipv4.into()),
Address::Ipv6(ipv6) => ::std::net::IpAddr::V6(ipv6.into()),
_ => unreachable!(),
}
}
}

#[cfg(all(feature = "std", feature = "proto-ipv4"))]
impl From<::std::net::Ipv4Addr> for Address {
fn from(ipv4: ::std::net::Ipv4Addr) -> Address {
Expand Down