File tree Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -815,6 +815,11 @@ struct mqtt_transport {
815
815
*/
816
816
enum mqtt_transport_type type ;
817
817
818
+ /** Name of the interface that the MQTT client instance should be bound to.
819
+ * Leave as NULL if not specified.
820
+ */
821
+ const char * if_name ;
822
+
818
823
/** Use either unsecured TCP or secured TLS transport */
819
824
union {
820
825
/** TCP socket transport for MQTT */
Original file line number Diff line number Diff line change @@ -29,6 +29,26 @@ int mqtt_client_tcp_connect(struct mqtt_client *client)
29
29
return - errno ;
30
30
}
31
31
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
+
32
52
#if defined(CONFIG_SOCKS )
33
53
if (client -> transport .proxy .addrlen != 0 ) {
34
54
ret = setsockopt (client -> transport .tcp .sock ,
@@ -41,8 +61,6 @@ int mqtt_client_tcp_connect(struct mqtt_client *client)
41
61
}
42
62
#endif
43
63
44
- NET_DBG ("Created socket %d" , client -> transport .tcp .sock );
45
-
46
64
size_t peer_addr_size = sizeof (struct sockaddr_in6 );
47
65
48
66
if (broker -> sa_family == AF_INET ) {
Original file line number Diff line number Diff line change @@ -37,6 +37,24 @@ int mqtt_client_tls_connect(struct mqtt_client *client)
37
37
38
38
NET_DBG ("Created socket %d" , client -> transport .tls .sock );
39
39
40
+ if (client -> transport .if_name != NULL ) {
41
+ struct ifreq ifname = { 0 };
42
+
43
+ strncpy (ifname .ifr_name , client -> transport .if_name ,
44
+ sizeof (ifname .ifr_name ) - 1 );
45
+
46
+ ret = zsock_setsockopt (client -> transport .tls .sock , SOL_SOCKET ,
47
+ SO_BINDTODEVICE , & ifname ,
48
+ sizeof (struct ifreq ));
49
+ if (ret < 0 ) {
50
+ NET_ERR ("Failed to bind ot interface %s error (%d)" ,
51
+ ifname .ifr_name , - errno );
52
+ goto error ;
53
+ }
54
+
55
+ NET_DBG ("Bound to interface %s" , ifname .ifr_name );
56
+ }
57
+
40
58
#if defined(CONFIG_SOCKS )
41
59
if (client -> transport .proxy .addrlen != 0 ) {
42
60
ret = setsockopt (client -> transport .tls .sock ,
You can’t perform that action at this time.
0 commit comments