1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#define MAXPACKETSIZE 512

#include <QDebug>
#include <QWidget>
#include <QTextEdit>
#include <QProcess>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QCoreApplication>
#include <QNetworkInterface>
#include <QList>
#include <QLabel>
#include <QUdpSocket>

class QTFTPWidget : public QWidget
{
    Q_OBJECT

public:
    explicit QTFTPWidget(QString localAddressString = QString(), int localPort = -1,
                           QString remoteAddressString = QString(), int remotePort = -1,
                           QWidget *parent = 0);
    ~QTFTPWidget();

    bool putByteArray(QString filename, QByteArray transmittingFile);
    bool getByteArray(QString filename, QByteArray *requestedFile);

    inline QString errorString() { return(lastErrorString); }

    QString remoteAddressString();
    QString localAddressString();
    int remotePortNumber();
    int localPortNumber();

public slots:
    inline void onSetLocalAddress(QString address) { localAddressSuppliedByUser=address; }
    inline void onSetLocalPort(int port) { localPortSuppliedByUser=port; }
    inline void onSetRemoteAddress(QString address) { remoteAddressSuppliedByUser=address; }
    inline void onSetRemotePort(int port) { remotePortSuppliedByUser=port; }

private:
    QString localAddressSuppliedByUser, remoteAddressSuppliedByUser;
    int localPortSuppliedByUser, remotePortSuppliedByUser;

    QString lastErrorString;
    QUdpSocket *socket;
    QComboBox *localAddressComboBox;
    QSpinBox *localPortSpinBox;
    QLineEdit *remoteAddressLineEdit;
    QSpinBox *remotePortSpinBox;

    bool bindSocket();
    QByteArray getFilePacket(QString filename);
    QByteArray putFilePacket(QString filename);
};

QTFTPWidget::QTFTPWidget(QString localAddressString, int localPort, QString remoteAddressString, int remotePort, QWidget *parent) : QWidget(parent),
                             localAddressSuppliedByUser(localAddressString), localPortSuppliedByUser(localPort),
                             remoteAddressSuppliedByUser(remoteAddressString), remotePortSuppliedByUser(remotePort)
{
    this->setWindowTitle(QString("TFTP Widget"));
    this->setLayout(new QVBoxLayout());
    this->layout()->setSpacing(1);

    QWidget *widget=new QWidget();
    widget->setLayout(new QHBoxLayout());
    widget->layout()->setContentsMargins(0, 0, 0, 0);

    localAddressComboBox = new QComboBox();
    localAddressComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    if (localAddressSuppliedByUser.isEmpty()){
        QList<QHostAddress> hstList = QNetworkInterface::allAddresses();
        for (int n=0; n<hstList.count(); n++){
            if (hstList.at(n).toIPv4Address()){
                QString string=hstList.at(n).toString();
                localAddressComboBox->addItem(string);
            }
        }
    } else {
        localAddressComboBox->addItem(localAddressSuppliedByUser);
        localAddressComboBox->setDisabled(true);
    }

    localPortSpinBox = new QSpinBox();
    localPortSpinBox->setFixedWidth(100);
    localPortSpinBox->setMaximum(65535);
    if (localPortSuppliedByUser > 0){
        localPortSpinBox->setMinimum(0);
        localPortSpinBox->setValue(localPortSuppliedByUser);
        localPortSpinBox->setDisabled(true);
    } else {
        localPortSpinBox->setMinimum(1024);
        localPortSpinBox->setValue(7755);
    }

    QLabel *label=new QLabel(QString("Local Address:"));
    label->setFixedWidth(120);

    widget->layout()->addWidget(label);
    widget->layout()->addWidget(localAddressComboBox);
    widget->layout()->addWidget(localPortSpinBox);
    this->layout()->addWidget(widget);

    widget=new QWidget();
    widget->setLayout(new QHBoxLayout());
    widget->layout()->setContentsMargins(0, 0, 0, 0);

    remoteAddressLineEdit = new QLineEdit(remoteAddressSuppliedByUser);
    remoteAddressLineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    if (!remoteAddressSuppliedByUser.isEmpty()){
        remoteAddressLineEdit->setReadOnly(true);
    }

    remotePortSpinBox = new QSpinBox();
    remotePortSpinBox->setFixedWidth(100);
    remotePortSpinBox->setMaximum(65535);
    if (remotePortSuppliedByUser > 0){
        remotePortSpinBox->setMinimum(0);
        remotePortSpinBox->setValue(remotePortSuppliedByUser);
        remotePortSpinBox->setDisabled(true);
    } else {
        remotePortSpinBox->setMinimum(1024);
        remotePortSpinBox->setValue(7755);
    }

    label=new QLabel(QString("Remote Address:"));
    label->setFixedWidth(120);

    widget->layout()->addWidget(label);
    widget->layout()->addWidget(remoteAddressLineEdit);
    widget->layout()->addWidget(remotePortSpinBox);
    this->layout()->addWidget(widget);

    widget=new QWidget();
    widget->setLayout(new QHBoxLayout());
    widget->layout()->setContentsMargins(0, 0, 0, 0);

    this->layout()->addWidget(widget);

    socket=NULL;
}

QTFTPWidget::~QTFTPWidget()
{
    if (socket) delete socket;
}

bool QTFTPWidget::bindSocket()
{
    // SEE IF WE ALREADY HAVE A BOUND SOCKET
    // AND IF SO, WE NEED TO DELETE IT
    if (socket != NULL) {
        delete socket;
    }

    // CREATE A NEW SOCKET
    socket=new QUdpSocket();

    // AND SEE IF WE CAN BIND IT TO A LOCAL IP ADDRESS AND PORT
    if (localAddressSuppliedByUser.isEmpty()){
        return(socket->bind(QHostAddress(localAddressComboBox->currentText()), localPortSpinBox->value()));
    } else {
        return(socket->bind(QHostAddress(localAddressSuppliedByUser), localPortSuppliedByUser));
    }
}

QString QTFTPWidget::remoteAddressString()
{
    return(remoteAddressLineEdit->text());
}

QString QTFTPWidget::localAddressString()
{
    return(localAddressComboBox->currentText());
}

int QTFTPWidget::remotePortNumber()
{
    return(remotePortSpinBox->value());
}

int QTFTPWidget::localPortNumber()
{
    return(localPortSpinBox->value());
}

bool QTFTPWidget::putByteArray(QString filename, QByteArray transmittingFile)
{
    // BIND OUR LOCAL SOCKET TO AN IP ADDRESS AND PORT
    if (!bindSocket()) {
        lastErrorString = socket->errorString();
        return(false);
    }

    // MAKE A LOCAL COPY OF THE REMOTE HOST ADDRESS AND PORT NUMBER
    QHostAddress hostAddress(QHostAddress(remoteAddressLineEdit->text()));
    int portNumber = remotePortSpinBox->value();

    // CREATE REQUEST PACKET AND SEND TO HOST
    // WAIT UNTIL MESSAGE HAS BEEN SENT, QUIT IF TIMEOUT IS REACHED
    QByteArray reqPacket=putFilePacket(filename);
    if (socket->writeDatagram(reqPacket, hostAddress, portNumber) != reqPacket.length()){
        lastErrorString = QString("did not send packet to host :( %1").arg(socket->errorString());
        return(false);
    }

    // CREATE PACKET COUNTERS TO KEEP TRACK OF MESSAGES
    unsigned short incomingPacketNumber=0;
    unsigned short outgoingPacketNumber=0;

    // NOW WAIT HERE FOR INCOMING DATA
    bool messageCompleteFlag=false;
    while (1){
        // WAIT FOR AN INCOMING PACKET
        if (socket->hasPendingDatagrams() || socket->waitForReadyRead(10000)){
            // ITERATE HERE AS LONG AS THERE IS ATLEAST A
            // PACKET HEADER'S WORTH OF DATA TO READ
            QByteArray incomingDatagram;
            incomingDatagram.resize(socket->pendingDatagramSize());
            socket->readDatagram(incomingDatagram.data(), incomingDatagram.length());

            // MAKE SURE FIRST BYTE IS 0
            char *buffer=incomingDatagram.data();
            char zeroByte=buffer[0];
            if (zeroByte != 0x00) {
                lastErrorString = QString("Incoming packet has invalid first byte (%1).").arg((int)zeroByte);
                return(false);
            }

            // READ UNSIGNED SHORT PACKET NUMBER USING LITTLE ENDIAN FORMAT
            // FOR THE INCOMING UNSIGNED SHORT VALUE BUT BIG ENDIAN FOR THE
            // INCOMING DATA PACKET
            unsigned short incomingMessageCounter;
            *((char*)&incomingMessageCounter+1)=buffer[2];
            *((char*)&incomingMessageCounter+0)=buffer[3];

            // CHECK INCOMING MESSAGE ID NUMBER AND MAKE SURE IT MATCHES
            // WHAT WE ARE EXPECTING, OTHERWISE WE'VE LOST OR GAINED A PACKET
            if (incomingMessageCounter == incomingPacketNumber){
                incomingPacketNumber++;
            } else {
                lastErrorString = QString("error on incoming packet number %1 vs expected %2").arg(incomingMessageCounter).arg(incomingPacketNumber);
                return(false);
            }

            // CHECK THE OPCODE FOR ANY ERROR CONDITIONS
            char opCode=buffer[1];
            if (opCode != 0x04) { /* ack packet should have code 4 and should be ack+1 the packet we just sent */
                lastErrorString = QString("Incoming packet returned invalid operation code (%1).").arg((int)opCode);
                return(false);
            } else {
                // SEE IF WE NEED TO SEND ANYMORE DATA PACKETS BY CHECKING END OF MESSAGE FLAG
                if (messageCompleteFlag) break;

                // SEND NEXT DATA PACKET TO HOST
                QByteArray transmitByteArray;
                transmitByteArray.append((char)0x00);
                transmitByteArray.append((char)0x03); // send data opcode
                transmitByteArray.append(*((char*)&outgoingPacketNumber+1));
                transmitByteArray.append(*((char*)&outgoingPacketNumber));

                // APPEND DATA THAT WE WANT TO SEND
                int numBytesAlreadySent=outgoingPacketNumber*MAXPACKETSIZE;
                int bytesLeftToSend=transmittingFile.length()-numBytesAlreadySent;
                if (bytesLeftToSend < MAXPACKETSIZE){
                    messageCompleteFlag=true;
                    if (bytesLeftToSend > 0){
                        transmitByteArray.append((transmittingFile.data()+numBytesAlreadySent), bytesLeftToSend);
                    }
                } else {
                    transmitByteArray.append((transmittingFile.data()+numBytesAlreadySent), MAXPACKETSIZE);
                }

                // SEND THE PACKET AND MAKE SURE IT GETS SENT
                if (socket->writeDatagram(transmitByteArray, hostAddress, portNumber) != transmitByteArray.length()){
                    lastErrorString = QString("did not send data packet to host :( %1").arg(socket->errorString());
                    return(false);
                }

                // NOW THAT WE'VE SENT AN ACK SIGNAL, INCREMENT SENT MESSAGE COUNTER
                outgoingPacketNumber++;
            }
        } else {
            lastErrorString = QString("No message received from host :( %1").arg(socket->errorString());
            return(false);
        }
    }
    lastErrorString = QString("no error");
    return(true);

}

bool QTFTPWidget::getByteArray(QString filename, QByteArray *requestedFile)
{
    // BIND OUR LOCAL SOCKET TO AN IP ADDRESS AND PORT
    if (!bindSocket()) {
        lastErrorString = socket->errorString();
        return(false);
    }

    // MAKE A LOCAL COPY OF THE REMOTE HOST ADDRESS AND PORT NUMBER
    QHostAddress hostAddress(QHostAddress(remoteAddressLineEdit->text()));
    int portNumber = remotePortSpinBox->value();

    // CLEAN OUT ANY INCOMING PACKETS
    while (socket->hasPendingDatagrams()){
        QByteArray byteArray;
        byteArray.resize(socket->pendingDatagramSize());
        socket->readDatagram(byteArray.data(), byteArray.length());
    }

    // CREATE REQUEST PACKET AND SEND TO HOST
    // WAIT UNTIL MESSAGE HAS BEEN SENT, QUIT IF TIMEOUT IS REACHED
    QByteArray reqPacket=getFilePacket(filename);
    if (socket->writeDatagram(reqPacket, hostAddress, portNumber) != reqPacket.length()){
        lastErrorString =  QString("did not send packet to host :( %1").arg(socket->errorString());
        return(false);
    }

    // CREATE PACKET COUNTERS TO KEEP TRACK OF MESSAGES
    unsigned short incomingPacketNumber=1;
    unsigned short outgoingPacketNumber=1;

    // NOW WAIT HERE FOR INCOMING DATA
    bool messageCompleteFlag=false;
    while (!messageCompleteFlag){
        // WAIT FOR AN INCOMING PACKET
        if (socket->hasPendingDatagrams() || socket->waitForReadyRead(10000)){
            // ITERATE HERE AS LONG AS THERE IS ATLEAST A
            // PACKET HEADER'S WORTH OF DATA TO READ
            QByteArray incomingDatagram;
            incomingDatagram.resize(socket->pendingDatagramSize());
            socket->readDatagram(incomingDatagram.data(), incomingDatagram.length());

            // MAKE SURE FIRST BYTE IS 0
            char *buffer=incomingDatagram.data();
            char zeroByte=buffer[0];
            if (zeroByte != 0x00) {
                lastErrorString = QString("Incoming packet has invalid first byte (%1).").arg((int)zeroByte);
                return(false);
            }

            // READ UNSIGNED SHORT PACKET NUMBER USING LITTLE ENDIAN FORMAT
            // FOR THE INCOMING UNSIGNED SHORT VALUE BUT BIG ENDIAN FOR THE
            // INCOMING DATA PACKET
            unsigned short incomingMessageCounter;
            *((char*)&incomingMessageCounter+1)=buffer[2];
            *((char*)&incomingMessageCounter+0)=buffer[3];

            // CHECK INCOMING MESSAGE ID NUMBER AND MAKE SURE IT MATCHES
            // WHAT WE ARE EXPECTING, OTHERWISE WE'VE LOST OR GAINED A PACKET
            if (incomingMessageCounter == incomingPacketNumber){
                incomingPacketNumber++;
            } else {
                lastErrorString = QString("error on incoming packet number %1 vs expected %2").arg(incomingMessageCounter).arg(incomingPacketNumber);
                return(false);
            }

            // COPY THE INCOMING FILE DATA
            QByteArray incomingByteArray(&buffer[4], incomingDatagram.length()-4);

            // SEE IF WE RECEIVED A COMPLETE 512 BYTES AND IF SO,
            // THEN THERE IS MORE INFORMATION ON THE WAY
            // OTHERWISE, WE'VE REACHED THE END OF THE RECEIVING FILE
            if (incomingByteArray.length() < MAXPACKETSIZE){
                messageCompleteFlag=true;
            }

            // APPEND THE INCOMING DATA TO OUR COMPLETE FILE
            requestedFile->append(incomingByteArray);

            // CHECK THE OPCODE FOR ANY ERROR CONDITIONS
            char opCode=buffer[1];
            if (opCode != 0x03) { /* ack packet should have code 3 (data) and should be ack+1 the packet we just sent */
                lastErrorString = QString("Incoming packet returned invalid operation code (%1).").arg((int)opCode);
                return(false);
            } else {
                // SEND PACKET ACKNOWLEDGEMENT BACK TO HOST REFLECTING THE INCOMING PACKET NUMBER
                QByteArray ackByteArray;
                ackByteArray.append((char)0x00);
                ackByteArray.append((char)0x04);
                ackByteArray.append(*((char*)&incomingMessageCounter+1));
                ackByteArray.append(*((char*)&incomingMessageCounter));

                // SEND THE PACKET AND MAKE SURE IT GETS SENT
                if (socket->writeDatagram(ackByteArray, hostAddress, portNumber) != ackByteArray.length()){
                    lastErrorString = QString("did not send ack packet to host :( %1").arg(socket->errorString());
                    return(false);
                }

                // NOW THAT WE'VE SENT AN ACK SIGNAL, INCREMENT SENT MESSAGE COUNTER
                outgoingPacketNumber++;
            }
        } else {
            lastErrorString = QString("No message received from host :( %1").arg(socket->errorString());
            return(false);
        }
    }
    return(true);
}

QByteArray QTFTPWidget::getFilePacket(QString filename)
{
    QByteArray byteArray(filename.toLatin1());
    byteArray.prepend((char)0x01); // OPCODE
    byteArray.prepend((char)0x00);
    byteArray.append((char)0x00);
    byteArray.append(QString("octet").toLatin1()); // MODE
    byteArray.append((char)0x00);

    return(byteArray);
}

QByteArray QTFTPWidget::putFilePacket(QString filename)
{
    QByteArray byteArray((char)0x00);
    byteArray.append((char)0x02); // OPCODE
    byteArray.append(filename.toLatin1());
    byteArray.append((char)0x00);
    byteArray.append(QString("octet").toLatin1()); // MODE
    byteArray.append((char)0x00);

    return(byteArray);
}