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 {
815815 */
816816 enum mqtt_transport_type type ;
817817
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+
818823 /** Use either unsecured TCP or secured TLS transport */
819824 union {
820825 /** 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)
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 ) {
Original file line number Diff line number Diff line change @@ -37,6 +37,24 @@ int mqtt_client_tls_connect(struct mqtt_client *client)
3737
3838 NET_DBG ("Created socket %d" , client -> transport .tls .sock );
3939
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+
4058#if defined(CONFIG_SOCKS )
4159 if (client -> transport .proxy .addrlen != 0 ) {
4260 ret = setsockopt (client -> transport .tls .sock ,
You can’t perform that action at this time.
0 commit comments