Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ESP32/DTLS13-wifi-station-client/main/client-dtls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ WOLFSSL_ESP_TASK dtls13_smp_client_task(void *pvParameters)
ESP_LOGI(TAG, "See ./include/client-dtls13.h to update settings.");
ESP_LOGI(TAG, "Setting server address to %s, port %d.",
TLS_SMP_SERVER_ADDRESS, SERV_PORT);
memset(&servAddr, 0, sizeof(servAddr));
XMEMSET(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(SERV_PORT);
if (inet_pton(AF_INET, TLS_SMP_SERVER_ADDRESS, &servAddr.sin_addr) < 1) {
Expand Down Expand Up @@ -199,10 +199,11 @@ WOLFSSL_ESP_TASK dtls13_smp_client_task(void *pvParameters)

ESP_LOGI(TAG, "Sending message");

strcpy(sendLine, "Hello World.");
XSTRCPY(sendLine, "Hello World.");

/* Send sendLine to the server */
if (wolfSSL_write(ssl, sendLine, strlen(sendLine)) != strlen(sendLine)) {
if (wolfSSL_write(ssl, sendLine, XSTRLEN(sendLine)) !=
XSTRLEN(sendLine)) {
err = wolfSSL_get_error(ssl, 0);
ESP_LOGE(TAG, "err = %d, %s\n",
err, wolfSSL_ERR_reason_error_string(err));
Expand Down
2 changes: 1 addition & 1 deletion ESP32/DTLS13-wifi-station-client/main/time_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int set_time(void)
ESP_LOGI(TAG, "sntp_setservername:");
for (i = 0; i < NTP_SERVER_COUNT; i++) {
const char* thisServer = ntpServerList[i];
if (strncmp(thisServer, "\x00", 1) == 0) {
if (XSTRNCMP(thisServer, "\x00", 1) == 0) {
/* just in case we run out of NTP servers */
break;
}
Expand Down
2 changes: 1 addition & 1 deletion ESP32/DTLS13-wifi-station-server/main/server-dtls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)

/* initialize network vars */
if (ret == WOLFSSL_SUCCESS) {
memset((char *)&servAddr, 0, sizeof(servAddr));
XMEMSET((char *)&servAddr, 0, sizeof(servAddr));
/* host-to-network-long conversion (htonl) */
/* host-to-network-short conversion (htons) */
servAddr.sin_family = AF_INET;
Expand Down
2 changes: 1 addition & 1 deletion ESP32/DTLS13-wifi-station-server/main/time_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ int set_time(void)
ESP_LOGI(TAG, "sntp_setservername:");
for (i = 0; i < NTP_SERVER_COUNT; i++) {
const char* thisServer = ntpServerList[i];
if (strncmp(thisServer, "\x00", 1) == 0) {
if (XSTRNCMP(thisServer, "\x00", 1) == 0) {
/* just in case we run out of NTP servers */
break;
}
Expand Down
25 changes: 15 additions & 10 deletions ESP32/TLS13-ENC28J60-client/main/enc28j60_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ int tls_smp_client_task() {
#endif /* WOLFSSL_TLS13 */

/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
XMEMSET(&servAddr, 0, sizeof(servAddr));

/* Fill in the server address */
servAddr.sin_family = AF_INET; /* using IPv4 */
Expand Down Expand Up @@ -220,13 +220,16 @@ int tls_smp_client_task() {
* a non-negative integer, the socket file descriptor.
*/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd > 0) {
if (sockfd >= 0) {
WOLFSSL_MSG("socket creation successful\n");
}
else {
// TODO show errno
char err_msg[128];
XSNPRINTF(err_msg, sizeof(err_msg),
"ERROR: failed to create a socket (errno = %d).\n",
errno);
WOLFSSL_ERROR_MSG(err_msg);
ret = WOLFSSL_FAILURE;
WOLFSSL_ERROR_MSG("ERROR: failed to create a socket.\n");
}
}
else {
Expand Down Expand Up @@ -258,8 +261,10 @@ int tls_smp_client_task() {
WOLFSSL_MSG("sockfd connect successful\n");
}
else {
// TODO show errno
WOLFSSL_ERROR_MSG("ERROR: socket connect failed\n");
char err_msg[128];
XSNPRINTF(err_msg, sizeof(err_msg),
"ERROR: socket connect failed (errno = %d)\n", errno);
WOLFSSL_ERROR_MSG(err_msg);
ret = WOLFSSL_FAILURE;
}
}
Expand Down Expand Up @@ -697,13 +702,13 @@ int tls_smp_client_task() {
*/
if (ret == WOLFSSL_SUCCESS) {

memset(buff, 0, BUFF_SIZE);
XMEMSET(buff, 0, BUFF_SIZE);

/* get the length of our message, never longer than the declared size */

/* TODO check for zero length */

len = strnlen(sendMessage, sendMessageSize);
len = XSTRLEN(sendMessage);

/* write the message over secure connection to the server */
if (wolfSSL_write(ssl, sendMessage, len) == len) {
Expand Down Expand Up @@ -783,7 +788,7 @@ int tls_smp_client_task() {
if (ret == WOLFSSL_SUCCESS) {
/* even though the result should be a zero-terminated string,
* we'll clear the receive buffer */
memset(buff, 0, BUFF_SIZE);
XMEMSET(buff, 0, BUFF_SIZE);

/* read the response data from our secure connection */
if (wolfSSL_read(ssl, buff, BUFF_SIZE - 1) > 0) {
Expand Down Expand Up @@ -946,7 +951,7 @@ int set_time() {
int i = 0;
for (i = 0; i < NTP_SERVER_COUNT; i++) {
const char* thisServer = ntpServerList[i];
if (strncmp(thisServer, "\x00", 1)) {
if (XSTRNCMP(thisServer, "\x00", 1)) {
/* just in case we run out of NTP servers */
break;
}
Expand Down
98 changes: 51 additions & 47 deletions ESP32/TLS13-ENC28J60-server/main/enc28j60_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ int tls_smp_server_task() {
#endif /* WOLFSSL_TLS13 */

/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
XMEMSET(&servAddr, 0, sizeof(servAddr));

/* Fill in the server address */
servAddr.sin_family = AF_INET; /* using IPv4 */
Expand Down Expand Up @@ -280,13 +280,16 @@ int tls_smp_server_task() {
* a non-negative integer, the socket file descriptor.
*/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd > 0) {
if (sockfd >= 0) {
WOLFSSL_MSG("socket creation successful\n");
}
else {
// TODO show errno
char err_msg[128];
XSNPRINTF(err_msg, sizeof(err_msg),
"ERROR: failed to create a socket (errno = %d).\n",
errno);
WOLFSSL_ERROR_MSG(err_msg);
ret = WOLFSSL_FAILURE;
WOLFSSL_ERROR_MSG("ERROR: failed to create a socket.\n");
}
}
else {
Expand Down Expand Up @@ -348,9 +351,13 @@ int tls_smp_server_task() {
WOLFSSL_MSG("setsockopt re-use addr successful\n");
}
else {
// TODO show errno
char err_msg[128];
XSNPRINTF(err_msg, sizeof(err_msg),
"ERROR: failed to setsockopt addr on "
"socket (errno = %d).\n",
errno);
WOLFSSL_ERROR_MSG(err_msg);
ret = WOLFSSL_FAILURE;
WOLFSSL_ERROR_MSG("ERROR: failed to setsockopt addr on socket.\n");
}
}
else {
Expand All @@ -370,10 +377,12 @@ int tls_smp_server_task() {
WOLFSSL_MSG("setsockopt re-use port successful\n");
}
else {
// TODO show errno
// ret = WOLFSSL_FAILURE;
// TODO what's up with the error?
WOLFSSL_ERROR_MSG("ERROR: failed to setsockopt port on socket. >> IGNORED << \n");
char err_msg[128];
XSNPRINTF(err_msg, sizeof(err_msg),
"ERROR: failed to setsockopt port on "
"socket (errno = %d). "
">> IGNORED << \n", errno);
WOLFSSL_ERROR_MSG(err_msg);
}
}
else {
Expand Down Expand Up @@ -738,21 +747,19 @@ int tls_smp_server_task() {
/* Accept client connections */
if ((mConnd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
== -1) {
// fprintf(stderr, "ERROR: failed to accept the connection\n\n");
ret = -1;
// TODO goto exit;
WOLFSSL_ERROR_MSG("ERROR: failed socket accept\n");
ret = WOLFSSL_FAILURE;
WOLFSSL_ERROR_MSG("ERROR: failed socket accept\n");
ret = WOLFSSL_FAILURE;
break;
}

/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) {
// fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
ret = -1;
//TODO goto exit;
WOLFSSL_ERROR_MSG("ERROR: filed wolfSSL_new during loop\n");
WOLFSSL_ERROR_MSG("ERROR: failed wolfSSL_new during loop\n");
ret = WOLFSSL_FAILURE;
}
close(mConnd);
mConnd = SOCKET_INVALID;
break;
}

/* Attach wolfSSL to the socket */
wolfSSL_set_fd(ssl, mConnd);
Expand All @@ -771,45 +778,42 @@ int tls_smp_server_task() {
if ((ret = wolfSSL_accept(ssl)) != WOLFSSL_SUCCESS) {
WOLFSSL_ERROR_MSG("ERROR: wolfSSL_accept\n");
ret = WOLFSSL_FAILURE;
// fprintf(stderr,
// "wolfSSL_accept error = %d\n",
// wolfSSL_get_error(ssl, ret));
// TODO goto exit;
}
else {
WOLFSSL_MSG("Client connected successfully\n");
}


#ifdef HAVE_SECRET_CALLBACK
wolfSSL_FreeArrays(ssl);
if (ret == WOLFSSL_SUCCESS) {
wolfSSL_FreeArrays(ssl);
}
#endif

/* Read the client data into our buff array */
memset(buff, 0, sizeof(buff));
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff) - 1)) < 0) {
// fprintf(stderr, "ERROR: failed to read\n");
//TODO goto exit;
if (ret == WOLFSSL_SUCCESS) {
XMEMSET(buff, 0, sizeof(buff));
if (wolfSSL_read(ssl, buff, sizeof(buff) - 1) <= 0) {
WOLFSSL_ERROR_MSG("ERROR: failed to read\n");
ret = WOLFSSL_FAILURE;
}
}

/* Print to stdout any data the client sends */
// printf("Client: %s\n", buff);

/* Check for server shutdown command */
if (strncmp(buff, "shutdown", 8) == 0) {
// printf("Shutdown command issued!\n");
mShutdown = 1;
}
if (ret == WOLFSSL_SUCCESS) {
/* Check for server shutdown command */
if (XSTRNCMP(buff, "shutdown", 8) == 0) {
mShutdown = 1;
}

/* Write our reply into buff */
memset(buff, 0, sizeof(buff));
memcpy(buff, reply, strlen(reply));
len = strnlen(buff, sizeof(buff));
/* Write our reply into buff */
XMEMSET(buff, 0, sizeof(buff));
XMEMCPY(buff, reply, XSTRLEN(reply));
len = XSTRLEN(buff);

/* Reply back to the client */
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
// fprintf(stderr, "ERROR: failed to write\n");
// TODO goto exit;
/* Reply back to the client */
if (wolfSSL_write(ssl, buff, len) != len) {
WOLFSSL_ERROR_MSG("ERROR: failed to write\n");
ret = WOLFSSL_FAILURE;
}
}

/* Cleanup after this connection */
Expand Down Expand Up @@ -967,7 +971,7 @@ int set_time() {
int i = 0;
for (i = 0; i < NTP_SERVER_COUNT; i++) {
const char* thisServer = ntpServerList[i];
if (strncmp(thisServer, "\x00", 1)) {
if (XSTRNCMP(thisServer, "\x00", 1)) {
/* just in case we run out of NTP servers */
break;
}
Expand Down
21 changes: 11 additions & 10 deletions ESP32/TLS13-wifi_station-client/main/station_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


#include <string.h>
#include <errno.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
Expand Down Expand Up @@ -192,7 +193,7 @@ int set_time() {
int i = 0;
for (i = 0; i < NTP_SERVER_COUNT; i++) {
const char* thisServer = ntpServerList[i];
if (strncmp(thisServer, "\x00", 1)) {
if (XSTRNCMP(thisServer, "\x00", 1)) {
/* just in case we run out of NTP servers */
break;
}
Expand Down Expand Up @@ -302,7 +303,7 @@ int tls_smp_client_task() {


/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
XMEMSET(&servAddr, 0, sizeof(servAddr));

/* Fill in the server address */
servAddr.sin_family = AF_INET; /* using IPv4 */
Expand Down Expand Up @@ -357,13 +358,13 @@ int tls_smp_client_task() {
* a non-negative integer, the socket file descriptor.
*/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd > 0) {
if (sockfd >= 0) {
ESP_LOGI(TAG,"socket creation successful\n");
}
else {
// TODO show errno
ESP_LOGE(TAG,
"ERROR: failed to create a socket (errno = %d).\n", errno);
ret = WOLFSSL_FAILURE;
ESP_LOGE(TAG, "ERROR: failed to create a socket.\n");
}
}
else {
Expand Down Expand Up @@ -398,8 +399,8 @@ int tls_smp_client_task() {
ESP_LOGI(TAG,"sockfd connect successful\n");
}
else {
// TODO show errno
ESP_LOGE(TAG, "ERROR: socket connect failed\n");
ESP_LOGE(TAG,
"ERROR: socket connect failed (errno = %d)\n", errno);
ret = WOLFSSL_FAILURE;
}
}
Expand Down Expand Up @@ -837,13 +838,13 @@ int tls_smp_client_task() {
*/
if (ret == WOLFSSL_SUCCESS) {

memset(buff, 0, BUFF_SIZE);
XMEMSET(buff, 0, BUFF_SIZE);

/* get the length of our message, never longer than the declared size */

/* TODO check for zero length */

len = strnlen(sendMessage, sendMessageSize);
len = XSTRLEN(sendMessage);

/* write the message over secure connection to the server */
if (wolfSSL_write(ssl, sendMessage, len) == len) {
Expand Down Expand Up @@ -923,7 +924,7 @@ int tls_smp_client_task() {
if (ret == WOLFSSL_SUCCESS) {
/* even though the result should be a zero-terminated string,
* we'll clear the receive buffer */
memset(buff, 0, BUFF_SIZE);
XMEMSET(buff, 0, BUFF_SIZE);

/* read the response data from our secure connection */
if (wolfSSL_read(ssl, buff, BUFF_SIZE - 1) > 0) {
Expand Down
Loading