From 2ad062c4643136eb6068a00bce492e2d7d1e630c Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sat, 10 Sep 2016 15:46:57 +0200 Subject: [PATCH] ifconfig program --- programs/ifconfig/Makefile | 14 +++++ programs/ifconfig/src/main.cpp | 111 +++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 programs/ifconfig/Makefile create mode 100644 programs/ifconfig/src/main.cpp diff --git a/programs/ifconfig/Makefile b/programs/ifconfig/Makefile new file mode 100644 index 00000000..0dd0190f --- /dev/null +++ b/programs/ifconfig/Makefile @@ -0,0 +1,14 @@ +.PHONY: default clean + +EXEC_NAME=ifconfig + +default: link + +include ../../cpp.mk + +$(eval $(call program_compile_cpp_folder,src)) +$(eval $(call program_link_executable,$(EXEC_NAME))) + +clean: + @ echo -e "Remove compiled files" + @ rm -rf debug diff --git a/programs/ifconfig/src/main.cpp b/programs/ifconfig/src/main.cpp new file mode 100644 index 00000000..42e18396 --- /dev/null +++ b/programs/ifconfig/src/main.cpp @@ -0,0 +1,111 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2016. +// Distributed under the terms of the MIT License. +// (See accompanying file LICENSE or copy at +// http://www.opensource.org/licenses/MIT) +//======================================================================= + +#include +#include +#include +#include +#include + +static constexpr const size_t BUFFER_SIZE = 4096; + +std::string read_file(const std::string& path){ + auto fd = tlib::open(path.c_str()); + + if(fd.valid()){ + auto info = tlib::stat(*fd); + + if(info.valid()){ + auto size = info->size; + + auto buffer = new char[size+1]; + + auto content_result = tlib::read(*fd, buffer, size); + + if(content_result.valid()){ + if(*content_result != size){ + //TODO Read more + } else { + buffer[size] = '\0'; + tlib::close(*fd); + return buffer; + } + } else { + tlib::printf("cat: error: %s\n", std::error_message(content_result.error())); + } + } else { + tlib::printf("cat: error: %s\n", std::error_message(info.error())); + } + + tlib::close(*fd); + } else { + tlib::printf("cat: error: %s\n", std::error_message(fd.error())); + } + + return ""; +} + +int main(int /*argc*/, char* /*argv*/[]){ + auto fd = tlib::open("/sys/net/"); + + if(fd.valid()){ + auto info = tlib::stat(*fd); + + if(info.valid()){ + auto entries_buffer = new char[BUFFER_SIZE]; + + auto entries_result = tlib::entries(*fd, entries_buffer, BUFFER_SIZE); + + if(entries_result.valid()){ + size_t position = 0; + + while(true){ + auto entry = reinterpret_cast(entries_buffer + position); + + std::string base_path = "/sys/net/"; + std::string entry_name = &entry->name; + + auto enabled = read_file(base_path + entry_name + "/enabled"); + + if(enabled == "true"){ + auto driver = read_file(base_path + entry_name + "/driver"); + auto ip = read_file(base_path + entry_name + "/ip"); + auto mac = read_file(base_path + entry_name + "/mac"); + + tlib::printf("%10s inet %s\n", &entry->name, ip.c_str()); + + if(!mac.empty() && mac != "0"){ + tlib::printf("%10s ether %s\n", "", mac.c_str()); + } + + tlib::printf("%10s driver %s\n", "", driver.c_str()); + } + + if(!entry->offset_next){ + break; + } + + tlib::printf("\n"); + + position += entry->offset_next; + } + } else { + tlib::printf("ifconfig: error: %s\n", std::error_message(entries_result.error())); + } + + delete[] entries_buffer; + } else { + tlib::printf("ifconfig: error: %s\n", std::error_message(info.error())); + } + + tlib::close(*fd); + } else { + tlib::printf("ifconfig: error: %s\n", std::error_message(fd.error())); + } + + return 0; +}