Topic: WOLFSSL CLIENT Error
Hi,
I am developing wolfssl Client in TI-RTOS. I am getting error "wolfSSL_connect()" Fail.
At server side It shows "Non-Blocking client trying to read data , Accept Fail".
Below is my code.
Void tcpClientTaskFxn(UArg arg0, UArg arg1)
{
int sockfd;
struct sockaddr_in servAddr;
char buff[256];
size_t len;
char wolfssl_object_Created_successfully = false;
char str[INET_ADDRSTRLEN];
static struct timeval timeout;
int optval;
int optlen = sizeof(optval);
int tmp1_sizeof_sockaddr, peer_name_response;
int ret;
/* declare wolfSSL objects */
WOLFSSL_CTX* ctx;
WOLFSSL* ssl;
while(1)
{
/* Initialize wolfSSL */
wolfSSL_Init();
Task_sleep(1000);
//----------------------------------------------------------------------------------------//
/* Create a socket that uses an internet IPv4 address,
* Sets the socket to be stream based (TCP),
* 0 means choose the default protocol. */
//----------------------------------------------------------------------------------------//
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
System_printf("########### tcpClient: socket Failed !!!!!!!!!!!!! \r\n");
System_flush();
goto Exit;
}
/* Create and initialize WOLFSSL_CTX */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL)
{
System_printf("########### tcpClient: failed to create WOLFSSL_CTX !!!!!!!!!!!!! \r\n");
System_flush();
goto Exit;
}
/* Load client certificates into WOLFSSL_CTX */
if (wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048,sizeof(ca_cert_der_2048)/sizeof(char), SSL_FILETYPE_ASN1) != SSL_SUCCESS)
{
System_printf("########### tcpClient: failed to load %s, please check the file !!!!!!!!!!!!! \r\n",ca_cert_der_2048);
System_flush();
goto Exit;
}
/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL)
{
System_printf("########### tcpClient: ERROR: failed to create WOLFSSL object !!!!!!!!!!!!! \r\n");
System_flush();
goto Exit;
}
/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET; /* using IPv4 */
servAddr.sin_port = htons(atoi(usr_s.socket_port));//htons(80);//htons(7);
/* Get the server IPv4 address from the command line call */
if(inet_pton(AF_INET, usr_s.socket_ip, &(servAddr.sin_addr)) != 1)
{
System_printf("########### tcpClient: ERROR invalid address !!!!!!!!!!!!! \r\n");
System_flush();
goto Exit;
}
/* Connect to the server */
if(connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr))== -1)
{
System_printf("########### tcpClient: ERROR: failed to connect !!!!!!!!!!!!! \r\n");
System_flush();
Task_sleep(1000);
}
tmp1_sizeof_sockaddr = sizeof(servAddr);
peer_name_response = getpeername(sockfd, (struct sockaddr *)&servAddr, &tmp1_sizeof_sockaddr);
System_printf("connect success: getpeername() = %d. sock_addr = %u!!\n", peer_name_response, (unsigned int)servAddr.sin_addr.s_addr);
System_flush();
Task_sleep(1000);
/* This function allows the application to determine if wolfSSL is using non-blocking I/O. If
wolfSSL is using non-blocking I/O, this function will return 1, otherwise 0. */
ret = wolfSSL_get_using_nonblock(ssl);
if (ret == 0)
{
/*underlying I/O is blocking*/
System_printf("+++++++++ SSL is in Blocking Mode ++++++++\r\n");
System_flush();
Task_sleep(1000);
}
/* Attach wolfSSL to the socket */
wolfSSL_set_fd(ssl, sockfd);
/* Connect to wolfSSL on the server side */
if(wolfSSL_connect(ssl) != SSL_SUCCESS)
{
System_printf("########### tcpClient: failed to connect to wolfSSL !!!!!!!!!!!!! \r\n");
System_flush();
Task_sleep(1000);
goto Exit;
}
/* Get a message for the server from stdin */
System_printf("Message for server: ");
memset(buff, 0, sizeof(buff));
sprintf(buff,"%s\r\n","Data_To Send Server");
len = strlen(buff);
/* Send the message to the server */
if (wolfSSL_write(ssl, buff, len) != len)
{
System_printf("########### tcpClient: failed to write !!!!!!!!!!!!! \r\n");
System_flush();
goto Exit;
}
/* Read the server data into our buff array */
memset(buff, 0, sizeof(buff));
if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1)
{
System_printf("########### tcpClient: failed to read !!!!!!!!!!!!! \r\n");
System_flush();
goto Exit;
}
/* Print to stdout any data the server sends */
System_printf("======>> Server: %s\n", buff);
Exit:
if(wolfssl_object_Created_successfully == true)
{
/* Cleanup and return */
wolfSSL_free(ssl); /* Free the wolfSSL object */
}
/* Close the connection to the server */
close(sockfd);
exitApp(ctx);
Task_sleep(5000);
}
}
please help me to find solution.