Multiple callbacks, fixed initialize ssl->lwipCtx, io.c

This commit is contained in:
Takashi Kojo
2014-02-17 17:40:42 +09:00
parent 78b897a07c
commit 2e69313eb3
2 changed files with 64 additions and 21 deletions

View File

@@ -41,6 +41,15 @@
#include <cyassl/ctaocrypt/memory.h> #include <cyassl/ctaocrypt/memory.h>
#include "https-nb.h" #include "https-nb.h"
#if 0
/*Enable debug*/
#include <cstdio>
#define DBG0_PRINTF(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG0_PRINTF(x, ...)
#endif
#if 0 #if 0
/*Enable debug*/ /*Enable debug*/
#include <cstdio> #include <cstdio>
@@ -89,7 +98,7 @@ void CyaSSL_HTTPS_Client_NB_init(void *nb,
https_nb->path = path ; https_nb->path = path ;
https_nb->stat = BEGIN ; https_nb->stat = BEGIN ;
} }
int CyaSSL_HTTPS_Client_NB(void *nb) int CyaSSL_HTTPS_Client_NB(void *nb)
{ {
int ret ; int ret ;
@@ -101,7 +110,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
switch(https_nb->stat) { switch(https_nb->stat) {
case BEGIN: case BEGIN:
printf("======= LwIP: HTTPS Client Test(%x): %d ====\n", nb, count ++) ; printf("== HTTPS Client(%x): %d ==\n", nb, count ++) ;
/*** Assuming LwIP has been initialized ***/ /*** Assuming LwIP has been initialized ***/
https_nb->stat = INITIALIZED ; https_nb->stat = INITIALIZED ;
case INITIALIZED: case INITIALIZED:
@@ -134,8 +143,8 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
case TCP_CONNECT: case TCP_CONNECT:
if(LwIP_cb_mutex)return ERR_OK ; if(LwIP_cb_mutex)return ERR_OK ;
else LwIP_cb_mutex = 1 ; else LwIP_cb_mutex = 1 ;
DBG_PRINTF("LwIPtest: TCP_CONNECT(%x)\n", https_nb) ; DBG_PRINTF("TCP_CONNECT(%x)\n", https_nb) ;
DBG_PRINTF("LwIPtest: Server IP Addrress(%d.%d.%d.%d)\n", DBG_PRINTF("Server IP Addrress(%d.%d.%d.%d)\n",
(*(unsigned long *)&https_nb->serverIP_em&0xff), (*(unsigned long *)&https_nb->serverIP_em&0xff),
(*(unsigned long *)&https_nb->serverIP_em>>8)&0xff, (*(unsigned long *)&https_nb->serverIP_em>>8)&0xff,
(*(unsigned long *)&https_nb->serverIP_em>>16)&0xff, (*(unsigned long *)&https_nb->serverIP_em>>16)&0xff,
@@ -144,6 +153,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
https_nb->serverPort, TcpConnectedCallback); https_nb->serverPort, TcpConnectedCallback);
if(ret == ERR_OK) { if(ret == ERR_OK) {
https_nb->wait_cnt = 0 ;
https_nb->stat = WAITING ; https_nb->stat = WAITING ;
return ERR_OK; return ERR_OK;
} else { } else {
@@ -153,7 +163,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
} }
case TCP_CONNECTED: case TCP_CONNECTED:
printf("LwIPtest: TCP CONNECTED(%x)\n", https_nb) ; DBG0_PRINTF("TCP CONNECTED(%x)\n", https_nb) ;
LwIP_cb_mutex = 0 ; LwIP_cb_mutex = 0 ;
/*CyaSSLv3_client_method() /*CyaSSLv3_client_method()
@@ -189,7 +199,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
} else { } else {
ret = CyaSSL_get_error(https_nb->ssl, NULL) ; ret = CyaSSL_get_error(https_nb->ssl, NULL) ;
if(ret == SSL_ERROR_WANT_READ) { if(ret == SSL_ERROR_WANT_READ) {
https_nb->ssl->lwipCtx.wait = -1 ; https_nb->ssl->lwipCtx.wait = 1000000 ;
https_nb->stat = SSL_CONN_WAITING ; https_nb->stat = SSL_CONN_WAITING ;
return ERR_OK ; return ERR_OK ;
} else { } else {
@@ -201,7 +211,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
case SSL_CONN_WAITING: case SSL_CONN_WAITING:
if(https_nb->ssl->lwipCtx.wait-- == 0) { if(https_nb->ssl->lwipCtx.wait-- <= 0) {
/* counting down after the callback for multiple callbacks */ /* counting down after the callback for multiple callbacks */
https_nb->stat = SSL_CONN ; https_nb->stat = SSL_CONN ;
LwIP_cb_mutex = 0 ; LwIP_cb_mutex = 0 ;
@@ -215,7 +225,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
int size ; int size ;
if(LwIP_cb_mutex)return ERR_OK ; if(LwIP_cb_mutex)return ERR_OK ;
else LwIP_cb_mutex = 1 ; /* lock */ else LwIP_cb_mutex = 1 ; /* lock */
printf("SSL CONNECTED(%x)\n", https_nb) ; DBG0_PRINTF("SSL CONNECTED(%x)\n", https_nb) ;
sprintf(sendBuff, sprintf(sendBuff,
"GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n",
https_nb->path, https_nb->hostname) ; https_nb->path, https_nb->hostname) ;
@@ -223,6 +233,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
CyaSSL_write(https_nb->ssl, sendBuff, size) ; CyaSSL_write(https_nb->ssl, sendBuff, size) ;
https_nb->wait_cnt = 0 ;
https_nb->stat = WAITING ; https_nb->stat = WAITING ;
return ERR_OK; return ERR_OK;
} }
@@ -235,9 +246,8 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
LwIP_cb_mutex = 0 ; LwIP_cb_mutex = 0 ;
memset(httpbuff, '\0', HTTP_BUFF_SIZE) ; memset(httpbuff, '\0', HTTP_BUFF_SIZE) ;
ret = CyaSSL_read(https_nb->ssl, httpbuff, HTTP_BUFF_SIZE) ; ret = CyaSSL_read(https_nb->ssl, httpbuff, HTTP_BUFF_SIZE) ;
printf("HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ; DBG0_PRINTF("HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ;
/* puts(httpbuff) ; */ /*DBG0_PRINTF*/puts(httpbuff) ;
/* puts("===================\n") ; */
} }
case SSL_CLOSE: case SSL_CLOSE:
{ {
@@ -260,7 +270,20 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
https_nb->idle ++ ; https_nb->idle ++ ;
if(https_nb->idle > 50000) if(https_nb->idle > 50000)
https_nb->stat = BEGIN ; https_nb->stat = BEGIN ;
return ERR_OK;
case WAITING: case WAITING:
if(https_nb->wait_cnt++ > 1000000) {
LwIP_cb_mutex = 0 ;
https_nb->wait_cnt = 0 ;
if((https_nb->stat >= SSL_CONN)&&(https_nb->stat < SSL_CLOSE)) {
ERR_PRINTF("Wait Time out, go to CyaSSL close") ;
https_nb->stat = SSL_CLOSE ;
} else {
ERR_PRINTF("Wait Time out, go to Begin") ;
https_nb->stat = TCP_CLOSE ;
}
}
return ERR_OK ;
default: default:
return ERR_OK; return ERR_OK;
} }
@@ -289,20 +312,23 @@ void *CyaSSL_HTTPS_ClientP_5 = (void *)&CyaSSL_HTTPS_Client_5 ;
#define HTTPS_PORT 443 #define HTTPS_PORT 443
#define IP_ADDR(a,b,c,d) (((a)|((b)<<8)|((c)<<16)|(d)<<24)) #define IP_ADDR(a,b,c,d) (((a)|((b)<<8)|((c)<<16)|(d)<<24))
static struct ip_addr server_em = { IP_ADDR(192,168,11,9) } ; static struct ip_addr server0_em = { IP_ADDR(192,168,11,9) } ;
static struct ip_addr server1_em = { IP_ADDR(31,13,68,33)} ;
void HTTPSClient_main_init() { void HTTPSClient_main_init() {
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_1, CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_1,
server_em, HTTPS_PORT, "xxx.com", "/") ; //server_em, HTTPS_PORT, "xxx.com", "/") ;
server1_em, HTTPS_PORT, "graph.facebook.com", "/takashikojo") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_2, CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_2,
server_em, HTTPS_PORT, "xxx.com", "/") ; server0_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_3, CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_3,
server_em, HTTPS_PORT, "xxx.com", "/") ; server1_em, HTTPS_PORT, "graph.facebook.com", "/takashikojo") ;
//server_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_4, CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_4,
server_em, HTTPS_PORT, "xxx.com", "/") ; server0_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_5, CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_5,
server_em, HTTPS_PORT, "xxx.com", "/") ; server0_em, HTTPS_PORT, "xxx.com", "/") ;
} }
void HTTPSClient_main(int i) void HTTPSClient_main(int i)
@@ -314,7 +340,7 @@ void HTTPSClient_main(int i)
if((i % 2) == 0) { /* wait for initializing TCP/IP, DHCP */ if((i % 2) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_2) ; CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_2) ;
} }
#if 0
if((i % 3) == 0) { /* wait for initializing TCP/IP, DHCP */ if((i % 3) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_3) ; CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_3) ;
} }
@@ -327,6 +353,7 @@ void HTTPSClient_main(int i)
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_5) ; CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_5) ;
} }
#endif
} }
#endif /* NO_MAIN_DRIVER */ #endif /* NO_MAIN_DRIVER */

View File

@@ -1070,6 +1070,10 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
#define DBG_PRINTF_CB(x, ...) #define DBG_PRINTF_CB(x, ...)
#endif #endif
#ifdef CYASSL_IAR_ARM
#include "intrinsics.h"
#endif
static void CyaSSL_PbufFree(void *vp) static void CyaSSL_PbufFree(void *vp)
{ {
struct pbuf *p ; struct pbuf *p ;
@@ -1078,7 +1082,7 @@ static void CyaSSL_PbufFree(void *vp)
while(p->next != NULL) while(p->next != NULL)
{ {
next = p->next; next = p->next;
pbuf_free(p); pbuf_free(p);
p = next; p = next;
} }
pbuf_free(p); pbuf_free(p);
@@ -1092,6 +1096,16 @@ static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL *ssl, int size)
int skipLen = 0 ; int skipLen = 0 ;
p = ssl->lwipCtx.pbuf ; p = ssl->lwipCtx.pbuf ;
#if defined(DEBUG_PBUF)
printf("WantRead Size=%d\n", size) ;
do {
printf("p=%x, p->len=%d, p->tot_len=%d\n", p, p->len, p->tot_len) ;
if(p != p->next)
p = p->next ;
else break ;
} while(p) ;
p = ssl->lwipCtx.pbuf ;
#endif
if(p->tot_len < (ssl->lwipCtx.pulled + size)) if(p->tot_len < (ssl->lwipCtx.pulled + size))
return 0 ; return 0 ;
@@ -1168,8 +1182,7 @@ err_t CyaSSL_LwIP_recv_cb(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t er
((ssl->options.acceptState != ACCEPT_BEGIN) && ((ssl->options.acceptState != ACCEPT_BEGIN) &&
(ssl->options.connectState != ACCEPT_THIRD_REPLY_DONE))) (ssl->options.connectState != ACCEPT_THIRD_REPLY_DONE)))
{ {
if(ssl->lwipCtx.wait < 0) /* wait for multiple callbacks */ ssl->lwipCtx.wait = 100000 ;
ssl->lwipCtx.wait = 10000 ;
} else if(ssl->lwipCtx.recv) } else if(ssl->lwipCtx.recv)
return ssl->lwipCtx.recv(ssl->lwipCtx.arg, pcb, p, err) ; return ssl->lwipCtx.recv(ssl->lwipCtx.arg, pcb, p, err) ;
/* user callback */ /* user callback */
@@ -1227,6 +1240,9 @@ int CyaSSL_SetIO_LwIP(CYASSL* ssl, void* pcb,
ssl->lwipCtx.recv = recv ; /* recv user callback */ ssl->lwipCtx.recv = recv ; /* recv user callback */
ssl->lwipCtx.sent = sent ; /* sent user callback */ ssl->lwipCtx.sent = sent ; /* sent user callback */
ssl->lwipCtx.arg = arg ; ssl->lwipCtx.arg = arg ;
ssl->lwipCtx.pbuf = 0 ;
ssl->lwipCtx.pulled = 0 ;
ssl->lwipCtx.wait = 0 ;
/* CyaSSL_LwIP_recv/sent_cb invokes recv/sent user callback in them. */ /* CyaSSL_LwIP_recv/sent_cb invokes recv/sent user callback in them. */
tcp_recv(pcb, CyaSSL_LwIP_recv_cb) ; tcp_recv(pcb, CyaSSL_LwIP_recv_cb) ;
tcp_sent(pcb, CyaSSL_LwIP_sent_cb) ; tcp_sent(pcb, CyaSSL_LwIP_sent_cb) ;