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