Topic: Getting WANT_READ -323 error

Hi, need help can't figure out why this happens. I like to use my sockets together with SO_RCVTIMEO. And library should handle this, but don't know if it does. Have added some code which i think causes this to happen.

int pahoclient_start( const char *ip, int port )
{
    /* Define a structure to hold the WolfSSL context. */
    WOLFSSL_CTX* ctx;
    WOLFSSL* ssl = NULL;
    int fd;

    /* Low level socket apis  */
    NetInit(&net);
    if(NetConnect(&fd, ip , port, 10000) < 0)
        return FAILURE;

    net.fd = fd;

    /* enable debugging */
#if defined(DEBUG_WOLFSSL)
    wolfSSL_Debugging_ON();
#endif

    /* Setup the WolfSSL library */
    wolfSSL_Init();

    /* Create and initialize WOLFSSL_CTX */
    if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
        fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
        goto exit;
    }

    /* Load client certificates into WOLFSSL_CTX */
    if (wolfSSL_CTX_load_verify_buffer(ctx, client_cert_home,client_cert_home_length, SSL_FILETYPE_PEM) != SSL_SUCCESS) {
        fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", client_cert);
        goto exit;
    }

    /* define client callbacks */
    wolfSSL_CTX_SetIOSend(ctx, TlsSocketSend);
    wolfSSL_CTX_SetIORecv(ctx, TlsSocketReceive);

    /* Create a WOLFSSL object */
    if ((ssl = wolfSSL_new(ctx)) == NULL) {
        fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
        goto exit;
    }

    /* Attach wolfSSL to the socket */
    wolfSSL_set_fd(ssl, fd);

    wolfSSL_SetIOReadCtx(ssl, (void *)&net);
    wolfSSL_SetIOWriteCtx(ssl, (void *)&net);

    /* Connect to wolfSSL on the server side */
    if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
        fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
        goto exit;
    }

    /* mqtt lower level apis */
    mqtt.mqttread = mqtt_read;
    mqtt.mqttwrite = mqtt_write;
    mqtt.disconnect = mqtt_disconnect;

    wolfssl = ssl;

    MQTTClientInit(&client, &mqtt, 10000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf));
    MQTTPacket_connectData    options = MQTTPacket_connectData_initializer;


    options.cleansession = 1;
    options.clientID.cstring = "ClientID";
    options.keepAliveInterval    = 6000;
    options.willFlag = 0;
    options.MQTTVersion = 3;
    options.username.cstring = "abcdef";
    options.password.cstring = "12345678";

    if(MQTTConnect(&client, &options) <0 )
        goto exit;

    if(MQTTSubscribe(&client, "huhu" , QOS1, MqttMessageArrived) < 0)
        goto exit;


    return MQTTSUCCESS;

exit:
    wolfSSL_free( ssl );
    wolfSSL_CTX_free( ctx );
    wolfSSL_Cleanup();
    NetDisconnect( &net.fd );
    return FAILURE;
}

int pahoclient_run(void)
{
    int rc = MQTTSUCCESS;

    if(MQTTIsConnected(&client)) {
        rc = MQTTYield(&client, 100);
    }

    return rc;
}

int NetRead( int* fd, char *Buf, int len, int timeout_ms)
{
    int rc;
    struct timeval tv;
    int so_error = 0;
    int index;

    /* check before entering */
    if(*fd < 0 || Buf == NULL || len <= 0)
        return -1;

    /* Set timeouts for socket */
    setup_timeout(&tv, timeout_ms);
    rc = setsockopt( *fd, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, sizeof(tv));
    LWIP_ASSERT("rcvtimeo sockopt", rc == 0);

    index = 0;

    /* Try reading amount of data */
    while( index < len ) {
        rc = read( *fd, &Buf[index], len - index );
        if(rc < 0) {
            /* Get error */
            socklen_t len = sizeof(so_error);
            getsockopt(*fd, SOL_SOCKET, SO_ERROR, &so_error, &len);
            if (so_error == 0) {
                LWIP_ASSERT("socketerror", so_error == 0);
                rc = so_error; /* return error */
                index = len;
                break;
            }
        }
        else {
            index += rc;
        }
    };

    LWIP_ASSERT("index == len", index == (int)len);

    return rc;
}

static int TlsSocketReceive(WOLFSSL* ssl, char *buf, int sz, void *ptr)
{
    int rc;
    Net *net = (Net*)ptr;
    (void)ssl; /* Not used */
    rc = net->read(&net->fd, (char*)buf, sz, 100);
    if (rc == 0) {
        rc = WOLFSSL_CBIO_ERR_WANT_READ; <--- this is the place causing the error
    }
    else if (rc < 0) {
        rc = WOLFSSL_CBIO_ERR_GENERAL;
    }
    return rc;
}

What is the correct way of using wolfssl together with lwip library and timeouts?
Thanks in advance

Share

Re: Getting WANT_READ -323 error

Hi MO380,

-323 WANT_READ is an expected error during a connection, this just means you should call the connect/read function again until the error goes away.  The error is indicating wolfSSL needs to read additional data which hasn't come in yet.

Check out our LWIP example here: https://github.com/wolfSSL/wolfssl-exam … aster/lwip

Thanks,
Kareem

Share

Re: Getting WANT_READ -323 error

Hi Kareem,

i'm already connected this is past me and i have also published a greeting to the broker. But after that my paho mqtt library needs to invoke repeatedly a function which handles receiving and also the keepalive, but here it fails. I'm using also my own socket not the one from LWIP. And those are implemented together with timeouts, not using a select on that. So it will stay in amount of time and come back. Well here i would expect 0 as a return, this would be considered a timeout, but unfortunately it comes back with -1. Checking the error returns 0. This should be fine i think so. But question is now how to exit the library correctly. I tried change the return value to WOLFSSL_CBIO_ERR_TIMEOUT, but gave me some other failure -308.

BTW, not able to upload any file.

Thanks,
MO

Share

Re: Getting WANT_READ -323 error

Hi MO380,

Please reach out to us at support [AT] wolfssl [DOT] com, you should be able to attach files there.  Please attach your user_settings.h/configure line in your ticket.

Thanks,
Kareem

Share