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 @@ -809,6 +809,11 @@ struct mqtt_transport {
809
809
*/
810
810
enum mqtt_transport_type type ;
811
811
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
+
812
817
/** Use either unsecured TCP or secured TLS transport */
813
818
union {
814
819
/** 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 @@ -32,6 +32,24 @@ int mqtt_client_tls_connect(struct mqtt_client *client)
32
32
33
33
NET_DBG ("Created socket %d" , client -> transport .tls .sock );
34
34
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
+
35
53
#if defined(CONFIG_SOCKS )
36
54
if (client -> transport .proxy .addrlen != 0 ) {
37
55
ret = setsockopt (client -> transport .tls .sock ,
You can’t perform that action at this time.
0 commit comments