Header:
#ifndef INDG_SOCKET_H
#define INDG_SOCKET_H
#include <QTcpSocket>
#include <QtGlobal>
class indg_socket : public QTcpSocket
{
Q_OBJECT
public:
explicit indg_socket(QObject *parent = 0);
qint32 computeauth(qint32 random1, qint32 random2, qint32 oper);
protected:
void readyRead();
private slots:
void readpackets();
};
#endif // INDG_SOCKET_H
#ifndef INDG_SOCKET_H
#define INDG_SOCKET_H
#include <QTcpSocket>
#include <QtGlobal>
class indg_socket : public QTcpSocket
{
Q_OBJECT
public:
explicit indg_socket(QObject *parent = 0);
qint32 computeauth(qint32 random1, qint32 random2, qint32 oper);
protected:
void readyRead();
private slots:
void readpackets();
};
#endif // INDG_SOCKET_H
To copy to clipboard, switch view to plain text mode
CPP:
#include "indg_socket.h"
#include <QtNetwork>
#include <QtDebug>
#include <QByteArray>
#include <QtGlobal>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
bool isClientAuthenticated;
qint32 random1;
qint32 random2;
qint32 oper;
qint32 result;
QByteArray auth; //65500 do you want to authenticate?
QByteArray authme; //65501 authenticate me
QByteArray authq; //65502 auth question for client
QByteArray autha; //65503 auth answer for server
QByteArray userkeys;//65504 i'm sending user keys
QByteArray wmp; //65505 whole map for client
QByteArray partd; //65506 partial data for client
QByteArray authok; //65508 auth OK
QByteArray authfailure; //65508 auth FAILED
QByteArray admstat; //65509 admin stats
QByteArray admstatq; //65510 admin stats query
QByteArray sockdata;
QDataStream dsauth(&auth,QIODevice::ReadWrite);
QDataStream dsauthme(&authme,QIODevice::ReadWrite);
QDataStream dsauthq(&authq,QIODevice::ReadWrite);
QDataStream dsautha(&autha,QIODevice::ReadWrite);
QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
QDataStream dswmp(&wmp,QIODevice::ReadWrite);
QDataStream dspartd(&partd,QIODevice::ReadWrite);
QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
QDataStream dsadmstatq(&admstatq,QIODevice::ReadWrite);
QDataStream dsauthok(&authok,QIODevice::ReadWrite);
QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
indg_socket::indg_socket(QObject *parent) :
QTcpSocket(parent)
{
//generate random numbers
srand(time(NULL));
random1=rand()%256;
//sleep(3);
srand(time(NULL));
random2=rand()%256;
srand((time(NULL)+5));
oper=rand()%5;
result=computeauth(random1, random2, oper);
dsauth << (quint16)65500;
dsauthme << (quint16)65501;
dsauthq << (quint16)65502;
dsautha << (quint16)65503;
dsuserkeys << (quint16)65504;
dsauthok << (quint16)65508;
dsauthfailure << (quint16)65509;
dsadmstatq << (quint16)65510;
isClientAuthenticated=false;
connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}
void indg_socket::readpackets()
{
sockdata=this->readAll();
sockdata.truncate(100); //truncate if packet greater than 100 bytes
if(sockdata==authme){
qDebug() << "client wanna authenticate!!!";
qDebug() << "sending question " << random1 << " op" << oper << " " << random2 << " = " << result;
authq.clear();
dsauthq << (quint16)65502 << (quint8)random1 << (quint8)random2 << (quint8)oper;
qDebug() << authq.toHex();
this->write(authq, 40);
sockdata.clear();
return;
}
if(sockdata==autha){
qDebug() << "getting auth answer from client... checking";
qDebug() << "OK";
this->write(authok);
sockdata.clear();
return;
}
if(sockdata.startsWith(admstatq)){
qDebug() << "getting auth answer from client... checking";
qDebug() << "OK";
this->write(authok);
sockdata.clear();
return;
}
return;
}
qint32 indg_socket::computeauth(qint32 random1, qint32 random2, qint32 oper)
{
qint8 res;
switch(oper){
case 1:
res=random1+random2;
return res;
case 2:
res=random1-random2;
return res;
case 3:
res=random1*random2;
return res;
case 4:
res=random1/random2;
return res;
}
}
#include "indg_socket.h"
#include <QtNetwork>
#include <QtDebug>
#include <QByteArray>
#include <QtGlobal>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
bool isClientAuthenticated;
qint32 random1;
qint32 random2;
qint32 oper;
qint32 result;
QByteArray auth; //65500 do you want to authenticate?
QByteArray authme; //65501 authenticate me
QByteArray authq; //65502 auth question for client
QByteArray autha; //65503 auth answer for server
QByteArray userkeys;//65504 i'm sending user keys
QByteArray wmp; //65505 whole map for client
QByteArray partd; //65506 partial data for client
QByteArray authok; //65508 auth OK
QByteArray authfailure; //65508 auth FAILED
QByteArray admstat; //65509 admin stats
QByteArray admstatq; //65510 admin stats query
QByteArray sockdata;
QDataStream dsauth(&auth,QIODevice::ReadWrite);
QDataStream dsauthme(&authme,QIODevice::ReadWrite);
QDataStream dsauthq(&authq,QIODevice::ReadWrite);
QDataStream dsautha(&autha,QIODevice::ReadWrite);
QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
QDataStream dswmp(&wmp,QIODevice::ReadWrite);
QDataStream dspartd(&partd,QIODevice::ReadWrite);
QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
QDataStream dsadmstatq(&admstatq,QIODevice::ReadWrite);
QDataStream dsauthok(&authok,QIODevice::ReadWrite);
QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
indg_socket::indg_socket(QObject *parent) :
QTcpSocket(parent)
{
//generate random numbers
srand(time(NULL));
random1=rand()%256;
//sleep(3);
srand(time(NULL));
random2=rand()%256;
srand((time(NULL)+5));
oper=rand()%5;
result=computeauth(random1, random2, oper);
dsauth << (quint16)65500;
dsauthme << (quint16)65501;
dsauthq << (quint16)65502;
dsautha << (quint16)65503;
dsuserkeys << (quint16)65504;
dsauthok << (quint16)65508;
dsauthfailure << (quint16)65509;
dsadmstatq << (quint16)65510;
isClientAuthenticated=false;
connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}
void indg_socket::readpackets()
{
sockdata=this->readAll();
sockdata.truncate(100); //truncate if packet greater than 100 bytes
if(sockdata==authme){
qDebug() << "client wanna authenticate!!!";
qDebug() << "sending question " << random1 << " op" << oper << " " << random2 << " = " << result;
authq.clear();
dsauthq << (quint16)65502 << (quint8)random1 << (quint8)random2 << (quint8)oper;
qDebug() << authq.toHex();
this->write(authq, 40);
sockdata.clear();
return;
}
if(sockdata==autha){
qDebug() << "getting auth answer from client... checking";
qDebug() << "OK";
this->write(authok);
sockdata.clear();
return;
}
if(sockdata.startsWith(admstatq)){
qDebug() << "getting auth answer from client... checking";
qDebug() << "OK";
this->write(authok);
sockdata.clear();
return;
}
return;
}
qint32 indg_socket::computeauth(qint32 random1, qint32 random2, qint32 oper)
{
qint8 res;
switch(oper){
case 1:
res=random1+random2;
return res;
case 2:
res=random1-random2;
return res;
case 3:
res=random1*random2;
return res;
case 4:
res=random1/random2;
return res;
}
}
To copy to clipboard, switch view to plain text mode
client side
#ifndef AIT_SOCKET_H
#define AIT_SOCKET_H
#include <QTcpSocket>
class ait_socket : public QTcpSocket
{
Q_OBJECT
public:
explicit ait_socket(QObject *parent = 0);
protected:
void readyRead();
private slots:
void readpackets();
};
#endif // AIT_SOCKET_H
#ifndef AIT_SOCKET_H
#define AIT_SOCKET_H
#include <QTcpSocket>
class ait_socket : public QTcpSocket
{
Q_OBJECT
public:
explicit ait_socket(QObject *parent = 0);
protected:
void readyRead();
private slots:
void readpackets();
};
#endif // AIT_SOCKET_H
To copy to clipboard, switch view to plain text mode
CPP
#include "ait_socket.h"
#include <QtNetwork>
#include <QtDebug>
#include <QByteArray>
#include <QtGlobal>
#include <stdio.h>
QByteArray auth; //65500 do you want to authenticate?
QByteArray authme; //65501 authenticate me
QByteArray authq; //65502 auth question for client
QByteArray autha; //65503 auth answer for server
QByteArray userkeys;//65504 i'm sending user keys
QByteArray wmp; //65505 whole map for client
QByteArray partd; //65506 partial data for client
QByteArray authok; //65508 auth OK
QByteArray authfailure; //65508 auth FAILED
QByteArray admstat; //65509 admin stats
QByteArray admstatq; //65510 admin stats query
QByteArray sockdata;
QDataStream dsauth(&auth,QIODevice::ReadWrite);
QDataStream dsauthme(&authme,QIODevice::ReadWrite);
QDataStream dsauthq(&authq,QIODevice::ReadWrite);
QDataStream dsautha(&autha,QIODevice::ReadWrite);
QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
QDataStream dswmp(&wmp,QIODevice::ReadWrite);
QDataStream dspartd(&partd,QIODevice::ReadWrite);
QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
QDataStream dsauthok(&authok,QIODevice::ReadWrite);
QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
ait_socket::ait_socket(QObject *parent) :
QTcpSocket(parent)
{
dsauth << (quint16)65500;
dsauthme << (quint16)65501;
dsauthq << (quint16)65502;
dsautha << (quint16)65503;
dsuserkeys << (quint16)65504;
dsauthok << (quint16)65508;
dsauthfailure << (quint16)65509;
connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}
void ait_socket::readpackets()
{
sockdata=this->readAll();
if(sockdata.startsWith(auth)){
qDebug() << "server asking for authentication!!!";
qDebug() << "OK, writing authme";
this->write(authme);
sockdata.clear();
return;
}
if(sockdata.startsWith(authq)){
qDebug() << "server sending auth question!!!";
qDebug() << sockdata.toHex();
qDebug() << "OK, writing autha";
this->write(autha);
sockdata.clear();
return;
}
if(sockdata.startsWith(authok)){
qDebug() << "auth passed!";
this->write(userkeys);
sockdata.clear();
return;
}
return;
}
#include "ait_socket.h"
#include <QtNetwork>
#include <QtDebug>
#include <QByteArray>
#include <QtGlobal>
#include <stdio.h>
QByteArray auth; //65500 do you want to authenticate?
QByteArray authme; //65501 authenticate me
QByteArray authq; //65502 auth question for client
QByteArray autha; //65503 auth answer for server
QByteArray userkeys;//65504 i'm sending user keys
QByteArray wmp; //65505 whole map for client
QByteArray partd; //65506 partial data for client
QByteArray authok; //65508 auth OK
QByteArray authfailure; //65508 auth FAILED
QByteArray admstat; //65509 admin stats
QByteArray admstatq; //65510 admin stats query
QByteArray sockdata;
QDataStream dsauth(&auth,QIODevice::ReadWrite);
QDataStream dsauthme(&authme,QIODevice::ReadWrite);
QDataStream dsauthq(&authq,QIODevice::ReadWrite);
QDataStream dsautha(&autha,QIODevice::ReadWrite);
QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
QDataStream dswmp(&wmp,QIODevice::ReadWrite);
QDataStream dspartd(&partd,QIODevice::ReadWrite);
QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
QDataStream dsauthok(&authok,QIODevice::ReadWrite);
QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
ait_socket::ait_socket(QObject *parent) :
QTcpSocket(parent)
{
dsauth << (quint16)65500;
dsauthme << (quint16)65501;
dsauthq << (quint16)65502;
dsautha << (quint16)65503;
dsuserkeys << (quint16)65504;
dsauthok << (quint16)65508;
dsauthfailure << (quint16)65509;
connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
}
void ait_socket::readpackets()
{
sockdata=this->readAll();
if(sockdata.startsWith(auth)){
qDebug() << "server asking for authentication!!!";
qDebug() << "OK, writing authme";
this->write(authme);
sockdata.clear();
return;
}
if(sockdata.startsWith(authq)){
qDebug() << "server sending auth question!!!";
qDebug() << sockdata.toHex();
qDebug() << "OK, writing autha";
this->write(autha);
sockdata.clear();
return;
}
if(sockdata.startsWith(authok)){
qDebug() << "auth passed!";
this->write(userkeys);
sockdata.clear();
return;
}
return;
}
To copy to clipboard, switch view to plain text mode
Bookmarks