1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

Merge remote-tracking branch 'remotes/esp8266/master' into httpClient

This commit is contained in:
Markus Sattler
2015-11-17 18:17:58 +01:00
18 changed files with 217 additions and 106 deletions

View File

@ -50,7 +50,6 @@ extern "C"
#define SSL_DEBUG_OPTS 0
#endif
#define SSL_RX_BUF_SIZE 4096
class SSLContext {
public:
@ -59,8 +58,6 @@ public:
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0);
}
++_ssl_ctx_refcnt;
_rxbuf = new cbuf(SSL_RX_BUF_SIZE);
}
~SSLContext() {
@ -73,8 +70,6 @@ public:
if (_ssl_ctx_refcnt == 0) {
ssl_ctx_free(_ssl_ctx);
}
delete _rxbuf;
}
void ref() {
@ -92,38 +87,50 @@ public:
}
int read(uint8_t* dst, size_t size) {
if (!_rxbuf->getSize()) {
_readAll();
if (!_available) {
if (!_readAll())
return 0;
}
size_t available = _rxbuf->getSize();
size_t will_read = (available < size) ? available : size;
return _rxbuf->read(reinterpret_cast<char*>(dst), will_read);
size_t will_copy = (_available < size) ? _available : size;
memcpy(dst, _read_ptr, will_copy);
_read_ptr += will_copy;
_available -= will_copy;
if (_available == 0) {
_read_ptr = nullptr;
}
return will_copy;
}
int read() {
optimistic_yield(100);
if (!_rxbuf->getSize()) {
_readAll();
if (!_available) {
if (!_readAll())
return -1;
}
return _rxbuf->read();
int result = _read_ptr[0];
++_read_ptr;
--_available;
if (_available == 0) {
_read_ptr = nullptr;
}
return result;
}
int peek() {
if (!_rxbuf->getSize()) {
_readAll();
if (!_available) {
if (!_readAll())
return -1;
}
return _rxbuf->peek();
return _read_ptr[0];
}
int available() {
auto rc = _rxbuf->getSize();
if (rc == 0) {
_readAll();
rc = _rxbuf->getSize();
auto cb = _available;
if (cb == 0) {
cb = _readAll();
} else {
optimistic_yield(100);
}
return rc;
return cb;
}
operator SSL*() {
@ -135,6 +142,8 @@ protected:
if (!_ssl)
return 0;
optimistic_yield(100);
uint8_t* data;
int rc = ssl_read(_ssl, &data);
if (rc <= 0) {
@ -144,25 +153,18 @@ protected:
}
return 0;
}
if (rc > _rxbuf->room()) {
DEBUGV("WiFiClientSecure rx overflow");
rc = _rxbuf->room();
}
int result = 0;
size_t sizeBefore = _rxbuf->getSize();
if (rc)
result = _rxbuf->write(reinterpret_cast<const char*>(data), rc);
DEBUGV("*** rb: %d + %d = %d\r\n", sizeBefore, rc, _rxbuf->getSize());
return result;
DEBUGV(":wcs ra %d", rc);
_read_ptr = data;
_available = rc;
return _available;
}
static SSL_CTX* _ssl_ctx;
static int _ssl_ctx_refcnt;
SSL* _ssl = nullptr;
int _refcnt = 0;
cbuf* _rxbuf;
const uint8_t* _read_ptr = nullptr;
size_t _available = 0;
};
SSL_CTX* SSLContext::_ssl_ctx = nullptr;
@ -313,14 +315,13 @@ bool WiFiClientSecure::verify(const char* fp, const char* url) {
while (pos < len && fp[pos] == ' ') {
++pos;
}
DEBUGV("pos:%d ", pos);
if (pos > len - 2) {
DEBUGV("fingerprint too short\r\n");
DEBUGV("pos:%d len:%d fingerprint too short\r\n", pos, len);
return false;
}
uint8_t high, low;
if (!parseHexNibble(fp[pos], &high) || !parseHexNibble(fp[pos+1], &low)) {
DEBUGV("invalid hex sequence: %c%c\r\n", fp[pos], fp[pos+1]);
DEBUGV("pos:%d len:%d invalid hex sequence: %c%c\r\n", pos, len, fp[pos], fp[pos+1]);
return false;
}
pos += 2;

View File

@ -38,33 +38,41 @@ extern "C" {
#include "cbuf.h"
#include "include/ClientContext.h"
WiFiServer::WiFiServer(uint16_t port)
WiFiServer::WiFiServer(IPAddress addr, uint16_t port)
: _port(port)
, _addr(addr)
, _pcb(nullptr)
, _unclaimed(nullptr)
, _discarded(nullptr)
{
_port = port;
_pcb = 0;
_unclaimed = 0;
_discarded = 0;
}
void WiFiServer::begin()
WiFiServer::WiFiServer(uint16_t port)
: _port(port)
, _addr((uint32_t) IPADDR_ANY)
, _pcb(nullptr)
, _unclaimed(nullptr)
, _discarded(nullptr)
{
err_t err;
}
void WiFiServer::begin() {
err_t err;
tcp_pcb* pcb = tcp_new();
if (!pcb)
return;
err = tcp_bind(pcb, INADDR_ANY, _port);
ip_addr_t local_addr;
local_addr.addr = (uint32_t) _addr;
err = tcp_bind(pcb, &local_addr, _port);
if (err != ERR_OK)
{
if (err != ERR_OK) {
tcp_close(pcb);
return;
}
tcp_pcb* listen_pcb = tcp_listen(pcb);
if (!listen_pcb)
{
if (!listen_pcb) {
tcp_close(pcb);
return;
}
@ -73,26 +81,30 @@ void WiFiServer::begin()
tcp_arg(listen_pcb, (void*) this);
}
void WiFiServer::setNoDelay(bool nodelay){
if(!_pcb) return;
if(nodelay) tcp_nagle_disable(_pcb);
else tcp_nagle_enable(_pcb);
void WiFiServer::setNoDelay(bool nodelay) {
if (!_pcb)
return;
if (nodelay)
tcp_nagle_disable(_pcb);
else
tcp_nagle_enable(_pcb);
}
bool WiFiServer::getNoDelay(){
if(!_pcb) return false;
return tcp_nagle_disabled(_pcb);
bool WiFiServer::getNoDelay() {
if (!_pcb)
return false;
return tcp_nagle_disabled(_pcb);
}
bool WiFiServer::hasClient(){
if (_unclaimed) return true;
return false;
}
WiFiClient WiFiServer::available(byte* status)
{
bool WiFiServer::hasClient() {
if (_unclaimed)
{
return true;
return false;
}
WiFiClient WiFiServer::available(byte* status) {
if (_unclaimed) {
WiFiClient result(_unclaimed);
_unclaimed = _unclaimed->next();
DEBUGV("WS:av\r\n");
@ -100,32 +112,28 @@ WiFiClient WiFiServer::available(byte* status)
}
optimistic_yield(1000);
return WiFiClient();
}
uint8_t WiFiServer::status() {
uint8_t WiFiServer::status() {
if (!_pcb)
return CLOSED;
return _pcb->state;
}
size_t WiFiServer::write(uint8_t b)
{
size_t WiFiServer::write(uint8_t b) {
return write(&b, 1);
}
size_t WiFiServer::write(const uint8_t *buffer, size_t size)
{
size_t WiFiServer::write(const uint8_t *buffer, size_t size) {
// write to all clients
// not implemented
return 0;
}
template<typename T>
T* slist_append_tail(T* head, T* item)
{
T* slist_append_tail(T* head, T* item) {
if (!head)
return item;
T* last = head;
@ -135,29 +143,23 @@ T* slist_append_tail(T* head, T* item)
return head;
}
int8_t WiFiServer::_accept(tcp_pcb* apcb, int8_t err)
{
int8_t WiFiServer::_accept(tcp_pcb* apcb, int8_t err) {
DEBUGV("WS:ac\r\n");
ClientContext* client = new ClientContext(apcb, &WiFiServer::_s_discard, this);
_unclaimed = slist_append_tail(_unclaimed, client);
tcp_accepted(_pcb);
// printf("WiFiServer::_accept\r\n");
return ERR_OK;
}
void WiFiServer::_discard(ClientContext* client)
{
void WiFiServer::_discard(ClientContext* client) {
// _discarded = slist_append_tail(_discarded, client);
DEBUGV("WS:dis\r\n");
// printf("WiFiServer::_discard\r\n");
}
int8_t WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, int8_t err)
{
int8_t WiFiServer::_s_accept(void *arg, tcp_pcb* newpcb, int8_t err) {
return reinterpret_cast<WiFiServer*>(arg)->_accept(newpcb, err);
}
void WiFiServer::_s_discard(void* server, ClientContext* ctx)
{
void WiFiServer::_s_discard(void* server, ClientContext* ctx) {
reinterpret_cast<WiFiServer*>(server)->_discard(ctx);
}

View File

@ -29,6 +29,7 @@ extern "C" {
}
#include "Server.h"
#include "IPAddress.h"
class ClientContext;
class WiFiClient;
@ -36,12 +37,14 @@ class WiFiClient;
class WiFiServer : public Server {
private:
uint16_t _port;
IPAddress _addr;
tcp_pcb* _pcb;
ClientContext* _unclaimed;
ClientContext* _discarded;
public:
WiFiServer(IPAddress addr, uint16_t port);
WiFiServer(uint16_t port);
WiFiClient available(uint8_t* status = NULL);
bool hasClient();
@ -56,7 +59,7 @@ public:
protected:
int8_t _accept(tcp_pcb* newpcb, int8_t err);
void _discard(ClientContext* client);
void _discard(ClientContext* client);
static int8_t _s_accept(void *arg, tcp_pcb* newpcb, int8_t err);
static void _s_discard(void* server, ClientContext* ctx);