govorox / SSLClient

SSLClient - generic secure client Arduino library using mbedtls
GNU General Public License v3.0
85 stars 39 forks source link

client_net_send - Buffer issue when writing data larger than the receiving buffer #45

Closed deeja closed 1 year ago

deeja commented 1 year ago

https://github.com/govorox/SSLClient/blob/d7cd4afeb10b0ce1c7a8c6c440d9a2cfaf473310/src/ssl_client.cpp#L134

If the buffer is smaller than the data being written, then the call fails.

My fix (although ugly) is this

 // Sending data in chunks using a small buffer
    const int bufferSize = 1024; // Size of the buffer I'm writing to
    int result;
    for (int i = 0; i < len; i += bufferSize)
    {
        int bytesToWrite;

// `min` was conflicting, so rather than fix the conflict just do an if
        if (bufferSize > len - i)
        {
            bytesToWrite = len - i;
        }
        else
        {
            bytesToWrite = bufferSize;
        }

        // Create a new buffer for each chunk
        unsigned char buffer[bytesToWrite];
        memcpy(buffer, &buf[i], bytesToWrite);

        // Send the buffer to the client
        result = client->write(buffer, bytesToWrite);
        if (result == 0)
        {
            log_e("write failed");
            result = MBEDTLS_ERR_NET_SEND_FAILED;
            break;
        }
    }
RobertByrnes commented 1 year ago

Hi @deeja Thanks for submitting this. It would be great to see this turn into a pull request to have it added to the repo. But first, would you please share a bit more around the issue. What is the Hardware, the platform (e.g. Arduino / PlatformIO / ESP_IDF that you used. Was this the only code change you implemented in making a fix? What was the exact error that came up? It would helpful if you could share that.

deeja commented 1 year ago

Hey @RobertByrnes
Arduino on PlatformIO Hardware: Onboard Lilygo A7670 / SIM7000 (WROVER ESP32) The TinyGsm RX Buffer is 1024 bytes so I'm guessing the TX is the same? https://github.com/Xinyuan-LilyGO/T-A7670X/blob/69c6275bfc2d6c1e233e5c1c7cfea5dca57e29f4/examples/ATdebug/ATdebug.ino#L8 Would create a PR, but honestly my C++ isn't up to scratch.

Yes, that was the only change. I'll find the error when I get a moment to look at it again

RobertByrnes commented 1 year ago

Hi @deeja try this implementation of your fix please and let me know how you get on - fix

RobertByrnes commented 1 year ago

Pull Request #46

deeja commented 1 year ago

Hey I'll check it out when the new devices arrive in a couple of weeks

RobertByrnes commented 1 year ago

HI @deeja try pulling the branch again - have just updated. I get an SSL connection OK with this code now...

deeja commented 1 year ago

Yeah, that looks to be working now. No issues with the connection. Thanks!