MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  QString n = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
  ipValidator = new QRegExpValidator(QRegExp("^" + n + "\\." + n + "\\." + n + "\\." + n + "$"));
  intValidator = new QRegExpValidator(QRegExp("[0-9]+"));
  hexValidator = new QRegExpValidator(QRegExp("[0-9a-fA-F]+"));
  base64Validator = new QRegExpValidator(QRegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"));

  setIpValidator();

  machine = new QStateMachine(this);

  QState *ipToInt = new QState();
  QState *intToHex = new QState();
  QState *hexToBase64 = new QState();
  QState *base64ToHex = new QState();
  QState *hexToInt = new QState();
  QState *intToIp = new QState();

  ipToInt->addTransition(ui->pushButton, SIGNAL(clicked()), intToHex);
  intToHex->addTransition(ui->pushButton, SIGNAL(clicked()), hexToBase64);
  hexToBase64->addTransition(ui->pushButton, SIGNAL(clicked()), base64ToHex);
  base64ToHex->addTransition(ui->pushButton, SIGNAL(clicked()), hexToInt);
  hexToInt->addTransition(ui->pushButton, SIGNAL(clicked()), intToIp);
  intToIp->addTransition(ui->pushButton, SIGNAL(clicked()), ipToInt);

  QObject::connect(ipToInt, SIGNAL(exited()), this, SLOT(convertIpToInt()));
  QObject::connect(intToHex, SIGNAL(exited()), this, SLOT(convertIntToHex()));
  QObject::connect(hexToBase64, SIGNAL(exited()), this, SLOT(convertHexToBase64()));
  QObject::connect(base64ToHex, SIGNAL(exited()), this, SLOT(convertBase64ToHex()));
  QObject::connect(hexToInt, SIGNAL(exited()), this, SLOT(convertHexToInt()));
  QObject::connect(intToIp, SIGNAL(exited()), this, SLOT(convertIntToIp()));

  machine->addState(ipToInt);
  machine->addState(intToHex);
  machine->addState(hexToBase64);
  machine->addState(base64ToHex);
  machine->addState(hexToInt);
  machine->addState(intToIp);

  machine->setInitialState(ipToInt);

  machine->start();
}
Пример #2
0
bool TcpClient::serachServerInNetwork()
{
    //Then find mask
    QList<QNetworkInterface> interface = QNetworkInterface::allInterfaces();
    for (int i = 0; i < interface.size(); i++)
    {
        QNetworkInterface item = interface.at(i);
        QList<QNetworkAddressEntry> entryList = item.addressEntries();

        for (int j = 0; j < entryList.size(); j++)
        {
            if (m_localHost == entryList.at(j).ip().toString()) {
                m_mask = entryList.at(j).netmask().toString();

                if (m_connectionState != 0) {

                    QList<int> ipList = convertIpToInt(m_localHost);
                    QList<int> maskList = convertIpToInt(m_mask);
                    QList<int> networkAddress;


                    int local = -1;

                    if (ipList.count() == maskList.count()) {

                        for (int j = 0; j < ipList.count(); j++) {
                            networkAddress.append((ipList.at(j) & maskList.at(j)));
                        }

                        for (int j = 0; j < ipList.count(); j++) {
                            if (ipList.at(j) != (ipList.at(j) & maskList.at(j))) {
                                local = j;
                                break;
                            }
                        }

                        if (local != -1) {
                            switch (local) {
                            case 1:
                            {
                                for (int l = networkAddress.at(1); l <= 255; l++) {
                                    for (int k = 0; k <= 255; k++) {
                                        for (int n = 0; n <= 255; n++) {
                                            QString tmpIP = QString::number(networkAddress.at(0)) + "." + QString::number(l) + "." + QString::number(k) + "." + QString::number(n);
                                            if (connectToHost(tmpIP, m_port)) {
                                                setServerIp(tmpIP);
                                                return true;
                                            }
                                        }
                                    }
                                }
                            } break;
                            case 2:
                            {
                                for (int k = networkAddress.at(2); k <= 255; k++) {
                                    for (int n = 0; n <= 255; n++) {
                                        QString tmpIP = QString::number(networkAddress.at(0)) + "." + QString::number(networkAddress.at(1)) + "." + QString::number(k) + "." + QString::number(n);
                                        if (connectToHost(tmpIP, m_port)) {
                                            setServerIp(tmpIP);
                                            return true;
                                        }
                                    }
                                }
                            } break;
                            case 3:
                            {
                                for (int n = networkAddress.at(3); n <= 255; n++) {
                                    QString tmpIP = QString::number(networkAddress.at(0)) + "." + QString::number(networkAddress.at(1)) + "." + QString::number(networkAddress.at(2)) + "." + QString::number(n);
                                    if (connectToHost(tmpIP, m_port)) {
                                        setServerIp(tmpIP);
                                        return true;
                                    }
                                }
                            } break;
                            case 0:
                            default:
                                return false;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}