Skip to content

Commit 5896e25

Browse files
Merge branch 'feat/bind_interface' into 'master'
feat: Add option to bind interface of use See merge request espressif/esp-mqtt!179
2 parents 63cfec7 + 363fbf7 commit 5896e25

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

include/mqtt_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ typedef struct esp_mqtt_client_config_t {
334334
bool disable_auto_reconnect; /*!< Client will reconnect to server (when errors/disconnect). Set
335335
`disable_auto_reconnect=true` to disable */
336336
esp_transport_handle_t transport; /*!< Custom transport handle to use. Warning: The transport should be valid during the client lifetime and is destroyed when esp_mqtt_client_destroy is called. */
337+
struct ifreq * if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
337338
} network; /*!< Network configuration */
338339
/**
339340
* Client task configuration

lib/include/mqtt_client_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ typedef struct {
9191
int message_retransmit_timeout;
9292
uint64_t outbox_limit;
9393
esp_transport_handle_t transport;
94+
struct ifreq * if_name;
9495
} mqtt_config_storage_t;
9596

9697
typedef enum {

mqtt_client.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdint.h>
2+
#include <stdlib.h>
23
#include "esp_err.h"
34
#include "esp_log.h"
45
#include "esp_heap_caps.h"
@@ -303,6 +304,9 @@ static esp_err_t esp_mqtt_client_create_transport(esp_mqtt_client_handle_t clien
303304
esp_transport_handle_t tcp = esp_transport_tcp_init();
304305
ESP_MEM_CHECK(TAG, tcp, return ESP_ERR_NO_MEM);
305306
esp_transport_set_default_port(tcp, MQTT_TCP_DEFAULT_PORT);
307+
if (client->config->if_name) {
308+
esp_transport_tcp_set_interface_name(tcp, client->config->if_name);
309+
}
306310
esp_transport_list_add(client->transport_list, tcp, MQTT_OVER_TCP_SCHEME);
307311
if (strncasecmp(client->config->scheme, MQTT_OVER_WS_SCHEME, sizeof(MQTT_OVER_WS_SCHEME)) == 0) {
308312
#if MQTT_ENABLE_WS
@@ -326,6 +330,9 @@ static esp_err_t esp_mqtt_client_create_transport(esp_mqtt_client_handle_t clien
326330
esp_transport_handle_t ssl = esp_transport_ssl_init();
327331
ESP_MEM_CHECK(TAG, ssl, return ESP_ERR_NO_MEM);
328332
esp_transport_set_default_port(ssl, MQTT_SSL_DEFAULT_PORT);
333+
if (client->config->if_name) {
334+
esp_transport_ssl_set_interface_name(ssl, client->config->if_name);
335+
}
329336
esp_transport_list_add(client->transport_list, ssl, MQTT_OVER_SSL_SCHEME);
330337
if (strncasecmp(client->config->scheme, MQTT_OVER_WSS_SCHEME, sizeof(MQTT_OVER_WSS_SCHEME)) == 0) {
331338
#if MQTT_ENABLE_WS
@@ -487,6 +494,15 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl
487494
} else {
488495
client->config->reconnect_timeout_ms = MQTT_RECON_DEFAULT_MS;
489496
}
497+
if (config->network.transport) {
498+
client->config->transport = config->network.transport;
499+
}
500+
501+
if (config->network.if_name) {
502+
client->config->if_name = calloc(1, sizeof(struct ifreq) + 1);
503+
ESP_MEM_CHECK(TAG, client->config->if_name, goto _mqtt_set_config_failed);
504+
memcpy(client->config->if_name, config->network.if_name, sizeof(struct ifreq));
505+
}
490506

491507
if (config->broker.verification.alpn_protos) {
492508
for (int i = 0; i < client->config->num_alpn_protos; i++) {

0 commit comments

Comments
 (0)