aboutsummaryrefslogtreecommitdiff
path: root/lib/dhcp/dhcp.cc
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2022-01-14 23:51:05 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2022-01-14 23:51:05 +0100
commitf9928a1a08c57fe853888119a996c3acc98ee09d (patch)
treec9770b76ffcc281da141f3aa2c595600372c0fca /lib/dhcp/dhcp.cc
downloadpio-nodemcuv2-dhcp-server-main.tar.gz
pio-nodemcuv2-dhcp-server-main.zip
Initial version of nodemcuv2 dhcp serverHEADmain
Able to offer IP address + DNS/Gateway ... Worked with devices at my hand.
Diffstat (limited to 'lib/dhcp/dhcp.cc')
-rw-r--r--lib/dhcp/dhcp.cc36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/dhcp/dhcp.cc b/lib/dhcp/dhcp.cc
new file mode 100644
index 0000000..1e5f785
--- /dev/null
+++ b/lib/dhcp/dhcp.cc
@@ -0,0 +1,36 @@
+// Copyright (c) 2022 Johannes Stoelp
+
+#include "dhcp.h"
+#include "utils.h"
+
+std::optional<struct option_view> get_option(const u8* opt, usize len, dhcp_option search_tag) {
+ const u8* end = opt + len;
+
+ while (opt < end) {
+ // Get tag of current option.
+ dhcp_option tag = from_raw<dhcp_option>(*opt++);
+
+ if (tag == dhcp_option::END) {
+ break;
+ }
+
+ // Extract length of current option.
+ usize len = *opt++;
+
+ if (tag == search_tag) {
+ if (static_cast<usize>(end - opt) < len) {
+ // If length is malformed.
+ return std::nullopt;
+ } else {
+ // Option found.
+ return {{opt, len}};
+ }
+ }
+
+ // Advance option iterator to beginning of next option.
+ opt = opt + len;
+ }
+
+ // Option not found.
+ return std::nullopt;
+}