Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] mosquitto_connect() fails to connect to broker but returns MOSQ_ERR_SUCCESS

I am in the process of upgrading a client program from using unsecured
anonymous MQTT, to TLS secured MQTT with client certificates. The
Broker is Mosquitto 1.4.11 (running on local host) and the client uses
libmosquitto 1.4.11.

If the broker is configured without TLS, then the client connects and
works fine. At the moment I intentionally have the broker configured
to use TLS, but not yet added the call to mosquitto_tls_set(). So I am
expecting the connection to fail, so that I can test the error
handling in my application. However, although the broker fails the
connection with log messages:

1495795497: OpenSSL Error: error:1408F10B:SSL
routines:SSL3_GET_RECORD:wrong version number
1495795497: Socket error on client <unknown>, disconnecting.
1495795717: New connection from 127.0.0.1 on port 8883.

The return value my code gets from mosquitto_connect() is
MOSQ_ERR_SUCCESS, so the application tries to carry on, and gets into
trouble later on.

The code making the connection is
    int conn_stat;
    conn_stat = mosquitto_connect(mosq, broker_host.c_str(), port, keepalive);

    if (MOSQ_ERR_SUCCESS == conn_stat)
    {
    ...
    } else {
    ...
    }
so nothing complex. I have stepped this in gdb and confirmed that the
value of conn_stat after the call is zero.

Is this something I am doing wrong, or a bug somewhere?

I have tried following the code down through libmosquitto, and ended
up looking in  _mosquitto_packet_queue(), all I could see was at line
189 of lib/net_mosq.c:
send(mosq->sockpairW, &sockpair_data, 1, 0);
where the return value isn't being checked. That said, I tried
stopping here in the debugger, and the single byte is written
correctly. Presumably the server doesn't realise that the connection
is invalid until this byte arrives?

After this the code returns right back out to my caller, which is why
it returns MOSQ_ERR_SUCCESS to me.

If this is inherent (the client doesn't yet know it has been chucked
out by the server), is there any way I can subsequently test to see if
the connection is valid?

What seems to happen at the moment is that the subsequent
mosquitto_will_set() call returns success (despite having no
connection), then mosquitto_subscribe() calls fail, because the broker
isn't connected.

Richard


Back to the top