From 36a3075c772b7e90013c9a77c3212514d6c1f578 Mon Sep 17 00:00:00 2001 From: Dan Nagle Date: Wed, 5 Jun 2024 21:02:50 -0500 Subject: [PATCH] now supporting proper responses in UDP server mode --- README.md | 23 +++++++++++++++++++++++ src/mainpacketreceiver.cpp | 5 ++++- src/mainwindow.cpp | 8 +++++++- src/packetnetwork.cpp | 2 +- src/tcpthread.cpp | 17 +++++++++++++---- src/tcpthread.h | 1 + src/threadedtcpserver.cpp | 1 + 7 files changed, 50 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d9374d5c..01a1a15e 100644 --- a/README.md +++ b/README.md @@ -506,6 +506,7 @@ See https://PacketSender.com/ for more information. Options: -h, --help Displays help on commandline options. + --help-all Displays help including Qt specific options. -v, --version Displays version information. -q, --quiet Quiet mode. Only output received data. -x, --hex Parse data-to-send as hex (default for @@ -516,6 +517,8 @@ Options: translation). -l, --listen Listen instead of send. Use bind options to specify port/IP. Otherwise, dynamic/All. + -r, --response Server mode response data in mixed-ascii. Macro + supported. -w, --wait Wait up to for a response after sending. Zero means do not wait (Default). -f, --file Send contents of specified path. Max 10 MiB for @@ -568,6 +571,7 @@ Use the existing bind options to configure the server. - -b for port - -B for iP - -t/-u/-s for TCP, UDP, or SSL +- -r to send an response (macro supported ASCII) Binding to dynamic port using TCP ```text @@ -598,6 +602,25 @@ Response HEX:48 65 6C 6C 6F 20 55 44 50 20 50 61 63 6B 65 74 Response ASCII:Hello UDP Packet ``` +Binding to port 8080 using UDP with current time response +```text +packetsender -l -u -b 8080 -r "{{TIME}}" +Loading response packet. +UDP Server started on 0.0.0.0:8080 +Use ctrl+c to exit. + +From: ::ffff:127.0.0.1, Port:59594 +Response Time:2024-06-05 20:48:18.180 +Response HEX:68 65 6C 6C 6F 20 70 61 63 6B 65 74 20 73 65 6E 64 65 72 +Response ASCII:hello packet sender + +From: You (Response), Port:59594 +Response Time:2024-06-05 20:48:18.182 +Response HEX:30 38 3a 34 38 3a 31 38 20 70 6d +Response ASCII:08:48:18 pm +``` + + Binding to IP 192.168.86.26, port 54321 using SSL ```text packetsender -l -s -B 192.168.86.26 -b 54321 diff --git a/src/mainpacketreceiver.cpp b/src/mainpacketreceiver.cpp index a09bbd44..dc0931cb 100644 --- a/src/mainpacketreceiver.cpp +++ b/src/mainpacketreceiver.cpp @@ -65,7 +65,10 @@ void MainPacketReceiver::readPendingDatagrams() QString data = Packet::macroSwap(packetReply.asciiString()); sendit.hexString = Packet::ASCIITohex(data); - emit sendPacket(sendit); + QHostAddress resolved = PacketNetwork::resolveDNS(sendit.toIP); + + udpSocket->writeDatagram(sendit.getByteArray(), resolved, sendit.port); + out << "\nFrom: " << sendit.fromIP << ", Port:" << sendit.port; out << "\nResponse Time:" << QDateTime::currentDateTime().toString(DATETIMEFORMAT); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 646f3f4e..09c4ae37 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -725,7 +725,13 @@ void MainWindow::toTrafficLog(Packet logPacket) if ((!logPacket.toIP.isEmpty() && !logPacket.fromIP.isEmpty()) || (!logPacket.errorString.isEmpty()) ) { - packetsLogged.prepend(logPacket); + + if(logPacket.isTCP() && logPacket.hexString.isEmpty() && logPacket.errorString.isEmpty()) { + QDEBUG() << "Discarded empty TCP packet"; + } else { + packetsLogged.prepend(logPacket); + } + } else { QDEBUG() << "Discarded packet"; } diff --git a/src/packetnetwork.cpp b/src/packetnetwork.cpp index 4c5272bf..b3097545 100755 --- a/src/packetnetwork.cpp +++ b/src/packetnetwork.cpp @@ -601,7 +601,7 @@ void PacketNetwork::readPendingDatagrams() sender = theDatagram.senderAddress(); senderPort = theDatagram.senderPort(); - QDEBUG() << "data size is" << datagram.size(); + QDEBUG() << "data size is" << datagram.size() << "Sender is" << sender << ":" << senderPort; // QDEBUG() << debugQByteArray(datagram); Packet udpPacket; diff --git a/src/tcpthread.cpp b/src/tcpthread.cpp index 925e148d..9d8f40f8 100755 --- a/src/tcpthread.cpp +++ b/src/tcpthread.cpp @@ -36,6 +36,7 @@ TCPThread::TCPThread(int socketDescriptor, QObject *parent) insidePersistent = false; isSecure = false; packetReply.clear(); + consoleMode = false; } @@ -47,6 +48,7 @@ TCPThread::TCPThread(Packet sendPacket, QObject *parent) incomingPersistent = false; insidePersistent = false; isSecure = false; + consoleMode = false; } void TCPThread::sendAnother(Packet sendPacket) @@ -144,25 +146,32 @@ void TCPThread::writeResponse(QSslSocket *sock, Packet tcpPacket) bool sendResponse = settings.value("sendReponse", false).toBool(); bool sendSmartResponse = settings.value("smartResponseEnableCheck", false).toBool(); QList smartList; + QString responseData = (settings.value("responseHex", "")).toString(); + int ipMode = settings.value("ipMode", 4).toInt(); smartList.clear(); + smartList.append(Packet::fetchSmartConfig(1, SETTINGSFILE)); smartList.append(Packet::fetchSmartConfig(2, SETTINGSFILE)); smartList.append(Packet::fetchSmartConfig(3, SETTINGSFILE)); smartList.append(Packet::fetchSmartConfig(4, SETTINGSFILE)); smartList.append(Packet::fetchSmartConfig(5, SETTINGSFILE)); + QByteArray smartData; + smartData.clear(); - QString responseData = (settings.value("responseHex", "")).toString(); - int ipMode = settings.value("ipMode", 4).toInt(); + if(consoleMode) { + responseData.clear(); + sendResponse = false; + sendSmartResponse = false; + } - QByteArray smartData; - smartData.clear(); if (sendSmartResponse) { smartData = Packet::smartResponseMatch(smartList, tcpPacket.getByteArray()); } + // This is pre-loaded from command line if(!packetReply.hexString.isEmpty()) { diff --git a/src/tcpthread.h b/src/tcpthread.h index 36e68293..7ef19f28 100755 --- a/src/tcpthread.h +++ b/src/tcpthread.h @@ -31,6 +31,7 @@ class TCPThread : public QThread bool isSecure; bool isEncrypted(); Packet packetReply; + bool consoleMode; signals: void error(QSslSocket::SocketError socketError); diff --git a/src/threadedtcpserver.cpp b/src/threadedtcpserver.cpp index 4cf09a56..2c16c76d 100644 --- a/src/threadedtcpserver.cpp +++ b/src/threadedtcpserver.cpp @@ -68,6 +68,7 @@ void ThreadedTCPServer::incomingConnection(qintptr socketDescriptor) TCPThread *thread = new TCPThread(socketDescriptor, this); thread->isSecure = encrypted; thread->packetReply = packetReply; + thread->consoleMode = consoleMode; QDEBUGVAR(thread->isSecure); if (persistentConnectCheck) { #ifndef CONSOLE_BUILD