Skip to content

feat(net): Add support for selecting the default network interface #9457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions libraries/Network/src/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "NetworkInterface.h"
#include "NetworkManager.h"
#include "esp_netif.h"
#include "esp_netif_defaults.h"
#include "esp_system.h"
Expand Down Expand Up @@ -538,6 +539,43 @@ String NetworkInterface::impl_name(void) const
return String(netif_name);
}

int NetworkInterface::impl_index() const
{
if(_esp_netif == NULL){
return -1;
}
return esp_netif_get_netif_impl_index(_esp_netif);
}

int NetworkInterface::route_prio() const
{
if(_esp_netif == NULL){
return -1;
}
return esp_netif_get_route_prio(_esp_netif);
}

bool NetworkInterface::setDefault()
{
if(_esp_netif == NULL){
return false;
}
esp_err_t err = esp_netif_set_default_netif(_esp_netif);
if(err != ESP_OK){
log_e("Failed to set default netif: %d", err);
return false;
}
return true;
}

bool NetworkInterface::isDefault() const
{
if(_esp_netif == NULL){
return false;
}
return esp_netif_get_default_netif() == _esp_netif;
}

uint8_t * NetworkInterface::macAddress(uint8_t* mac) const
{
if(!mac || _esp_netif == NULL){
Expand Down
5 changes: 4 additions & 1 deletion libraries/Network/src/NetworkInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "esp_netif_types.h"
#include "esp_event.h"
#include "Arduino.h"
#include "NetworkManager.h"
#include "Printable.h"

typedef enum {
Expand Down Expand Up @@ -54,6 +53,10 @@ class NetworkInterface: public Printable {
const char * ifkey() const;
const char * desc() const;
String impl_name() const;
int impl_index() const;
int route_prio() const;
bool setDefault();
bool isDefault() const;

uint8_t * macAddress(uint8_t* mac) const;
String macAddress() const;
Expand Down
18 changes: 17 additions & 1 deletion libraries/Network/src/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "NetworkManager.h"
#include "NetworkInterface.h"
#include "IPAddress.h"
#include "esp_netif.h"
#include "lwip/dns.h"
Expand Down Expand Up @@ -133,6 +132,23 @@ bool NetworkManager::setHostname(const char * name)

NetworkInterface * getNetifByID(Network_Interface_ID id);

bool NetworkManager::setDefaultInterface(NetworkInterface & ifc)
{
return ifc.setDefault();
}

NetworkInterface * NetworkManager::getDefaultInterface()
{
esp_netif_t * netif = esp_netif_get_default_netif();
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i){
NetworkInterface * iface = getNetifByID((Network_Interface_ID)i);
if(iface != NULL && iface->netif() == netif){
return iface;
}
}
return NULL;
}

size_t NetworkManager::printTo(Print & out) const {
size_t bytes = 0;

Expand Down
4 changes: 4 additions & 0 deletions libraries/Network/src/NetworkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "NetworkEvents.h"
#include "NetworkInterface.h"
#include "IPAddress.h"
#include "WString.h"

Expand All @@ -18,6 +19,9 @@ class NetworkManager : public NetworkEvents, public Printable {
uint8_t * macAddress(uint8_t * mac);
String macAddress();

bool setDefaultInterface(NetworkInterface & ifc);
NetworkInterface * getDefaultInterface();

size_t printTo(Print & out) const;

static const char * getHostname();
Expand Down