Skip to content

Commit

Permalink
now supporting proper responses in UDP server mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dannagle committed Jun 6, 2024
1 parent 36441a9 commit 36a3075
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -516,6 +517,8 @@ Options:
translation).
-l, --listen Listen instead of send. Use bind options to specify
port/IP. Otherwise, dynamic/All.
-r, --response <ascii> Server mode response data in mixed-ascii. Macro
supported.
-w, --wait <ms> Wait up to <milliseconds> for a response after
sending. Zero means do not wait (Default).
-f, --file <path> Send contents of specified path. Max 10 MiB for
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/mainpacketreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
2 changes: 1 addition & 1 deletion src/packetnetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 13 additions & 4 deletions src/tcpthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TCPThread::TCPThread(int socketDescriptor, QObject *parent)
insidePersistent = false;
isSecure = false;
packetReply.clear();
consoleMode = false;


}
Expand All @@ -47,6 +48,7 @@ TCPThread::TCPThread(Packet sendPacket, QObject *parent)
incomingPersistent = false;
insidePersistent = false;
isSecure = false;
consoleMode = false;
}

void TCPThread::sendAnother(Packet sendPacket)
Expand Down Expand Up @@ -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<SmartResponseConfig> 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()) {

Expand Down
1 change: 1 addition & 0 deletions src/tcpthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TCPThread : public QThread
bool isSecure;
bool isEncrypted();
Packet packetReply;
bool consoleMode;

signals:
void error(QSslSocket::SocketError socketError);
Expand Down
1 change: 1 addition & 0 deletions src/threadedtcpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 36a3075

Please sign in to comment.