LwIP native TCP

This commit is contained in:
Takashi Kojo
2014-01-31 08:49:39 +09:00
parent e28d256197
commit 07af9913c3
15 changed files with 6664 additions and 6 deletions

View File

@@ -0,0 +1,283 @@
/* HTTPS-NB.c
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of CyaSSL.
*
* CyaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CyaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cyassl/ctaocrypt/settings.h>
#if defined(CYASSL_MDK_ARM)
#include <stdio.h>
#include <string.h>
#include <rtl.h>
#include "cyassl_MDK_ARM.h"
#endif
#if defined(CYASSL_IAR_ARM)
#include <stdio.h>
#include <string.h>
#endif
#if defined(CYASSL_LWIP)
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#endif
#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/ssl.h>
#include <cyassl/ctaocrypt/memory.h>
#include "SSL-NB.h"
#include "HTTPS-NB.h"
#if 0
/*Enable debug*/
#include <cstdio>
#define DBG_PRINTF(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG_PRINTF(x, ...)
#endif
#define ERR_PRINTF(x, ...) printf("[HTTPSClient:ERROR]"x"\r\n", ##__VA_ARGS__);
static unsigned long localPort = 0 ;
static unsigned long getPort(void) {
return (localPort++ + 0x200) & 0x7fff ;
}
static err_t DataConnectedCallback (void *arg, struct tcp_pcb *pcb, s8_t 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 *cb, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct pbuf *next ;
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
DBG_PRINTF("LwIPtest: Data Received(DataReceiveCallback), pbuf->len=%d, err=%d\n", p->tot_len , err) ;
if(p==0) { /* throw away */
return ERR_OK ;
}
if(*(enum HTTPS_Stat *)(ssl_nb->arg) == WAITING) {
*(enum HTTPS_Stat *)(ssl_nb->arg) = HTTP_RECEIVE ;
} else {
CyaSSL_PbufFree(p) ;
tcp_recved(pcb,p->tot_len) ;
return ERR_OK ;
}
/* put it into the queue */
if(ssl_nb->pbuf) {
next = ssl_nb->pbuf ;
while(1) {
DBG_PRINTF("pbuf=%x, pbuf->next=%x, ",ssl_nb->pbuf, next) ;
if(next->next)
next = next->next ;
else break ;
}
next->next = p ;
ssl_nb->pbuf->tot_len += p->tot_len ;
} else {
ssl_nb->pbuf = p ;
}
ssl_nb->pulled = 0 ;
if(ssl_nb->wait < 0)
ssl_nb->wait = 1000 ;
ssl_nb->pulled = 0 ;
return ERR_OK;
}
static int count = 0 ;
void CyaSSL_HTTPS_Client_NB_init(void *nb,
struct ip_addr svIP, unsigned long svPort, char *host, char *path)
{
CYASSL_HTTPS_NB *https_nb ;
https_nb = (CYASSL_HTTPS_NB *)nb ;
https_nb->serverIP_em = svIP ;
https_nb->serverPort = svPort ;
https_nb->hostname = host ;
https_nb->path = path ;
https_nb->stat = BEGIN ;
}
int CyaSSL_HTTPS_Client_NB(void *nb)
{
char *p ;
int ret ;
CYASSL_HTTPS_NB *https_nb ;
CYASSL_NB *ssl_nb ;
https_nb = (CYASSL_HTTPS_NB *)nb ;
ssl_nb = (CYASSL_NB *)&(https_nb->ssl_nb) ;
/*CyaSSL_Debugging_ON() ; */
switch(https_nb->stat) {
case BEGIN:
printf("======= LwIP: HTTPS Client Test(%x): %d =========\n", nb, count ++) ;
p = (char *)malloc(1) ;
printf("Warter mark: %x\n", p) ;
free(p) ;
/*** 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) ;
} else {
ERR_PRINTF("tcp_new, ret=%d\n", https_nb->pcb) ;
https_nb->stat = IDLE ;
return !ERR_OK ;
}
tcp_arg(https_nb->pcb, (void *)&https_nb->stat) ;
https_nb->localPort = getPort() ;
printf("local Port=%d\n", https_nb->localPort) ;
ret = tcp_bind (https_nb->pcb, &(https_nb->localIP_em),
https_nb->localPort) ;
if(ret == ERR_OK) {
https_nb->stat = TCP_CONNECT ;
return ERR_OK;
} else {
ERR_PRINTF("tcp_bind, ret=%d\n", ret) ;
https_nb->stat = INITIALIZED ;
return !ERR_OK ;
}
case TCP_CONNECT:
DBG_PRINTF("LwIPtest: TCP_CONNECT\n") ;
DBG_PRINTF("LwIPtest: Server IP Addrress(%d.%d.%d.%d)\n",
(*(unsigned long *)&https_nb->serverIP_em&0xff),
(*(unsigned long *)&https_nb->serverIP_em>>8)&0xff,
(*(unsigned long *)&https_nb->serverIP_em>>16)&0xff,
(*(unsigned long *)&https_nb->serverIP_em>>24)&0xff) ;
if(CyaSSL_cb_mutex)return ERR_OK ;
else CyaSSL_cb_mutex = 1 ;
ret = tcp_connect(https_nb->pcb, &(https_nb->serverIP_em),
https_nb->serverPort, DataConnectedCallback);
if(ret == ERR_OK) {
https_nb->stat = WAITING ;
return ERR_OK;
} else {
ERR_PRINTF("tcp_connect, ret=%d\n", ret) ;
https_nb->stat = TCP_CLOSE ;
return !ERR_OK;
}
case TCP_CONNECTED:
printf("LwIPtest: TCP CONNECTED\n") ;
CyaSSL_cb_mutex = 0 ;
ret = CyaSSL_init_NB(&(https_nb->ssl_nb), https_nb->pcb) ;
if(ret != ERR_OK) {
https_nb->stat = TCP_CLOSE ;
return !ERR_OK ;
}
https_nb->stat = SSL_CONN ;
case SSL_CONN: /* handshaking */
ret = CyaSSL_connecting_NB(&(https_nb->ssl_nb)) ;
if(ret == SSL_NB_ERROR) {
ERR_PRINTF("CyaSSL_connecting, ret=%d\n", ret) ;
https_nb->stat = SSL_CLOSE ;
return !ERR_OK ;
} else if(ret == SSL_NB_CONNECTED)
https_nb->stat = HTTP_SEND ;
else return ERR_OK;
case HTTP_SEND:
{
#define SEND_BUFF_SIZE 100
char sendBuff[SEND_BUFF_SIZE] ;
int size ;
printf("LwIPtest: SSL CONNECTED\n") ;
CyaSSL_NB_setCallbackArg(ssl_nb, &(https_nb->stat)) ;
tcp_sent(https_nb->pcb, DataSentCallback);
tcp_recv(https_nb->pcb, DataReceiveCallback);
DBG_PRINTF("LwIPtest: HTTPS GET\n") ;
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) ;
if(CyaSSL_cb_mutex)return ERR_OK ;
else CyaSSL_cb_mutex = 1 ; /* lock */
CyaSSL_write(ssl_nb->ssl, sendBuff, size) ;
https_nb->stat = WAITING ;
return ERR_OK;
}
case HTTP_RECEIVE:
{
#define HTTP_BUFF_SIZE 2048
char httpbuff[HTTP_BUFF_SIZE] ;
memset(httpbuff, '\0', HTTP_BUFF_SIZE) ;
ret = CyaSSL_read(ssl_nb->ssl, httpbuff, HTTP_BUFF_SIZE) ;
printf("LwIPtest: HTTPS GET, Received(%d)\n",strlen(httpbuff)) ;
CyaSSL_cb_mutex = 0 ;
/* puts(httpbuff) ;*/
puts("===================\n") ;
}
case SSL_CLOSE:
CyaSSL_close_NB(ssl_nb) ;
https_nb->stat = TCP_CLOSE ;
return ERR_OK ;
case TCP_CLOSE:
tcp_close(https_nb->pcb) ;
https_nb->idle = 0 ;
https_nb->stat = IDLE ;
case IDLE:
https_nb->idle ++ ;
if(https_nb->idle > 50000)
https_nb->stat = BEGIN ;
case WAITING:
default:
return ERR_OK;
}
}

View File

@@ -0,0 +1,68 @@
/* HTTPS-NB.h
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of CyaSSL.
*
* CyaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CyaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
extern int CyaSSL_GetDataFromPbuf(char *buff, struct pbuf *p, int size) ;
#define IP4_LONG_2_ADDR(ipaddr, iplong) \
(ipaddr)->addr = htonl(((u32_t)(iplong) & 0xFF000000) | \
((u32_t)(iplong) & 0xFF0000) | \
((u32_t)(iplong) & 0xFF00) | \
(u32_t)(iplong) & 0xFF)
#define IP_ADDR(a,b,c,d) (((a)|((b)<<8)|((c)<<16)|(d)<<24))
enum HTTPS_Stat {
BEGIN,
GET_MYIP,
INITIALIZED,
TCP_CONNECT,
TCP_CONNECTED,
SSL_INIT,
SSL_CONN,
HTTP_SEND,
HTTP_RECEIVE,
HTTP_DONE,
SSL_CLOSE,
TCP_CLOSE,
WAITING,
IDLE
} ;
#define HTTPS_PORT 443
typedef struct {
CYASSL_NB ssl_nb ;
enum HTTPS_Stat stat ;
struct tcp_pcb * pcb ;
unsigned long ipaddress ;
struct ip_addr localIP_em;
unsigned long localPort ;
struct ip_addr serverIP_em ;
unsigned long serverPort ;
char *hostname ;
char *path ;
int idle ;
} CYASSL_HTTPS_NB ;
extern void CyaSSL_HTTPS_Client_NB_init(void *nb,
struct ip_addr svIP, unsigned long svPort, char *host, char *path) ;
extern int CyaSSL_HTTPS_Client_NB(void *nb) ;

View File

@@ -0,0 +1,96 @@
/* HTTPS-MAIN.c
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of CyaSSL.
*
* CyaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CyaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/ssl.h>
#include <cyassl/ctaocrypt/memory.h>
#include "SSL-NB.h"
#include "HTTPS-NB.h"
#include "HTTPS-main.h"
CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_1 ;
void *CyaSSL_HTTPS_ClientP_1 = (void *)&CyaSSL_HTTPS_Client_1 ;
CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_2 ;
void *CyaSSL_HTTPS_ClientP_2 = (void *)&CyaSSL_HTTPS_Client_2 ;
CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_3 ;
void *CyaSSL_HTTPS_ClientP_3 = (void *)&CyaSSL_HTTPS_Client_3 ;
CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_4 ;
void *CyaSSL_HTTPS_ClientP_4 = (void *)&CyaSSL_HTTPS_Client_4 ;
CYASSL_HTTPS_NB CyaSSL_HTTPS_Client_5 ;
void *CyaSSL_HTTPS_ClientP_5 = (void *)&CyaSSL_HTTPS_Client_5 ;
extern void CyaSSL_HTTPS_Client_NB_init(void *nb,
struct ip_addr svIP, unsigned long svPort, char *host, char *path) ;
#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 int i = 0 ;
void HTTPSClient_main_init() {
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_1,
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,
server_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_4,
server_em, HTTPS_PORT, "xxx.com", "/") ;
CyaSSL_HTTPS_Client_NB_init(CyaSSL_HTTPS_ClientP_5,
server_em, HTTPS_PORT, "xxx.com", "/") ;
}
void HTTPSClient_main(void)
{
if(i++ < 10000)return ;
if((i % 1) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_1) ;
}
if((i % 2) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_2) ;
}
if((i % 3) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_3) ;
}
if((i % 4) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_4) ;
}
#if 0
if((i % 5) == 0) { /* wait for initializing TCP/IP, DHCP */
CyaSSL_HTTPS_Client_NB(CyaSSL_HTTPS_ClientP_5) ;
}
#endif
}

View File

@@ -0,0 +1,210 @@
/* SSL-NB.c
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of CyaSSL.
*
* CyaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CyaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cyassl/ctaocrypt/settings.h>
#if defined(CYASSL_MDK_ARM)
#include <stdio.h>
#include <string.h>
#include <rtl.h>
#include "cyassl_MDK_ARM.h"
#endif
#if defined(CYASSL_IAR_ARM)
#include <stdio.h>
#include <string.h>
#endif
#if defined(CYASSL_LWIP)
#include "lwip/tcp.h"
#include "lwip/pbuf.h"
#include "lwip/sockets.h"
#endif
#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/ssl.h>
#include <cyassl/internal.h>
#include <SSL-NB.h>
#if 0
/*Enable debug*/
#include <cstdio>
#define DBG_PRINTF(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG_PRINTF(x, ...)
#endif
#define ERR_PRINTF(x, ...) printf("[SSLClient:ERROR]"x"\r\n", ##__VA_ARGS__);
#if 0
/*Enable debug*/
#define DBG_PRINTF_CB(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG_PRINTF_CB(x, ...)
#endif
CyaSSL_CALLBACK_MUTEX CyaSSL_cb_mutex = 0 ;
static err_t CyaSSL_connectCallback(void *cb, struct tcp_pcb *pcb, struct pbuf *p, s8_t err)
{
struct pbuf *next ;
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
if((cb == NULL)||(pcb == NULL))
ERR_PRINTF("CyaSSL_connectCallBack, cb=%x, pcb=%d\n", cb, pcb) ;
if(p && (err == 0)) {
printf("pbuf=%x\n", p) ;
DBG_PRINTF_CB("LwIPtest: CyaSSL connect, started(CyaSSL_connectCallBack1), 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) ;
return ERR_OK; /* don't go to SSL_CONN */
}
if(ssl_nb->pbuf) {
next = ssl_nb->pbuf ;
while(1) {
if(next->next)
next = next->next ;
else break ;
}
next->next = p ;
ssl_nb->pbuf->tot_len += p->tot_len ;
} else {
ssl_nb->pbuf = p ;
}
ssl_nb->pulled = 0 ;
if(ssl_nb->wait < 0)
ssl_nb->wait = 10000 ;
return ERR_OK;
}
static err_t DataSentCallback (void *arg, struct tcp_pcb *pcb, u16_t err)
{
DBG_PRINTF_CB("LwIPtest: Data Sent(SentCallBack1), err=%d\n", err) ;
return ERR_OK;
}
int CyaSSL_init_NB(CYASSL_NB *nb, struct tcp_pcb * pcb)
{
CYASSL_NB *ssl_nb ;
ssl_nb = nb ;
/*CyaSSLv3_client_method()
CyaTLSv1_client_method()
CyaTLSv1_1_client_method()
CyaTLSv1_2_client_method() */
ssl_nb->ctx = CyaSSL_CTX_new(CyaTLSv1_2_client_method());
if (ssl_nb->ctx == NULL) {
ERR_PRINTF("CyaSSL_CTX_new: unable to get ctx");
return !ERR_OK ;
}
CyaSSL_CTX_set_verify(ssl_nb->ctx, SSL_VERIFY_NONE, 0);
ssl_nb->ssl = CyaSSL_new(ssl_nb->ctx);
if (ssl_nb->ssl == NULL) {
ERR_PRINTF("CyaSSL_new: unable to get SSL object");
return !ERR_OK ;
}
ssl_nb->pcb = pcb ;
ssl_nb->pbuf = NULL ;
ssl_nb->pulled = 0 ;
ssl_nb->stat = SSL_NB_CONN ;
/* set up callbacks */
CyaSSL_SetIOReadCtx (ssl_nb->ssl, (void *)ssl_nb) ;
CyaSSL_SetIOWriteCtx(ssl_nb->ssl, (void *)ssl_nb) ;
tcp_recv(ssl_nb->pcb, CyaSSL_connectCallback);
tcp_sent(ssl_nb->pcb, DataSentCallback);
tcp_arg(ssl_nb->pcb, (void *)ssl_nb) ;
CyaSSL_SetVersion(ssl_nb->ssl, CYASSL_TLSV1_2) ;
CyaSSL_set_using_nonblock(ssl_nb->ssl, (0==0)) ;
/* Non-blocking CyaSSL_connect */
DBG_PRINTF("Return CyaSSL_init_NB = %x\n", ssl_nb) ;
return ERR_OK ;
}
/*** Non-Bloking Cyassl_connect, ... */
/* to be called in infinit loop ***/
int CyaSSL_connecting_NB(CYASSL_NB *ssl_nb)
{
int ret ;
switch(ssl_nb->stat) {
case SSL_NB_CONN:
if(CyaSSL_cb_mutex)return SSL_NB_WAITING ;
ret = CyaSSL_connect(ssl_nb->ssl);
DBG_PRINTF("LwIPtest: SSL Connecting(CyaSSL_connect), ret = %d\n", ret) ;
if(ret == SSL_CONNECT_WAITING) {
if(CyaSSL_cb_mutex)
return SSL_NB_WAITING ;
else CyaSSL_cb_mutex = 1 ; /* lock */
ssl_nb->wait = -1 ; /* wait until first callback */
ssl_nb->stat = SSL_NB_WAITING ;
return SSL_NB_CONNECTING ;
} else if(ret == SSL_CONNECTING) {
return SSL_NB_CONNECTING ;
} else if(ret == SSL_SUCCESS) {
ssl_nb->stat = SSL_NB_WAITING ;
DBG_PRINTF("LwIPtest: SSL Connected\n") ;
return SSL_NB_CONNECTED ;
} else {
ret = CyaSSL_get_error(ssl_nb->ssl, NULL) ;
ssl_nb->stat = SSL_NB_WAITING ;
return SSL_NB_CONNECTING ;
}
case SSL_NB_WAITING:
if(ssl_nb->wait-- == 0) { /* counting down after the callback
for multiple callbacks */
ssl_nb->stat = SSL_NB_CONN ;
CyaSSL_cb_mutex = 0 ;
}
return SSL_NB_CONNECTING ;
default:
return SSL_NB_ERROR ;
}
}
/** disconnect */
int CyaSSL_close_NB(CYASSL_NB *ssl_nb)
{
CyaSSL_shutdown(ssl_nb->ssl);
CyaSSL_free(ssl_nb->ssl);
CyaSSL_CTX_free(ssl_nb->ctx);
ssl_nb->stat = SSL_NB_BEGIN ;
return ERR_OK ;
}
void CyaSSL_NB_setCallbackArg(CYASSL_NB *ssl_nb, void *arg)
{
ssl_nb->arg = arg ;
}

View File

@@ -0,0 +1,68 @@
/* SSLcon-NB.h
*
* Copyright (C) 2006-2013 wolfSSL Inc.
*
* This file is part of CyaSSL.
*
* CyaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CyaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifndef __SSLCONN_NB_H__
#define __SSLCONN_NB_H__
#define mem_malloc malloc
#define mem_free free
enum SSL_Stat {
SSL_NB_BEGIN,
SSL_NB_WAITING,
SSL_NB_CONN,
} ;
enum SSL_NB_Ret {
SSL_NB_ERROR,
SSL_NB_CONNECTING,
SSL_NB_CONNECTED,
} ;
typedef struct CyaSSL_nb {
CYASSL *ssl ;
CYASSL_CTX *ctx ;
struct tcp_pcb * pcb ;
int pulled ;
struct pbuf *pbuf ;
enum SSL_Stat stat ;
int wait ;
void * arg ; /* arg for application */
int idle_count ;
} CYASSL_NB ;
extern int CyaSSL_init_NB(CYASSL_NB *nb, struct tcp_pcb * pcb) ;
extern int CyaSSL_connecting_NB(CYASSL_NB *ssl_nb) ;
extern int CyaSSL_close_NB(CYASSL_NB *ssl_nb) ;
extern void CyaSSL_NB_setCallbackArg(CYASSL_NB *ssl_nb, void *arg) ;
/* Set it to CYASSL_NB.arg for callback arg */
extern int CyaSSL_write(struct CYASSL *pcbSSL, const void *buffer, int len) ;
extern int CyaSSL_recv(struct CYASSL *pcbSSL, void *buffer, int len, int flg) ;
extern int CyaSSL_read(struct CYASSL *pcbSSL, void *buffer, int len) ;
extern void CyaSSL_PbufFree(struct pbuf * p) ;
typedef int CyaSSL_CALLBACK_MUTEX ;
extern CyaSSL_CALLBACK_MUTEX CyaSSL_cb_mutex ;
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<workspace>
<project>
<path>$WS_DIR$\CyaSSL-Lib.ewp</path>
</project>
<batchBuild/>
</workspace>

View File

@@ -51,7 +51,8 @@
#include <windows.h>
#include <wincrypt.h>
#else
#if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM)
#if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM) \
&& !defined(CYASSL_IAR_ARM)
#include <fcntl.h>
#ifndef EBSNET
#include <unistd.h>
@@ -484,7 +485,8 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
return 0;
}
#elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK)
#elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK) \
|| defined(CYASSL_IAR_ARM)
#warning "write a real random seed!!!!, just for testing now"

View File

@@ -81,6 +81,8 @@
/* Uncomment next line if using QL SEP settings */
/* #define CYASSL_QL */
/* Uncomment next line if using LwIP native TCP socket settings */
/* #define HAVE_LWIP_NATIVE */
#include <cyassl/ctaocrypt/visibility.h>
@@ -114,6 +116,14 @@
#include "nx_api.h"
#endif
#if defined(HAVE_LWIP_NATIVE) /* using LwIP native TCP socket */
#define CYASSL_LWIP
#define NO_WRITEV
#define SINGLE_THREADED
#define CYASSL_USER_IO
#define NO_FILESYSTEM
#endif
#ifdef MICROCHIP_PIC32
#define SIZEOF_LONG_LONG 8
#define SINGLE_THREADED

View File

@@ -960,8 +960,11 @@ int SetCipherList(Suites*, const char* list);
#ifdef HAVE_NETX
CYASSL_LOCAL int NetX_Receive(CYASSL *ssl, char *buf, int sz, void *ctx);
CYASSL_LOCAL int NetX_Send(CYASSL *ssl, char *buf, int sz, void *ctx);
#endif /* HAVE_NETX */
#endif
#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);
#endif /* HAVE_{tcp stack} */
/* CyaSSL Cipher type just points back to SSL */
struct CYASSL_CIPHER {
@@ -1515,7 +1518,8 @@ typedef void (*hmacfp) (CYASSL*, byte*, const byte*, word32, int, int);
/* client connect state for nonblocking restart */
enum ConnectState {
CONNECT_BEGIN = 0,
CONNECT_INITIAL = 0,
CONNECT_BEGIN,
CLIENT_HELLO_SENT,
HELLO_AGAIN, /* HELLO_AGAIN s for DTLS case */
HELLO_AGAIN_REPLY,

View File

@@ -675,7 +675,10 @@ enum { /* ssl Constants */
SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN = 103,
SSL_R_SSLV3_ALERT_BAD_CERTIFICATE = 104,
PEM_BUFSIZE = 1024
PEM_BUFSIZE = 1024,
/* for non-blocking CyaSSL_connect */
SSL_CONNECTING = 201, /* Ready for the next step */
SSL_CONNECT_WAITING = 202 /* Waiting for the response */
};

View File

@@ -397,6 +397,11 @@ int InitSSL_Ctx(CYASSL_CTX* ctx, CYASSL_METHOD* method)
ctx->CBIORecv = NetX_Receive;
ctx->CBIOSend = NetX_Send;
#endif
#ifdef HAVE_LWIP_NATIVE
ctx->CBIORecv = CyaSSL_LwIP_Receive ;
ctx->CBIOSend = CyaSSL_LwIP_Send ;
#endif
ctx->partialWrite = 0;
ctx->verifyCallback = 0;

108
src/io.c
View File

@@ -1045,3 +1045,111 @@ void CyaSSL_SetIO_NetX(CYASSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
#endif /* HAVE_NETX */
#ifdef HAVE_LWIP_NATIVE
#include "lwip/tcp.h"
#include "lwip/pbuf.h"
#include "lwip/sockets.h"
#include "SSL-NB.h"
#if 0
/*Enable debug*/
#define DBG_PRINTF_CB(x, ...) printf("[HTTPSClient : DBG]"x"\r\n", ##__VA_ARGS__);
#else
/*Disable debug*/
#define DBG_PRINTF_CB(x, ...)
#endif
void CyaSSL_PbufFree(struct pbuf *p)
{
struct pbuf * next;
while(p->next != NULL)
{
next = p->next;
pbuf_free(p);
p = next;
}
pbuf_free(p);
}
static int CyaSSL_GetDataFromPbuf(char *buff, CYASSL_NB *ssl_nb, int size)
{
struct pbuf *p ;
struct pbuf *p_next ;
int totalLen ;
int skipLen = 0 ;
p = ssl_nb->pbuf ;
if(p->tot_len < (ssl_nb->pulled + size))
return 0 ;
while(p) { /* skip the part pulled before */
if(p->len && p->len > (ssl_nb->pulled - skipLen) ){
skipLen = (ssl_nb->pulled - skipLen) ;
break ;
} else {
skipLen += p->len ;
if(p->next)
p = p->next ;
else return 0 ;
}
}
totalLen = 0 ;
while(p){
if(p->len) {
if((p->len - skipLen) > (size - totalLen)) { /* buffer full */
memcpy(&buff[totalLen], (const char *)&(((char *)(p->payload))[skipLen]), size-totalLen) ;
totalLen = size ;
break ;
} else {
memcpy(&buff[totalLen], (const char *)&(((char *)(p->payload))[skipLen]), p->len - skipLen) ;
totalLen += (p->len-skipLen) ;
skipLen = 0 ;
}
}
if(p->next){
p_next = p->next ;
p = p_next ;
} else break ;
}
ssl_nb->pulled += totalLen ;
if(ssl_nb->pbuf->tot_len <= ssl_nb->pulled) {
CyaSSL_PbufFree(ssl_nb->pbuf) ;
ssl_nb->pbuf = NULL ;
tcp_recved(ssl_nb->pcb,ssl_nb->pbuf->tot_len) ;
}
return totalLen;
}
int CyaSSL_LwIP_Receive(CYASSL* ssl, char *buf, int sz, void *cb)
{
int ret ;
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
DBG_PRINTF_CB("CyaSSL_LwIP_Receive: ssl_nb = %x\n", ssl_nb) ;
if(ssl_nb->pbuf) {
DBG_PRINTF_CB("Received Len=%d, Want Len= %d\n", ssl_nb->pbuf->tot_len, sz) ;
ret = CyaSSL_GetDataFromPbuf(buf, ssl_nb, sz) ;
if(ret == 0)
ret = CYASSL_CBIO_ERR_WANT_READ ;
} else {
DBG_PRINTF_CB("No Received Data\n") ;
ret = CYASSL_CBIO_ERR_WANT_READ ;
}
return ret ;
}
int CyaSSL_LwIP_Send(CYASSL* ssl, char *buf, int sz, void *cb)
{
CYASSL_NB *ssl_nb ;
ssl_nb = (CYASSL_NB *)cb ;
DBG_PRINTF_CB("CyaSSL_LwIP_Send: ssl_nb = %x\n", ssl_nb) ;
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) ;
tcp_write(ssl_nb->pcb, buf, sz, TCP_WRITE_FLAG_COPY) ;
return sz ;
}
#endif /* HAVE_LWIP_NATIVE */