8000 first step for SSL (wss) support · robokoding/arduinoWebSockets@093797a · GitHub
[go: up one dir, main page]

Skip to content

Commit 093797a

Browse files
committed
first step for SSL (wss) support
1 parent 7a22dad commit 093797a

File tree

5 files changed

+204
-71
lines changed

5 files changed

+204
-71
lines changed

src/WebSockets.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * rea
7474
*/
7575
void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool mask, bool fin, bool headerToPayload) {
7676

77-
if(!client->tcp.connected()) {
77+
if(client->tcp && !client->tcp->connected()) {
7878
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] not Connected!?\n", client->num);
7979
return;
8080
}
@@ -171,14 +171,14 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
171171
// header has be added to payload
172172
// payload is forced to reserved 14 Byte but we may not need all based on the length and mask settings
173173
// offset in payload is calculatetd 14 - headerSize
174-
client->tcp.write(&payload[(14 - headerSize)], (length + headerSize));
174+
client->tcp->write(&payload[(14 - headerSize)], (length + headerSize));
175175
} else {
176176
// send header
177-
client->tcp.write(&buffer[0], headerSize);
177+
client->tcp->write(&buffer[0], headerSize);
178178

179179
if(payload && length > 0) {
180180
// send payload
181-
client->tcp.write(&payload[0], length);
181+
client->tcp->write(&payload[0], length);
182182
}
183183
}
184184
}
@@ -385,7 +385,7 @@ bool WebSockets::readWait(WSclient_t * client, uint8_t *out, size_t n) {
385385
size_t len;
386386

387387
while(n > 0) {
388-
if(!client->tcp.connected()) {
388+
if(client->tcp && !client->tcp->connected()) {
389389
DEBUG_WEBSOCKETS("[readWait] not connected!\n");
390390
return false;
391391
}
@@ -395,14 +395,14 @@ bool WebSockets::readWait(WSclient_t * client, uint8_t *out, size_t n) {
395395
return false;
396396
}
397397

398-
if(!client->tcp.available()) {
398+
if(!client->tcp->available()) {
399399
#ifdef ESP8266
400400
delay(0);
401401
#endif
402402
continue;
403403
}
404404

405-
len = client->tcp.read((uint8_t*) out, n);
405+
len = client->tcp->read((uint8_t*) out, n);
406406
if(len) {
407407
t = millis();
408408
out += len;

src/WebSockets.h

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,58 @@
2727

2828
#include <Arduino.h>
2929

30+
#define DEBUG_WEBSOCKETS(...) Serial1.printf( __VA_ARGS__ )
31+
32+
#ifndef DEBUG_WEBSOCKETS
33+
#define DEBUG_WEBSOCKETS(...)
34+
#endif
35+
3036
#ifdef ESP8266
31-
#include <ESP8266WiFi.h>
37+
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
3238
#else
33-
#include <UIPEthernet.h>
34-
#ifndef UIPETHERNET_H
35-
#include <Ethernet.h>
36-
#include <SPI.h>
39+
//atmega328p has only 2KB ram!
40+
#define WEBSOCKETS_MAX_DATA_SIZE (1024)
3741
#endif
42+
43+
#define WEBSOCKETS_TCP_TIMEOUT (1500)
44+
45+
#define NETWORK_ESP8266 (1)
46+
#define NETWORK_W5100 (2)
47+
#define NETWORK_ENC28J60 (3)
48+
49+
50+
// select Network type based
51+
#ifdef ESP8266
52+
#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266
53+
#else
54+
#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
3855
#endif
3956

40-
//#define DEBUG_WEBSOCKETS(...) Serial1.printf( __VA_ARGS__ )
4157

42-
#ifndef DEBUG_WEBSOCKETS
43-
#define DEBUG_WEBSOCKETS(...)
58+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
59+
60+
#ifndef ESP8266
61+
#error "network type ESP8266 only possible on the ESP mcu!"
62+
#endif
63+
64+
#include <ESP8266WiFi.h>
65+
#define WEBSOCKETS_NETWORK_CLASS WiFiClient
66+
67+
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
68+
69+
#include <Ethernet.h>
70+
#include <SPI.h>
71+
#define WEBSOCKETS_NETWORK_CLASS EthernetClient
72+
73+
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)
74+
75+
#include <UIPEthernet.h>
76+
#define WEBSOCKETS_NETWORK_CLASS UIPClient
77+
78+
#else
79+
#error "no network type selected!"
4480
#endif
4581

46-
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
47-
#define WEBSOCKETS_TCP_TIMEOUT (1500)
4882

4983
typedef enum {
5084
WSC_NOT_CONNECTED,
@@ -75,15 +109,14 @@ typedef struct {
75109
uint8_t num; ///< connection number
76110

77111
WSclientsStatus_t status;
78-
#ifdef ESP8266
79-
WiFiClient tcp;
80-
#else
81-
#ifdef UIPETHERNET_H
82-
UIPClient tcp;
83-
#else
84-
EthernetClient tcp;
85-
#endif
112+
113+
WEBSOCKETS_NETWORK_CLASS * tcp;
114+
115+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
116+
bool isSSL; ///< run in ssl mode
117+
WiFiClientSecure * ssl;
86118
#endif
119+
87120
String cUrl; ///< http url
88121
uint16_t cCode; ///< http code
89122

src/WebSocketsClient.cpp

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
WebSocketsClient::WebSocketsClient() {
2929
_cbEvent = NULL;
3030
_client.num = 0;
31-
3231
}
3332

3433
WebSocketsClient::~WebSocketsClient() {
@@ -42,7 +41,13 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url)
4241
_host = host;
4342
_port = port;
4443

44+
_client.num = 0;
4545
_client.status = WSC_NOT_CONNECTED;
46+
_client.tcp = NULL;
47+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
48+
_client.isSSL = false;
49+
_client.ssl = NULL;
50+
#endif
4651
_client.cUrl = url;
4752
_client.cCode = 0;
4853
_client.cIsUpgrade = false;
@@ -53,8 +58,12 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url)
5358
_client.cExtensions = "";
5459
_client.cVersion = 0;
5560

61+
#ifdef ESP8266
62+
randomSeed(RANDOM_REG32);
63+
#else
5664
// todo find better seed
5765
randomSeed(millis());
66+
#endif
5867
}
5968

6069
void WebSocketsClient::begin(String host, uint16_t port, String url) {
@@ -66,16 +75,44 @@ void WebSocketsClient::begin(String host, uint16_t port, String url) {
6675
*/
6776
void WebSocketsClient::loop(void) {
6877
if(!clientIsConnected(&_client)) {
69-
if(_client.tcp.connect(_host.c_str(), _port)) {
78+
79+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
80+
if(_client.isSSL) {
81+
DEBUG_WEBSOCKETS("[WS-Client] connect wss...\n");
82+
if(_client.ssl) {
83+
delete _client.ssl;
84+
_client.ssl = NULL;
85+
_client.tcp = NULL;
86+
}
87+
_client.ssl = new WiFiClientSecure();
88+
_client.tcp = _client.ssl;
89+
} else {
90+
DEBUG_WEBSOCKETS("[WS-Client] connect ws...\n");
91+
if(_client.tcp) {
92+
delete _client.tcp;
93+
_client.tcp = NULL;
94+
}
95+
_client.tcp = new WiFiClient();
96+
}
97+
#else
98+
_client.tcp = new WEBSOCKETS_NETWORK_CLASS();
99+
#endif
100+
101+
if(!_client.tcp) {
102+
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!");
103+
return;
104+
}
105+
106+
if(_client.tcp->connect(_host.c_str(), _port)) {
70107
DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port);
71108

72109
_client.status = WSC_HEADER;
73110

74111
// set Timeout for readBytesUntil and readStringUntil
75-
_client.tcp.setTimeout(WEBSOCKETS_TCP_TIMEOUT);
112+
_client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
76113

77-
#ifdef ESP8266
78-
_client.tcp.setNoDelay(true);
114+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
115+
_client.tcp->setNoDelay(true);
79116
#endif
80117

81118
// send Header to Server
@@ -190,9 +227,25 @@ void WebSocketsClient::messageRecived(WSclient_t * client, WSopcode_t opcode, ui
190227
*/
191228
void WebSocketsClient::clientDisconnect(WSclient_t * client) {
192229

230+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
231+
if(client->isSSL && client->ssl) {
232+
if(client->ssl->connected()) {
233+
client->ssl->flush();
234+
client->ssl->stop();
235+
}
236+
delete client->ssl;
237+
client->ssl = NULL;
238+
client->tcp = NULL;
239+
}
240+
#endif
241+
193242
if(client->tcp) {
194-
client->tcp.flush();
195-
client->tcp.stop();
243+
if(client->tcp->connected()) {
244+
client->tcp->flush();
245+
client->tcp->stop();
246+
}
247+
delete client->tcp;
248+
client->tcp = NULL;
196249
}
197250

198251
client->cCode = 0;
@@ -218,7 +271,11 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
218271
*/
219272
bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
220273

221-
if(client->tcp.connected()) {
274+
if(!client->tcp) {
275+
return false;
276+
}
277+
278+
if(client->tcp->connected()) {
222279
if(client->status != WSC_NOT_CONNECTED) {
223280
return true;
224281
}
@@ -230,14 +287,20 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
230287
clientDisconnect(client);
231288
}
232289
}
290+
291+
if(client->tcp) {
292+
// do cleanup
293+
clientDisconnect(client);
294+
}
295+
233296
return false;
234297
}
235298

236299
/**
237300
* Handel incomming data from Client
238301
*/
239302
void WebSocketsClient::handleClientData(void) {
240-
int len = _client.tcp.available();
303+
int len = _client.tcp->available();
241304
if(len > 0) {
242305
switch(_client.status) {
243306
case WSC_HEADER:
@@ -289,7 +352,7 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
289352

290353
handshake += "\r\n";
291354

292-
client->tcp.write(handshake.c_str(), handshake.length());
355+
client->tcp->write(handshake.c_str(), handshake.length());
293356

294357
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%uus).\n", (micros() - start));
295358

@@ -301,7 +364,7 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
301364
*/
302365
void WebSocketsClient::handleHeader(WSclient_t * client) {
303366

304-
String headerLine = client->tcp.readStringUntil('\n');
367+
String headerLine = client->tcp->readStringUntil('\n');
305368
headerLine.trim(); // remove \r
306369

307370
if(headerLine.length() > 0) {
@@ -392,7 +455,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client) {
392455

393456
} else {
394457
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
395-
client->tcp.write("This is a webSocket client!");
458+
client->tcp->write("This is a webSocket client!");
396459
clientDisconnect(client);
397460
}
398461
}

0 commit comments

Comments
 (0)
0