Skip to content

Commit 2977041

Browse files
committed
Cherry picked commits from origin/master (1.0.0-RC2).
1 parent 09a2fec commit 2977041

File tree

14 files changed

+203
-60
lines changed

14 files changed

+203
-60
lines changed

cores/esp32/Server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class Server: public Print
2626
{
2727
public:
28-
virtual void begin() =0;
28+
virtual void begin(uint16_t port=0) =0;
2929
};
3030

3131
#endif

cores/esp32/WString.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -881,3 +881,31 @@ float String::toFloat(void) const
881881
}
882882
return 0;
883883
}
884+
885+
886+
unsigned char String::equalsConstantTime(const String &s2) const {
887+
// To avoid possible time-based attacks present function
888+
// compares given strings in a constant time.
889+
if(len != s2.len)
890+
return 0;
891+
//at this point lengths are the same
892+
if(len == 0)
893+
return 1;
894+
//at this point lenghts are the same and non-zero
895+
const char *p1 = buffer;
896+
const char *p2 = s2.buffer;
897+
unsigned int equalchars = 0;
898+
unsigned int diffchars = 0;
899+
while(*p1) {
900+
if(*p1 == *p2)
901+
++equalchars;
902+
else
903+
++diffchars;
904+
++p1;
905+
++p2;
906+
}
907+
//the following should force a constant time eval of the condition without a compiler "logical shortcut"
908+
unsigned char equalcond = (equalchars == len);
909+
unsigned char diffcond = (diffchars == 0);
910+
return (equalcond & diffcond); //bitwise AND
911+
}

cores/esp32/WString.h

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class String
212212
unsigned char operator <=(const String &rhs) const;
213213
unsigned char operator >=(const String &rhs) const;
214214
unsigned char equalsIgnoreCase(const String &s) const;
215+
unsigned char equalsConstantTime(const String &s) const;
215216
unsigned char startsWith(const String &prefix) const;
216217
unsigned char startsWith(const String &prefix, unsigned int offset) const;
217218
unsigned char endsWith(const String &suffix) const;

cores/esp32/esp32-hal-i2c.c

-9
Original file line numberDiff line numberDiff line change
@@ -990,15 +990,6 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
990990
}
991991

992992
if(eBits&EVENT_DONE) { // no gross timeout
993-
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
994-
uint32_t expected =(totalBytes*10*1000)/i2cGetFrequency(i2c);
995-
if((tAfter-tBefore)>(expected+1)) { //used some of the timeout Period
996-
// expected can be zero due to small packets
997-
log_e("TimeoutRecovery: expected=%ums, actual=%ums",expected,(tAfter-tBefore));
998-
i2cDumpI2c(i2c);
999-
i2cDumpInts(i2c->num);
1000-
}
1001-
#endif
1002993
switch(i2c->error) {
1003994
case I2C_OK :
1004995
reason = I2C_ERROR_OK;

cores/esp32/esp32-hal-uart.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,14 @@ void uartFlush(uart_t* uart)
315315
UART_MUTEX_LOCK();
316316
while(uart->dev->status.txfifo_cnt);
317317

318-
uart->dev->conf0.txfifo_rst = 1;
319-
uart->dev->conf0.txfifo_rst = 0;
318+
// Due to hardware issue, we can not use fifo_rst to reset uart fifo.
319+
// See description about UART_TXFIFO_RST and UART_RXFIFO_RST in <<esp32_technical_reference_manual>> v2.6 or later.
320+
321+
// we read the data out and make `fifo_len == 0 && rd_addr == wr_addr`.
322+
while(uart->dev->status.rxfifo_cnt != 0 || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
323+
READ_PERI_REG(UART_FIFO_REG(uart->num));
324+
}
320325

321-
uart->dev->conf0.rxfifo_rst = 1;
322-
uart->dev->conf0.rxfifo_rst = 0;
323326
UART_MUTEX_UNLOCK();
324327
}
325328

libraries/ArduinoOTA/src/ArduinoOTA.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ int ArduinoOTAClass::parseInt(){
149149

150150
String ArduinoOTAClass::readStringUntil(char end){
151151
String res = "";
152-
char value;
152+
int value;
153153
while(true){
154154
value = _udp_ota.read();
155-
if(value == '\0' || value == end){
155+
if(value <= 0 || value == end){
156156
return res;
157157
}
158-
res += value;
158+
res += (char)value;
159159
}
160160
return res;
161161
}

libraries/FS/src/FS.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ enum SeekMode {
4747
class File : public Stream
4848
{
4949
public:
50-
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
50+
File(FileImplPtr p = FileImplPtr()) : _p(p) {
51+
_timeout = 0;
52+
}
5153

5254
size_t write(uint8_t) override;
5355
size_t write(const uint8_t *buf, size_t size) override;

libraries/HTTPClient/src/HTTPClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void HTTPClient::end(void)
252252
bool HTTPClient::connected()
253253
{
254254
if(_tcp) {
255-
return (_tcp->connected() || (_tcp->available() > 0));
255+
return ((_tcp->available() > 0) || _tcp->connected());
256256
}
257257
return false;
258258
}

libraries/WiFi/src/WiFiClient.cpp

+149-37
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include "WiFiClient.h"
21+
#include "WiFi.h"
2122
#include <lwip/sockets.h>
2223
#include <lwip/netdb.h>
2324
#include <errno.h>
@@ -30,6 +31,118 @@
3031
#undef write
3132
#undef read
3233

34+
class WiFiClientRxBuffer {
35+
private:
36+
size_t _size;
37+
uint8_t *_buffer;
38+
size_t _pos;
39+
size_t _fill;
40+
int _fd;
41+
bool _failed;
42+
43+
size_t r_available()
44+
{
45+
if(_fd < 0){
46+
return 0;
47+
}
48+
int count;
49+
int res = lwip_ioctl_r(_fd, FIONREAD, &count);
50+
if(res < 0) {
51+
_failed = true;
52+
return 0;
53+
}
54+
return count;
55+
}
56+
57+
size_t fillBuffer()
58+
{
59+
if(!_buffer){
60+
_buffer = (uint8_t *)malloc(_size);
61+
}
62+
if(_fill && _pos == _fill){
63+
_fill = 0;
64+
_pos = 0;
65+
}
66+
if(!_buffer || _size <= _fill || !r_available()) {
67+
return 0;
68+
}
69+
int res = recv(_fd, _buffer + _fill, _size - _fill, MSG_DONTWAIT);
70+
if (res < 0 && errno != EWOULDBLOCK) {
71+
_failed = true;
72+
return 0;
73+
}
74+
_fill += res;
75+
return res;
76+
}
77+
78+
public:
79+
WiFiClientRxBuffer(int fd, size_t size=1436)
80+
:_size(size)
81+
,_buffer(NULL)
82+
,_pos(0)
83+
,_fill(0)
84+
,_fd(fd)
85+
,_failed(false)
86+
{
87+
//_buffer = (uint8_t *)malloc(_size);
88+
}
89+
90+
~WiFiClientRxBuffer()
91+
{
92+
free(_buffer);
93+
}
94+
95+
bool failed(){
96+
return _failed;
97+
}
98+
99+
int read(uint8_t * dst, size_t len){
100+
if(!dst || !len || (_pos == _fill && !fillBuffer())){
101+
return -1;
102+
}
103+
size_t a = _fill - _pos;
104+
if(len <= a || ((len - a) <= (_size - _fill) && fillBuffer() >= (len - a))){
105+
if(len == 1){
106+
*dst = _buffer[_pos];
107+
} else {
108+
memcpy(dst, _buffer + _pos, len);
109+
}
110+
_pos += len;
111+
return len;
112+
}
113+
size_t left = len;
114+
size_t toRead = a;
115+
uint8_t * buf = dst;
116+
memcpy(buf, _buffer + _pos, toRead);
117+
_pos += toRead;
118+
left -= toRead;
119+
buf += toRead;
120+
while(left){
121+
if(!fillBuffer()){
122+
return len - left;
123+
}
124+
a = _fill - _pos;
125+
toRead = (a > left)?left:a;
126+
memcpy(buf, _buffer + _pos, toRead);
127+
_pos += toRead;
128+
left -= toRead;
129+
buf += toRead;
130+
}
131+
return len;
132+
}
133+
134+
int peek(){
135+
if(_pos == _fill && !fillBuffer()){
136+
return -1;
137+
}
138+
return _buffer[_pos];
139+
}
140+
141+
size_t available(){
142+
return _fill - _pos + r_available();
143+
}
144+
};
145+
33146
class WiFiClientSocketHandle {
34147
private:
35148
int sockfd;
@@ -57,6 +170,7 @@ WiFiClient::WiFiClient():_connected(false),next(NULL)
57170
WiFiClient::WiFiClient(int fd):_connected(true),next(NULL)
58171
{
59172
clientSocketHandle.reset(new WiFiClientSocketHandle(fd));
173+
_rxBuffer.reset(new WiFiClientRxBuffer(fd));
60174
}
61175

62176
WiFiClient::~WiFiClient()
@@ -68,13 +182,15 @@ WiFiClient & WiFiClient::operator=(const WiFiClient &other)
68182
{
69183
stop();
70184
clientSocketHandle = other.clientSocketHandle;
185+
_rxBuffer = other._rxBuffer;
71186
_connected = other._connected;
72187
return *this;
73188
}
74189

75190
void WiFiClient::stop()
76191
{
77192
clientSocketHandle = NULL;
193+
_rxBuffer = NULL;
78194
_connected = false;
79195
}
80196

@@ -99,18 +215,17 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
99215
return 0;
100216
}
101217
clientSocketHandle.reset(new WiFiClientSocketHandle(sockfd));
218+
_rxBuffer.reset(new WiFiClientRxBuffer(sockfd));
102219
_connected = true;
103220
return 1;
104221
}
105222

106223
int WiFiClient::connect(const char *host, uint16_t port)
107224
{
108-
struct hostent *server;
109-
server = gethostbyname(host);
110-
if (server == NULL) {
225+
IPAddress srv((uint32_t)0);
226+
if(!WiFiGenericClass::hostByName(host, srv)){
111227
return 0;
112228
}
113-
IPAddress srv((const uint8_t *)(server->h_addr));
114229
return connect(srv, port);
115230
}
116231

@@ -241,13 +356,29 @@ size_t WiFiClient::write_P(PGM_P buf, size_t size)
241356
return write(buf, size);
242357
}
243358

244-
int WiFiClient::read(uint8_t *buf, size_t size)
359+
size_t WiFiClient::write(Stream &stream)
245360
{
246-
if(!available()) {
247-
return -1;
361+
uint8_t * buf = (uint8_t *)malloc(1360);
362+
if(!buf){
363+
return 0;
248364
}
249-
int res = recv(fd(), buf, size, MSG_DONTWAIT);
250-
if(res < 0 && errno != EWOULDBLOCK) {
365+
size_t toRead = 0, toWrite = 0, written = 0;
366+
size_t available = stream.available();
367+
while(available){
368+
toRead = (available > 1360)?1360:available;
369+
toWrite = stream.readBytes(buf, toRead);
370+
written += write(buf, toWrite);
371+
available = stream.available();
372+
}
373+
free(buf);
374+
return written;
375+
}
376+
377+
int WiFiClient::read(uint8_t *buf, size_t size)
378+
{
379+
int res = -1;
380+
res = _rxBuffer->read(buf, size);
381+
if(_rxBuffer->failed()) {
251382
log_e("%d", errno);
252383
stop();
253384
}
@@ -256,31 +387,25 @@ int WiFiClient::read(uint8_t *buf, size_t size)
256387

257388
int WiFiClient::peek()
258389
{
259-
if(!available()) {
260-
return -1;
261-
}
262-
uint8_t data = 0;
263-
int res = recv(fd(), &data, 1, MSG_PEEK);
264-
if(res < 0 && errno != EWOULDBLOCK) {
390+
int res = _rxBuffer->peek();
391+
if(_rxBuffer->failed()) {
265392
log_e("%d", errno);
266393
stop();
267394
}
268-
return data;
395+
return res;
269396
}
270397

271398
int WiFiClient::available()
272399
{
273400
if(!_connected) {
274401
return 0;
275402
}
276-
int count;
277-
int res = lwip_ioctl_r(fd(), FIONREAD, &count);
278-
if(res < 0) {
403+
int res = _rxBuffer->available();
404+
if(_rxBuffer->failed()) {
279405
log_e("%d", errno);
280406
stop();
281-
return 0;
282407
}
283-
return count;
408+
return res;
284409
}
285410

286411
// Though flushing means to send all pending data,
@@ -313,22 +438,9 @@ uint8_t WiFiClient::connected()
313438
if (_connected) {
314439
uint8_t dummy;
315440
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);
316-
if (res <= 0) {
317-
switch (errno) {
318-
case ENOTCONN:
319-
case EPIPE:
320-
case ECONNRESET:
321-
case ECONNREFUSED:
322-
case ECONNABORTED:
323-
_connected = false;
324-
break;
325-
default:
326-
_connected = true;
327-
break;
328-
}
329-
}
330-
else {
331-
_connected = true;
441+
if (res <= 0 && errno != EWOULDBLOCK) {
442+
_connected = false;
443+
log_i("Disconnected: RES: %d, ERR: %d", res, errno);
332444
}
333445
}
334446
return _connected;

0 commit comments

Comments
 (0)