Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with a QTcpSocket
QtWS25 Last Chance

Problem with a QTcpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
socketssl
14 Posts 3 Posters 6.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • moffa13M Offline
    moffa13M Offline
    moffa13
    wrote on last edited by moffa13
    #5

    I already did that; the problem is with my with slot because it's not called.

    So with this code

    void Connection::sslErrors(const QList<QSslError> &errors){
        std::cout << "SSL error !";
    }
    

    The error message is not written. Here's my connect :

      connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
    

    As I said only de error signal is called

    What did I do wrong ?

    kshegunovK 1 Reply Last reply
    0
    • moffa13M moffa13

      I already did that; the problem is with my with slot because it's not called.

      So with this code

      void Connection::sslErrors(const QList<QSslError> &errors){
          std::cout << "SSL error !";
      }
      

      The error message is not written. Here's my connect :

        connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
      

      As I said only de error signal is called

      What did I do wrong ?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #6

      @moffa13
      Yeah, I'm talking nonsense. sslErrors() will not be emitted before the handshake has completed, which in your case it doesn't. See instead QSslSocket::startClientEncryption and QSslSocket::startServerEncryption, which are specifically tailored for delayed handshakes.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • moffa13M Offline
        moffa13M Offline
        moffa13
        wrote on last edited by
        #7

        I don't really see how to do this

        kshegunovK 1 Reply Last reply
        0
        • moffa13M moffa13

          I don't really see how to do this

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #8

          @moffa13
          As far as understand it (I haven't done this) you create your socket as usual. But instead of calling QSslSocket::connectToHostEncrypted , you call the regular connectToHost. And at one point, when you want to upgrade to an encrypted connection you call the QSslSocket::startClientEncryption. There are a few notes in the docs of how to do it for the server side too. However, I'm not that convinced that is what you want to do ... am I misunderstand you?

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • moffa13M Offline
            moffa13M Offline
            moffa13
            wrote on last edited by moffa13
            #9

            Actually I am the server so I'm calling QSslSocket::startServerEncryption then the error is thrown. See the code:

            void SSLServer::incomingConnection(qintptr socketDescriptor)
            {
                QScopedPointer<QSslSocket> socket(new QSslSocket(this));
                if (socket->setSocketDescriptor(socketDescriptor)) {
            
                    socket->addCaCertificate(certificate);
                    socket->setLocalCertificate(certificate);
                    socket->setPrivateKey(key);
                    
                    socket->startServerEncryption();
                    queue.enqueue(socket.take());
                }
            }
            

            Maybe I can do something with the slot(error) because at this point I can see the ssl error (handshake failed) and if I sleep the program the connection is closed yet. So, with this:

            void Connection::error(QAbstractSocket::SocketError e){
                std::cout << "SSL error ! code : " << e;
            }
            

            This is written "SSL error ! code : 13" and if I add _sleep(1000) the connection is not dropped yet so maybe I can do something with it.

            kshegunovK 1 Reply Last reply
            0
            • moffa13M moffa13

              Actually I am the server so I'm calling QSslSocket::startServerEncryption then the error is thrown. See the code:

              void SSLServer::incomingConnection(qintptr socketDescriptor)
              {
                  QScopedPointer<QSslSocket> socket(new QSslSocket(this));
                  if (socket->setSocketDescriptor(socketDescriptor)) {
              
                      socket->addCaCertificate(certificate);
                      socket->setLocalCertificate(certificate);
                      socket->setPrivateKey(key);
                      
                      socket->startServerEncryption();
                      queue.enqueue(socket.take());
                  }
              }
              

              Maybe I can do something with the slot(error) because at this point I can see the ssl error (handshake failed) and if I sleep the program the connection is closed yet. So, with this:

              void Connection::error(QAbstractSocket::SocketError e){
                  std::cout << "SSL error ! code : " << e;
              }
              

              This is written "SSL error ! code : 13" and if I add _sleep(1000) the connection is not dropped yet so maybe I can do something with it.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #10

              @moffa13
              If you get "SSL error ! code : 13" then you should also get the sslErrors() signal. You could try calling ignoreSslErrors() inside the error handler and this will hopefully prevent the socket from closing the connection.

              Kind regards.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • moffa13M Offline
                moffa13M Offline
                moffa13
                wrote on last edited by
                #11

                I wrote this :

                
                connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
                connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
                
                ...
                
                void Connection::error(QAbstractSocket::SocketError e){
                    std::cout << "SSL error ! code : " << e;
                    m_socket->ignoreSslErrors();
                    write("Please use SSL");
                }
                
                void Connection::sslErrors(const QList<QSslError> &errors){
                    std::cout << "Fuck it ";
                    std::cout << "SSL error " << errors.length();
                    m_socket->ignoreSslErrors();
                    write("Please use SSL");
                }
                

                Only the first works and the ignoreSslErrors() doesn't change anything.

                kshegunovK 1 Reply Last reply
                0
                • moffa13M moffa13

                  I wrote this :

                  
                  connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
                  connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
                  
                  ...
                  
                  void Connection::error(QAbstractSocket::SocketError e){
                      std::cout << "SSL error ! code : " << e;
                      m_socket->ignoreSslErrors();
                      write("Please use SSL");
                  }
                  
                  void Connection::sslErrors(const QList<QSslError> &errors){
                      std::cout << "Fuck it ";
                      std::cout << "SSL error " << errors.length();
                      m_socket->ignoreSslErrors();
                      write("Please use SSL");
                  }
                  

                  Only the first works and the ignoreSslErrors() doesn't change anything.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #12

                  @moffa13
                  That is strange. I'm sorry I don't know, I can't see anything wrong with the snippets you provided. As far as I understand it, the signal should be raised (and you should get your slot executed).

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • moffa13M Offline
                    moffa13M Offline
                    moffa13
                    wrote on last edited by moffa13
                    #13

                    Hello,

                    I'm sending to you a short version my code, maybe you can try something ? If you want me to write it here, I'll do it.

                    https://mega.nz/#!0AQxzYjZ!PDXxC-QgsWlBJYnwtQEk16jNdQiXy-d-S4AfkRWeoPc

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      marcbf
                      wrote on last edited by
                      #14

                      I currently have a very similar problem. I'm implementing a send mail client which should gracefully fall back to an unencrypted connection if encryption fails (and the user has decided to go ahead anyway). For that purpose I've connected to the QAbstractSocket::error() signal.

                      While I do get the signal it's ultimately moot since the code in question (QSslSocketBackendPrivate::startHandshake() in qsslsocket_openssl.cpp) immediately closes the socket by calling QSslSocket::abort() after emitting the signal. Because of that calling QSslSocket::ignoreSslErrors() is never an option.

                      I currently have no workaround in place. Maybe the only solution is to connect to the disconnected() signal and then check whether the connection was closed because of a handshake error. If it was, then open a new unencrypted socket/connection. I'm open to suggestions, though... :-)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved