//Copyright 2022, FOSS-VG Developers and Contributers // // Author(s): // BodgeMaster, Shwoomple // //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 #include #include #include #include #include #include "../lib/cli.hpp" #include "../lib/error.hpp" #define EXIT_SUCCESS 0 #define EXIT_RUNTIME 1 #define EXIT_USAGE 2 #define EXIT_UNIMPLEMENTED 3 // TCP v4 server sockpp::tcp_socket tcpSocket; sockpp::tcp_acceptor tcpAcceptor; // TCP v4 client sockpp::tcp_connector tcpConnector; bool exitProgram = false; bool ipv4 = false; bool ipv6 = false; bool tcp = false; bool udp = false; bool outgoing = false; std::string host; in_port_t port; void signalHandler(int signal) { // shut down gracefully exitProgram = true; // tell sockpp to close TCP socket if open because it blocks when trying // to read and there is no data tcpAcceptor.shutdown(); if (tcpSocket) { tcpSocket.shutdown(SHUT_RD); } std::cerr << "Received signal " << signal << ", bye!" << std::endl; std::exit(signal); } void readFromTCPSocket() { ssize_t byteCount; uint8_t buffer[1536]; while (!exitProgram && (outgoing? (byteCount = tcpConnector.read(buffer, sizeof(buffer))) > 0 : (byteCount = tcpSocket.read(buffer, sizeof(buffer))) > 0)) { for (ssize_t i=0; i flags; flags.push_back(CLI::Flag('4', "ipv4", "use IPv4, either this or IPv6 has to be specified")); flags.push_back(CLI::Flag('6', "ipv6", "use IPv6, either this or IPv4 has to be specified")); flags.push_back(CLI::Flag('t', "tcp", "use TCP, either this or UDP has to be specified")); flags.push_back(CLI::Flag('u', "udp", "use UDP, either this or TCP has to be specified")); flags.push_back(CLI::Flag('h', "help", "print this information and exit")); flags.push_back(CLI::Flag('l', "license", "print license information and exit")); std::vector options; options.push_back(CLI::Option('c', "connect", "HOST", "make an outgoing connection to HOST instead of listening for an incoming connection")); std::vector arguments; arguments.push_back(CLI::Argument("PORT", "the port to lsiten on (or connect to)")); CLI::ArgumentsParser cliParser = CLI::ArgumentsParser(argc, argv, flags, options, arguments, "Arbitrary TCP/UDP connections in hex format", "Note: Make sure your terminal is set up so it doesn't buffer input and, if desired, disable input echoing.\n\tSomething like `stty -echo cbreak` should do but it will also disable some key combinations."); if (cliParser.getFlag("help").value) { std::cout << cliParser.getUsage() << std::endl; return EXIT_SUCCESS; } if (cliParser.getFlag("license").value) { std::cout << "Copyright 2022, FOSS-VG Developers and Contributers\n" << "\n" << "Hexnet is part of the FOSS-VG development tool suite.\n" << "\n" << "This program is free software: you can redistribute it and/or modify it\n" << "under the terms of the GNU Affero General Public License as published\n" << "by the Free Software Foundation, version 3.\n" << "\n" << "This program is distributed in the hope that it will be useful,\n" << "but WITHOUT ANY WARRANTY; without even the implied\n" << "warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" << "See the GNU Affero General Public License for more details.\n" << "\n" << "You should have received a copy of the GNU Affero General Public License\n" << "version 3 along with this program.\n" << "If not, see https://www.gnu.org/licenses/agpl-3.0.en.html" << std::endl; return EXIT_SUCCESS; } if (cliParser.wrongUsage) { std::cout << cliParser.getUsage() << std::endl; return EXIT_USAGE; } ipv4 = cliParser.getFlag("ipv4").value; ipv6 = cliParser.getFlag("ipv6").value; tcp = cliParser.getFlag("tcp").value; udp = cliParser.getFlag("udp").value; if (cliParser.getOption("connect").errorCode != ErrorCodes::NOT_PRESENT) { outgoing = true; host = cliParser.getOption("connect").value; } if (!(ipv4 || ipv6) || (ipv4 && ipv6) || !(tcp || udp) || (tcp && udp)) { std::cout << cliParser.getUsage() << std::endl; return EXIT_USAGE; } port = (in_port_t) std::stoi(cliParser.getArgument(0).value); if (outgoing) { if (tcp) { std::cerr << "Connecting to " << host << " on port " << (int) port << " (TCP)..." << std::endl; if (ipv4) { // TCP v4 out tcpConnector = sockpp::tcp_connector({host, port}); if (!tcpConnector) { std::cerr << "Error connecting to " << host << " on port " << port << std::endl; std::cerr << tcpConnector.last_error_str() << std::endl; return EXIT_RUNTIME; } std::thread threadReadFromTCP = std::thread(readFromTCPSocket); //std::thread threadWriteToTCP = std::thread(writeToTCPSocket); threadReadFromTCP.join(); //threadWriteToTCP.join(); std::cout << std::endl; return EXIT_SUCCESS; } else { // TCP v6 out return EXIT_UNIMPLEMENTED; } } else { std::cerr << "Connecting to " << host << " on port " << (int) port << " (UDP)..." << std::endl; if (ipv4) { // UDP v4 out return EXIT_UNIMPLEMENTED; } else { // UDP v6 out return EXIT_UNIMPLEMENTED; } } } else { if (tcp) { std::cerr << "Listening on port " << (int) port << " (TCP)..." << std::endl; if (ipv4) { // TCP v4 in tcpAcceptor = sockpp::tcp_acceptor(port); if (!tcpAcceptor) { std::cerr << "Error while creating TCP acceptor: " << tcpAcceptor.last_error_str() << std::endl; return EXIT_RUNTIME; } sockpp::inet_address peer; tcpSocket = tcpAcceptor.accept(&peer); std::cerr << "Incoming connection from " << peer << std::endl; if (!tcpSocket) { std::cerr << "Error on incoming connection: " << tcpAcceptor.last_error_str() << std::endl; return EXIT_RUNTIME; } std::thread threadReadFromTCP = std::thread(readFromTCPSocket); //std::thread threadWriteToTCP = std::thread(writeToTCPSocket); threadReadFromTCP.join(); //threadWriteToTCP.join(); std::cout << std::endl; return EXIT_SUCCESS; } else { // TCP v6 in return EXIT_UNIMPLEMENTED; } } else { std::cerr << "Listening on port " << (int) port << " (UDP)..." << std::endl; if (ipv4) { // UDP v4 in return EXIT_UNIMPLEMENTED; } else { // UDP v6 in return EXIT_UNIMPLEMENTED; } } } }