mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 12:14:38 +02:00
LwIP, native tcp socket, v0.2
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
/* HTTPS-NB.c
|
/* https-nb.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2006-2014 wolfSSL Inc.
|
* Copyright (C) 2006-2014 wolfSSL Inc.
|
||||||
*
|
*
|
||||||
@@ -59,60 +59,21 @@ static unsigned long getPort(void) {
|
|||||||
return (localPort++ + 0x200) & 0x7fff ;
|
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 ;
|
*(enum HTTPS_Stat *)arg = TCP_CONNECTED ;
|
||||||
return ERR_OK;
|
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)
|
static err_t DataReceiveCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
||||||
{
|
{
|
||||||
struct pbuf *next ;
|
DBG_PRINTF("DataReceiveCallback, pbuf->len=%d, err=%d\n", p->tot_len , err) ;
|
||||||
CYASSL *ssl ;
|
if(*(enum HTTPS_Stat *)(arg) == WAITING) {
|
||||||
ssl = (CYASSL *)arg ;
|
*(enum HTTPS_Stat *)(arg) = HTTP_RECEIVE ;
|
||||||
|
|
||||||
DBG_PRINTF("LwIPtest: Data Received(DataReceiveCallback), pbuf->len=%d, err=%d\n", p->tot_len , err) ;
|
|
||||||
|
|
||||||
if(p==0) { /* throw away */
|
|
||||||
return ERR_OK ;
|
return ERR_OK ;
|
||||||
|
} else 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int count = 0 ;
|
static int count = 0 ;
|
||||||
|
|
||||||
@@ -140,14 +101,15 @@ 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("======= LwIP: HTTPS Client Test(%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:
|
||||||
https_nb->pcb = tcp_new();
|
https_nb->pcb = tcp_new();
|
||||||
if(https_nb->pcb) {
|
if(https_nb->pcb) {
|
||||||
tcp_arg(https_nb->pcb, (void *)&(https_nb->stat)) ;
|
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 {
|
} else {
|
||||||
ERR_PRINTF("tcp_new, ret=%d\n", https_nb->pcb) ;
|
ERR_PRINTF("tcp_new, ret=%d\n", https_nb->pcb) ;
|
||||||
https_nb->stat = IDLE ;
|
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>>16)&0xff,
|
||||||
(*(unsigned long *)&https_nb->serverIP_em>>24)&0xff) ;
|
(*(unsigned long *)&https_nb->serverIP_em>>24)&0xff) ;
|
||||||
ret = tcp_connect(https_nb->pcb, &(https_nb->serverIP_em),
|
ret = tcp_connect(https_nb->pcb, &(https_nb->serverIP_em),
|
||||||
https_nb->serverPort, DataConnectedCallback);
|
https_nb->serverPort, TcpConnectedCallback);
|
||||||
|
|
||||||
if(ret == ERR_OK) {
|
if(ret == ERR_OK) {
|
||||||
https_nb->stat = WAITING ;
|
https_nb->stat = WAITING ;
|
||||||
@@ -212,18 +174,17 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
|
|||||||
return !ERR_OK ;
|
return !ERR_OK ;
|
||||||
}
|
}
|
||||||
|
|
||||||
CyaSSL_SetIO_LwIP(https_nb->ssl, https_nb->pcb);
|
CyaSSL_SetIO_LwIP(https_nb->ssl, https_nb->pcb,
|
||||||
CyaSSL_SetVersion(https_nb->ssl, CYASSL_TLSV1_2) ;
|
DataReceiveCallback, NULL, (void *)&https_nb->stat);
|
||||||
|
|
||||||
https_nb->stat = SSL_CONN ;
|
https_nb->stat = SSL_CONN ;
|
||||||
|
|
||||||
case SSL_CONN: /* handshaking */
|
case SSL_CONN: /* handshaking */
|
||||||
|
|
||||||
if(LwIP_cb_mutex) return ERR_OK ;
|
if(LwIP_cb_mutex) return ERR_OK ;
|
||||||
ret = CyaSSL_connect(https_nb->ssl);
|
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) {
|
if(ret == SSL_SUCCESS) {
|
||||||
https_nb->stat = SSL_CONN_WAITING ;
|
DBG_PRINTF("SSL Connected\n") ;
|
||||||
DBG_PRINTF("LwIPtest: SSL Connected\n") ;
|
|
||||||
https_nb->stat = HTTP_SEND ;
|
https_nb->stat = HTTP_SEND ;
|
||||||
} else {
|
} else {
|
||||||
ret = CyaSSL_get_error(https_nb->ssl, NULL) ;
|
ret = CyaSSL_get_error(https_nb->ssl, NULL) ;
|
||||||
@@ -239,8 +200,9 @@ int CyaSSL_HTTPS_Client_NB(void *nb)
|
|||||||
return ERR_OK ;
|
return ERR_OK ;
|
||||||
|
|
||||||
case SSL_CONN_WAITING:
|
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 ;
|
https_nb->stat = SSL_CONN ;
|
||||||
LwIP_cb_mutex = 0 ;
|
LwIP_cb_mutex = 0 ;
|
||||||
}
|
}
|
||||||
@@ -253,12 +215,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("LwIPtest: SSL CONNECTED(%x)\n", https_nb) ;
|
printf("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) ;
|
|
||||||
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) ;
|
||||||
@@ -278,9 +235,9 @@ 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("LwIPtest: HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ;
|
printf("HTTPS GET(%x), Received(%d)\n",https_nb, strlen(httpbuff)) ;
|
||||||
/* puts(httpbuff) ; */
|
/* puts(httpbuff) ; */
|
||||||
puts("===================\n") ;
|
/* puts("===================\n") ; */
|
||||||
}
|
}
|
||||||
case SSL_CLOSE:
|
case SSL_CLOSE:
|
||||||
{
|
{
|
||||||
@@ -332,7 +289,7 @@ 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(xxx,xxx,xxx,xxx) } ;
|
static struct ip_addr server_em = { IP_ADDR(192,168,11,9) } ;
|
||||||
|
|
||||||
void HTTPSClient_main_init() {
|
void HTTPSClient_main_init() {
|
||||||
|
|
||||||
|
@@ -964,9 +964,9 @@ int SetCipherList(Suites*, const char* list);
|
|||||||
#ifdef HAVE_LWIP_NATIVE
|
#ifdef HAVE_LWIP_NATIVE
|
||||||
CYASSL_LOCAL int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb);
|
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 int CyaSSL_LwIP_Receive(CYASSL* ssl, char *buf, int sz, void *cb);
|
||||||
CYASSL_LOCAL void CyaSSL_NB_setCallbackArg(CYASSL *ssl, void *arg) ;
|
CYASSL_API int CyaSSL_SetIO_LwIP(CYASSL* ssl, void *pcb,
|
||||||
CYASSL_LOCAL void CyaSSL_PbufFree(void *p);
|
tcp_recv_fn recv, tcp_sent_fn sent, void *arg);
|
||||||
#endif /* HAVE_{tcp stack} */
|
#endif /* HAVE_LWIP_NATIVE */
|
||||||
|
|
||||||
/* CyaSSL Cipher type just points back to SSL */
|
/* CyaSSL Cipher type just points back to SSL */
|
||||||
struct CYASSL_CIPHER {
|
struct CYASSL_CIPHER {
|
||||||
@@ -1806,6 +1806,8 @@ typedef struct DtlsMsg {
|
|||||||
/* LwIP native tpc socket context */
|
/* LwIP native tpc socket context */
|
||||||
typedef struct LwIP_native_Ctx {
|
typedef struct LwIP_native_Ctx {
|
||||||
struct tcp_pcb * pcb ;
|
struct tcp_pcb * pcb ;
|
||||||
|
tcp_recv_fn recv ;
|
||||||
|
tcp_sent_fn sent ;
|
||||||
int pulled ;
|
int pulled ;
|
||||||
struct pbuf *pbuf ;
|
struct pbuf *pbuf ;
|
||||||
int wait ;
|
int wait ;
|
||||||
|
@@ -940,7 +940,7 @@ CYASSL_API void CyaSSL_SetIOWriteFlags(CYASSL* ssl, int flags);
|
|||||||
ULONG waitoption);
|
ULONG waitoption);
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_LWIP_NATIVE
|
#ifdef HAVE_LWIP_NATIVE
|
||||||
CYASSL_API int CyaSSL_SetIO_LwIP(CYASSL* ssl, void *pcb);
|
#include "lwip/tcp.h"
|
||||||
#endif
|
#endif
|
||||||
typedef int (*CallbackGenCookie)(CYASSL* ssl, unsigned char* buf, int sz,
|
typedef int (*CallbackGenCookie)(CYASSL* ssl, unsigned char* buf, int sz,
|
||||||
void* ctx);
|
void* ctx);
|
||||||
|
41
src/io.c
41
src/io.c
@@ -1070,7 +1070,7 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
|
|||||||
#define DBG_PRINTF_CB(x, ...)
|
#define DBG_PRINTF_CB(x, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void CyaSSL_PbufFree(void *vp)
|
static void CyaSSL_PbufFree(void *vp)
|
||||||
{
|
{
|
||||||
struct pbuf *p ;
|
struct pbuf *p ;
|
||||||
struct pbuf * next;
|
struct pbuf * next;
|
||||||
@@ -1134,18 +1134,18 @@ static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL *ssl, int size)
|
|||||||
return totalLen;
|
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 ;
|
struct pbuf *next ;
|
||||||
CYASSL *ssl ;
|
CYASSL *ssl ;
|
||||||
ssl = (CYASSL *)cb ;
|
ssl = (CYASSL *)cb ;
|
||||||
|
|
||||||
if((cb == NULL)||(pcb == NULL))
|
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)) {
|
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 {
|
}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 */
|
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.pbuf = p ;
|
||||||
}
|
}
|
||||||
ssl->lwipCtx.pulled = 0 ;
|
ssl->lwipCtx.pulled = 0 ;
|
||||||
if(ssl->lwipCtx.wait < 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)))
|
||||||
|
{
|
||||||
|
if(ssl->lwipCtx.wait < 0) /* wait for multiple callbacks */
|
||||||
ssl->lwipCtx.wait = 10000 ;
|
ssl->lwipCtx.wait = 10000 ;
|
||||||
|
} else if(ssl->lwipCtx.recv)
|
||||||
|
return ssl->lwipCtx.recv(ssl->lwipCtx.arg, pcb, p, err) ;
|
||||||
|
/* user callback */
|
||||||
return ERR_OK;
|
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;
|
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)
|
int CyaSSL_SetIO_LwIP(CYASSL* ssl, void* pcb,
|
||||||
{
|
tcp_recv_fn recv, tcp_sent_fn sent, void *arg)
|
||||||
ssl->lwipCtx.arg = arg ;
|
|
||||||
}
|
|
||||||
|
|
||||||
int CyaSSL_SetIO_LwIP(CYASSL* ssl, void* pcb)
|
|
||||||
{
|
{
|
||||||
if (ssl && pcb) {
|
if (ssl && pcb) {
|
||||||
ssl->lwipCtx.pcb = (struct tcp_pcb *)pcb ;
|
ssl->lwipCtx.pcb = (struct tcp_pcb *)pcb ;
|
||||||
tcp_recv(pcb, CyaSSL_connectCallback);
|
ssl->lwipCtx.recv = recv ; /* recv user callback */
|
||||||
tcp_sent(pcb, DataSentCallback);
|
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) ;
|
tcp_arg (pcb, (void *)ssl) ;
|
||||||
} else return BAD_FUNC_ARG ;
|
} else return BAD_FUNC_ARG ;
|
||||||
return ERR_OK ;
|
return ERR_OK ;
|
||||||
|
Reference in New Issue
Block a user