Skip to content
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

ClientSecure.available() fix for connection closed by remote socket #9869

Merged

Conversation

JAndrassy
Copy link
Contributor

for connection closed by remote socket the NetworkClientSecure.available() method:

  1. logged an error.
  2. returned a negative value (the error code)

The error logging is skipped for error code returned in this case. This error code is skipped in ssl_client.cpp _handle_error too.

I tested it with modified WiFiClientInsecure example

#include <NetworkClientSecure.h>
#include <WiFi.h>

const char *ssid = "your-ssid";          // your network SSID (name of wifi network)
const char *password = "your-password";  // your network password

const char *server = "arduino.tips";  // Server URL

NetworkClientSecure client;

void setup() {
  Serial.begin(115200);
  delay(500);

  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.print("Connected to ");
  Serial.println(ssid);

  Serial.println("\nStarting connection to server...");
  client.setInsecure();  //skip verification
  if (!client.connect(server, 443)) {
    Serial.println("Connection failed!");
  } else {
    Serial.println("Connected to server!");
    client.println("GET /asciilogo.txt HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();

    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
        break;
      }
    }
    while (client.available()) {
      char c = client.read();
      Serial.write(c);
    }
    client.stop();
  }
}

void loop() {
  delay(1);
}

without the fix it logged

[  2572][E][NetworkClientSecure.cpp:308] available(): Closing connection on failed available check
[  2587][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
�

the setSocketOption error and the strange character were the result of read which was executed because available() didn't return a 0 but a negative value.

Copy link
Contributor

github-actions bot commented Jun 15, 2024

Messages
📖 🎉 Good Job! All checks are passing!

👋 Hello JAndrassy, we appreciate your contribution to this project!


Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against 714e32c

Copy link
Contributor

github-actions bot commented Jun 15, 2024

Test Results

 56 files   56 suites   7m 6s ⏱️
 21 tests  18 ✅ 0 💤 3 ❌
135 runs  131 ✅ 0 💤 4 ❌

For more details on these failures, see this check.

Results for commit 714e32c.

♻️ This comment has been updated with latest results.

Copy link
Contributor

Memory usage test (comparing PR against master branch)

The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.

MemoryFLASH [bytes]FLASH [%]RAM [bytes]RAM [%]
TargetDECINCDECINCDECINCDECINC
ESP32S3000.000.00000.000.00
ESP32S2000.000.00000.000.00
ESP32C3000.000.00000.000.00
ESP32C6000.000.00000.000.00
ESP32000.000.00000.000.00
Click to expand the detailed deltas report [usage change in BYTES]
TargetESP32S3ESP32S2ESP32C3ESP32C6ESP32
ExampleFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAM
NetworkClientSecure/examples/WiFiClientInsecure----------
NetworkClientSecure/examples/WiFiClientPSK----------
NetworkClientSecure/examples/WiFiClientSecure----------
NetworkClientSecure/examples/WiFiClientSecureEnterprise----------
NetworkClientSecure/examples/WiFiClientSecureProtocolUpgrade----------
NetworkClientSecure/examples/WiFiClientShowPeerCredentials----------
NetworkClientSecure/examples/WiFiClientTrustOnFirstUse----------

@me-no-dev
Copy link
Member

NetworkClientSecure::available() returns int, so is it wrong to return negative value?

@JAndrassy
Copy link
Contributor Author

JAndrassy commented Jun 17, 2024

https://www.arduino.cc/reference/en/language/functions/communication/stream/streamavailable/

Returns The number of bytes available to read.

many test it as boolean, as does the example from the NetworkClientSecure library used to test this PR

@JAndrassy JAndrassy force-pushed the clientsecure_remote_closed_fix branch from 49b4d21 to 714e32c Compare June 17, 2024 08:20
@VojtechBartoska VojtechBartoska added the Area: WiFi Issue related to WiFi label Jun 17, 2024
@VojtechBartoska VojtechBartoska added the Status: Review needed Issue or PR is awaiting review label Jun 17, 2024
@me-no-dev me-no-dev added Status: Pending Merge Pull Request is ready to be merged and removed Status: Review needed Issue or PR is awaiting review labels Jun 17, 2024
@me-no-dev me-no-dev merged commit 5c22402 into espressif:master Jun 17, 2024
111 of 117 checks passed
@JAndrassy JAndrassy deleted the clientsecure_remote_closed_fix branch June 18, 2024 04:54
@everslick
Copy link
Contributor

Will this be backported to 2.0.x also?

@me-no-dev
Copy link
Member

we have no such plans. IDF 4.4. goes EOL next month. You are welcome to add PR and we will merge it though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: WiFi Issue related to WiFi Status: Pending Merge Pull Request is ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants