From b3cd8709fb813367c980ef54e43d4feda7f7a3e8 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 12 Jul 2022 05:13:01 +0200 Subject: [PATCH] Tools: Add WIP hexnet tool. This will be similar to hexdump and netcat in one. Yes, I am using this as an exercise for myself to figure out networking. --- scripts/build.sh | 3 +- src/tools/hexnet.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/tools/hexnet.cpp diff --git a/scripts/build.sh b/scripts/build.sh index e02b832..720f544 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -72,7 +72,8 @@ echo "Building tools..." mkdir -pv bin/tools # add compile commands to this array COMPILE_COMMANDS=( - "$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -Lbin/lib -l:nbt.so -o bin/tools/dumpnbt" + "$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -Lbin/lib -l:nbt.so -o bin/tools/dumpnbt", + "$CXX_WITH_FLAGS src/tools/hexnet.cpp -Ldependencies/sockpp-0.7.1/build -l:libsockpp.so -Idependencies/sockpp-0.7.1/include -o bin/tools/hexnet" ) for command in ${!COMPILE_COMMANDS[@]}; do echo "${COMPILE_COMMANDS[command]}" diff --git a/src/tools/hexnet.cpp b/src/tools/hexnet.cpp new file mode 100644 index 0000000..28483e8 --- /dev/null +++ b/src/tools/hexnet.cpp @@ -0,0 +1,125 @@ +//Copyright 2022, FOSS-VG Developers and Contributers +// +//This program is free software: you can redistribute it and/or modify it +//under the terms of the GNU Affero General Public License as published +//by the Free Software Foundation, version 3. +// +//This program is distributed in the hope that it will be useful, +//but WITHOUT ANY WARRANTY; without even the implied +//warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +//See the GNU Affero General Public License for more details. +// +//You should have received a copy of the GNU Affero General Public License +//version 3 along with this program. +//If not, see https://www.gnu.org/licenses/agpl-3.0.en.html + +#include +#include +#include +#include + +#define usage std::cerr << "Usage: " << argv[0] << " [-4|-6] [-t|-u] " << std::endl << " may be hexadecimal (prefixed with 0x) or binary (prefixed with 0b)." << std::endl + +int main(int argc, char* argv[]){ + + // Argument parsing ################################################ + bool ipv4 = true; + bool ipv6 = true; + bool tcp = true; + bool udp = true; + uint16_t port; + if (argc<2) { + usage; + return 1; + } + for (int i=1; i18) { + std::cerr << argument << " is too big for a valid port." << std::endl; + usage; + return 1; + } + port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 2); + } else if (argument.substr(0, 2)=="0x" || argument.substr(0, 2)=="0X") { + // check for four digit hex number + for (char const &digit : argument.substr(2,argument.length())) { + if (std::isxdigit(digit)==0) { + std::cerr << argument << " is not a valid port." << digit << std::endl; + usage; + return 1; + } + } + if (argument.length()>6) { + std::cerr << argument << " is too big for a valid port." << std::endl; + usage; + return 1; + } + port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 16); + } else { + // check for decimal number between 0 and 65536 exclusively + for (char const &digit : argument) { + if (std::isdigit(digit)==0) { + std::cerr << argument << " is not a valid port." << std::endl; + usage; + return 1; + } + } + // using unsigned long here because that's how stoul() + // is defined + unsigned long argumentNumber = std::stoul(argument, nullptr, 10); + if (argumentNumber > 65535) { + std::cerr << argument << " is too big for a valid port." << std::endl; + usage; + return 1; + } + port = (uint16_t) argumentNumber; + } + break; // last argument anyway + } + usage; + return 1; + } + // Argument parsing end ############################################ + + //TODO: implement the actual tool +}