How to findout which network the PC is connected now - c#

I am writing an application in C# and one of the functionality requires to perform different actions based on network connected.
For example;
When PC connected to home network network cable perform MethodA
When PC connected to home network but Wifi then perform MethodB
When PC connected to Office network on building 1 then perform MethodC
When PC connected to Office network on building 2 then perform MethodD
When PC connected to Friends Home network perform MethodE
Here how do I distinguish between different networks?

I would use some information about the network to figure this. If you are on wireless you'll know that the wired nic doesn't have an IP. Then based on the IP scheme you can make an inference of where you are at. You're going to have to know some information about the network.
For example, if your work assigns you an IP in the subnet ranges 10.1.0.1-40 and your home network gives you 192.168.1.2-255 then you can use that information to dictate what your application does.
Unfortunately this is a pretty loose coupling and if the routing scheme changes then your application may break. I'd make this kind of setting configurable, where you create a config that maps ip schemes to "locations" and you use that as your basis of what you do.
If you have your networks mapped to locations in windows you can use the answer in this question to help How do I determine "Network Location" in .NET?.

I believe you could reliably detect whether the machine is connected via wireless or cable to the local router.
However, going further than that I think you'll run into a lot of issues.
If they are on Vista or above you should be able to get the "name" of the network in use. However, this would have to be properly configured AND not jacked with by the end user.
Barring that, you could certainly check the ip address range assigned to you... However that assumes building 1 and 2 issue different ranges and those ranges aren't the same as ones issued by the home network. Further it's entirely likely that the home network and the friends house network are on the same internal range.
I'd say, your application is much better off asking the user what method to use or by giving them the name of the method or by asking some other type of question to give you a hint.

Related

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.

Get Host-IPs of wireless network

Possible Duplicate:
Get all IP-Hosts in Lan from mobile device
How can I get programmaticaly all the hosts in a wireless network?
I know the wlan I'm working in and I am connected to it. Now I want to show a list of the hosts (or at least their IP-Addresses).
How can I accomplish this, and are there special points if I work on windows mobile with compact framework and want to do that?
There are lots of ways. For example:
ARP:
http://msdn.microsoft.com/en-us/library/aa366358%28VS.85%29.aspx
WMI:
http://weblogs.sqlteam.com/mladenp/archive/2010/11/04/find-only-physical-network-adapters-with-wmi-win32_networkadapter-class.aspx
ICMP:
http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/8a528983-915b-4d94-836e-804b03e6261f
Etc
The only way to get all the hosts in a network is to use network scanning.
You could ping all the valid IPs of the network the device is in.
Or you could check every valid IP of the network on the local DNS service for a name and list the different names you found with corresponding IP.
An IP would be valid if the Bits corresponding to the subnet mask are the same to the IP the device has.
The easy answer is: You can't. Going more in detail: you can't unless you have a protocol to discover your hosts, for example, NetBIOS. You can call NetBEUI (NetBios User Interface) through pInvoke. Look at this previous post:
Netbios support in .NET?
Another approach you can use is to ping all the IP addresses in your WLAN range and wait for responses. This is not a good approach as it is very resource consuming.

Find out Chat session is through VPN connection

Is there a way to find out whether the current chat session is happening through a VPN connection programatically (using either C# or Java) ?
I don't have access to network tools to do a R&D ( first of all not sure whether it can be tracked live), hence i haven't tried anything.
Any help/pointers/directions will of great help.
Thanks
I believe it would depend on how the VPN is set up.
If the VPN is connected using a software endpoint then it will normally have a simulated network adaptor that you could most likely find the name of (the hardware drivers display name not the connection name) and compare it to a list of known VPN network adaptors.
If the VPN is connected using a hardware endpoint like a firewall or other dedicated network device then there may be no way to tell without some sort of man in the middle hardware approach.

How can I get only computers on LAN based on IP address range?

I am writing a C# Winforms program to get all the computers that are connected to my LAN, based on the given IP address range.
I am using the Ping class to identify the computers.
But the problem here is I am getting responses from all the devices like (printer, IP phone, etc) that are connected to LAN.
Please help me in filtering these devices and to get only computers from LAN.
It's impossible to tell using PING whether a network node is a computer/printer/ip phone etc.
You would need to use a higher level protocol like NETBIOS or DNS to work this out but it depends very much on how the devices are implemented.
Well, chances are that the printer, phone, etc. have general purpose computers in them running a TCP/IP stack and some specialized server software, so it boils down to what your definition of what a "computer" is... still, it sounds like more trouble than it's worth, unless you can identify something that only what you think of as computers will have, and that all of them will have. On a Windows network, SMB, perhaps - but that could catch a NAS that exposes storage over SMB.
I don't think there's a general way to do what you want to do. Like Lloyd pointed out, nmap does some fingerprinting, but that is far from perfect (it will usually get pretty close to a specific OS version, but it can't tell what kind of physical device is running that OS).

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.

Categories