LwIP, native TCP socket, ver 2
This commit is contained in:
parent
52e661df05
commit
23bc584caf
@ -1,4 +1,4 @@
|
||||
/* HTTPS-NB.c
|
||||
/* https-nb.c
|
||||
*
|
||||
* Copyright (C) 2006-2014 wolfSSL Inc.
|
||||
*
|
||||
@ -22,7 +22,7 @@
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include <cyassl/ctaocrypt/settings.h>
|
||||
|
||||
#if defined(HAVE_LWIP_NATIVE)
|
||||
@ -59,61 +59,22 @@ static unsigned long getPort(void) {
|
||||
return (localPort++ + 0x200) & 0x7fff ;
|
||||
}
|
||||
|
||||
static err_t DataConnectedCallback (void *arg, struct tcp_pcb *pcb, s8_t err)
|
||||
static err_t TcpConnectedCallback (void *arg, struct tcp_pcb *pcb, s8_t err)
|
||||
{
|
||||
DBG_PRINTF("DataConnectedCallback(arg=%x, pcb=%x, err=%x)\n", arg, pcb, err) ;
|
||||
DBG_PRINTF("TcpConnectedCallback(arg=%x, pcb=%x, err=%x)\n", arg, pcb, err) ;
|
||||
*(enum HTTPS_Stat *)arg = TCP_CONNECTED ;
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static err_t DataSentCallback (void *arg, struct tcp_pcb *pcb, u16_t err)
|
||||
{
|
||||
DBG_PRINTF("LwIPtest: Data Sent(SentCallBack1)\n") ;
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static err_t DataReceiveCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
||||
{
|
||||
struct pbuf *next ;
|
||||
CYASSL *ssl ;
|
||||
ssl = (CYASSL *)arg ;
|
||||
|
||||
DBG_PRINTF("LwIPtest: Data Received(DataReceiveCallback), pbuf->len=%d, err=%d\n", p->tot_len , err) ;
|
||||
|
||||
if(p==0) { /* throw away */
|
||||
DBG_PRINTF("DataReceiveCallback, pbuf->len=%d, err=%d\n", p->tot_len , err) ;
|
||||
if(*(enum HTTPS_Stat *)(arg) == WAITING) {
|
||||
*(enum HTTPS_Stat *)(arg) = HTTP_RECEIVE ;
|
||||
return ERR_OK ;
|
||||
}
|
||||
if(*(enum HTTPS_Stat *)(ssl->lwipCtx.arg) == WAITING) {
|
||||
*(enum HTTPS_Stat *)(ssl->lwipCtx.arg) = HTTP_RECEIVE ;
|
||||
} else {
|
||||
CyaSSL_PbufFree(p) ;
|
||||
tcp_recved(pcb,p->tot_len) ;
|
||||
return ERR_OK ;
|
||||
}
|
||||
/* put it into the queue */
|
||||
if(ssl->lwipCtx.pbuf) {
|
||||
next = ssl->lwipCtx.pbuf ;
|
||||
while(1) {
|
||||
DBG_PRINTF("pbuf=%x, pbuf->next=%x, ",ssl->lwipCtx.pbuf, next) ;
|
||||
if(next->next)
|
||||
next = next->next ;
|
||||
else break ;
|
||||
}
|
||||
next->next = p ;
|
||||
ssl->lwipCtx.pbuf->tot_len += p->tot_len ;
|
||||
} else {
|
||||
ssl->lwipCtx.pbuf = p ;
|
||||
}
|
||||
ssl->lwipCtx.pulled = 0 ;
|
||||
|
||||
if(ssl->lwipCtx.wait < 0)
|
||||
ssl->lwipCtx.wait = 1000 ;
|
||||
ssl->lwipCtx.pulled = 0 ;
|
||||
return ERR_OK;
|
||||
} else return !ERR_OK ;
|
||||
}
|
||||
|
||||
|
||||
static int count = 0 ;
|
||||
|
||||
void CyaSSL_HTTPS_Client_NB_init(void *nb,
|
||||
@ -140,14 +101,15 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
|
||||
|
||||
switch(https_nb->stat) {
|
||||
case BEGIN:
|
||||
printf("======= LwIP: HTTPS Client Test(%x): %d =========\n", nb, count ++) ;
|
||||
printf("======= LwIP: HTTPS Client Test(%x): %d ====\n", nb, count ++) ;
|
||||
/*** Assuming LwIP has been initialized ***/
|
||||
https_nb->stat = INITIALIZED ;
|
||||
case INITIALIZED:
|
||||
https_nb->pcb = tcp_new();
|
||||
if(https_nb->pcb) {
|
||||
tcp_arg(https_nb->pcb, (void *)&(https_nb->stat)) ;
|
||||
DBG_PRINTF("LwIPtest: New PCB(tcp_new=%x), &https->stat=%x\n", https_nb->pcb, &https_nb->stat) ;
|
||||
DBG_PRINTF("New PCB(tcp_new=%x), &https->stat=%x\n",
|
||||
https_nb->pcb, &https_nb->stat) ;
|
||||
} else {
|
||||
ERR_PRINTF("tcp_new, ret=%d\n", https_nb->pcb) ;
|
||||
https_nb->stat = IDLE ;
|
||||
@ -179,7 +141,7 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
|
||||
(*(unsigned long *)&https_nb->serverIP_em>>16)&0xff,
|
||||
(*(unsigned long *)&https_nb->serverIP_em>>24)&0xff) ;
|
||||
ret = tcp_connect(https_nb->pcb, &(https_nb->serverIP_em),
|
||||
https_nb->serverPort, DataConnectedCallback);
|
||||
https_nb->serverPort, TcpConnectedCallback);
|
||||
|
||||
if(ret == ERR_OK) {
|
||||
https_nb->stat = WAITING ;
|
||||
@ -212,35 +174,35 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
|
||||
return !ERR_OK ;
|
||||
}
|
||||
|
||||
CyaSSL_SetIO_LwIP(https_nb->ssl, https_nb->pcb);
|
||||
CyaSSL_SetVersion(https_nb->ssl, CYASSL_TLSV1_2) ;
|
||||
https_nb->stat = SSL_CONN ;
|
||||
CyaSSL_SetIO_LwIP(https_nb->ssl, https_nb->pcb,
|
||||
DataReceiveCallback, NULL, (void *)&https_nb->stat);
|
||||
|
||||
https_nb->stat = SSL_CONN ;
|
||||
|
||||
case SSL_CONN: /* handshaking */
|
||||
|
||||
if(LwIP_cb_mutex) return ERR_OK ;
|
||||
ret = CyaSSL_connect(https_nb->ssl);
|
||||
DBG_PRINTF("LwIPtest: SSL Connecting(CyaSSL_connect), ret = %d\n", ret) ;
|
||||
DBG_PRINTF("CyaSSL_connect, ret = %d\n", ret) ;
|
||||
if(ret == SSL_SUCCESS) {
|
||||
https_nb->stat = SSL_CONN_WAITING ;
|
||||
DBG_PRINTF("LwIPtest: SSL Connected\n") ;
|
||||
DBG_PRINTF("SSL Connected\n") ;
|
||||
https_nb->stat = HTTP_SEND ;
|
||||
} else {
|
||||
ret = CyaSSL_get_error(https_nb->ssl, NULL) ;
|
||||
if(ret == SSL_ERROR_WANT_READ) {
|
||||
https_nb->ssl->lwipCtx.wait = -1 ;
|
||||
https_nb->stat = SSL_CONN_WAITING ;
|
||||
https_nb->ssl->lwipCtx.wait = -1 ;
|
||||
https_nb->stat = SSL_CONN_WAITING ;
|
||||
return ERR_OK ;
|
||||
} else {
|
||||
ERR_PRINTF("CyaSSL_connecting_NB:ssl=%x, ret=%d\n", https_nb->ssl, ret) ;
|
||||
return !ERR_OK ;
|
||||
}
|
||||
}
|
||||
return ERR_OK ;
|
||||
|
||||
return ERR_OK ;
|
||||
|
||||
case SSL_CONN_WAITING:
|
||||
if(https_nb->ssl->lwipCtx.wait-- == 0) { /* counting down after the callback
|
||||
for multiple callbacks */
|
||||
|
||||
if(https_nb->ssl->lwipCtx.wait-- == 0) {
|
||||
/* counting down after the callback for multiple callbacks */
|
||||
https_nb->stat = SSL_CONN ;
|
||||
LwIP_cb_mutex = 0 ;
|
||||
}
|
||||
@ -253,23 +215,18 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
|
||||
int size ;
|
||||
if(LwIP_cb_mutex)return ERR_OK ;
|
||||
else LwIP_cb_mutex = 1 ; /* lock */
|
||||
printf("LwIPtest: SSL CONNECTED(%x)\n", https_nb) ;
|
||||
CyaSSL_NB_setCallbackArg(https_nb->ssl, &(https_nb->stat)) ;
|
||||
tcp_sent(https_nb->pcb, DataSentCallback);
|
||||
tcp_recv(https_nb->pcb, DataReceiveCallback);
|
||||
|
||||
DBG_PRINTF("LwIPtest: HTTPS GET(%x)\n", https_nb) ;
|
||||
printf("SSL CONNECTED(%x)\n", https_nb) ;
|
||||
sprintf(sendBuff,
|
||||
"GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n",
|
||||
https_nb->path, https_nb->hostname) ;
|
||||
size = strlen((char const *)sendBuff) ;
|
||||
|
||||
|
||||
CyaSSL_write(https_nb->ssl, sendBuff, size) ;
|
||||
|
||||
https_nb->stat = WAITING ;
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
case HTTP_RECEIVE:
|
||||
{
|
||||
#define HTTP_BUFF_SIZE 2048
|
||||
@ -278,9 +235,9 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
|
||||
LwIP_cb_mutex = 0 ;
|
||||
memset(httpbuff, '\0', HTTP_BUFF_SIZE) ;
|
||||
ret = CyaSSL_read(https_nb->ssl, httpbuff, HTTP_BUFF_SIZE) ;
|
||||
printf("LwIPtest: HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ;
|
||||
/* puts(httpbuff) ;*/
|
||||
puts("===================\n") ;
|
||||
printf("HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ;
|
||||
/* puts(httpbuff) ; */
|
||||
/* puts("===================\n") ; */
|
||||
}
|
||||
case SSL_CLOSE:
|
||||
{
|
||||
@ -332,12 +289,12 @@ void *CyaSSL_HTTPS_ClientP_5 = (void *)&CyaSSL_HTTPS_Client_5 ;
|
||||
|
||||
#define HTTPS_PORT 443
|
||||
#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 server_em = { IP_ADDR(192,168,11,9) } ;
|
||||
|
||||
void HTTPSClient_main_init() {
|
||||
|
||||
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_1,
|
||||
server_em, HTTPS_PORT, "xxx.com", "/") ;
|
||||
server_em, HTTPS_PORT, "xxx.com", "/") ;
|
||||
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_2,
|
||||
server_em, HTTPS_PORT, "xxx.com", "/") ;
|
||||
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_3,
|
||||
|
@ -964,9 +964,9 @@ int SetCipherList(Suites*, const char* list);
|
||||
#ifdef HAVE_LWIP_NATIVE
|
||||
CYASSL_LOCAL int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb);
|
||||
CYASSL_LOCAL int CyaSSL_LwIP_Receive(CYASSL* ssl, char *buf, int sz, void *cb);
|
||||
CYASSL_LOCAL void CyaSSL_NB_setCallbackArg(CYASSL *ssl, void *arg) ;
|
||||
CYASSL_LOCAL void CyaSSL_PbufFree(void *p);
|
||||
#endif /* HAVE_{tcp stack} */
|
||||
CYASSL_API int CyaSSL_SetIO_LwIP(CYASSL* ssl, void *pcb,
|
||||
tcp_recv_fn recv, tcp_sent_fn sent, void *arg);
|
||||
#endif /* HAVE_LWIP_NATIVE */
|
||||
|
||||
/* CyaSSL Cipher type just points back to SSL */
|
||||
struct CYASSL_CIPHER {
|
||||
@ -1806,6 +1806,8 @@ typedef struct DtlsMsg {
|
||||
/* LwIP native tpc socket context */
|
||||
typedef struct LwIP_native_Ctx {
|
||||
struct tcp_pcb * pcb ;
|
||||
tcp_recv_fn recv ;
|
||||
tcp_sent_fn sent ;
|
||||
int pulled ;
|
||||
struct pbuf *pbuf ;
|
||||
int wait ;
|
||||
|
@ -940,7 +940,7 @@ CYASSL_API void CyaSSL_SetIOWriteFlags(CYASSL* ssl, int flags);
|
||||
ULONG waitoption);
|
||||
#endif
|
||||
#ifdef HAVE_LWIP_NATIVE
|
||||
CYASSL_API int CyaSSL_SetIO_LwIP(CYASSL* ssl, void *pcb);
|
||||
#include "lwip/tcp.h"
|
||||
#endif
|
||||
typedef int (*CallbackGenCookie)(CYASSL* ssl, unsigned char* buf, int sz,
|
||||
void* ctx);
|
||||
|
47
src/io.c
47
src/io.c
@ -1059,7 +1059,7 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
|
||||
#else
|
||||
/*Disable debug*/
|
||||
#define DBG_PRINTF(x, ...)
|
||||
#define ERR_PRINTF(x, ...) err_sys(x)
|
||||
#define ERR_PRINTF(x, ...)
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
@ -1070,7 +1070,7 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
|
||||
#define DBG_PRINTF_CB(x, ...)
|
||||
#endif
|
||||
|
||||
void CyaSSL_PbufFree(void *vp)
|
||||
static void CyaSSL_PbufFree(void *vp)
|
||||
{
|
||||
struct pbuf *p ;
|
||||
struct pbuf * next;
|
||||
@ -1134,18 +1134,18 @@ static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL *ssl, int size)
|
||||
return totalLen;
|
||||
}
|
||||
|
||||
err_t CyaSSL_connectCallback(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t err)
|
||||
err_t CyaSSL_LwIP_recv_cb(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t err)
|
||||
{
|
||||
struct pbuf *next ;
|
||||
CYASSL *ssl ;
|
||||
ssl = (CYASSL *)cb ;
|
||||
|
||||
if((cb == NULL)||(pcb == NULL))
|
||||
ERR_PRINTF("CyaSSL_connectCallBack, cb=%x, pcb=%d\n", cb, pcb) ;
|
||||
ERR_PRINTF("CyaSSL_LwIP_recv_cb, cb=%x, pcb=%d\n", cb, pcb) ;
|
||||
if(p && (err == 0)) {
|
||||
DBG_PRINTF_CB("CyaSSL_connectCallBack, pbuf=%x, err=%d, tot_len=%d\n", p, err, p->tot_len) ;
|
||||
DBG_PRINTF_CB("CyaSSL_LwIP_recv_cb, pbuf=%x, err=%d, tot_len=%d\n", p, err, p->tot_len) ;
|
||||
}else {
|
||||
ERR_PRINTF("CyaSSL_connectCallBack, pbuf=%x, err=%d\n", p, err) ;
|
||||
ERR_PRINTF("CyaSSL_LwIP_recv_cb, pbuf=%x, err=%d\n", p, err) ;
|
||||
return ERR_OK; /* don't go to SSL_CONN */
|
||||
}
|
||||
|
||||
@ -1162,14 +1162,23 @@ err_t CyaSSL_connectCallback(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t
|
||||
ssl->lwipCtx.pbuf = p ;
|
||||
}
|
||||
ssl->lwipCtx.pulled = 0 ;
|
||||
if(ssl->lwipCtx.wait < 0)
|
||||
ssl->lwipCtx.wait = 10000 ;
|
||||
return ERR_OK;
|
||||
|
||||
if(((ssl->options.connectState != CONNECT_BEGIN) &&
|
||||
(ssl->options.connectState != SECOND_REPLY_DONE))||
|
||||
((ssl->options.acceptState != ACCEPT_BEGIN) &&
|
||||
(ssl->options.connectState != ACCEPT_THIRD_REPLY_DONE)))
|
||||
{
|
||||
if(ssl->lwipCtx.wait < 0) /* wait for multiple callbacks */
|
||||
ssl->lwipCtx.wait = 10000 ;
|
||||
} else if(ssl->lwipCtx.recv)
|
||||
return ssl->lwipCtx.recv(ssl->lwipCtx.arg, pcb, p, err) ;
|
||||
/* user callback */
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t DataSentCallback (void *arg, struct tcp_pcb *pcb, u16_t err)
|
||||
err_t CyaSSL_LwIP_sent_cb(void *arg, struct tcp_pcb *pcb, u16_t err)
|
||||
{
|
||||
DBG_PRINTF_CB("LwIPtest: Data Sent(SentCallBack1), err=%d\n", err) ;
|
||||
DBG_PRINTF_CB("CaSSL_LwIP_write_cb, err=%d\n", err) ;
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
@ -1205,17 +1214,17 @@ int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb)
|
||||
}
|
||||
}
|
||||
|
||||
void CyaSSL_NB_setCallbackArg(CYASSL *ssl, void *arg)
|
||||
{
|
||||
ssl->lwipCtx.arg = arg ;
|
||||
}
|
||||
|
||||
int CyaSSL_SetIO_LwIP(CYASSL* ssl, void* pcb)
|
||||
int CyaSSL_SetIO_LwIP(CYASSL* ssl, void* pcb,
|
||||
tcp_recv_fn recv, tcp_sent_fn sent, void *arg)
|
||||
{
|
||||
if (ssl && pcb) {
|
||||
ssl->lwipCtx.pcb = (struct tcp_pcb *)pcb ;
|
||||
tcp_recv(pcb, CyaSSL_connectCallback);
|
||||
tcp_sent(pcb, DataSentCallback);
|
||||
ssl->lwipCtx.recv = recv ; /* recv user callback */
|
||||
ssl->lwipCtx.sent = sent ; /* sent user callback */
|
||||
ssl->lwipCtx.arg = arg ;
|
||||
/* CyaSSL_LwIP_recv/sent_cb invokes recv/sent user callback in them. */
|
||||
tcp_recv(pcb, CyaSSL_LwIP_recv_cb) ;
|
||||
tcp_sent(pcb, CyaSSL_LwIP_sent_cb) ;
|
||||
tcp_arg (pcb, (void *)ssl) ;
|
||||
} else return BAD_FUNC_ARG ;
|
||||
return ERR_OK ;
|
||||
|
Loading…
Reference in New Issue
Block a user