Curtis
Curtis

Reputation: 5892

Is there a way to specify which NetworkInterface .net will use?

I would like to run a series of tests on a device using its Wifi and LAN connections. I have two network interface cards, one is WiFi, the other is wired. The device has a single IP address that is accessible through both Network Interfaces. Is there a way to guarantee that my machine is using a particular Network Interface so that I can run all tests through the wired connection, and then run all tests through the Wifi connection?

Keep in mind, both connections share the same subnet, so either will successfully connect to the device. I just want to be able to switch back and forth and specify which connection I'm using at any given time.

I'm using HttpClient on .net with C#. Is there a way to force HttpClient to use a particular Network Interface? It seems to automatically choose one based on IP Address, but in this case, there are 2 Network Interfaces that can work. I need to be able to programatically switch back and forth to run my automated tests.

I have noticed something called an 'Interface Metric' that can be changed manually via IP4 Properties Advanced TCP/IP Settings. Will the smaller numbered interface be used every time? Is there a way to change this value programatically?

MORE CLARIFICATION: The reason I didn't directly jump to Disabling the WiFi NIC or the Wired NIC back and forth was when you disable the WiFi NIC, you disconnect the device it was connected to and it doesn't automatically reconnect when you enable it. (even when you say 'connect automatically' it doesn't connect when re-enabled). However, I found a quick solution. Set the Interface Metric on the Wired NIC to be lower than the Interface Metric on the WiFi NIC. Then, just enable and disable the Wired NIC. When disabled, the WiFi NIC will be used by the system. When enabled, the Wired NIC will be used by the system because its Interface Metric is set lower. Now, I just need to figure out how to programatically enable/disable the Wired NIC...

Upvotes: 2

Views: 3717

Answers (4)

Pau Dominguez
Pau Dominguez

Reputation: 188

You can use the library WlanAPI.cs

static public void Connect_to_wifi(string ssid)
            {
                WlanClient client = new WlanClient();
                foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                {
                    // Lists all networks with WEP security
                    //Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
                    //foreach (Wlan.WlanAvailableNetwork network in networks)
                    //{
                    //    if (network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.WEP)
                    //    {
                    //        Console.WriteLine("Found WEP network with SSID {0}.", network.profileName);
                    //    }
                    //}
    
                    // Retrieves XML configurations of existing profiles.
                    // This can assist you in constructing your own XML configuration
                    // (that is, it will give you an example to follow).
                    foreach (Wlan.WlanProfileInfo profileInfo in wlanIface.GetProfiles())
                    {
                        if (profileInfo.profileName == ssid)
                        {
                            wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, wlanIface.GetProfileXml(profileInfo.profileName), true);
                            wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileInfo.profileName);
                            break;
                        }
                    }
                }
            }

Upvotes: 2

Curtis
Curtis

Reputation: 5892

I ended up solving this by setting the Interface Metric on the wired NIC to be lower than the Interface Metric on the Wifi NIC. In this way, I was able to simply disable and enable the Wired NIC which would effectively change which NIC was being used at any time during the test. To Disable the NIC, I use the netsh.exe command with the following code. The 'command' was either 'enable' or 'disable' depending if I was turning it on or off:

        System.Diagnostics.Process p = new System.Diagnostics.Process
        {
            StartInfo =
            {
                FileName = "netsh.exe",
                Arguments = $"interface set interface \"{nicName}\" {command}",
                UseShellExecute = false,
                RedirectStandardOutput = true
            }
        };

        bool started = p.Start();

Upvotes: 1

Mike Petrichenko
Mike Petrichenko

Reputation: 1024

Best way is to use WinSock bind: TCP/IP connection on a specific interface

But once you have single IP for bith NICs then:

Disable NIC from PowerShell: https://learn.microsoft.com/en-us/powershell/module/netadapter/disable-netadapter?view=win10-ps

Calling PowerShell from c#: https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/

Upvotes: 1

RB.
RB.

Reputation: 37182

One way would be to update your route table to force traffic for a particular IP onto a particular NIC. The route table is what is used to decide which NIC to send traffic out on.

You could do this in a couple of ways:

1) Use P/Invoke to update the route table with C# code (see here for a good example, but basically you are calling CreateIpForwardEntry).

2) Use Process.Start to call route.exe, e.g.

route.exe ADD destination_network MASK subnet_mask  gateway_ip metric_cost

and

route.exe CHANGE destination_network MASK subnet_mask  gateway_ip metric_cost

Upvotes: 2

Related Questions