tools/hexnet: Implement udp partially.
parent
25bec4c587
commit
ca7b121c4d
|
@ -20,6 +20,8 @@
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <sockpp/tcp_acceptor.h>
|
#include <sockpp/tcp_acceptor.h>
|
||||||
#include <sockpp/tcp6_acceptor.h>
|
#include <sockpp/tcp6_acceptor.h>
|
||||||
|
#include <sockpp/udp_socket.h>
|
||||||
|
#include <sockpp/udp6_socket.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
@ -42,10 +44,13 @@ std::string host;
|
||||||
in_port_t port;
|
in_port_t port;
|
||||||
sockpp::tcp_socket* tcpSocket;
|
sockpp::tcp_socket* tcpSocket;
|
||||||
sockpp::tcp6_socket* tcp6Socket;
|
sockpp::tcp6_socket* tcp6Socket;
|
||||||
|
sockpp::udp_socket* udpSocket;
|
||||||
sockpp::tcp_acceptor tcpAcceptor;
|
sockpp::tcp_acceptor tcpAcceptor;
|
||||||
sockpp::tcp6_acceptor tcp6Acceptor;
|
sockpp::tcp6_acceptor tcp6Acceptor;
|
||||||
std::mutex tcpSocketMutex;
|
std::mutex tcpSocketMutex;
|
||||||
std::mutex tcp6SocketMutex;
|
std::mutex tcp6SocketMutex;
|
||||||
|
std::mutex udpSocketMutex;
|
||||||
|
std::mutex udp6SocketMutex;
|
||||||
std::mutex consoleMutex;
|
std::mutex consoleMutex;
|
||||||
// used for coordinated graceful exit across threads
|
// used for coordinated graceful exit across threads
|
||||||
bool exitProgram = false;
|
bool exitProgram = false;
|
||||||
|
@ -119,6 +124,27 @@ void readFromTCP6Socket(sockpp::tcp6_socket* socket, int64_t mtu){
|
||||||
consoleMutex.unlock();
|
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; i<numBytes; i++){
|
||||||
|
std::cout << std::hex << std::setfill('0') << std::setw(2) << (short) buffer[i];
|
||||||
|
}
|
||||||
|
std::cout.flush();
|
||||||
|
consoleMutex.unlock();
|
||||||
|
udpSocketMutex.lock();
|
||||||
|
}
|
||||||
|
udpSocketMutex.unlock();
|
||||||
|
consoleMutex.lock();
|
||||||
|
std::cerr << std::endl << "Connection closed." << std::endl;
|
||||||
|
consoleMutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
void writeToTCPSocket(sockpp::tcp_socket* socket){
|
void writeToTCPSocket(sockpp::tcp_socket* socket){
|
||||||
while (!exitProgram) {
|
while (!exitProgram) {
|
||||||
//TODO: Implement locking/unlocking/threading shenanigans
|
//TODO: Implement locking/unlocking/threading shenanigans
|
||||||
|
@ -138,6 +164,7 @@ void writeToTCP6Socket(sockpp::tcp6_socket* socket){
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
|
sockpp::socket_initializer socketInitializer;
|
||||||
|
|
||||||
std::signal(SIGINT, signalHandler);
|
std::signal(SIGINT, signalHandler);
|
||||||
std::signal(SIGTERM, signalHandler);
|
std::signal(SIGTERM, signalHandler);
|
||||||
|
@ -201,7 +228,7 @@ int main(int argc, char* argv[]){
|
||||||
}
|
}
|
||||||
if (cliParser.getFlag("ipv4").value || cliParser.getFlag("ipv6").value) {
|
if (cliParser.getFlag("ipv4").value || cliParser.getFlag("ipv6").value) {
|
||||||
ipv4 = cliParser.getFlag("ipv4").value;
|
ipv4 = cliParser.getFlag("ipv4").value;
|
||||||
ipv6 = cliParser.getFlag("ipv4").value;
|
ipv6 = cliParser.getFlag("ipv6").value;
|
||||||
}
|
}
|
||||||
if (cliParser.getFlag("tcp").value || cliParser.getFlag("udp").value) {
|
if (cliParser.getFlag("tcp").value || cliParser.getFlag("udp").value) {
|
||||||
tcp = cliParser.getFlag("tcp").value;
|
tcp = cliParser.getFlag("tcp").value;
|
||||||
|
@ -219,16 +246,24 @@ int main(int argc, char* argv[]){
|
||||||
port = (in_port_t) std::stoi(cliParser.getArgument(0).value);
|
port = (in_port_t) std::stoi(cliParser.getArgument(0).value);
|
||||||
|
|
||||||
if (listenMode) {
|
if (listenMode) {
|
||||||
if (udp) {
|
if (udp && ipv4) {
|
||||||
std::cerr << "UDP support is not implemented yet." << std::endl;
|
std::cerr << "Listening on port " << port << "." << std::endl;
|
||||||
return EXIT_UNIMPLEMENTED;
|
|
||||||
|
if(!udpSocket->bind(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) {
|
if (ipv6) {
|
||||||
//std::cerr << "IPv6 support is not implented yet." << std::endl;
|
|
||||||
//return EXIT_UNIMPLEMENTED;
|
|
||||||
std::cerr << "Listening on port " << port << "." << std::endl;
|
std::cerr << "Listening on port " << port << "." << std::endl;
|
||||||
sockpp::socket_initializer socketInitializer;
|
|
||||||
tcp6Acceptor = sockpp::tcp6_acceptor(port);
|
tcp6Acceptor = sockpp::tcp6_acceptor(port);
|
||||||
|
|
||||||
if(!tcp6Acceptor){
|
if(!tcp6Acceptor){
|
||||||
|
@ -258,7 +293,7 @@ int main(int argc, char* argv[]){
|
||||||
if(ipv4){
|
if(ipv4){
|
||||||
std::cerr << "Listening on port " << port << "." << std::endl;
|
std::cerr << "Listening on port " << port << "." << std::endl;
|
||||||
|
|
||||||
sockpp::socket_initializer socketInitializer;
|
|
||||||
tcpAcceptor = sockpp::tcp_acceptor(port);
|
tcpAcceptor = sockpp::tcp_acceptor(port);
|
||||||
|
|
||||||
if (!tcpAcceptor) {
|
if (!tcpAcceptor) {
|
||||||
|
|
Loading…
Reference in New Issue