From 24836ebeeeea10e70252360ddb8dc5f00686c4b1 Mon Sep 17 00:00:00 2001 From: Joel Guittet Date: Wed, 27 Sep 2023 00:16:44 +0200 Subject: [PATCH 1/2] cmake: retrieve mender-mcu-client version --- esp-idf/CMakeLists.txt | 4 ++++ zephyr/CMakeLists.txt | 2 ++ 2 files changed, 6 insertions(+) diff --git a/esp-idf/CMakeLists.txt b/esp-idf/CMakeLists.txt index bb706dd..39c8c07 100755 --- a/esp-idf/CMakeLists.txt +++ b/esp-idf/CMakeLists.txt @@ -62,3 +62,7 @@ idf_component_register( if (CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT) idf_component_optional_requires(PRIVATE espressif__esp_websocket_client esp_event msgpack-c) endif() + +# Retrieve mender-mcu-client version +file (STRINGS "${CMAKE_CURRENT_LIST_DIR}/../VERSION" MENDER_CLIENT_VERSION) +add_definitions("-DMENDER_CLIENT_VERSION=\"${MENDER_CLIENT_VERSION}\"") diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 08bfc73..f741905 100755 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -51,4 +51,6 @@ if(CONFIG_MENDER_MCU_CLIENT) "${CMAKE_CURRENT_LIST_DIR}/../platform/net/zephyr/src/mender-websocket.c" "${CMAKE_CURRENT_LIST_DIR}/../platform/board/zephyr/src/mender-shell.c" ) + file (STRINGS "${CMAKE_CURRENT_LIST_DIR}/../VERSION" MENDER_CLIENT_VERSION) + add_definitions("-DMENDER_CLIENT_VERSION=\"${MENDER_CLIENT_VERSION}\"") endif() From be5e5a10c46ac36e4d56476a54f80241f87afb9c Mon Sep 17 00:00:00 2001 From: Joel Guittet Date: Wed, 27 Sep 2023 00:16:55 +0200 Subject: [PATCH 2/2] platform: introduce http user-agent header --- platform/net/curl/src/mender-http.c | 14 ++++++++++++++ platform/net/curl/src/mender-websocket.c | 14 ++++++++++++++ platform/net/esp-idf/src/mender-http.c | 8 +++++++- platform/net/esp-idf/src/mender-websocket.c | 8 +++++++- platform/net/zephyr/src/mender-http.c | 15 ++++++++++++++- platform/net/zephyr/src/mender-websocket.c | 17 +++++++++++++++-- 6 files changed, 71 insertions(+), 5 deletions(-) mode change 100755 => 100644 platform/net/esp-idf/src/mender-http.c diff --git a/platform/net/curl/src/mender-http.c b/platform/net/curl/src/mender-http.c index c252cf6..6d263be 100644 --- a/platform/net/curl/src/mender-http.c +++ b/platform/net/curl/src/mender-http.c @@ -30,6 +30,15 @@ #include "mender-log.h" #include "mender-utils.h" +/** + * @brief HTTP User-Agent + */ +#ifdef MENDER_CLIENT_VERSION +#define MENDER_HTTP_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-http) curl/" LIBCURL_VERSION +#else +#define MENDER_HTTP_USER_AGENT "mender-mcu-client (mender-http) curl/" LIBCURL_VERSION +#endif /* MENDER_CLIENT_VERSION */ + /** * @brief User data */ @@ -123,6 +132,11 @@ mender_http_perform(char * jwt, ret = MENDER_FAIL; goto END; } + if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_USERAGENT, MENDER_HTTP_USER_AGENT))) { + mender_log_error("Unable to set HTTP User-Agent: %s", curl_easy_strerror(err)); + ret = MENDER_FAIL; + goto END; + } if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2))) { mender_log_error("Unable to set TLSv1.2: %s", curl_easy_strerror(err)); ret = MENDER_FAIL; diff --git a/platform/net/curl/src/mender-websocket.c b/platform/net/curl/src/mender-websocket.c index a2d55c1..b5124dc 100644 --- a/platform/net/curl/src/mender-websocket.c +++ b/platform/net/curl/src/mender-websocket.c @@ -45,6 +45,15 @@ #define CONFIG_MENDER_WEBSOCKET_THREAD_PRIORITY (0) #endif /* CONFIG_MENDER_WEBSOCKET_THREAD_PRIORITY */ +/** + * @brief WebSocket User-Agent + */ +#ifdef MENDER_CLIENT_VERSION +#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-websocket) curl/" LIBCURL_VERSION +#else +#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client (mender-websocket) curl/" LIBCURL_VERSION +#endif /* MENDER_CLIENT_VERSION */ + /** * @brief Websocket handle */ @@ -165,6 +174,11 @@ mender_websocket_connect( ret = MENDER_FAIL; goto FAIL; } + if (CURLE_OK != (err_curl = curl_easy_setopt(((mender_websocket_handle_t *)*handle)->client, CURLOPT_USERAGENT, MENDER_WEBSOCKET_USER_AGENT))) { + mender_log_error("Unable to set HTTP User-Agent: %s", curl_easy_strerror(err_curl)); + ret = MENDER_FAIL; + goto END; + } if (CURLE_OK != (err_curl = curl_easy_setopt(((mender_websocket_handle_t *)*handle)->client, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2))) { mender_log_error("Unable to set TLSv1.2: %s", curl_easy_strerror(err_curl)); ret = MENDER_FAIL; diff --git a/platform/net/esp-idf/src/mender-http.c b/platform/net/esp-idf/src/mender-http.c old mode 100755 new mode 100644 index 45d8237..c289f00 --- a/platform/net/esp-idf/src/mender-http.c +++ b/platform/net/esp-idf/src/mender-http.c @@ -32,6 +32,11 @@ #include "mender-log.h" #include "mender-utils.h" +/** + * @brief HTTP User-Agent + */ +#define MENDER_HTTP_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-http) esp-idf/" IDF_VER + /** * @brief Mender HTTP configuration */ @@ -86,7 +91,8 @@ mender_http_perform(char * jwt, } /* Configuration of the client */ - esp_http_client_config_t config = { .url = (NULL != url) ? url : path, .crt_bundle_attach = esp_crt_bundle_attach, .buffer_size_tx = 2048 }; + esp_http_client_config_t config + = { .url = (NULL != url) ? url : path, .user_agent = MENDER_HTTP_USER_AGENT, .crt_bundle_attach = esp_crt_bundle_attach, .buffer_size_tx = 2048 }; /* Initialization of the client */ if (NULL == (client = esp_http_client_init(&config))) { diff --git a/platform/net/esp-idf/src/mender-websocket.c b/platform/net/esp-idf/src/mender-websocket.c index 9fbc7c6..0fdbf4d 100644 --- a/platform/net/esp-idf/src/mender-websocket.c +++ b/platform/net/esp-idf/src/mender-websocket.c @@ -60,6 +60,11 @@ #define CONFIG_MENDER_WEBSOCKET_PING_INTERVAL (60) #endif /* CONFIG_MENDER_WEBSOCKET_PING_INTERVAL */ +/** + * @brief WebSocket User-Agent + */ +#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-websocket) esp-idf/" IDF_VER + /** * @brief Websocket handle */ @@ -143,6 +148,7 @@ mender_websocket_connect( /* Configuration of the client */ esp_websocket_client_config_t config = { .uri = (NULL != url) ? url : path, + .user_agent = MENDER_WEBSOCKET_USER_AGENT, .crt_bundle_attach = esp_crt_bundle_attach, .reconnect_timeout_ms = CONFIG_MENDER_WEBSOCKET_RECONNECT_TIMEOUT, .network_timeout_ms = CONFIG_MENDER_WEBSOCKET_NETWORK_TIMEOUT, @@ -309,4 +315,4 @@ mender_websocket_event_handler(void *handler_args, esp_event_base_t base, int32_ mender_log_error("Unknown event occurred (event_id=%d)", event_id); break; } -} \ No newline at end of file +} diff --git a/platform/net/zephyr/src/mender-http.c b/platform/net/zephyr/src/mender-http.c index d82e173..ff8ce53 100644 --- a/platform/net/zephyr/src/mender-http.c +++ b/platform/net/zephyr/src/mender-http.c @@ -25,11 +25,17 @@ * SOFTWARE. */ +#include #include #include "mender-http.h" #include "mender-log.h" #include "mender-net.h" +/** + * @brief HTTP User-Agent + */ +#define MENDER_HTTP_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-http) zephyr/" KERNEL_VERSION_STRING + /** * @brief Receive buffer length */ @@ -97,7 +103,7 @@ mender_http_perform(char * jwt, mender_err_t ret; struct http_request request; mender_http_request_context request_context; - char * header_fields[4] = { NULL, NULL, NULL, NULL }; + char * header_fields[5] = { NULL, NULL, NULL, NULL, NULL }; size_t header_index = 0; char * host = NULL; char * port = NULL; @@ -132,6 +138,13 @@ mender_http_perform(char * jwt, goto END; } request.recv_buf_len = MENDER_HTTP_RECV_BUF_LENGTH; + if (NULL == (header_fields[header_index] = malloc(strlen("User-Agent: ") + strlen(MENDER_HTTP_USER_AGENT) + strlen("\r\n") + 1))) { + mender_log_error("Unable to allocate memory"); + ret = MENDER_FAIL; + goto END; + } + sprintf(header_fields[header_index], "User-Agent: %s\r\n", MENDER_HTTP_USER_AGENT); + header_index++; if (NULL != jwt) { if (NULL == (header_fields[header_index] = (char *)malloc(strlen("Authorization: Bearer ") + strlen(jwt) + strlen("\r\n") + 1))) { mender_log_error("Unable to allocate memory"); diff --git a/platform/net/zephyr/src/mender-websocket.c b/platform/net/zephyr/src/mender-websocket.c index 09048ad..54ad44c 100644 --- a/platform/net/zephyr/src/mender-websocket.c +++ b/platform/net/zephyr/src/mender-websocket.c @@ -25,9 +25,10 @@ * SOFTWARE. */ +#include +#include #include #include -#include #include "mender-log.h" #include "mender-net.h" #include "mender-websocket.h" @@ -67,6 +68,11 @@ #define CONFIG_MENDER_WEBSOCKET_REQUEST_TIMEOUT (3000) #endif /* CONFIG_MENDER_WEBSOCKET_REQUEST_TIMEOUT */ +/** + * @brief WebSocket User-Agent + */ +#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-websocket) zephyr/" KERNEL_VERSION_STRING + /** * @brief Websocket receive buffer length */ @@ -125,7 +131,7 @@ mender_websocket_connect( assert(NULL != handle); mender_err_t ret; struct websocket_request request; - char * header_fields[2] = { NULL, NULL }; + char * header_fields[3] = { NULL, NULL, NULL }; size_t header_index = 0; char * host = NULL; char * port = NULL; @@ -162,6 +168,13 @@ mender_websocket_connect( goto FAIL; } request.tmp_buf_len = MENDER_WEBSOCKET_RECV_BUF_LENGTH; + if (NULL == (header_fields[header_index] = malloc(strlen("User-Agent: ") + strlen(MENDER_WEBSOCKET_USER_AGENT) + strlen("\r\n") + 1))) { + mender_log_error("Unable to allocate memory"); + ret = MENDER_FAIL; + goto FAIL; + } + sprintf(header_fields[header_index], "User-Agent: %s\r\n", MENDER_WEBSOCKET_USER_AGENT); + header_index++; if (NULL != jwt) { if (NULL == (header_fields[header_index] = malloc(strlen("Authorization: Bearer ") + strlen(jwt) + strlen("\r\n") + 1))) { mender_log_error("Unable to allocate memory");