tools/hexnet: Implement TCP v6 handler, rename readFromTCPSocket to readFromTCP

Soda
BodgeMaster 2022-10-22 01:18:44 +02:00
parent 5574cdb4bf
commit 7ae843039c
1 changed files with 61 additions and 8 deletions

View File

@ -23,6 +23,8 @@
#include <string> #include <string>
#include <sockpp/tcp_acceptor.h> #include <sockpp/tcp_acceptor.h>
#include <sockpp/tcp_connector.h> #include <sockpp/tcp_connector.h>
#include <sockpp/tcp6_acceptor.h>
#include <sockpp/tcp6_connector.h>
#include <thread> #include <thread>
#include <vector> #include <vector>
@ -40,6 +42,11 @@ sockpp::tcp_socket tcpSocket;
sockpp::tcp_acceptor tcpAcceptor; sockpp::tcp_acceptor tcpAcceptor;
// TCP v4 client // TCP v4 client
sockpp::tcp_connector tcpConnector; sockpp::tcp_connector tcpConnector;
// TCP v6 server
sockpp::tcp6_socket tcp6Socket;
sockpp::tcp6_acceptor tcp6Acceptor;
// TCP v6 client
sockpp::tcp6_connector tcp6Connector;
bool exitProgram = false; bool exitProgram = false;
@ -66,7 +73,7 @@ void signalHandler(int signal) {
std::exit(EXIT_SIGNAL); std::exit(EXIT_SIGNAL);
} }
void readFromTCPSocket() { void readFromTCP() {
ssize_t byteCount; ssize_t byteCount;
uint8_t buffer[1536]; uint8_t buffer[1536];
while (!exitProgram && (outgoing? (byteCount = tcpConnector.read(buffer, sizeof(buffer))) > 0 : (byteCount = tcpSocket.read(buffer, sizeof(buffer))) > 0)) { while (!exitProgram && (outgoing? (byteCount = tcpConnector.read(buffer, sizeof(buffer))) > 0 : (byteCount = tcpSocket.read(buffer, sizeof(buffer))) > 0)) {
@ -77,6 +84,17 @@ void readFromTCPSocket() {
} }
} }
void readFromTCP6() {
ssize_t byteCount;
uint8_t buffer[1536];
while (!exitProgram && (outgoing? (byteCount = tcp6Connector.read(buffer, sizeof(buffer))) > 0 : (byteCount = tcp6Socket.read(buffer, sizeof(buffer))) > 0)) {
for (ssize_t i=0; i<byteCount; i++) {
std::cout << std::hex << std::setfill('0') << std::setw(2) << (short) buffer[i] << " ";
}
std::cout.flush();
}
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::signal(SIGINT, signalHandler); std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler); std::signal(SIGTERM, signalHandler);
@ -156,8 +174,8 @@ int main(int argc, char* argv[]) {
return EXIT_RUNTIME; return EXIT_RUNTIME;
} }
std::thread threadReadFromTCP = std::thread(readFromTCPSocket); std::thread threadReadFromTCP = std::thread(readFromTCP);
//std::thread threadWriteToTCP = std::thread(writeToTCPSocket); //std::thread threadWriteToTCP = std::thread(writeToTCP);
threadReadFromTCP.join(); threadReadFromTCP.join();
//threadWriteToTCP.join(); //threadWriteToTCP.join();
@ -166,10 +184,24 @@ int main(int argc, char* argv[]) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else { } else {
// TCP v6 out // TCP v6 out
return EXIT_UNIMPLEMENTED; tcp6Connector = sockpp::tcp6_connector({host, port});
if (!tcp6Connector) {
std::cerr << "Error connecting to " << host << " on port " << port << std::endl;
std::cerr << tcp6Connector.last_error_str() << std::endl;
return EXIT_RUNTIME;
}
std::thread threadReadFromTCP6 = std::thread(readFromTCP6);
//std::thread threadWriteToTCP6 = std::thread(writeToTCP6);
threadReadFromTCP6.join();
//threadWriteToTCP6.join();
std::cout << std::endl;
return EXIT_SUCCESS;
} }
} else { } else {
std::cerr << "Connecting to " << host << " on port " << (int) port << " (UDP)..." << std::endl; std::cerr << "Talking to " << host << " on port " << (int) port << " (UDP)..." << std::endl;
if (ipv4) { if (ipv4) {
// UDP v4 out // UDP v4 out
return EXIT_UNIMPLEMENTED; return EXIT_UNIMPLEMENTED;
@ -197,8 +229,8 @@ int main(int argc, char* argv[]) {
return EXIT_RUNTIME; return EXIT_RUNTIME;
} }
std::thread threadReadFromTCP = std::thread(readFromTCPSocket); std::thread threadReadFromTCP = std::thread(readFromTCP);
//std::thread threadWriteToTCP = std::thread(writeToTCPSocket); //std::thread threadWriteToTCP = std::thread(writeToTCP);
threadReadFromTCP.join(); threadReadFromTCP.join();
//threadWriteToTCP.join(); //threadWriteToTCP.join();
@ -207,7 +239,28 @@ int main(int argc, char* argv[]) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else { } else {
// TCP v6 in // TCP v6 in
return EXIT_UNIMPLEMENTED; tcp6Acceptor = sockpp::tcp6_acceptor(port);
if (!tcp6Acceptor) {
std::cerr << "Error while creating TCP acceptor: " << tcp6Acceptor.last_error_str() << std::endl;
return EXIT_RUNTIME;
}
sockpp::inet6_address peer;
tcp6Socket = tcp6Acceptor.accept(&peer);
std::cerr << "Incoming connection from " << peer << std::endl;
if (!tcp6Socket) {
std::cerr << "Error on incoming connection: " << tcp6Acceptor.last_error_str() << std::endl;
return EXIT_RUNTIME;
}
std::thread threadReadFromTCP6 = std::thread(readFromTCP6);
//std::thread threadWriteToTCP6 = std::thread(writeToTCP6);
threadReadFromTCP6.join();
//threadWriteToTCP6.join();
std::cout << std::endl;
return EXIT_SUCCESS;
} }
} else { } else {
std::cerr << "Listening on port " << (int) port << " (UDP)..." << std::endl; std::cerr << "Listening on port " << (int) port << " (UDP)..." << std::endl;