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>
This commit is contained in:
parent
0938eccd6d
commit
35af68b840
3 changed files with 43 additions and 2 deletions
|
@ -809,6 +809,11 @@ struct mqtt_transport {
|
||||||
*/
|
*/
|
||||||
enum mqtt_transport_type type;
|
enum mqtt_transport_type type;
|
||||||
|
|
||||||
|
/** Name of the interface that the MQTT client instance should be bound to.
|
||||||
|
* Leave as NULL if not specified.
|
||||||
|
*/
|
||||||
|
const char *if_name;
|
||||||
|
|
||||||
/** Use either unsecured TCP or secured TLS transport */
|
/** Use either unsecured TCP or secured TLS transport */
|
||||||
union {
|
union {
|
||||||
/** TCP socket transport for MQTT */
|
/** TCP socket transport for MQTT */
|
||||||
|
|
|
@ -29,6 +29,26 @@ int mqtt_client_tcp_connect(struct mqtt_client *client)
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NET_DBG("Created socket %d", client->transport.tcp.sock);
|
||||||
|
|
||||||
|
if (client->transport.if_name != NULL) {
|
||||||
|
struct ifreq ifname = { 0 };
|
||||||
|
|
||||||
|
strncpy(ifname.ifr_name, client->transport.if_name,
|
||||||
|
sizeof(ifname.ifr_name) - 1);
|
||||||
|
|
||||||
|
ret = zsock_setsockopt(client->transport.tcp.sock, SOL_SOCKET,
|
||||||
|
SO_BINDTODEVICE, &ifname,
|
||||||
|
sizeof(struct ifreq));
|
||||||
|
if (ret < 0) {
|
||||||
|
NET_ERR("Failed to bind ot interface %s error (%d)",
|
||||||
|
ifname.ifr_name, -errno);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
NET_DBG("Bound to interface %s", ifname.ifr_name);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_SOCKS)
|
#if defined(CONFIG_SOCKS)
|
||||||
if (client->transport.proxy.addrlen != 0) {
|
if (client->transport.proxy.addrlen != 0) {
|
||||||
ret = setsockopt(client->transport.tcp.sock,
|
ret = setsockopt(client->transport.tcp.sock,
|
||||||
|
@ -41,8 +61,6 @@ int mqtt_client_tcp_connect(struct mqtt_client *client)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
NET_DBG("Created socket %d", client->transport.tcp.sock);
|
|
||||||
|
|
||||||
size_t peer_addr_size = sizeof(struct sockaddr_in6);
|
size_t peer_addr_size = sizeof(struct sockaddr_in6);
|
||||||
|
|
||||||
if (broker->sa_family == AF_INET) {
|
if (broker->sa_family == AF_INET) {
|
||||||
|
|
|
@ -32,6 +32,24 @@ int mqtt_client_tls_connect(struct mqtt_client *client)
|
||||||
|
|
||||||
NET_DBG("Created socket %d", client->transport.tls.sock);
|
NET_DBG("Created socket %d", client->transport.tls.sock);
|
||||||
|
|
||||||
|
if (client->transport.if_name != NULL) {
|
||||||
|
struct ifreq ifname = { 0 };
|
||||||
|
|
||||||
|
strncpy(ifname.ifr_name, client->transport.if_name,
|
||||||
|
sizeof(ifname.ifr_name) - 1);
|
||||||
|
|
||||||
|
ret = zsock_setsockopt(client->transport.tls.sock, SOL_SOCKET,
|
||||||
|
SO_BINDTODEVICE, &ifname,
|
||||||
|
sizeof(struct ifreq));
|
||||||
|
if (ret < 0) {
|
||||||
|
NET_ERR("Failed to bind ot interface %s error (%d)",
|
||||||
|
ifname.ifr_name, -errno);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
NET_DBG("Bound to interface %s", ifname.ifr_name);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_SOCKS)
|
#if defined(CONFIG_SOCKS)
|
||||||
if (client->transport.proxy.addrlen != 0) {
|
if (client->transport.proxy.addrlen != 0) {
|
||||||
ret = setsockopt(client->transport.tls.sock,
|
ret = setsockopt(client->transport.tls.sock,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue