0

I am implementing two applications wherein, one application is for publishing messages on topic and another application is an subsriber to the same topic. These two applications are working normally without tls code in them.

But with tls code lines, the subscriber application getting disconnected automatically whenever the publisher application publishes the message on the topic i.e., the MQTTAsync_ConnectionLost call back is getting called. These two applications are following async communication mechanism.

Request someone to please help, to resolve it. Logs of subscriber and publisher are mentioned below.

Publisher App Logs of tls code:

Waiting for publication of Hello World! on topic MQTTExamplesSSL for client with ClientID: ExampleClientPubSSL

Successful connection

Message with token value 1 delivery confirmed

Successful disconnection

Subscriber app logs of tls code:

Successful connection

Subscribing to topic MQTTExamplesSSL for client ExampleClientSubSSL using QoS2

Press Q to quit

Subscribe succeeded

Connection lost

cause: (null)

Reconnecting

connecting is success now

Segmentation fault

Code files of mytest_publishssl.c and mytest_subscribessl.c:

mytest_publishssl.c:

#include <stdio.h>
#include "MQTTAsync.h"

#define ADDRESS     "ssl://192.168.0.104:8883"
#define CLIENTID    "ExampleClientPubSSL"
#define TOPIC       "MQTTExamplesSSL"
#define PAYLOAD     "Hello World!"
#define QOS         2
#define TIMEOUT     10000L

const char* server_key_file = "ca.pem";

volatile MQTTAsync_token deliveredtoken;
int finished = 0;
void connlost(void *context, char *cause)
{
        MQTTAsync client = (MQTTAsync)context;
        MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
        int rc;
        printf("\nConnection lost\n");
        printf("     cause: %s\n", cause);
        printf("Reconnecting\n");
        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
        if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start connect, return code %d\n", rc);
                finished = 1;
        }
}
void onDisconnect(void* context, MQTTAsync_successData* response)
{
        printf("Successful disconnection\n");
        finished = 1;
}
void onSend(void* context, MQTTAsync_successData* response)
{
        MQTTAsync client = (MQTTAsync)context;
        MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
        int rc;
        printf("Message with token value %d delivery confirmed\n", response->token);
        opts.onSuccess = onDisconnect;
        opts.context = client;
        if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start sendMessage, return code %d\n", rc);
                exit(-1);       
        }
    //printf("Message sent is success \n");
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
    int i;
    char* payloadptr;
    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");
    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    printf("End of the message \n");

    printf("Inside the message arrived function \n");
    return 1;
}
void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
        printf("Connect failed, rc %d\n", response ? response->code : 0);
        finished = 1;
}

void onConnect(void* context, MQTTAsync_successData* response)
{
        MQTTAsync client = (MQTTAsync)context;
        MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
        MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
        int rc;
        printf("Successful connection\n");

        opts.onSuccess = onSend;
        opts.context = client;
        pubmsg.payload = PAYLOAD;
        pubmsg.payloadlen = strlen(PAYLOAD);
        pubmsg.qos = QOS;
        pubmsg.retained = 0;
        deliveredtoken = 0;
        if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start sendMessage, return code %d\n", rc);
                exit(-1);       
        }
}
int main(int argc, char* argv[])
{
        MQTTAsync client;
        MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;
        MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
        MQTTAsync_token token;
        int rc;
        MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
        MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);
        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
    conn_opts.username = "Pravallika";
    conn_opts.password = "Pravallika";
        conn_opts.onSuccess = onConnect;
        conn_opts.onFailure = onConnectFailure;
        conn_opts.context = client;

    conn_opts.ssl = &sslopts;
    if (server_key_file != NULL)
        conn_opts.ssl->trustStore = server_key_file; /*file of certificates trusted by client*/
    //opts.ssl->keyStore = options.client_key_file; /*file of certificate for client to present to server*/
    //conn_opts.ssl->enabledCipherSuites = 1;

        if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start connect, return code %d\n", rc);
                exit(-1);       
        }
        printf("Waiting for publication of %s\n"
         "on topic %s for client with ClientID: %s\n",
         PAYLOAD, TOPIC, CLIENTID);
        while (!finished)
                #if defined(WIN32)
                        Sleep(100);
                #else
                        usleep(10000L);
                #endif
        MQTTAsync_destroy(&client);
        return rc;
}

mytest_subscribessl.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"

#define ADDRESS     "ssl://192.168.0.104:8883"
#define CLIENTID    "ExampleClientSubSSL"
#define TOPIC       "MQTTExamplesSSL"
//#define PAYLOAD     "Hello World!"
#define QOS         2
#define TIMEOUT     10000L
volatile MQTTAsync_token deliveredtoken;
int disc_finished = 0;
int subscribed = 0;
int finished = 0;

const char* server_key_file = "ca.pem";

void connlost(void *context, char *cause)
{
        MQTTAsync* client = (MQTTAsync*)context;
        MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
        int rc;
        printf("\nConnection lost\n");
        printf("     cause: %s\n", cause);
        printf("Reconnecting\n");
        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
        if ((rc = MQTTAsync_connect(*client, &conn_opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start connect, return code %d\n", rc);
            finished = 1;
        }
    else
    {
        printf("connecting is success now \n");
    }
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
    int i;
    char* payloadptr;
    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");
    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    printf("End of the message \n");
    MQTTAsync_freeMessage(&message);
    MQTTAsync_free(topicName);
    return 1;
}
void onDisconnect(void* context, MQTTAsync_successData* response)
{
        printf("Successful disconnection\n");
        disc_finished = 1;
}
void onSubscribe(void* context, MQTTAsync_successData* response)
{
        printf("Subscribe succeeded\n");
        subscribed = 1;
}
void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
{
        printf("Subscribe failed, rc %d\n", response ? response->code : 0);
        finished = 1;
}
void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
        printf("Connect failed, rc %d\n", response ? response->code : 0);
        finished = 1;
}
void onConnect(void* context, MQTTAsync_successData* response)
{
        MQTTAsync client = (MQTTAsync)context;
        MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
        MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
        int rc;
        printf("Successful connection\n");
        printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
           "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
        opts.onSuccess = onSubscribe;
        opts.onFailure = onSubscribeFailure;
        opts.context = client;
        deliveredtoken = 0;
        if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start subscribe, return code %d\n", rc);
                exit(-1);       
        }
}
int main(int argc, char* argv[])
{
        MQTTAsync client;
        MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
        MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
        MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
    MQTTAsync_SSLOptions sslopts = MQTTAsync_SSLOptions_initializer;

        MQTTAsync_token token;
        int rc;
        int ch;
        MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
        MQTTAsync_setCallbacks(client, &client, connlost, msgarrvd, NULL);
        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
    conn_opts.username = "Pravallika";
    conn_opts.password = "Pravallika";
        conn_opts.onSuccess = onConnect;
        conn_opts.onFailure = onConnectFailure;
        conn_opts.context = client;

    conn_opts.ssl = &sslopts;
    if (server_key_file != NULL)
        conn_opts.ssl->trustStore = server_key_file; /*file of certificates trusted by client*/
    //conn_opts.ssl->enabledCipherSuites = 1;

        if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start connect, return code %d\n", rc);
                exit(-1);       
        }
        while   (!subscribed)
                #if defined(WIN32)
                        Sleep(100);
                #else
                        usleep(10000L);
                #endif
        if (finished)
                goto exit;
        do 
        {
                ch = getchar();
        } while (ch!='Q' && ch != 'q');
        disc_opts.onSuccess = onDisconnect;
        if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start disconnect, return code %d\n", rc);
                exit(-1);       
        }
        while   (!disc_finished)
                #if defined(WIN32)
                        Sleep(100);
                #else
                        usleep(10000L);
                #endif
exit:
        MQTTAsync_destroy(&client);
        return rc;
}

Please help if i am missing anything for tls functionality.

Pravallika
  • 19
  • 5
  • **Segmentation Falut**. Please search for `How to debug segmentation fault core dumped on Linux`. Execute `ulimit -c unlimited` command; Compile your code with debugging symbols ON (use `-g` flag with gcc); Execute your program once more to make it crash again; It will create a `core` file in the same directory. Use `gdb core `; Then form the `gdb` prompt call `bt` or `bt full` for full backtrace. – Gaurav Pathak Jun 23 '17 at 10:27
  • Thank you Gaurav but i am looking for suggestions like anything i am missing in my program to achieve tls functionality? What might be reason for connection lost in subscriber program whenever the message is published to topic by publisher program? – Pravallika Jun 24 '17 at 04:16

0 Answers0