Skip to content

Commit

Permalink
added dtls supported checks in logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dannagle committed Oct 8, 2023
1 parent 09f4ea2 commit b028a3c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
43 changes: 42 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ int main(int argc, char *argv[])
bool ipv6 = parser.isSet(bindIPv6Option);
bool ipv4 = parser.isSet(bindIPv4Option);
bool http = parser.isSet(httpOption);
bool dtls = parser.isSet(dtlsOption);

bool okbps = false;
bool okrate = false;
Expand Down Expand Up @@ -644,6 +645,7 @@ int main(int argc, char *argv[])
QDEBUGVAR(udp);
QDEBUGVAR(ssl);
QDEBUGVAR(http);
QDEBUGVAR(dtls);
QDEBUGVAR(sslNoError);
QDEBUGVAR(name);
QDEBUGVAR(data);
Expand All @@ -659,7 +661,7 @@ int main(int argc, char *argv[])

//NOW LETS DO THIS!

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

if(dtls) {
OUTIF() << "Attempting DTLS";

bool implemented = QSslSocket::isClassImplemented(QSsl::ImplementedClass::Dtls);
QDEBUGVAR(implemented);
if(!implemented) {
OUTIF() << "The SSL backend \"" << QSslSocket::sslLibraryBuildVersionString() << "\" does not support DTLS";
OUTPUT();
return -1;
}



QDtls a(QSslSocket::SslClientMode);
QUdpSocket b;
a.setPeer(theAddress, port);
auto config = QSslConfiguration::defaultDtlsConfiguration();
config.setDtlsCookieVerificationEnabled(false);
a.setDtlsConfiguration(config);
b.connectToHost(theAddress, port);

bool v = a.doHandshake(&b);
OUTIF() << v;

b.waitForReadyRead(-1);
quint16 port;
QHostAddress address;
QByteArray server_hello(b.pendingDatagramSize(), Qt::Uninitialized);
b.readDatagram(server_hello.data(), server_hello.size(), &address, &port);
OUTIF() << b.errorString();
bool k = a.doHandshake(&b, server_hello);
OUTIF() << k;


OUTPUT();
return 0;

}

if (tcp || ssl) {
QSslSocket sock;

Expand Down
59 changes: 35 additions & 24 deletions src/packetnetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,37 +856,48 @@ void PacketNetwork::packetToSend(Packet sendpacket)
}

if (sendpacket.isDTLS()) {
QUdpSocket * sendUDP;
bool oneoff = false;
if(!udpServers.isEmpty()) {
sendUDP = udpServers.first();
} else {
QDEBUG() << "No server. Create a one-off";
sendUDP = new QUdpSocket(this);
sendUDP->bind(0);
oneoff = true;
}

if(sendUDP->state() == QAbstractSocket::BoundState) {

sendpacket.fromPort = sendUDP->localPort();
QDEBUG() << "Sending data to :" << sendpacket.toIP << ":" << sendpacket.port;
bool implemented = QSslSocket::isClassImplemented(QSsl::ImplementedClass::Dtls);
QDEBUGVAR(implemented);
if(!implemented) {
sendpacket.errorString = "The SSL backend \"" + QSslSocket::sslLibraryBuildVersionString() + "\" does not support DTLS";
emit packetSent(sendpacket);
} else {
QUdpSocket * sendUDP;
bool oneoff = false;
if(!udpServers.isEmpty()) {
sendUDP = udpServers.first();
} else {
QDEBUG() << "No server. Create a one-off";
sendUDP = new QUdpSocket(this);
sendUDP->bind(0);
oneoff = true;
}

QHostAddress resolved = resolveDNS(sendpacket.toIP);
if(sendUDP->state() == QAbstractSocket::BoundState) {

QDtls clientDtls(QSslSocket::SslClientMode);
clientDtls.setPeer(resolved, sendpacket.port, sendpacket.toIP);
clientDtls.doHandshake(sendUDP);
sendpacket.fromPort = sendUDP->localPort();
QDEBUG() << "Sending data to :" << sendpacket.toIP << ":" << sendpacket.port;

QDEBUG() << "result:" << sendUDP->writeDatagram(sendpacket.getByteArray(), resolved, sendpacket.port);
emit packetSent(sendpacket);
QHostAddress resolved = resolveDNS(sendpacket.toIP);

}
QDtls clientDtls(QSslSocket::SslClientMode);
clientDtls.setPeer(resolved, sendpacket.port, sendpacket.toIP);
clientDtls.doHandshake(sendUDP);

QDEBUG() << "result:" << sendUDP->writeDatagram(sendpacket.getByteArray(), resolved, sendpacket.port);
emit packetSent(sendpacket);

}


if(oneoff) {
sendUDP->waitForBytesWritten();
sendUDP->close();
sendUDP->deleteLater();
}

if(oneoff) {
sendUDP->waitForBytesWritten();
sendUDP->close();
sendUDP->deleteLater();
}


Expand Down

0 comments on commit b028a3c

Please sign in to comment.