socket emulation
parent
2c423825ff
commit
4b64a7bd22
|
@ -28,4 +28,7 @@
|
|||
#define LWIP_PROVIDE_ERRNO
|
||||
#define LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX 1
|
||||
|
||||
/* wolfTCP config */
|
||||
#define WOLF_SOCKET 1
|
||||
|
||||
#endif
|
||||
|
|
|
@ -72,7 +72,7 @@ extern time_t myTime(time_t *);
|
|||
// #define WOLFSSL_SP_384 /* Enable ECC 384-bit SECP384R1 support */
|
||||
|
||||
// #define WOLFSSL_SP_CACHE_RESISTANT
|
||||
// #define WOLFSSL_SP_MATH /* only SP math - disables integer.c/tfm.c */
|
||||
#define WOLFSSL_SP_MATH /* only SP math - disables integer.c/tfm.c */
|
||||
#define WOLFSSL_SP_MATH_ALL /* use SP math for all key sizes and curves */
|
||||
|
||||
// #define WOLFSSL_SP_NO_MALLOC
|
||||
|
@ -254,8 +254,9 @@ extern time_t myTime(time_t *);
|
|||
#if 1
|
||||
#define HAVE_CURVE448
|
||||
#define HAVE_ED448 /* ED448 Requires SHA512 */
|
||||
#define WOLFSSL_SHAKE256
|
||||
|
||||
/* Optionally use small math (less flash usage, but much slower) */
|
||||
/* Optionally use small math (less flash usage, but much slower) */
|
||||
#if 0
|
||||
#define CURVED448_SMALL
|
||||
#endif
|
||||
|
|
|
@ -28,9 +28,38 @@
|
|||
#include "wolf/blink.h"
|
||||
#include "wolf/common.h"
|
||||
|
||||
/* This is just for, inet_pton, sockaddr_in */
|
||||
#undef LWIP_SOCKET
|
||||
#define LWIP_SOCKET 1
|
||||
#define LWIP_IPV4 1
|
||||
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/inet.h"
|
||||
|
||||
#if WOLF_SOCKET
|
||||
|
||||
#undef SOCKET_T
|
||||
#undef socket
|
||||
#undef close
|
||||
#undef inet_pton
|
||||
#undef connect
|
||||
#undef recv
|
||||
#undef send
|
||||
#define SOCKET_T WOLF_SOCKET_T
|
||||
#define socket wolf_TCPsocket
|
||||
#define close wolf_TCPclose
|
||||
#define inet_pton wolf_inet_pton
|
||||
#define connect wolf_TCPconnect
|
||||
#define recv wolf_TCPread
|
||||
#define send wolf_TCPwrite
|
||||
|
||||
#endif
|
||||
|
||||
#define DEBUG_printf printf
|
||||
#define BUF_SIZE (4096*2)
|
||||
|
||||
typedef u32_t socklen_t;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *tcp_pcb;
|
||||
ip_addr_t remote_addr;
|
||||
|
@ -40,10 +69,13 @@ typedef struct {
|
|||
bool complete;
|
||||
int run_count;
|
||||
bool connected;
|
||||
} WOLF_SOCKET_T;
|
||||
} *WOLF_SOCKET_T;
|
||||
|
||||
WOLF_SOCKET_T *wolf_TCPsocket(void);
|
||||
static err_t wolf_TCPfree(WOLF_SOCKET_T *);
|
||||
bool wolf_TCPconnect(WOLF_SOCKET_T *, const char*, uint32_t);
|
||||
int wolf_TCPread (WOLF_SOCKET_T *, unsigned char *, long unsigned int);
|
||||
int wolf_TCPwrite(WOLF_SOCKET_T *, const unsigned char *, long unsigned int);
|
||||
|
||||
|
||||
WOLF_SOCKET_T wolf_TCPsocket(void);
|
||||
int wolf_inet_pton(int af, const char *ip_str, void *ip_dst);
|
||||
int wolf_TCPclose(WOLF_SOCKET_T sock);
|
||||
int wolf_TCPconnect(WOLF_SOCKET_T, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int wolf_TCPread (WOLF_SOCKET_T, unsigned char *, unsigned long);
|
||||
int wolf_TCPwrite(WOLF_SOCKET_T, const unsigned char *, unsigned long);
|
|
@ -43,7 +43,7 @@ static void dump_bytes(const uint8_t *p, uint32_t len)
|
|||
}
|
||||
|
||||
|
||||
err_t wolf_TCPclose(WOLF_SOCKET_T *sock)
|
||||
int wolf_TCPclose(WOLF_SOCKET_T sock)
|
||||
{
|
||||
err_t err = ERR_OK;
|
||||
if (sock->tcp_pcb != NULL)
|
||||
|
@ -65,7 +65,7 @@ err_t wolf_TCPclose(WOLF_SOCKET_T *sock)
|
|||
return err;
|
||||
}
|
||||
|
||||
static err_t tcp_result(WOLF_SOCKET_T *sock, int status)
|
||||
static err_t tcp_result(WOLF_SOCKET_T sock, int status)
|
||||
{
|
||||
if (status == 0) {
|
||||
DEBUG_printf("test success\n");
|
||||
|
@ -78,7 +78,7 @@ static err_t tcp_result(WOLF_SOCKET_T *sock, int status)
|
|||
|
||||
static err_t lwip_cb_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
|
||||
{
|
||||
WOLF_SOCKET_T *sock = (WOLF_SOCKET_T *)arg;
|
||||
WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg;
|
||||
|
||||
sock->sent_len += len;
|
||||
|
||||
|
@ -102,7 +102,8 @@ static err_t lwip_cb_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
|
|||
|
||||
static err_t lwip_cb_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
|
||||
{
|
||||
WOLF_SOCKET_T *sock = (WOLF_SOCKET_T *)arg;
|
||||
WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg;
|
||||
|
||||
if (err != ERR_OK)
|
||||
{
|
||||
DEBUG_printf("connect failed %d\n", err);
|
||||
|
@ -127,14 +128,13 @@ static void lwip_cb_client_err(void *arg, err_t err)
|
|||
|
||||
static err_t lwip_cb_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
{
|
||||
WOLF_SOCKET_T *sock = (WOLF_SOCKET_T *)arg;
|
||||
WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg;
|
||||
if (!p) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
cyw43_arch_lwip_check();
|
||||
if (p->tot_len > 0)
|
||||
{
|
||||
if (p->tot_len > 0) {
|
||||
const uint16_t buffer_left = BUF_SIZE - sock->buffer_len;
|
||||
sock->buffer_len += pbuf_copy_partial(p, sock->buffer + sock->buffer_len,
|
||||
p->tot_len > buffer_left ? buffer_left : p->tot_len, 0);
|
||||
|
@ -144,10 +144,26 @@ static err_t lwip_cb_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p
|
|||
return ERR_OK;
|
||||
}
|
||||
|
||||
bool wolf_TCPconnect(WOLF_SOCKET_T *sock, const char *ip, uint32_t port)
|
||||
int wolf_inet_pton(int af, const char *ip_str, void *ip_dst)
|
||||
{
|
||||
ip4addr_aton(ip, &sock->remote_addr);
|
||||
sock->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&sock->remote_addr));
|
||||
(void)af;
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)ip_dst;
|
||||
|
||||
return ip4addr_aton(ip_str, ip_dst);
|
||||
}
|
||||
|
||||
static u32_t swapBytes(u16_t a)
|
||||
{
|
||||
u8_t *p = (u8_t *)&a;
|
||||
return p[0] << 8 | p[1];
|
||||
}
|
||||
|
||||
int wolf_TCPconnect(WOLF_SOCKET_T sock, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
int err;
|
||||
struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
|
||||
|
||||
sock->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&addr_in->sin_addr));
|
||||
if (!sock->tcp_pcb) {
|
||||
DEBUG_printf("failed to create pcb\n");
|
||||
return false;
|
||||
|
@ -162,13 +178,17 @@ bool wolf_TCPconnect(WOLF_SOCKET_T *sock, const char *ip, uint32_t port)
|
|||
sock->buffer_len = 0;
|
||||
|
||||
cyw43_arch_lwip_begin();
|
||||
err_t err = tcp_connect(sock->tcp_pcb, &sock->remote_addr, port, lwip_cb_client_connected);
|
||||
err = tcp_connect(sock->tcp_pcb, (const ip_addr_t *)&addr_in->sin_addr,
|
||||
swapBytes(addr_in->sin_port), lwip_cb_client_connected);
|
||||
cyw43_arch_lwip_end();
|
||||
if (err != ERR_OK) {
|
||||
DEBUG_printf("wolf_TCPconnect: Failed");
|
||||
return WOLF_FAIL;
|
||||
}
|
||||
while (sock->connected != true) {
|
||||
sock->connected = false;
|
||||
|
||||
while (sock->connected != true)
|
||||
{
|
||||
cyw43_arch_poll();
|
||||
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
||||
}
|
||||
|
@ -176,18 +196,17 @@ bool wolf_TCPconnect(WOLF_SOCKET_T *sock, const char *ip, uint32_t port)
|
|||
}
|
||||
|
||||
// get a new TCP client
|
||||
WOLF_SOCKET_T *wolf_TCPsocket()
|
||||
WOLF_SOCKET_T wolf_TCPsocket()
|
||||
{
|
||||
WOLF_SOCKET_T *sock = calloc(1, sizeof(WOLF_SOCKET_T));
|
||||
WOLF_SOCKET_T sock = calloc(1, sizeof(*sock));
|
||||
if (!sock) {
|
||||
DEBUG_printf("failed to allocate state\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
int wolf_TCPwrite(WOLF_SOCKET_T *sock, const unsigned char *buff, long unsigned int len)
|
||||
int wolf_TCPwrite(WOLF_SOCKET_T sock, const unsigned char *buff, long unsigned int len)
|
||||
{
|
||||
int ret;
|
||||
int i;
|
||||
|
@ -198,23 +217,19 @@ int wolf_TCPwrite(WOLF_SOCKET_T *sock, const unsigned char *buff, long unsigned
|
|||
if (ret == ERR_OK) {
|
||||
tcp_output(sock->tcp_pcb);
|
||||
}
|
||||
while(sock->sent_len < len) {
|
||||
cyw43_arch_poll();
|
||||
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
||||
}
|
||||
sock->sent_len = 0;
|
||||
return (int)len;
|
||||
}
|
||||
|
||||
int wolf_TCPread(WOLF_SOCKET_T *sock, unsigned char *buff, long unsigned int len)
|
||||
int wolf_TCPread(WOLF_SOCKET_T sock, unsigned char *buff, long unsigned int len)
|
||||
{
|
||||
int recv_len;
|
||||
int remained;
|
||||
int i;
|
||||
#define POLLING 200
|
||||
|
||||
for(i=0; i<POLLING; i++) {
|
||||
while (1) { /* no timeout for now */
|
||||
if(sock->buffer_len > 0) {
|
||||
recv_len = len < sock->buffer_len ? len : sock->buffer_len;
|
||||
recv_len = len <= sock->buffer_len ? len : sock->buffer_len;
|
||||
memcpy(buff, sock->buffer, recv_len);
|
||||
if(recv_len >= len) {
|
||||
remained = sock->buffer_len - recv_len;
|
||||
|
@ -226,6 +241,6 @@ int wolf_TCPread(WOLF_SOCKET_T *sock, unsigned char *buff, long unsigned int len
|
|||
return recv_len;
|
||||
}
|
||||
cyw43_arch_poll();
|
||||
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
||||
cyw43_arch_wait_for_work_until(make_timeout_time_ms(10));
|
||||
}
|
||||
}
|
|
@ -27,46 +27,58 @@
|
|||
#include "wolf/tcp.h"
|
||||
#include "wolf/wifi.h"
|
||||
#include "wolf/blink.h"
|
||||
#include "lwip/tcp.h"
|
||||
|
||||
#define TCP_PORT 1111
|
||||
|
||||
void tcpClient_test(void)
|
||||
{
|
||||
int i;
|
||||
int err;
|
||||
int ret;
|
||||
#define BUFF_SIZE 2048
|
||||
char buffer[BUFF_SIZE];
|
||||
#define SIZE_OF_CLIENT_HELLO 815
|
||||
char msg[SIZE_OF_CLIENT_HELLO] = "\026\003\003\003\052\001\000\003\046 Fake Client Hello";
|
||||
|
||||
WOLF_SOCKET_T *sock = wolf_TCPsocket();
|
||||
SOCKET_T sock;
|
||||
struct sockaddr_in servAddr;
|
||||
|
||||
sock = socket();
|
||||
if (!sock)
|
||||
{
|
||||
printf("ERROR:wolf_TCPsocke()\n");
|
||||
return;
|
||||
}
|
||||
if (wolf_TCPconnect(sock, TEST_TCP_SERVER_IP, TCP_PORT) != WOLF_SUCCESS) {
|
||||
|
||||
memset(&servAddr, 0, sizeof(servAddr));
|
||||
servAddr.sin_family = AF_INET; /* using IPv4 */
|
||||
servAddr.sin_port = htons(TCP_PORT); /* on DEFAULT_PORT */
|
||||
|
||||
if (inet_pton(AF_INET, TEST_TCP_SERVER_IP, &servAddr.sin_addr) != 1) {
|
||||
fprintf(stderr, "ERROR: invalid address\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (connect(sock,(struct sockaddr*) &servAddr, sizeof(servAddr)) != WOLF_SUCCESS) {
|
||||
printf("ERROR:wolf_TCPconnect()\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf("Writing to server: %s\n", msg);
|
||||
err = wolf_TCPwrite(sock, msg, sizeof(msg));
|
||||
if (err < 0) {
|
||||
DEBUG_printf("Failed to write data. err=%d\n", err);
|
||||
ret = send(sock, msg, sizeof(msg));
|
||||
if (ret < 0) {
|
||||
DEBUG_printf("Failed to write data. err=%d\n", ret);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
err = wolf_TCPread(sock, buffer, BUFF_SIZE);
|
||||
if (err < 0) {
|
||||
DEBUG_printf("Failed to read data. err=%d\n", err);
|
||||
ret = recv(sock, buffer, BUFF_SIZE);
|
||||
if (ret < 0) {
|
||||
DEBUG_printf("Failed to read data. err=%d\n", ret);
|
||||
goto exit;
|
||||
}
|
||||
printf("Message: %s\n", buffer);
|
||||
|
||||
exit:
|
||||
free(sock);
|
||||
close(sock);
|
||||
}
|
||||
|
||||
void main(void)
|
||||
|
|
|
@ -45,19 +45,18 @@ int wolf_cb_TCPwrite(WOLFSSL *ssl, const unsigned char *buff, long unsigned int
|
|||
{
|
||||
(void)ssl;
|
||||
unsigned long ret;
|
||||
WOLF_SOCKET_T *sock = (WOLF_SOCKET_T *)ctx;
|
||||
ret = wolf_TCPwrite(sock, buff, len);
|
||||
|
||||
SOCKET_T sock = (SOCKET_T)ctx;
|
||||
ret = send(sock, buff, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int wolf_cb_TCPread(WOLFSSL *ssl, unsigned char *buff, long unsigned int len, void *ctx)
|
||||
{
|
||||
(void)ssl;
|
||||
WOLF_SOCKET_T *sock = (WOLF_SOCKET_T *)ctx;
|
||||
SOCKET_T sock = (SOCKET_T)ctx;
|
||||
int ret;
|
||||
|
||||
ret = wolf_TCPread(sock, buff, len);
|
||||
ret = recv(sock, buff, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -69,7 +68,9 @@ void tlsClient_test(void)
|
|||
static char buffer[BUFF_SIZE];
|
||||
char msg[] = "Hello Server";
|
||||
|
||||
WOLF_SOCKET_T *sock = NULL;
|
||||
SOCKET_T sock;
|
||||
struct sockaddr_in servAddr;
|
||||
|
||||
WOLFSSL_CTX *ctx = NULL;
|
||||
WOLFSSL *ssl = NULL;
|
||||
|
||||
|
@ -91,16 +92,28 @@ void tlsClient_test(void)
|
|||
wolfSSL_SetIORecv(ctx, (CallbackIORecv)wolf_cb_TCPread);
|
||||
wolfSSL_SetIOSend(ctx, (CallbackIOSend)wolf_cb_TCPwrite);
|
||||
|
||||
if ((sock = wolf_TCPsocket()) == NULL) {
|
||||
sock = socket();
|
||||
if (!sock)
|
||||
{
|
||||
printf("ERROR:wolf_TCPsocke()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((ret = wolf_TCPconnect(sock, TEST_TCP_SERVER_IP, TCP_PORT) != WOLF_SUCCESS)) {
|
||||
printf("ERROR:wolf_TCPconnect\n");
|
||||
memset(&servAddr, 0, sizeof(servAddr));
|
||||
servAddr.sin_family = AF_INET; /* using IPv4 */
|
||||
servAddr.sin_port = htons(TCP_PORT); /* on DEFAULT_PORT */
|
||||
|
||||
if (inet_pton(AF_INET, TEST_TCP_SERVER_IP, &servAddr.sin_addr) != 1) {
|
||||
fprintf(stderr, "ERROR: invalid address\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (connect(sock,(struct sockaddr*) &servAddr, sizeof(servAddr)) != WOLF_SUCCESS) {
|
||||
printf("ERROR:wolf_TCPconnect()\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
||||
if ((ssl = wolfSSL_new(ctx)) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
|
||||
ret = -1;
|
||||
|
@ -110,7 +123,8 @@ void tlsClient_test(void)
|
|||
wolfSSL_SetIOReadCtx(ssl, sock);
|
||||
wolfSSL_SetIOWriteCtx(ssl, sock);
|
||||
|
||||
if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) {
|
||||
printf("TLS Connecting\n");
|
||||
if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) {
|
||||
fprintf(stderr, "ERROR: failed to connect to wolfSSL(%d)\n",
|
||||
wolfSSL_get_error(ssl, ret));
|
||||
goto exit;
|
||||
|
|
Loading…
Reference in New Issue