Skip to content

Commit 35af68b

Browse files
rlubosdkalowsk
authored andcommitted
net: mqtt: Allow to bind client to a specific interface
Add a new "if_name" pointer to the transport configuration structure, allowing the application to bind MQTT client to a specific network interface. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent 0938ecc commit 35af68b

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

include/zephyr/net/mqtt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,11 @@ struct mqtt_transport {
809809
*/
810810
enum mqtt_transport_type type;
811811

812+
/** Name of the interface that the MQTT client instance should be bound to.
813+
* Leave as NULL if not specified.
814+
*/
815+
const char *if_name;
816+
812817
/** Use either unsecured TCP or secured TLS transport */
813818
union {
814819
/** TCP socket transport for MQTT */

subsys/net/lib/mqtt/mqtt_transport_socket_tcp.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ int mqtt_client_tcp_connect(struct mqtt_client *client)
2929
return -errno;
3030
}
3131

32+
NET_DBG("Created socket %d", client->transport.tcp.sock);
33+
34+
if (client->transport.if_name != NULL) {
35+
struct ifreq ifname = { 0 };
36+
37+
strncpy(ifname.ifr_name, client->transport.if_name,
38+
sizeof(ifname.ifr_name) - 1);
39+
40+
ret = zsock_setsockopt(client->transport.tcp.sock, SOL_SOCKET,
41+
SO_BINDTODEVICE, &ifname,
42+
sizeof(struct ifreq));
43+
if (ret < 0) {
44+
NET_ERR("Failed to bind ot interface %s error (%d)",
45+
ifname.ifr_name, -errno);
46+
goto error;
47+
}
48+
49+
NET_DBG("Bound to interface %s", ifname.ifr_name);
50+
}
51+
3252
#if defined(CONFIG_SOCKS)
3353
if (client->transport.proxy.addrlen != 0) {
3454
ret = setsockopt(client->transport.tcp.sock,
@@ -41,8 +61,6 @@ int mqtt_client_tcp_connect(struct mqtt_client *client)
4161
}
4262
#endif
4363

44-
NET_DBG("Created socket %d", client->transport.tcp.sock);
45-
4664
size_t peer_addr_size = sizeof(struct sockaddr_in6);
4765

4866
if (broker->sa_family == AF_INET) {

subsys/net/lib/mqtt/mqtt_transport_socket_tls.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ int mqtt_client_tls_connect(struct mqtt_client *client)
3232

3333
NET_DBG("Created socket %d", client->transport.tls.sock);
3434

35+
if (client->transport.if_name != NULL) {
36+
struct ifreq ifname = { 0 };
37+
38+
strncpy(ifname.ifr_name, client->transport.if_name,
39+
sizeof(ifname.ifr_name) - 1);
40+
41+
ret = zsock_setsockopt(client->transport.tls.sock, SOL_SOCKET,
42+
SO_BINDTODEVICE, &ifname,
43+
sizeof(struct ifreq));
44+
if (ret < 0) {
45+
NET_ERR("Failed to bind ot interface %s error (%d)",
46+
ifname.ifr_name, -errno);
47+
goto error;
48+
}
49+
50+
NET_DBG("Bound to interface %s", ifname.ifr_name);
51+
}
52+
3553
#if defined(CONFIG_SOCKS)
3654
if (client->transport.proxy.addrlen != 0) {
3755
ret = setsockopt(client->transport.tls.sock,

0 commit comments

Comments
 (0)