diff --git a/src/tools/hexnet.cpp b/src/tools/hexnet.cpp index 574df87..77c795d 100644 --- a/src/tools/hexnet.cpp +++ b/src/tools/hexnet.cpp @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include #include @@ -42,10 +44,13 @@ std::string host; in_port_t port; sockpp::tcp_socket* tcpSocket; sockpp::tcp6_socket* tcp6Socket; +sockpp::udp_socket* udpSocket; sockpp::tcp_acceptor tcpAcceptor; sockpp::tcp6_acceptor tcp6Acceptor; std::mutex tcpSocketMutex; std::mutex tcp6SocketMutex; +std::mutex udpSocketMutex; +std::mutex udp6SocketMutex; std::mutex consoleMutex; // used for coordinated graceful exit across threads bool exitProgram = false; @@ -119,6 +124,27 @@ void readFromTCP6Socket(sockpp::tcp6_socket* socket, int64_t mtu){ consoleMutex.unlock(); } +void readFromUDPSocket(sockpp::udp_socket* socket, int64_t mtu){ + ssize_t numBytes; + uint8_t buffer[mtu]; + typename sockpp::udp_socket::addr_t srcAddr; + udpSocketMutex.lock(); + while(!exitProgram && (numBytes = socket->recv_from(buffer, sizeof(buffer), &srcAddr) > 0)){ + udpSocketMutex.unlock(); + consoleMutex.lock(); + for(ssize_t i=0; ibind(sockpp::inet_address("localhost", port))){ + std::cerr << "Error while binding UDP socket: " << udpSocket->last_error_str() << std::endl; + return EXIT_RUNTIME; + } + + std::thread threadReadFromUDP = std::thread(readFromUDPSocket, udpSocket, mtu); + threadReadFromUDP.join(); + + delete udpSocket; + return EXIT_SUCCESS; } if (ipv6) { - //std::cerr << "IPv6 support is not implented yet." << std::endl; - //return EXIT_UNIMPLEMENTED; std::cerr << "Listening on port " << port << "." << std::endl; - sockpp::socket_initializer socketInitializer; + tcp6Acceptor = sockpp::tcp6_acceptor(port); if(!tcp6Acceptor){ @@ -258,7 +293,7 @@ int main(int argc, char* argv[]){ if(ipv4){ std::cerr << "Listening on port " << port << "." << std::endl; - sockpp::socket_initializer socketInitializer; + tcpAcceptor = sockpp::tcp_acceptor(port); if (!tcpAcceptor) {