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.
BodgeMaster-unfinished
BodgeMaster 2022-07-12 05:13:01 +02:00
parent eaedc0cb56
commit b3cd8709fb
2 changed files with 127 additions and 1 deletions

View File

@ -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]}"

125
src/tools/hexnet.cpp Normal file
View File

@ -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 <iostream>
#include <string>
#include <cstdint>
#include <cctype>
#define usage std::cerr << "Usage: " << argv[0] << " [-4|-6] [-t|-u] <port>" << std::endl << "<port> 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; i<argc; i++) {
std::string argument(argv[i]);
if (argument=="-4") {
ipv4 = true;
ipv6 = false;
continue;
}
if (argument=="-6") {
ipv4 = false;
ipv6 = true;
continue;
}
if (argument=="-t") {
tcp = true;
udp = false;
continue;
}
if (argument=="-u") {
tcp = false;
udp = true;
continue;
}
if (argument=="-h") {
usage;
return 0;
}
if (argument=="--help") {
usage;
return 0;
}
if (i==argc-1) {
if (argument.substr(0, 2)=="0b" || argument.substr(0, 2)=="0B") {
// check for 16 bit binary number
for (char const &digit : argument.substr(2,argument.length())) {
if (digit=='0' || digit=='1') {
;
} else {
std::cerr << argument << " is not a valid port." << std::endl;
usage;
return 1;
}
}
if (argument.length()>18) {
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
}