add handler logic to handle SysCableCheck
Add handler logic to handler for SysCableCheck such that it splits the
true IPMI processing from the business logic.
Tested: Only ran unit-tests (added new ones).
Change-Id: Ieec35cc8839dcd3cfb864b68ffbd1a45d1326fee
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/handler.cpp b/handler.cpp
index 6a346ba..8d06f36 100644
--- a/handler.cpp
+++ b/handler.cpp
@@ -16,6 +16,17 @@
#include "handler.hpp"
+#include "errors.hpp"
+
+#include <ipmid/api.h>
+
+#include <cstdio>
+#include <filesystem>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <tuple>
+
// The phosphor-host-ipmi daemon requires a configuration that maps
// the if_name to the IPMI LAN channel. However, that doesn't strictly
// define which is meant to be used for NCSI.
@@ -36,12 +47,53 @@
{
namespace ipmi
{
+namespace fs = std::filesystem;
std::tuple<std::uint8_t, std::string> Handler::getEthDetails() const
{
return std::make_tuple(NCSI_IPMI_CHANNEL, NCSI_IF_NAME_STR);
}
+std::int64_t Handler::getRxPackets(const std::string& name) const
+{
+ std::ostringstream opath;
+ opath << "/sys/class/net/" << name << "/statistics/rx_packets";
+ std::string path = opath.str();
+
+ // Minor sanity & security check (of course, I'm less certain if unicode
+ // comes into play here.
+ //
+ // Basically you can't easily inject ../ or /../ into the path below.
+ if (name.find("/") != std::string::npos)
+ {
+ std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
+ throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
+ }
+
+ std::error_code ec;
+ if (!fs::exists(path, ec))
+ {
+ std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
+ throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
+ }
+ // We're uninterested in the state of ec.
+
+ int64_t count = 0;
+ std::ifstream ifs;
+ ifs.exceptions(std::ifstream::failbit);
+ try
+ {
+ ifs.open(path);
+ ifs >> count;
+ }
+ catch (std::ios_base::failure& fail)
+ {
+ throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
+ }
+
+ return count;
+}
+
Handler handlerImpl;
} // namespace ipmi