diff --git a/src/tools/hexnet.cpp b/src/tools/hexnet.cpp index 6e3bf13..e73fb4b 100644 --- a/src/tools/hexnet.cpp +++ b/src/tools/hexnet.cpp @@ -17,8 +17,12 @@ //If not, see https://www.gnu.org/licenses/agpl-3.0.en.html #include +#include #include #include +#include +#include +#include #include #include @@ -30,22 +34,57 @@ #define EXIT_USAGE 2 #define EXIT_UNIMPLEMENTED 3 -std::mutex mutexStdin; -std::mutex mutexStdout; -std::mutex mutexNetIncoming; -std::mutex mutexNetOutgoing; +// 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 - std::cerr << "Received signal " << signal << ", shutting down." << std::endl; + 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]; + if (outgoing) { + while (!exitProgram && (byteCount = tcpConnector.read(buffer, sizeof(buffer))) > 0) { + for (ssize_t i=0; i 0) { + for (ssize_t i=0; i