From 5d5a8dbabdd61dd6d7eb3e42b7b63155e2fa6e87 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Thu, 20 Feb 2014 15:38:35 +0900 Subject: [PATCH] client.c for LwIP native socket, v0.2 --- cyassl/test.h | 12 ++++++++++-- examples/client/client.c | 5 ++--- examples/client/client.h | 28 +++++++++++++++++----------- src/io.c | 17 ++++++++--------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/cyassl/test.h b/cyassl/test.h index c5e2799d0..15c1e526d 100644 --- a/cyassl/test.h +++ b/cyassl/test.h @@ -37,7 +37,7 @@ #else #include #include -#ifndef CYASSL_LEANPSK +#if !defined(CYASSL_LEANPSK) && !defined(HAVE_LWIP_NATIVE) #include #include #include @@ -59,6 +59,14 @@ #define SNPRINTF snprintf #endif /* USE_WINDOWS_API */ +#if defined(HAVE_LWIP_NATIVE) + #include + #include "lwip/tcp.h" + #include "lwip/sockets.h" + #include "lwip/inet.h" + #include "lwip/ip_addr.h" +#endif + #ifdef HAVE_CAVIUM #include "cavium_sysdep.h" #include "cavium_common.h" @@ -81,7 +89,7 @@ /* HPUX doesn't use socklent_t for third parameter to accept, unless _XOPEN_SOURCE_EXTENDED is defined */ -#if !defined(__hpux__) && !defined(CYASSL_MDK_ARM) +#if !defined(__hpux__) && !defined(CYASSL_MDK_ARM) && !defined(CYASSL_IAR_ARM) typedef socklen_t* ACCEPT_THIRD_T; #else #if defined _XOPEN_SOURCE_EXTENDED diff --git a/examples/client/client.c b/examples/client/client.c index 52763d143..852639313 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -658,15 +658,14 @@ CASE(CLIENT_SSL_CONN): /* see note at top of README */ int err = CyaSSL_get_error(ssl, 0); #if defined(HAVE_LWIP_NATIVE) - if(err == SSL_ERROR_WANT_READ) - BREAK ; + if(err == SSL_ERROR_WANT_READ) + BREAK ; #endif char buffer[CYASSL_MAX_ERROR_SZ]; printf("err = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer)); err_sys("SSL_connect failed"); /* if you're getting an error here */ - BREAK ; } #else timeout.tv_sec = 2; diff --git a/examples/client/client.h b/examples/client/client.h index a243b6601..b4acf4122 100644 --- a/examples/client/client.h +++ b/examples/client/client.h @@ -30,27 +30,33 @@ static enum Client_Stat { CLIENT_TCP_CONN , CLIENT_SSL_CONN , CLIENT_SSL_READ , - } client_stat ; +} client_stat ; + +int tcp_connect_nb(struct tcp_pcb **pcb, const char* ip, word16 port, int udp); +void tcp_CloseSocket_nb(struct tcp_pcb *pcb) ; +#define tcp_connect(s, h, p, f) tcp_connect_nb(s, h, p, f) +#define CloseSocket(s) tcp_CloseSocket_nb(s) +#define CyaSSL_set_fd(ssl, s) - int tcp_connect_nb(struct tcp_pcb **pcb, const char* ip, word16 port, int udp); - void tcp_CloseSocket_nb(struct tcp_pcb *pcb) ; - #define tcp_connect(s, h, p, f) tcp_connect_nb(s, h, p, f) - #define CloseSocket(s) tcp_CloseSocket_nb(s) - #define CyaSSL_set_fd(ssl, s) #endif + #if defined(HAVE_LWIP_NATIVE) -#define SWITCH_STAT switch(client_stat) { case CLIENT_BEGIN: + +#define SWITCH_STAT switch(client_stat) { case CLIENT_BEGIN: #define CASE(stat) client_stat = stat ; case stat -#define BREAK break -#define END_SWITCH } -#define STATIC_NB static -#else +#define BREAK break +#define END_SWITCH } +#define STATIC_NB static + +#else + #define SWITCH_STAT #define CASE(value) #define BREAK #define END_SWITHCH #define STATIC_NB + #endif diff --git a/src/io.c b/src/io.c index 1d90de774..3af0e6339 100644 --- a/src/io.c +++ b/src/io.c @@ -1096,6 +1096,7 @@ static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL *ssl, int size) int skipLen = 0 ; p = ssl->lwipCtx.pbuf ; + #if defined(DEBUG_PBUF) printf("WantRead Size=%d\n", size) ; do { @@ -1176,14 +1177,8 @@ err_t CyaSSL_LwIP_recv_cb(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t er ssl->lwipCtx.pbuf = p ; } ssl->lwipCtx.pulled = 0 ; - - if(((ssl->options.connectState != CONNECT_BEGIN) && - (ssl->options.connectState != SECOND_REPLY_DONE))|| - ((ssl->options.acceptState != ACCEPT_BEGIN) && - (ssl->options.connectState != ACCEPT_THIRD_REPLY_DONE))) - { - ssl->lwipCtx.wait = 100000 ; - } else if(ssl->lwipCtx.recv) + ssl->lwipCtx.wait = 10000 ; + if(ssl->lwipCtx.recv) return ssl->lwipCtx.recv(ssl->lwipCtx.arg, pcb, p, err) ; /* user callback */ return ERR_OK; @@ -1206,6 +1201,10 @@ int CyaSSL_LwIP_Receive(CYASSL* ssl, char *buf, int sz, void *cb) DBG_PRINTF_CB("CyaSSL_LwIP_Receive: ssl_nb = %x\n", ssl) ; if(ssl->lwipCtx.pbuf) { + if(ssl->lwipCtx.wait){ + ssl->lwipCtx.wait-- ; + return CYASSL_CBIO_ERR_WANT_READ ; + } DBG_PRINTF_CB("Received Len=%d, Want Len= %d\n", ssl->lwipCtx.pbuf->tot_len, sz) ; ret = CyaSSL_GetDataFromPbuf(buf, ssl, sz) ; if(ret == 0) @@ -1221,7 +1220,7 @@ int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb) { err_t ret ; - DBG_PRINTF_CB("CyaSSL_LwIP_Send: ssl = %x\n", ssl) ; + DBG_PRINTF_CB("CyaSSL_LwIP_Send: ssl = %x, pcb = %x\n", ssl, ssl->lwipCtx.pcb) ; DBG_PRINTF_CB("Send buf[0,1,2,3]=%x,%x,%x,%x, sz=%d\n", buf[0], buf[1], buf[2], buf[3], sz) ; ret = tcp_write(ssl->lwipCtx.pcb, buf, sz, TCP_WRITE_FLAG_COPY) ; if(ret == ERR_OK)