Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

libraries/SocketWrapper: Fix agrs and improve error handling.#238

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
pennam merged 2 commits intoarduino:mainfromiabdalkader:fix_sockets
Nov 5, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
41 changes: 24 additions & 17 deletionslibraries/SocketWrapper/SocketWrapper.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,8 +30,8 @@ class ZephyrSocketWrapper {
bool connect(const char *host, uint16_t port) {

// Resolve address
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo hints = {0};
struct addrinfo *res = nullptr;
bool rv = true;

hints.ai_family = AF_INET;
Expand DownExpand Up@@ -102,24 +102,27 @@ class ZephyrSocketWrapper {
}

#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
bool connectSSL(const char *host, uint16_t port, char *ca_certificate_pem = nullptr) {
bool connectSSL(const char *host, uint16_t port,constchar *ca_certificate_pem = nullptr) {

// Resolve address
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo hints = {0};
struct addrinfo *res = nullptr;

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

int resolve_attempts = 100;
int ret;
bool rv =true;
bool rv =false;

sec_tag_t sec_tag_opt[] = {
CA_CERTIFICATE_TAG,
};

uint32_t timeo_optval = 100;
struct timeval timeout_opt = {
.tv_sec = 0,
.tv_usec = 100000,
};

while (resolve_attempts--) {
ret = getaddrinfo(host, String(port).c_str(), &hints, &res);
Expand All@@ -132,33 +135,33 @@ class ZephyrSocketWrapper {
}

if (ret != 0) {
rv = false;
goto exit;
}

if (ca_certificate_pem != nullptr) {
ret = tls_credential_add(CA_CERTIFICATE_TAG, TLS_CREDENTIAL_CA_CERTIFICATE,
ca_certificate_pem, strlen(ca_certificate_pem) + 1);
if (ret != 0) {
goto exit;
}
}

sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
if (sock_fd < 0) {
rv = false;
goto exit;
}

setsockopt(sock_fd, SOL_TLS,TLS_SEC_TAG_LIST, sec_tag_opt, sizeof(sec_tag_opt));

setsockopt(sock_fd,SOL_TLS, TLS_HOSTNAME, host, strlen(host));

setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_optval, sizeof(timeo_optval));
if (setsockopt(sock_fd, SOL_TLS,TLS_HOSTNAME, host, strlen(host)) ||
setsockopt(sock_fd, SOL_TLS, TLS_SEC_TAG_LIST, sec_tag_opt, sizeof(sec_tag_opt)) ||
setsockopt(sock_fd,SOL_SOCKET, SO_RCVTIMEO, &timeout_opt, sizeof(timeout_opt))) {
goto exit;
}

if (::connect(sock_fd, res->ai_addr, res->ai_addrlen) < 0) {
::close(sock_fd);
sock_fd = -1;
rv = false;
goto exit;
}

rv = true;
is_ssl = true;

exit:
Expand All@@ -167,6 +170,10 @@ class ZephyrSocketWrapper {
res = nullptr;
}

if (!rv && sock_fd >= 0) {
::close(sock_fd);
sock_fd = -1;
}
return rv;
}
#endif
Expand Down
4 changes: 2 additions & 2 deletionslibraries/SocketWrapper/ZephyrClient.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,8 +32,8 @@ class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {
return ret;
}
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
int connectSSL(const char *host, uint16_t port, char *cert) {
auto ret = ZephyrSocketWrapper::connectSSL((char *)host, port, cert);
int connectSSL(const char *host, uint16_t port,constchar *cert) {
auto ret = ZephyrSocketWrapper::connectSSL(host, port, cert);
if (ret) {
_connected = true;
}
Expand Down
2 changes: 1 addition & 1 deletionlibraries/SocketWrapper/ZephyrSSLClient.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,7 @@ class ZephyrSSLClient : public ZephyrClient {
return connectSSL(host, port, nullptr);
}

int connect(const char *host, uint16_t port, char *cert) {
int connect(const char *host, uint16_t port,constchar *cert) {
return connectSSL(host, port, cert);
}
};
Expand Down
116 changes: 116 additions & 0 deletionslibraries/WiFi/examples/WiFiWebClientTLS/WiFiWebClientTLS.ino
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
/*
Web client with single CA certificate test

This sketch demonstrates how to load your own TLS CA certificates
and use them to establish a secure connection to a remote server,
such as www.howsmyssl.com.
*/

#include <WiFi.h>
#include <ZephyrSSLClient.h>
#include "arduino_secrets.h"

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

char server[] = "www.howsmyssl.com";
int port = 443;

ZephyrSSLClient client;
int status = WL_IDLE_STATUS;

// ISRG Root X1 (Let's Encrypt root CA certificate)
const char isrg_root_x1[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n"
"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n"
"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n"
"WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n"
"ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n"
"MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP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ByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5\n"
"ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur\n"
"TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n"
"jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n"
"oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n"
"4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n"
"mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n"
"emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n"
"-----END CERTIFICATE-----\n";

void setup() {
Serial.begin(115200);
while (!Serial) {
;
}

// Check for WiFi module
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("Communication with WiFi module failed!");
return;
}

// Connect to WiFi
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(3000);
}

Serial.println("Connected to wifi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());

Serial.println("\nStarting connection to server...");

if (client.connect(server, port, isrg_root_x1)) {
Serial.println("Connected to server!");

// Make HTTP request
client.println("GET /a/check HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
} else {
Serial.println("Connection failed!");
}
}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();

// do nothing forevermore:
while (true)
;
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""

[8]ページ先頭

©2009-2025 Movatter.jp