mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

Getting a segmentation fault when using mosquitto connect after using mosquitto_tls_set

Open matthew-g-austin opened this issue 4 years ago • 1 comments

mosquitto_t * TLSInit(mosq_config_t * mConfig,  void user_message_callback (void *f (mosquitto_t *,void *, const mosquitto_message_t *)), void * user_connect_callback)
{
    
    int keepalive = 60;
    bool clean_session = true;
    printDivider();
    mosquitto_t * mosq = NULL;
    mosquitto_t * ret = NULL;
    mosquittoLibInit();
    mosq = mosquitto_new(NULL, clean_session, NULL);
    uint16_t err = 0;
    if (mosq == NULL)
    {
        fprintf(stderr, "couldn't create mosquitto object\n");
        ret = NULL;
    }
    else
    {
        err = mosquitto_tls_set(mosq, mConfig->caFile, mConfig->caPath, mConfig->certFile, mConfig->keyFile, NULL); // last parameter can be a call back atm none is needed
        if (err == MOSQ_ERR_SUCCESS)
        {
            mosquitto_log_callback_set(mosq, logCallback);
            mosquitto_connect_callback_set(mosq, user_connect_callback);
            mosquitto_message_callback_set(mosq, user_message_callback);
            mosquitto_subscribe_callback_set(mosq, subscribeCallback);
            char buff[256] = { 0 };
            sprintf(buff, "caFile=%s;caPath=%s;certFile=%s;keyFile=%s;", mConfig->caFile, mConfig->caPath, mConfig->certFile, mConfig->keyFile);
            printLine(buff, __LINE__);
            memset(buff, 0, sizeof(buff));
            uint16_t port =  mConfig->port;
            strcpy(buff, mConfig->host);
            if (mosquitto_connect(mosq, buff, port, keepalive) == 0) // if connect fails
            {
                shutdown(mosq);
                printLine(stderr, "Unable to connect.\n");
                ret = NULL;
            }
            else
            {
                printLine("Made it to line %d", __LINE__);
                ret = mosq;
                mosquitto_loop_start(mosq);
            }
        }
        else
        {
            printLine("Error = %d", err);
            shutdown(mosq);
            ret = NULL;
        }
        
    }
    
    return ret;
}

Am I doing something wrong here. I've checked all of my paths and tested them.

matthew-g-austin avatar Dec 13 '21 20:12 matthew-g-austin

This looks ok, but without more details it is difficult to say. One comment - mosquitto_connect() will return 0 on success.

ralight avatar May 19 '22 16:05 ralight