How to select ethernet or wifi to send TCP? - c#

Here is my problem: My computer is connected to internet with ethernet cable, and also connected to a wifi module. But when I want to send TCP to my module, it always choose ethernet by default.
I have to disconnect my cable to send TCP to my module, but I will need the two connections and know how to swap between them.
How can I choose the interface i want in c#?
Can I swap between them in a single program?
I already use System.Net and the Managed Wifi API.
Thank you for your answers.
Edit:
As #Someprogrammerdude and #sam suggest, i use socket.bind() with the IP address of the default gateway of my wifi to send TCP on it. But when I try to bind, there the requested address is not valid in this context error. So I checked the default gateway address (sry french) :
ipconfig
We can see that my wifi default gateway is 192.168.1.2 and ethernet 192.168.1.1. I tried to put them "manually" but i have the same result.. It cannot be the port i checked myself on the module that he is well opened
Edit 2:
Nvm, i had to bind 192.168.1.100, and not 192.168.1.2. Thank you for your answers, you helped me a lot.

Which interface is used is decided by the local routing table. A cable interface (often) is faster, so it'll get a lower network metric and is subsequently preferred. With DHCP clients, this may also be configured from the DHCP server (if it allows manual settings).
Depending on your system, you can manipulate the interfaces' network metrics (likely need to deactivate DHCP for that) and if you'd like to prefer wireless, just reduce its metric below that of the Ethernet interface. Alternatively, you can add routes to certain services you want to be transferred via wireless to the wireless interface with a lower metric.

Related

Communicate between computers through Ethernet cable

I want to connect two computers with an Ethernet cable without the user having to set up a LAN through the Control Panel and then transfer data between two instances of my app - one running on each computer.
As soon as I have a connection with an IP address for each computer, I know I can easily transfer a file by using Sockets, Pipes(?), WCF... But both computers are also connected via wifi to a router, so how can I somehow tell the computer that for a specific IP address - please use the cable? (I might be getting all of this wrong. Please feel free to correct me.)
I can use a UWP app or a .net (Winform/WPF) app. Either technology is fine. But this must be done in code, not by the users setting up a LAN through the Control Panel. Also, IPv6 is fine too.
Each network card has its own IP address, so both of the computers will have IP address for the WIFI network and another IP address for the ad-hoc link.
The addresses for the ad-hoc link can be set to an address in one IP segment, for example, 10.0.0.1 and 10.0.0.2, if the WIFI addresses are in the range of 192.168.X.X.
Your OS will know which card is related to each IP segment.
Start with System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().
Filter out the Type=EtherNet interfaces and make sure they're Up. You may also have to watch out for virtual adapters etc.
From the selected adapter you can get Address Information etc. The IPV4 address should probably be enough to get you started.

Best way to configure network devices that have an invalid / non matching network configuration setup?

We're developing some hardware devices in our company that need to communicate through TCP on the network with our application. The devices have some restrictions, such as they do not have a display or a dotmatrix to show any current configuration, such as IP or mac address. Therefore, it is not possible to configure the IP configuration directly on the device.
When they are connected to the network for the first time, there is a high chance, that their current network configuration does not match with the network being connected to.
E.g.
They are configured to use DHCP and the network does not provide any DHCP.
They are configured to use a manual IP, such as 192.168.1.1 with a subnet mask of 255.255.0.0, but the network is set up to use 10.X.X.X as IP range with a network mask of 255.0.0.0
We now want to develop a standalone application, that lists all devices being connected to the network and change the IP configuration to a specific one (matching the network needs).
What needs to be set up on the device (running Linux) in order to be capable of the things posted above?
What needs to be set up on the client side application in order to display the devices and reconfigure them if mismatching? The Application should be a .NET WPF / C# application if possible, admin rights can be aquired if needed.
Are there any restrictions / scenarios under which we are not able to detect those devices or set up the new network settings?
Leaving out any security issues I would use a udp broadcast to 255.255.255.255 which will been received by any devices. When you put all valid subnets into the payload the client will know that it is wrongly configured and has to answer back as broadcast (since it might be in a different subnet and a unicast is impossible without any valid routing tables)
But please keep in mind that this can open security issues since you will publish on scans all valid subnets which might been very interesting for attackers.
If you know e.g. the Mac addresses of all possible device you may could encrypt the UDP payload which can only been encrypted with the right Mac addresses.
This is what DHCP, BOOTP, RARP are for. Using DHCP and friends to do this sort of thing means a device plonked on a network stands the best possible chance of getting a valid config in the first place.
If you MUST have your own app doing the management of the issue of IP config, then perhaps consider using the DHCP protocol in your management/server app.
If your devices must not speak to a random DHCP server then consider having them either ignore responses that don't have certain attributes in the reply (bit inefficient since it may keep asking and may tie up IPs) or use DHCP on different ports... At least you're not reinventing the wheel then.
Using existing protocols has the benefit that network analysers can understand what's going on too, system admins can debug and make intuitive guesses as to problems, etc.

Find and assign a unique IP address to a device

I need to assign a unique IP address (local) for a device we will be connecting into our system via. USB. The actually assigning I will do over RS232 (don't ask!) by telling the device what it's IP is.
I would like to somehow find a usable address relative to the local PC in order to tell the device which to use. Is there a way of enumerating or finding a usable address in C#? I don't want to simply use a fixed one in case of possible clashes with other devices.
I've done a search here and there's lots of people wanting to find the current IP, or the IP of an already existing device, but I can't find much about generating a usable, unique one.
Thanks for any assistance you can give me.
Assigning static IP addresses (even if you ping them) is a bad idea.
What happens if the IP address you've assigned is to a device that happens to be offline right now?
Use DHCP if you can, and consider prompting the user if you can not retrieve an IP address from DHCP as to what it should be assigned to.
I am not ware of a native C# library for DHCP client's, but using pinvoke will get you what you're after. Example code (not tested) here, http://www.ianatkinson.net/computing/dhcpcsharp.htm
From http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol
The Dynamic Host Configuration Protocol (DHCP) is a network
configuration protocol for hosts on Internet Protocol (IP) networks.
Computers that are connected to IP networks must be configured before
they can communicate with other hosts. The most essential information
needed is an IP address, and a default route and routing prefix. DHCP
eliminates the manual task by a network administrator. It also
provides a central database of devices that are connected to the
network and eliminates duplicate resource assignments. In addition to
IP addresses, DHCP also provides other configuration information,
particularly the IP addresses of local Domain Name Server (DNS),
network boot servers, or other service hosts. DHCP is used for IPv4 as
well as IPv6. While both versions serve much the same purpose, the
details of the protocol for IPv4 and IPv6 are sufficiently different
that they may be considered separate protocols.[1] Hosts that do not
use DHCP for address configuration may still use it to obtain other
configuration information. Alternatively, IPv6 hosts may use stateless
address autoconfiguration. IPv4 hosts may use link-local addressing to
achieve limited local connectivity.
Ok, not really sophisticated or anything, but why don't you just try to ping the ips, starting by the lowest, and pick the first one which doesn't respond?

Monitor Internet Connection using C#

I want to monitor my router to see what is currently being downloaded and what application/Software that initiate it, who is the user doing this and download speedthat user occupy
i got all credential needed for both my wireless connection and the router
any head start for sth like that in C#?
the idea
First of all: Do you even know that your router can do this? Most routers I've seen do not have this level of traffic understanding and management.
If your router does, then there is one of two ways that such data is generally made available:
1) Through a HTTP interface (password protected)
or
2) Through an SNMP interface
To retrieve the appropriate HTTP URL, you simply get it as you would any other web page. Try something like: http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
To retrieve the SNMP settings, there are many options; try this question: What SNMP library for .NET makes traps, sets or gets simple?
However, most likely, your router will not actually have this information available.
So, edit: If your router doesn't support side-chaining, and doesn't support SNMP or similar statistics, then you can't do this in the general sense.
You could spin up a Linux box as the gateway for the machines, and use NAT session statistics to monitor this. The way to do this is to have two network interfaces (logical or physical), one of which gets an IP from your modem, and the other which is the default gateway for the wireless network. Turn on IP forwarding and masquerading (NAT) as well as a DHCP server for the wireless network side. Now, you can use iptables to look at active NAT sessions and how much data has been transferred. You can also use packet filters for more specific information.
Also, if you know which machine is doing the downloading, and are running Windows, you can use WMI ("perfmon.exe" to plot this) to see how much data is being transfered on the actual machine.

Detect WOL possibility

I'd like to detect if Wake On Lan is possible.
On my router (Tomato firmware) there is a table with info - when displays device "Active (In ARP)" - it's possible to turn this device by WOL (offline linux pc).
I wonder if it is achieved by router only function or I can do this in C# or C? Function SendArp can detect MAC adress and do "arping" but it is not what I would like to do.
Your router can't detect if your computer (or any other network device) supports Wake-On-Lan. All your router can do is send out a WOL package and hoping that it will wake up.
If the calling device respects the packet and wakes up must be configured at the device itself and there exists nothing within the OSI layers 4 to 1 which can tell you if a device supports WOL.
If you like to send a WOL packet from your PC using C#, you find plenty examples by using your favourite search engine. Here is one example from Bart de Smet.
Update
The message "Active (In ARP)" doesn't mean that your router detected that it is possible to send a WOL packet. It just tells you that within the routers ARP cache currently is a matching entry for this IP or MAC address. Such a cache has every network device (also your pc). In Windows just open the command line and enter arp -a to see the cache of your windows machine. Here you'll get a list of the stored mac adresses for sending to an IP address. A black hat can try to manipulate this cache to redirect your ip communication. Further informations about this can be found at wikipedia.
So this message just tells you, that your router had recently an ip connection to this device. That's it. But it can't tell you if your device is currently able to handle a WOL packet (cause it is power connected or not).
You're probably best checking out http://msdn.microsoft.com/en-us/library/ff566341(VS.85).aspx
This will let you query boolean for power management and the device specific "wake enabled" state.
Given that it's WMI, you should be able to get to this remotely, but it does need a couple of registry keys set to expose the WMI class.
You get the info about whether a machine supports WOL from the BIOS.
If it is supported, make sure it's turned on. Many machines have the capability disabled by default.
To route WOL packets through the network you need to forward port 9 on the router to 255.255.255.255 (Brodcast-to-all) IP Address.

Categories