Force TCP connection onto a particular interface


I have a board where I have implemented an ethernet interface with an ESP32. I would like to selectively use one (or the other) interface for interactions with the cloud and other devices on the LAN. For example, I would like to launch a TCP connection on the ethernet port while remaining connected to the cloud on the wifi interface. If I start up my board with no wifi STA configured I can make requests on TCP ports through the ethernet interface. If the wifi STA is configured and I start up my board, mongoose routes all port openings through the wifi interface instead of the ethernet interface and I can’t communicate with my desired client.

Is there a way to force a tcp connection using mg_connect to use a specific interface (ie; ethernet)?


Mongoose-OS uses lwIP (or it uses the IDF and the IDF uses lwIP; I’m not sure these days…
If it is the IDF version, it can be heavily modified so your mileage may vary).
Anyway, lwIP is not Linux, its routing is basic and fundamentally static. Depending on how you assign your interface addresses and whether you select an interface as default or not, lwIP will choose an interface as the source. All traffic that is not for directly connected networks will need a router and so will take the default interface.
Are your Ethernet and WiFi addresses in different networks ?
Your WiFi will surely get a default route through DHCP, does your Ethernet get a default route ?
What addresses are you trying to connect to ? Can you describe your scenario ?
I don’t remember right now, I think there was some sort of static addressing built-in, you should check at the lwIP users list, here. There were talks about adding some source routing… again, check the list.


Hello scaprile,
Thanks for your quick response!

My two networks are on different networks and can be characterized by this rough diagram:
I would like to be able to connect to a static IP on the ethernet port (LAN; not necessarily even connected to any WAN) while the ESP32 remains connected to a wifi AP (doing cloud connection stuff, SNTP etc). The best analogy I can draw here is using the ESP32 as a wifi bridge mode unit to connect an ethernet only device to a wifi AP.

I’ll have a look at LwIP. Thanks for the suggestion!


Forgive me if I’m obsessive, I’ve seen far too many networking errors and since I do not know you I can’t tell whether you are an expert in networking or not. A diagram like that I can infer from your question, the address space I can’t. Since you don’t tell, I can’t check for errors.
The ESP32 will not work as a router unless you dive into lwIP and add proper code to the routing hooks they provide. To work as a bridge you will need to write a bridge netif. I guess you mean the ESP32 will work as an application gateway or some sort of proxy where it relays application data, not IP datagrams, to a host on the Internet.
In such a case, and providing LAN network address space belongs to a private space and devices have proper address and mask settings for that address space, TCP connections open from the ESP32 to a host on the LAN should go out of the Ethernet interface (unless the IDF or Mongoose-OS are configuring lwIP otherwise).
Anyway, if you connect from your LAN host, it should work


Sorry If I am not being clear here. I don’t intend to forward network IP datagrams. I intend to capture proprietary data from a device on the LAN over TCP and then forward it to the cloud via the wifi interface. I want to be able to do this without having to disconnect the wifi or the ethernet interface, if possible.

I believe what you are suggesting is that if I ensure that the Wifi address space and the ethernet address space are not on the same subnet I should be good to go.

Thanks again!


Yes indeed!
In case that doesn’t happen, dig on lwIP and let me know, I’m curious about it.


It worked! If the wifi and ethernet are on different subnets, everything appears to work ok…
Thanks for the help!