| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** BSD License Usage | 
| 18 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 19 | ** as follows: | 
| 20 | ** | 
| 21 | ** "Redistribution and use in source and binary forms, with or without | 
| 22 | ** modification, are permitted provided that the following conditions are | 
| 23 | ** met: | 
| 24 | **   * Redistributions of source code must retain the above copyright | 
| 25 | **     notice, this list of conditions and the following disclaimer. | 
| 26 | **   * Redistributions in binary form must reproduce the above copyright | 
| 27 | **     notice, this list of conditions and the following disclaimer in | 
| 28 | **     the documentation and/or other materials provided with the | 
| 29 | **     distribution. | 
| 30 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 31 | **     contributors may be used to endorse or promote products derived | 
| 32 | **     from this software without specific prior written permission. | 
| 33 | ** | 
| 34 | ** | 
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 46 | ** | 
| 47 | ** $QT_END_LICENSE$ | 
| 48 | ** | 
| 49 | ****************************************************************************/ | 
| 50 |  | 
| 51 | #include <QtCore> | 
| 52 | #include <QtNetwork> | 
| 53 |  | 
| 54 | #include "addressdialog.h" | 
| 55 | #include "association.h" | 
| 56 | #include "mainwindow.h" | 
| 57 | #include "ui_mainwindow.h" | 
| 58 |  | 
| 59 | #include <utility> | 
| 60 |  | 
| 61 | MainWindow::MainWindow(QWidget *parent) | 
| 62 |     : QMainWindow(parent), | 
| 63 |       ui(new Ui::MainWindow), | 
| 64 |       nameTemplate(QStringLiteral("Alice (clone number %1)" )) | 
| 65 | { | 
| 66 |     ui->setupUi(this); | 
| 67 |     updateUi(); | 
| 68 | } | 
| 69 |  | 
| 70 | MainWindow::~MainWindow() | 
| 71 | { | 
| 72 |     delete ui; | 
| 73 | } | 
| 74 |  | 
| 75 | //! [0] | 
| 76 |  | 
| 77 | const QString colorizer(QStringLiteral("<font color=\"%1\">%2</font><br>" )); | 
| 78 |  | 
| 79 | void MainWindow::addErrorMessage(const QString &message) | 
| 80 | { | 
| 81 |     ui->clientMessages->insertHtml(text: colorizer.arg(QStringLiteral("Crimson" ), args: message)); | 
| 82 | } | 
| 83 |  | 
| 84 | void MainWindow::addWarningMessage(const QString &message) | 
| 85 | { | 
| 86 |     ui->clientMessages->insertHtml(text: colorizer.arg(QStringLiteral("DarkOrange" ), args: message)); | 
| 87 | } | 
| 88 |  | 
| 89 | void MainWindow::addInfoMessage(const QString &message) | 
| 90 | { | 
| 91 |     ui->clientMessages->insertHtml(text: colorizer.arg(QStringLiteral("DarkBlue" ), args: message)); | 
| 92 | } | 
| 93 |  | 
| 94 | void MainWindow::addServerResponse(const QString &clientInfo, const QByteArray &datagram, | 
| 95 |                                    const QByteArray &plainText) | 
| 96 | { | 
| 97 |     static const QString messageColor = QStringLiteral("DarkMagenta" ); | 
| 98 |     static const QString formatter = QStringLiteral("<br>---------------"  | 
| 99 |                                                     "<br>%1 received a DTLS datagram:<br> %2"  | 
| 100 |                                                     "<br>As plain text:<br> %3" ); | 
| 101 |  | 
| 102 |     const QString html = formatter.arg(args: clientInfo, args: QString::fromUtf8(str: datagram.toHex(separator: ' ')), | 
| 103 |                                        args: QString::fromUtf8(str: plainText)); | 
| 104 |     ui->serverMessages->insertHtml(text: colorizer.arg(a1: messageColor, a2: html)); | 
| 105 | } | 
| 106 |  | 
| 107 | //! [0] | 
| 108 |  | 
| 109 | void MainWindow::on_connectButton_clicked() | 
| 110 | { | 
| 111 |     if (lookupId != -1) { | 
| 112 |         QHostInfo::abortHostLookup(lookupId); | 
| 113 |         lookupId = -1; | 
| 114 |         port = 0; | 
| 115 |         updateUi(); | 
| 116 |         return; | 
| 117 |     } | 
| 118 |  | 
| 119 |     AddressDialog dialog; | 
| 120 |     if (dialog.exec() != QDialog::Accepted) | 
| 121 |         return; | 
| 122 |  | 
| 123 |     const QString hostName = dialog.remoteName(); | 
| 124 |     if (hostName.isEmpty()) | 
| 125 |         return addWarningMessage(message: tr(s: "Host name or address required to connect" )); | 
| 126 |  | 
| 127 |     port = dialog.remotePort(); | 
| 128 |     QHostAddress remoteAddress; | 
| 129 |     if (remoteAddress.setAddress(hostName)) | 
| 130 |         return startNewConnection(address: remoteAddress); | 
| 131 |  | 
| 132 |     addInfoMessage(message: tr(s: "Looking up the host ..." )); | 
| 133 |     lookupId = QHostInfo::lookupHost(name: hostName, receiver: this, SLOT(lookupFinished(QHostInfo))); | 
| 134 |     updateUi(); | 
| 135 | } | 
| 136 |  | 
| 137 | void MainWindow::updateUi() | 
| 138 | { | 
| 139 |     ui->connectButton->setText(lookupId == -1 ? tr(s: "Connect ..." ) : tr(s: "Cancel lookup" )); | 
| 140 |     ui->shutdownButton->setEnabled(connections.size() != 0); | 
| 141 | } | 
| 142 |  | 
| 143 | void MainWindow::lookupFinished(const QHostInfo &hostInfo) | 
| 144 | { | 
| 145 |     if (hostInfo.lookupId() != lookupId) | 
| 146 |         return; | 
| 147 |  | 
| 148 |     lookupId = -1; | 
| 149 |     updateUi(); | 
| 150 |  | 
| 151 |     if (hostInfo.error() != QHostInfo::NoError) { | 
| 152 |         addErrorMessage(message: hostInfo.errorString()); | 
| 153 |         return; | 
| 154 |     } | 
| 155 |  | 
| 156 |     const QList<QHostAddress> foundAddresses = hostInfo.addresses(); | 
| 157 |     if (foundAddresses.empty()) { | 
| 158 |         addWarningMessage(message: tr(s: "Host not found" )); | 
| 159 |         return; | 
| 160 |     } | 
| 161 |  | 
| 162 |     const auto remoteAddress = foundAddresses.at(i: 0); | 
| 163 |     addInfoMessage(message: tr(s: "Connecting to: %1" ).arg(a: remoteAddress.toString())); | 
| 164 |     startNewConnection(address: remoteAddress); | 
| 165 | } | 
| 166 |  | 
| 167 | void MainWindow::startNewConnection(const QHostAddress &address) | 
| 168 | { | 
| 169 |     AssocPtr newConnection(new DtlsAssociation(address, port, nameTemplate.arg(a: nextId))); | 
| 170 |     connect(sender: newConnection.data(), signal: &DtlsAssociation::errorMessage, receiver: this, slot: &MainWindow::addErrorMessage); | 
| 171 |     connect(sender: newConnection.data(), signal: &DtlsAssociation::warningMessage, receiver: this, slot: &MainWindow::addWarningMessage); | 
| 172 |     connect(sender: newConnection.data(), signal: &DtlsAssociation::infoMessage, receiver: this, slot: &MainWindow::addInfoMessage); | 
| 173 |     connect(sender: newConnection.data(), signal: &DtlsAssociation::serverResponse, receiver: this, slot: &MainWindow::addServerResponse); | 
| 174 |     connections.push_back(t: std::move(newConnection)); | 
| 175 |     connections.back()->startHandshake(); | 
| 176 |     updateUi(); | 
| 177 |  | 
| 178 |     ++nextId; | 
| 179 | } | 
| 180 |  | 
| 181 | void MainWindow::on_shutdownButton_clicked() | 
| 182 | { | 
| 183 |     connections.clear(); | 
| 184 |     updateUi(); | 
| 185 | } | 
| 186 |  |