Bind outgoing socket connection to specific interface doesn't always work on Windows 11
For a VPN app on a Windows 11 machine I have my regular wifi interface (192.168.3.7) and a Wintun Tunnel interface (configured to have IP 192.168.3.6). The routing tables are set up so that all traffic should go to the Wintun Tunnel by default. However the app still needs to be able to make some connections using the wifi interface. I tried creating a socket and binding it to that interface's IP. On one of my Windows 11 machines that works as expected and the socket's traffic is routed to wifi. But on another machine the traffic is still routed to Wintun. Why doesn't this setup work correctly on both machines? Are there other settings somewhere that affect routing decisions?
Example Python code for how I create the socket and bind it to the wifi interface:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("192.168.3.7",0))
sock.connect(("www.google.com",80)) # these TCP packets still get sent to the Wintun Tunnel
And the routing info on the device:
===========================================================================
Interface List
11...........................Wintun Tunnel
9...ce 47 40 77 44 bc ......Microsoft Wi-Fi Direct Virtual Adapter
2...ee 47 40 77 44 bc ......Microsoft Wi-Fi Direct Virtual Adapter #2
8...cc 47 40 77 44 bc ......Realtek RTL8852BE WiFi 6 802.11ax PCIe Adapter
18...cc 47 40 77 44 bd ......Bluetooth Device (Personal Area Network)
1...........................Software Loopback Interface 1
===========================================================================
IPv4 Route Table
===========================================================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 192.168.3.1 192.168.3.7 30
0.0.0.0 0.0.0.0 On-link 192.168.3.6 6
127.0.0.0 255.0.0.0 On-link 127.0.0.1 331
127.0.0.1 255.255.255.255 On-link 127.0.0.1 331
127.255.255.255 255.255.255.255 On-link 127.0.0.1 331
192.168.3.0 255.255.255.0 On-link 192.168.3.7 286
192.168.3.0 255.255.255.0 On-link 192.168.3.6 261
192.168.3.6 255.255.255.255 On-link 192.168.3.6 261
192.168.3.7 255.255.255.255 On-link 192.168.3.7 286
192.168.3.255 255.255.255.255 On-link 192.168.3.7 286
192.168.3.255 255.255.255.255 On-link 192.168.3.6 261
224.0.0.0 240.0.0.0 On-link 127.0.0.1 331
224.0.0.0 240.0.0.0 On-link 192.168.3.7 286
224.0.0.0 240.0.0.0 On-link 192.168.3.6 261
255.255.255.255 255.255.255.255 On-link 127.0.0.1 331
255.255.255.255 255.255.255.255 On-link 192.168.3.7 286
255.255.255.255 255.255.255.255 On-link 192.168.3.6 261
===========================================================================
Persistent Routes:
None