From bbe97dd080228deaab790a9e591a08c819ff99df Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Tue, 5 Mar 2024 00:13:07 -0800 Subject: [PATCH] Improve CLI error handling for setmtu command --- one.cpp | 2 +- service/OneService.cpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/one.cpp b/one.cpp index 23912856c..8d37ebdf2 100644 --- a/one.cpp +++ b/one.cpp @@ -591,7 +591,7 @@ static int cli(int argc,char **argv) printf("200 setmtu OK" ZT_EOL_S); return 0; } else { - printf("no link match found, new MTU was not applied" ZT_EOL_S); + printf("%d Failed to set MTU: %s" ZT_EOL_S, scode, responseBody.c_str()); return 1; } return 0; diff --git a/service/OneService.cpp b/service/OneService.cpp index 866ed7282..8fee13648 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -1543,7 +1543,7 @@ public: // control plane endpoints std::string bondShowPath = "/bond/show/([0-9a-fA-F]{10})"; std::string bondRotatePath = "/bond/rotate/([0-9a-fA-F]{10})"; - std::string setBondMtuPath = "/bond/setmtu/([0-9]{3,5})/([a-zA-Z0-9_]{1,16})/([0-9a-fA-F\\.\\:]{1,39})"; + std::string setBondMtuPath = "/bond/setmtu/([0-9]{1,6})/([a-zA-Z0-9_]{1,16})/([0-9a-fA-F\\.\\:]{1,39})"; std::string configPath = "/config"; std::string configPostPath = "/config/settings"; std::string healthPath = "/health"; @@ -1719,12 +1719,21 @@ public: auto setMtu = [&, setContent](const httplib::Request &req, httplib::Response &res) { if (!_node->bondController()->inUse()) { - setContent(req, res, ""); + setContent(req, res, "Bonding layer isn't active yet"); + res.status = 400; + return; + } + uint32_t mtu = atoi(req.matches[1].str().c_str()); + if (mtu < 68 || mtu > 65535) { + setContent(req, res, "Specified MTU is not reasonable"); res.status = 400; return; } - uint16_t mtu = atoi(req.matches[1].str().c_str()); res.status = _node->bondController()->setAllMtuByTuple(mtu, req.matches[2].str().c_str(), req.matches[3].str().c_str()) ? 200 : 400; + if (res.status == 400) { + setContent(req, res, "Unable to find specified link"); + return; + } setContent(req, res, "{}"); }; _controlPlane.Post(setBondMtuPath, setMtu);