initial commit

This commit is contained in:
fuckwit 2023-05-22 20:13:21 +02:00
commit e189168144
11 changed files with 681 additions and 0 deletions

View File

@ -0,0 +1,118 @@
unset shellHook
PATH=${PATH:-}
nix_saved_PATH="$PATH"
XDG_DATA_DIRS=${XDG_DATA_DIRS:-}
nix_saved_XDG_DATA_DIRS="$XDG_DATA_DIRS"
BASH='/noshell'
HOSTTYPE='x86_64'
IFS='
'
IN_NIX_SHELL='impure'
export IN_NIX_SHELL
LINENO='76'
MACHTYPE='x86_64-pc-linux-gnu'
NIX_BUILD_CORES='0'
export NIX_BUILD_CORES
NIX_STORE='/nix/store'
export NIX_STORE
OLDPWD=''
export OLDPWD
OPTERR='1'
OSTYPE='linux-gnu'
PATH='/path-not-set'
export PATH
PS4='+ '
builder='/nix/store/kbcrs84s1x8yd5bp1nq6q6ihda8nd2lp-bash-interactive-5.2-p15/bin/bash'
export builder
dontAddDisableDepTrack='1'
export dontAddDisableDepTrack
name='devenv-shell'
export name
out='/home/patrick/Sandbox/dhcp/outputs/out'
export out
outputs='out'
shellHook='
# Remove all the unnecessary noise that is set by the build env
unset NIX_BUILD_TOP NIX_BUILD_CORES NIX_STORE
unset TEMP TEMPDIR TMP TMPDIR
# $name variable is preserved to keep it compatible with pure shell https://github.com/sindresorhus/pure/blob/47c0c881f0e7cfdb5eaccd335f52ad17b897c060/pure.zsh#L235
unset builder out shellHook stdenv system
# Flakes stuff
unset dontAddDisableDepTrack outputs
# For `nix develop`. We get /noshell on Linux and /sbin/nologin on macOS.
if [[ "$SHELL" == "/noshell" || "$SHELL" == "/sbin/nologin" ]]; then
export SHELL=/nix/store/kbcrs84s1x8yd5bp1nq6q6ihda8nd2lp-bash-interactive-5.2-p15/bin/bash
fi
# https://github.com/numtide/devshell/issues/158
PATH=${PATH#/path-not-set:}
export DEVENV_PROFILE=/nix/store/93qx8jlgc6hpcqr3jcsr7by6lzipah3g-devenv-profile
# add installed packages to PATH
export PATH="$DEVENV_PROFILE/bin:$PATH"
# prepend common compilation lookup paths
export PKG_CONFIG_PATH="$DEVENV_PROFILE/lib/pkgconfig:${PKG_CONFIG_PATH-}"
export LD_LIBRARY_PATH="$DEVENV_PROFILE/lib:${LD_LIBRARY_PATH-}"
export LIBRARY_PATH="$DEVENV_PROFILE/lib:${LIBRARY_PATH-}"
export C_INCLUDE_PATH="$DEVENV_PROFILE/include:${C_INCLUDE_PATH-}"
# these provide shell completions / default config options
export XDG_DATA_DIRS="$DEVENV_PROFILE/share:${XDG_DATA_DIRS-}"
export XDG_CONFIG_DIRS="$DEVENV_PROFILE/etc/xdg:${XDG_CONFIG_DIRS-}"
export DEVENV_DOTFILE='\''/home/patrick/Sandbox/dhcp/.devenv'\''
export DEVENV_PROFILE='\''/nix/store/93qx8jlgc6hpcqr3jcsr7by6lzipah3g-devenv-profile'\''
export DEVENV_ROOT='\''/home/patrick/Sandbox/dhcp'\''
export DEVENV_STATE='\''/home/patrick/Sandbox/dhcp/.devenv/state'\''
export GREET='\''devenv'\''
hello
git --version
export PS1="\[\e[0;34m\](devenv)\[\e[0m\] ${PS1-}"
# set path to locales on non-NixOS Linux hosts
if [ -z "${LOCALE_ARCHIVE-}" ]; then
export LOCALE_ARCHIVE=/nix/store/nm00g2nm2wsyh30jvkgi168kh6yvw4sl-glibc-locales-2.37-8/lib/locale/locale-archive
fi
# note what environments are active, but make sure we don'\''t repeat them
if [[ ! "${DIRENV_ACTIVE-}" =~ (^|:)"$PWD"(:|$) ]]; then
export DIRENV_ACTIVE="$PWD:${DIRENV_ACTIVE-}"
fi
# devenv helper
if [ ! type -p direnv &>/dev/null && -f .envrc ]; then
echo "You have .envrc but direnv command is not installed."
echo "Please install direnv: https://direnv.net/docs/installation.html"
fi
mkdir -p .devenv
rm -f .devenv/profile
ln -s /nix/store/93qx8jlgc6hpcqr3jcsr7by6lzipah3g-devenv-profile .devenv/profile
'
export shellHook
stdenv='/nix/store/fi1k3vh3a70g8cpwmrnc553vvccrny54-naked-stdenv'
export stdenv
system='x86_64-linux'
export system
runHook ()
{
eval "$shellHook";
unset runHook
}
PATH="$PATH:$nix_saved_PATH"
XDG_DATA_DIRS="$XDG_DATA_DIRS:$nix_saved_XDG_DATA_DIRS"
export NIX_BUILD_TOP="$(mktemp -d -t nix-shell.XXXXXX)"
export TMP="$NIX_BUILD_TOP"
export TMPDIR="$NIX_BUILD_TOP"
export TEMP="$NIX_BUILD_TOP"
export TEMPDIR="$NIX_BUILD_TOP"
eval "$shellHook"

View File

@ -0,0 +1,115 @@
unset shellHook
PATH=${PATH:-}
nix_saved_PATH="$PATH"
XDG_DATA_DIRS=${XDG_DATA_DIRS:-}
nix_saved_XDG_DATA_DIRS="$XDG_DATA_DIRS"
BASH='/noshell'
HOSTTYPE='x86_64'
IFS='
'
IN_NIX_SHELL='impure'
export IN_NIX_SHELL
LINENO='76'
MACHTYPE='x86_64-pc-linux-gnu'
NIX_BUILD_CORES='0'
export NIX_BUILD_CORES
NIX_STORE='/nix/store'
export NIX_STORE
OLDPWD=''
export OLDPWD
OPTERR='1'
OSTYPE='linux-gnu'
PATH='/path-not-set'
export PATH
PS4='+ '
builder='/nix/store/kbcrs84s1x8yd5bp1nq6q6ihda8nd2lp-bash-interactive-5.2-p15/bin/bash'
export builder
dontAddDisableDepTrack='1'
export dontAddDisableDepTrack
name='devenv-shell'
export name
out='/home/patrick/Sandbox/dhcp/outputs/out'
export out
outputs='out'
shellHook='
# Remove all the unnecessary noise that is set by the build env
unset NIX_BUILD_TOP NIX_BUILD_CORES NIX_STORE
unset TEMP TEMPDIR TMP TMPDIR
# $name variable is preserved to keep it compatible with pure shell https://github.com/sindresorhus/pure/blob/47c0c881f0e7cfdb5eaccd335f52ad17b897c060/pure.zsh#L235
unset builder out shellHook stdenv system
# Flakes stuff
unset dontAddDisableDepTrack outputs
# For `nix develop`. We get /noshell on Linux and /sbin/nologin on macOS.
if [[ "$SHELL" == "/noshell" || "$SHELL" == "/sbin/nologin" ]]; then
export SHELL=/nix/store/kbcrs84s1x8yd5bp1nq6q6ihda8nd2lp-bash-interactive-5.2-p15/bin/bash
fi
# https://github.com/numtide/devshell/issues/158
PATH=${PATH#/path-not-set:}
export DEVENV_PROFILE=/nix/store/fd3xw4ffw33w8c70nw5d768ss3sxg211-devenv-profile
# add installed packages to PATH
export PATH="$DEVENV_PROFILE/bin:$PATH"
# prepend common compilation lookup paths
export PKG_CONFIG_PATH="$DEVENV_PROFILE/lib/pkgconfig:${PKG_CONFIG_PATH-}"
export LD_LIBRARY_PATH="$DEVENV_PROFILE/lib:${LD_LIBRARY_PATH-}"
export LIBRARY_PATH="$DEVENV_PROFILE/lib:${LIBRARY_PATH-}"
export C_INCLUDE_PATH="$DEVENV_PROFILE/include:${C_INCLUDE_PATH-}"
# these provide shell completions / default config options
export XDG_DATA_DIRS="$DEVENV_PROFILE/share:${XDG_DATA_DIRS-}"
export XDG_CONFIG_DIRS="$DEVENV_PROFILE/etc/xdg:${XDG_CONFIG_DIRS-}"
export DEVENV_DOTFILE='\''/home/patrick/Sandbox/dhcp/.devenv'\''
export DEVENV_PROFILE='\''/nix/store/fd3xw4ffw33w8c70nw5d768ss3sxg211-devenv-profile'\''
export DEVENV_ROOT='\''/home/patrick/Sandbox/dhcp'\''
export DEVENV_STATE='\''/home/patrick/Sandbox/dhcp/.devenv/state'\''
export RUST_SRC_PATH='\''/nix/store/8n69a7ralcwkzfxhzm9va5nlj5risdz6-rust-src-stable-2023-04-20/lib/rustlib/src/rust/library'\''
export PS1="\[\e[0;34m\](devenv)\[\e[0m\] ${PS1-}"
# set path to locales on non-NixOS Linux hosts
if [ -z "${LOCALE_ARCHIVE-}" ]; then
export LOCALE_ARCHIVE=/nix/store/nm00g2nm2wsyh30jvkgi168kh6yvw4sl-glibc-locales-2.37-8/lib/locale/locale-archive
fi
# note what environments are active, but make sure we don'\''t repeat them
if [[ ! "${DIRENV_ACTIVE-}" =~ (^|:)"$PWD"(:|$) ]]; then
export DIRENV_ACTIVE="$PWD:${DIRENV_ACTIVE-}"
fi
# devenv helper
if [ ! type -p direnv &>/dev/null && -f .envrc ]; then
echo "You have .envrc but direnv command is not installed."
echo "Please install direnv: https://direnv.net/docs/installation.html"
fi
mkdir -p .devenv
rm -f .devenv/profile
ln -s /nix/store/fd3xw4ffw33w8c70nw5d768ss3sxg211-devenv-profile .devenv/profile
'
export shellHook
stdenv='/nix/store/fi1k3vh3a70g8cpwmrnc553vvccrny54-naked-stdenv'
export stdenv
system='x86_64-linux'
export system
runHook ()
{
eval "$shellHook";
unset runHook
}
PATH="$PATH:$nix_saved_PATH"
XDG_DATA_DIRS="$XDG_DATA_DIRS:$nix_saved_XDG_DATA_DIRS"
export NIX_BUILD_TOP="$(mktemp -d -t nix-shell.XXXXXX)"
export TMP="$NIX_BUILD_TOP"
export TMPDIR="$NIX_BUILD_TOP"
export TEMP="$NIX_BUILD_TOP"
export TEMPDIR="$NIX_BUILD_TOP"
eval "$shellHook"

View File

@ -0,0 +1,115 @@
unset shellHook
PATH=${PATH:-}
nix_saved_PATH="$PATH"
XDG_DATA_DIRS=${XDG_DATA_DIRS:-}
nix_saved_XDG_DATA_DIRS="$XDG_DATA_DIRS"
BASH='/noshell'
HOSTTYPE='x86_64'
IFS='
'
IN_NIX_SHELL='impure'
export IN_NIX_SHELL
LINENO='76'
MACHTYPE='x86_64-pc-linux-gnu'
NIX_BUILD_CORES='0'
export NIX_BUILD_CORES
NIX_STORE='/nix/store'
export NIX_STORE
OLDPWD=''
export OLDPWD
OPTERR='1'
OSTYPE='linux-gnu'
PATH='/path-not-set'
export PATH
PS4='+ '
builder='/nix/store/kbcrs84s1x8yd5bp1nq6q6ihda8nd2lp-bash-interactive-5.2-p15/bin/bash'
export builder
dontAddDisableDepTrack='1'
export dontAddDisableDepTrack
name='devenv-shell'
export name
out='/home/patrick/Sandbox/dhcp/outputs/out'
export out
outputs='out'
shellHook='
# Remove all the unnecessary noise that is set by the build env
unset NIX_BUILD_TOP NIX_BUILD_CORES NIX_STORE
unset TEMP TEMPDIR TMP TMPDIR
# $name variable is preserved to keep it compatible with pure shell https://github.com/sindresorhus/pure/blob/47c0c881f0e7cfdb5eaccd335f52ad17b897c060/pure.zsh#L235
unset builder out shellHook stdenv system
# Flakes stuff
unset dontAddDisableDepTrack outputs
# For `nix develop`. We get /noshell on Linux and /sbin/nologin on macOS.
if [[ "$SHELL" == "/noshell" || "$SHELL" == "/sbin/nologin" ]]; then
export SHELL=/nix/store/kbcrs84s1x8yd5bp1nq6q6ihda8nd2lp-bash-interactive-5.2-p15/bin/bash
fi
# https://github.com/numtide/devshell/issues/158
PATH=${PATH#/path-not-set:}
export DEVENV_PROFILE=/nix/store/fd3xw4ffw33w8c70nw5d768ss3sxg211-devenv-profile
# add installed packages to PATH
export PATH="$DEVENV_PROFILE/bin:$PATH"
# prepend common compilation lookup paths
export PKG_CONFIG_PATH="$DEVENV_PROFILE/lib/pkgconfig:${PKG_CONFIG_PATH-}"
export LD_LIBRARY_PATH="$DEVENV_PROFILE/lib:${LD_LIBRARY_PATH-}"
export LIBRARY_PATH="$DEVENV_PROFILE/lib:${LIBRARY_PATH-}"
export C_INCLUDE_PATH="$DEVENV_PROFILE/include:${C_INCLUDE_PATH-}"
# these provide shell completions / default config options
export XDG_DATA_DIRS="$DEVENV_PROFILE/share:${XDG_DATA_DIRS-}"
export XDG_CONFIG_DIRS="$DEVENV_PROFILE/etc/xdg:${XDG_CONFIG_DIRS-}"
export DEVENV_DOTFILE='\''/home/patrick/Sandbox/dhcp/.devenv'\''
export DEVENV_PROFILE='\''/nix/store/fd3xw4ffw33w8c70nw5d768ss3sxg211-devenv-profile'\''
export DEVENV_ROOT='\''/home/patrick/Sandbox/dhcp'\''
export DEVENV_STATE='\''/home/patrick/Sandbox/dhcp/.devenv/state'\''
export RUST_SRC_PATH='\''/nix/store/8n69a7ralcwkzfxhzm9va5nlj5risdz6-rust-src-stable-2023-04-20/lib/rustlib/src/rust/library'\''
export PS1="\[\e[0;34m\](devenv)\[\e[0m\] ${PS1-}"
# set path to locales on non-NixOS Linux hosts
if [ -z "${LOCALE_ARCHIVE-}" ]; then
export LOCALE_ARCHIVE=/nix/store/nm00g2nm2wsyh30jvkgi168kh6yvw4sl-glibc-locales-2.37-8/lib/locale/locale-archive
fi
# note what environments are active, but make sure we don'\''t repeat them
if [[ ! "${DIRENV_ACTIVE-}" =~ (^|:)"$PWD"(:|$) ]]; then
export DIRENV_ACTIVE="$PWD:${DIRENV_ACTIVE-}"
fi
# devenv helper
if [ ! type -p direnv &>/dev/null && -f .envrc ]; then
echo "You have .envrc but direnv command is not installed."
echo "Please install direnv: https://direnv.net/docs/installation.html"
fi
mkdir -p .devenv
rm -f .devenv/profile
ln -s /nix/store/fd3xw4ffw33w8c70nw5d768ss3sxg211-devenv-profile .devenv/profile
'
export shellHook
stdenv='/nix/store/fi1k3vh3a70g8cpwmrnc553vvccrny54-naked-stdenv'
export stdenv
system='x86_64-linux'
export system
runHook ()
{
eval "$shellHook";
unset runHook
}
PATH="$PATH:$nix_saved_PATH"
XDG_DATA_DIRS="$XDG_DATA_DIRS:$nix_saved_XDG_DATA_DIRS"
export NIX_BUILD_TOP="$(mktemp -d -t nix-shell.XXXXXX)"
export TMP="$NIX_BUILD_TOP"
export TMPDIR="$NIX_BUILD_TOP"
export TEMP="$NIX_BUILD_TOP"
export TEMPDIR="$NIX_BUILD_TOP"
eval "$shellHook"

3
.envrc Normal file
View File

@ -0,0 +1,3 @@
source_url "https://raw.githubusercontent.com/cachix/devenv/d1f7b48e35e6dee421cfd0f51481d17f77586997/direnvrc" "sha256-YBzqskFZxmNb3kYVoKD9ZixoPXJh1C9ZvTLGFRkauZ0="
use devenv

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/target
/Cargo.lock
# Devenv
.devenv*
devenv.local.nix

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "dhcp"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.nom]
version = "7"
default-features = false

177
devenv.lock Normal file
View File

@ -0,0 +1,177 @@
{
"nodes": {
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1684536222,
"narHash": "sha256-jbcTwWLSNZQPmf8+PKtw+3RgwMxbSarMlerIH8feGdw=",
"owner": "cachix",
"repo": "devenv",
"rev": "2f1d49aeee1378c44293ca2f61bf3a9004986fb7",
"type": "github"
},
"original": {
"dir": "src/modules",
"owner": "cachix",
"repo": "devenv",
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1684736520,
"narHash": "sha256-8WjS3Mmr4DPBsSV2iPkYmxr0/YPpkhX10qT/0YybSsc=",
"owner": "nix-community",
"repo": "fenix",
"rev": "f435c0d000726dc5ccfabde567443c2495957706",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1660459072,
"narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "a20de23b925fd8264fd7fad6454652e142fd7f73",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1684668519,
"narHash": "sha256-KkVvlXTqdLLwko9Y0p1Xv6KQ9QTcQorrU098cGilb7c=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "85340996ba67cc02f01ba324e18b1306892ed6f5",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1678872516,
"narHash": "sha256-/E1YwtMtFAu2KUQKV/1+KFuReYPANM2Rzehk84VxVoc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9b8e5abb18324c7fe9f07cb100c3cd4a29cda8b8",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-22.11",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1684195081,
"narHash": "sha256-IKnQUSBhQTChFERxW2AzuauVpY1HRgeVzAjNMAA4B6I=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "96eabec58248ed8f4b0ad59e7ce9398018684fdc",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"devenv": "devenv",
"fenix": "fenix",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1684672829,
"narHash": "sha256-xDUpgRcACQQREemfVQnft6AImqF2+dbxmHupw6gzSho=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "7ac161ce77dfa31dc39337c2543a1d777e70c16e",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

8
devenv.nix Normal file
View File

@ -0,0 +1,8 @@
{ pkgs, ... }:
{
languages.rust = {
enable = true;
version = "stable";
};
}

8
devenv.yaml Normal file
View File

@ -0,0 +1,8 @@
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/nixpkgs-unstable
fenix:
url: github:nix-community/fenix
inputs:
nixpkgs:
follows: nixpkgs

BIN
dhcp_discover.bin Normal file

Binary file not shown.

120
src/lib.rs Normal file
View File

@ -0,0 +1,120 @@
use std::{net::Ipv4Addr, path::Path};
use nom::{
bytes::complete::take,
number::complete::{be_u16, be_u32, be_u8},
IResult,
};
#[derive(Debug)]
struct DhcpMessage<'a> {
operation: Operation,
hardware_type: HardwareType,
hardware_address_length: u8,
hops: u8,
transaction_id: u32,
seconds: u16,
flags: u16,
client_address: Ipv4Addr,
server_address: Ipv4Addr,
gateway_address: Ipv4Addr,
client_hardware_address: &'a [u8],
server_hostname: &'a str,
boot_file_name: &'a Path,
}
#[derive(Debug)]
enum Operation {
Request = 1,
Reply = 2,
}
impl From<u8> for Operation {
fn from(value: u8) -> Self {
match value {
1 => Operation::Request,
2 => Operation::Reply,
_ => todo!(),
}
}
}
#[derive(Debug)]
enum HardwareType {
Reserved(u8),
Unassigned(u8),
Ethernet,
}
impl From<u8> for HardwareType {
fn from(value: u8) -> Self {
use HardwareType::*;
match value {
a @ 0 => Reserved(a),
1 => Ethernet,
v => Unassigned(v),
}
}
}
fn parse_dhcp_operation(i: &[u8]) -> IResult<&[u8], Operation> {
be_u8(i).map(|(i, v)| (i, v.into()))
}
fn parse_dhcp_hardware_type(i: &[u8]) -> IResult<&[u8], HardwareType> {
be_u8(i).map(|(i, v)| (i, v.into()))
}
fn parse_ipv4_address(i: &[u8]) -> IResult<&[u8], Ipv4Addr> {
let (i, val) = take(4usize)(i)?;
let ip = Ipv4Addr::new(val[0], val[1], val[2], val[3]);
Ok((i, ip))
}
fn parse_dhcp_packet(packet: &[u8]) -> IResult<&[u8], DhcpMessage> {
let (packet, operation) = parse_dhcp_operation(packet)?;
let (packet, hardware_type) = parse_dhcp_hardware_type(packet)?;
let (packet, hardware_address_length) = be_u8(packet)?;
let (packet, hops) = be_u8(packet)?;
let (packet, transaction_id) = be_u32(packet)?;
let (packet, seconds) = be_u16(packet)?;
let (packet, flags) = be_u16(packet)?;
let (packet, client_address) = parse_ipv4_address(packet)?;
let (packet, server_address) = parse_ipv4_address(packet)?;
let (packet, gateway_address) = parse_ipv4_address(packet)?;
let (packet, client_hardware_address) = take(6usize)(packet)?;
Ok((
packet,
DhcpMessage {
operation,
hardware_type,
hardware_address_length,
hops,
transaction_id,
seconds,
flags,
client_address,
server_address,
gateway_address,
client_hardware_address,
server_hostname: "",
boot_file_name: Path::new(""),
},
))
}
#[cfg(test)]
mod tests {
static DHCP_PACKET: &'static [u8] = include_bytes!("../dhcp_discover.bin");
static DHCP_PACKET2: &'static [u8] = &[
0x01, 0x01, 0x06, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x7f, 0x00, 0x00,
0x01, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01, 0x54, 0x05, 0xdb, 0xa6, 0x96, 0xd0,
];
#[test]
fn parses() {
let res = super::parse_dhcp_packet(DHCP_PACKET2).unwrap();
dbg!(res);
}
}