Skip to content

Commit

Permalink
added DTLS command line options and DTLS detection
Browse files Browse the repository at this point in the history
  • Loading branch information
dannagle committed Jul 30, 2024
1 parent 1a08cbb commit 54a8502
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 18 deletions.
102 changes: 85 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ int main(int argc, char *argv[])
QCommandLineOption udpOption(QStringList() << "u" << "udp", "Send UDP.");
parser.addOption(udpOption);


QCommandLineOption dtlsOption(QStringList() << "dtls", "Send DTLS.");
if(PacketNetwork::DTLSisSupported()) {
parser.addOption(dtlsOption);
}

// A single option with a value
QCommandLineOption httpOption(QStringList() << "http", "Send HTTP. Allowed values are GET (default) and POST", "http");
parser.addOption(httpOption);
Expand Down Expand Up @@ -341,8 +347,30 @@ int main(int argc, char *argv[])
}
bool tcp = parser.isSet(tcpOption);
bool udp = parser.isSet(udpOption);
bool dtls = parser.isSet(dtlsOption);

if(dtls) {
if(!PacketNetwork::DTLSisSupported()) {
OUTIF() << "DTLS is not supported in this installation. ";
OUTPUT();
return -1;
}
}


bool ssl = parser.isSet(sslOption);
bool sslNoError = parser.isSet(sslNoErrorOption);

if(ssl) {
if(!QSslSocket::supportsSsl()) {
OUTIF() << "SSL is not supported in this installation. ";
OUTPUT();
return -1;
}
}



bool ipv6 = parser.isSet(bindIPv6Option);
bool ipv4 = parser.isSet(bindIPv4Option);
bool http = parser.isSet(httpOption);
Expand Down Expand Up @@ -500,18 +528,30 @@ int main(int argc, char *argv[])
ipv6 = false;
ipv4 = true;
http = false;
dtls = false;
}


if (tcp && udp) {
OUTIF() << "Warning: both TCP and UDP set. Defaulting to TCP.";
udp = false;
dtls = false;
}
if (tcp && dtls) {
OUTIF() << "Warning: both TCP and DTLS set. Defaulting to TCP.";
udp = false;
dtls = false;
}
if (tcp && ssl) {
OUTIF() << "Warning: both TCP and SSL set. Defaulting to SSL.";
tcp = false;
}

if (udp && dtls) {
OUTIF() << "Warning: both UDP and DTLS set. Defaulting to DTLS.";
udp = false;
}

if (http && tcp) {
OUTIF() << "Warning: both HTTP and TCP set. Defaulting to HTTP.";
tcp = false;
Expand Down Expand Up @@ -576,11 +616,11 @@ int main(int argc, char *argv[])
}
}

if (!tcp && !udp && !ssl && !http) {
if (!tcp && !udp && !ssl && !http && !dtls) {
tcp = true;
}

if ((tcp || ssl || http) && intense) {
if ((tcp || ssl || dtls || http) && intense) {
OUTIF() << "Warning: Intense Traffic is UDP only.";
}

Expand All @@ -589,12 +629,12 @@ int main(int argc, char *argv[])
tcp = false;
ssl = false;
http = false;
dtls = false;
}

QSettings settings(SETTINGSFILE, QSettings::IniFormat);
bool translateMacroSend = settings.value("translateMacroSendCheck", true).toBool();


if(server) {
bool bindResult = false;
MainPacketReceiver * receiver = new MainPacketReceiver(nullptr);
Expand All @@ -615,24 +655,29 @@ int main(int argc, char *argv[])
}

QString bindmode = "";
if(udp) {
bindmode = "UDP";
QUdpSocket sock;
bindResult = receiver->initUDP(bindIP, bind);
bind = receiver->udpSocket->localPort();
bindIP = receiver->udpSocket->localAddress().toString();
if(dtls) {
// TODO: how to set up DTLS server?
} else {
if(tcp) {
bindmode = "TCP";
}
if(ssl) {
bindmode = "SSL";
if(udp) {
bindmode = "UDP";
QUdpSocket sock;
bindResult = receiver->initUDP(bindIP, bind);
bind = receiver->udpSocket->localPort();
bindIP = receiver->udpSocket->localAddress().toString();
} else {
if(tcp) {
bindmode = "TCP";
}
if(ssl) {
bindmode = "SSL";
}
bindResult = receiver->initSSL(bindIP, bind, ssl);
bind = receiver->tcpServer->serverPort();
bindIP = receiver->tcpServer->serverAddress().toString();
}
bindResult = receiver->initSSL(bindIP, bind, ssl);
bind = receiver->tcpServer->serverPort();
bindIP = receiver->tcpServer->serverAddress().toString();
}


bindIP = bindIP.toUpper();
if(bindResult) {
OUTIF() << bindmode << " Server started on " << bindIP << ":" << bind;
Expand Down Expand Up @@ -677,6 +722,7 @@ int main(int argc, char *argv[])
ssl = sendPacket.isSSL();
tcp = sendPacket.isTCP();
udp = sendPacket.isUDP();
dtls = sendPacket.isDTLS();
http = sendPacket.isHTTP() || sendPacket.isHTTPS();

if (data.isEmpty() && (!http)) {
Expand All @@ -696,14 +742,18 @@ int main(int argc, char *argv[])
udp = true;
ssl = false;
tcp = false;
dtls = false;
}
if (parser.isSet(tcpOption)) {
tcp = true;
http = false;
udp = false;
dtls = false;
}
if (parser.isSet(sslOption)) {
ssl = true;
tcp = true;
dtls = false;
http = false;
}

Expand All @@ -712,6 +762,7 @@ int main(int argc, char *argv[])
ssl = false;
tcp = false;
http = false;
dtls = false;
}
}

Expand Down Expand Up @@ -760,6 +811,7 @@ int main(int argc, char *argv[])
QDEBUGVAR(ipv6);
QDEBUGVAR(tcp);
QDEBUGVAR(udp);
QDEBUGVAR(dtls);
QDEBUGVAR(ssl);
QDEBUGVAR(http);
QDEBUGVAR(sslNoError);
Expand All @@ -780,6 +832,14 @@ int main(int argc, char *argv[])

//NOW LETS DO THIS!

if (dtls && !PacketNetwork::DTLSisSupported()) {
OUTIF() << "Error: This computer does not support DTLS.";
OUTIF() << "The expected SSL version is " << QSslSocket::sslLibraryBuildVersionString();
OUTPUT();
return -1;
}


if (ssl && !QSslSocket::supportsSsl()) {
OUTIF() << "Error: This computer does not have a native SSL library.";
OUTIF() << "The expected SSL version is " << QSslSocket::sslLibraryBuildVersionString();
Expand Down Expand Up @@ -925,6 +985,14 @@ int main(int argc, char *argv[])
recvData.clear();
int bytesWriten = 0;


if(dtls) {
//TODO: How to set up DTLS client?


}


if (tcp || ssl) {
QSslSocket sock;

Expand Down
20 changes: 20 additions & 0 deletions src/packetnetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ QString PacketNetwork::getIPmode()
return ipMode;
}


bool PacketNetwork::DTLSisSupported()
{

#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)

return false;
#endif

if (!QSslSocket::supportsSsl()) {
return false;
}

// For now, only openssl is supported.
QString sslVersion = QSslSocket::sslLibraryBuildVersionString();
return sslVersion.toLower().contains("openssl");

}


bool PacketNetwork::DTLSListening()
{
QUdpSocket * dtls;
Expand Down
3 changes: 2 additions & 1 deletion src/packetnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class PacketNetwork : public QObject
void reJoinMulticast();
void leaveMulticast();
QUdpSocket * findMulticast(QString multicast);
signals:
static bool DTLSisSupported();
signals:
void packetReceived(Packet sendpacket);
void toStatusBar(const QString & message, int timeout = 0, bool override = false);
void packetSent(Packet sendpacket);
Expand Down

0 comments on commit 54a8502

Please sign in to comment.