Skip to content

feat(ESP8266): Add Max Fragment Length Negotiation for TLS connection #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Changelog
## Version 3.4.0 (in progres)
### Features
- [#88](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/88) - ESP8266 only - Added Max Fragment Length Negotiation for TLS communicaton to reduce memory allocation. If server supports MFLN, it saves ~15kB. Standalone InfluxDB OSS server doesn't support MFLN, Cloud yes. To leverage MFLN for standalone OSS, a reverse proxy needs to be used.

### Documentation
- [#87](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/87) - Fixed include file name in the Readme

## Version 3.3.0 (2020-07-07)
- [NEW] Added possibility skip server certification validation (`setInsecure()` method)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ InfluxDBCloud uses secure communication (https) and we need to tell the client t
Connection parameters are almost the same as above, the only difference is that server URL now points to the InfluxDB Cloud 2, where you've got after you've finished creating InfluxDB Cloud 2 subscription. You will find correct server URL in `InfluxDB UI -> Load Data -> Client Libraries`.
```cpp
//Include also InfluxClould 2 CA certificate
#include <InfluxCloud.h>
#include <InfluxDbCloud.h>
// InfluxDB 2 server or cloud url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries)
#define INFLUXDB_URL "influxdb-url"
// InfluxDB 2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
Expand Down
55 changes: 50 additions & 5 deletions src/InfluxDbClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ static const char RetryAfter[] = "Retry-After";
static const char TransferEnconding[] = "Transfer-Encoding";

static String escapeJSONString(String &value);

#if defined(ESP8266)
bool checkMFLN(BearSSL::WiFiClientSecure *client, String url);
#endif
static String precisionToString(WritePrecision precision, uint8_t version = 2) {
switch(precision) {
case WritePrecision::US:
Expand Down Expand Up @@ -220,6 +222,7 @@ bool InfluxDBClient::init() {
if (_insecure) {
wifiClientSec->setInsecure();
}
checkMFLN(wifiClientSec, _serverUrl);
#elif defined(ESP32)
WiFiClientSecure *wifiClientSec = new WiFiClientSecure;
if(_certInfo && strlen_P(_certInfo) > 0) {
Expand All @@ -236,6 +239,52 @@ bool InfluxDBClient::init() {
return true;
}

// parse URL for host and port and call probeMaxFragmentLength
#if defined(ESP8266)
bool checkMFLN(BearSSL::WiFiClientSecure *client, String url) {
int index = url.indexOf(':');
if(index < 0) {
return false;
}
String protocol = url.substring(0, index);
int port = -1;
url.remove(0, (index + 3)); // remove http:// or https://

if (protocol == "http") {
// set default port for 'http'
port = 80;
} else if (protocol == "https") {
// set default port for 'https'
port = 443;
} else {
return false;
}
index = url.indexOf('/');
String host = url.substring(0, index);
url.remove(0, index); // remove host
// check Authorization
index = host.indexOf('@');
if(index >= 0) {
host.remove(0, index + 1); // remove auth part including @
}
// get port
index = host.indexOf(':');
if(index >= 0) {
String portS = host;
host = host.substring(0, index); // hostname
portS.remove(0, (index + 1)); // remove hostname + :
port = portS.toInt(); // get port
}
INFLUXDB_CLIENT_DEBUG("probeMaxFragmentLength to %s:%d\n", host.c_str(), port);
bool mfln = client->probeMaxFragmentLength(host, port, 1024);
INFLUXDB_CLIENT_DEBUG(" MFLN:%s\n", mfln ? "yes" : "no");
if (mfln) {
client->setBufferSizes(1024, 1024);
}
return mfln;
}
#endif //ESP8266

InfluxDBClient::~InfluxDBClient() {
if(_pointsBuffer) {
delete [] _pointsBuffer;
Expand All @@ -248,10 +297,6 @@ InfluxDBClient::~InfluxDBClient() {
}

void InfluxDBClient::clean() {
// if(_wifiClient) {
// delete _wifiClient;
// _wifiClient = nullptr;
// }
_wifiClient = nullptr;
#if defined(ESP8266)
if(_cert) {
Expand Down