Get Host-IPs of wireless network - c#

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.

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.

How can I ping all devices which are in my local network and get responce from them?

I want to find all devices on my local network using c#.
The solutions that have at the moment is based on pinging each ip on my local network. something that explained here:
How to Perform Multiple "Pings" in Parallel using C#
The problem is that if I have more than one network interface on my PC (for example one wifi and one Ethernet) how can I understand which one should I ping? How to find suitable address range for each network? and so on. If the subnet mask is very big (for example when subnet is 255.255.0.0, it would takes a lot of time to do this.
There are some other suggested solution such as this:
Locating computer on subnet/network
But how can I ping the broadcast address and find who is answering? Ping in C# only return one result for one IP (Am I right)?
Is there any better solution?
"But how can I ping the broadcast address and find who is answering?" There is no such thing as a broadcast ping. If you look at a network scanning tool like nmap, which has a "ping sweep" feature, the way it identifies nodes on the network is through individual pings.

How to findout which network the PC is connected now

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.

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?

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).

Categories