I'm creating some small electronic modules for home automation based on Arduino chips, with Ethernet connectivity. I will try to make them get an IP address with DHCP if possible, but I'm also wanting to build a C# application to configure these modules, and I'd like my software to scan the network to auto-detect my modules, as an HP printer driver can auto-detect the printers on the network for instance.
I thought of :
getting the computer's IP and Mask to determine the IP set
then multi-thread pings or HTTP requests to every IP of the set
check the responses and get the mac addresses
see if the mac address matches my set
do an specific http request to retrieve the "model" of the module, and know how to interact with him
Is this algorithm plausible or do anyone know of a more efficient way ? Like broadcasting requests on the broadcast address and see what gets back ? Any samples or ideas ?
Totally plausible. Direct low-level access to put arp requests out and get the arp-table back is little tricky, but there are several questions here on the site.
Be advised that a device does not have to answer to ping to be able to function on the network. Check the documentation on the chips you use.
Assuming you only want to discover over the same network segment, I would actually suggest using a broadcast message instead (UDP). of course you will have to develop the side on the device to respond to that broadcast and listen to those responses. once you get a response, you can start a direct communication to the device using your preferred method.
Related
I am trying to tell if a mac address is present on a network using c#
We are trying to make a whos in the office dashboard and we will check to see if the person's cell phone connected via wifi (who we will get the mac address from) is
I am not very strong in networking so I am not sure where to even start.
You can run a windows arp -a command line to find out all the devices that are connected to the current network.
You can then parse out the mac address of each line to figure out who is in the office.
MAC addresses are stripped off at the first router/switch, so they're not going to be useful for determining what is connected, unless you can access the monitoring port on your router/switch and you only have one. If you're on a multi-hop network, it's useless.
edit
OK, I've removed a bunch of stuff I said, now that I understand you're talking about cell phones and wifi connections.
The very easiest way to do this that I can come up with is to set your router's DHCP lease time to something short like 10 minutes, then ask the router "who is connected".
The short least time will auto-renew whenever it expires as long as the client is still there.
Many routers display the current DHCP leases on a management page. You can scrape the page with your app and get a list of all currently active DHCP leases.
You can also get it from /tmp/dnsmasq.leases (on the router) if you're running a router that uses dnsmasq. (the location may change but /tmp is pretty common).
The first solution requires parsing a web page and the second requires getting a plain text file from the router and parsing it.
In any case, the best way to know "who is connected" is to ask the device they're connected to.
I would ask if it is possible to get information about connected devices in the local wireless network.
Is there any api for xamarin to do that?
It doesn't seem like Xamarin has an API for this, but Microsoft introduced a Ping (documentation here) and a NetworkInterface class (documentation here) in .NET 2.0 that you could use.
Essentially, what you need to do, is to ping the IP adresses on your local network, which you can do asynchronously using SendAsync(). You then register the callbacks by registering an EventHandler to PingCompleted. The callback will contain a PingCompletedEventArgs which you can use to retrieve basic information (e.g. IP, MAC and Hostname) about the device.
You can find a complete guide of the implementation here.
I hope this helps.
Note: Of course, it all depends on what type of information you expect to retrieve. Although, I think you can only expect to retrieve the basic information about devices on your network, as everything else could potentially be a security risk.
Secondary note: Depending on which devices you wish to discover on your network, you could also use the SNMP component which will allow you to discover devices such as routers, switches, printers, and so on.
I am thinking you are looking for which devices at layer-2 have registered with your wireless controller/accept-point/router and by being connected you are referring to how many have successfully establish 802.x handshake (implying security exchange and channel establishment has occurred).
There exists a tool to do this work (never tested this on my mac) and you can check Flying Squirrel for the above purpose. This will ofcourse be an independent sniffer over the wireless network and you'll require the password used for wifi etc. for making this work.
If you have access to a device that exposes instrumentation (MIB) then you can get that information from the device by SNMP walking the device. At this level you'll of course get the MAC address table and if DHCP is running on your router then perhaps access to DHCP table can give you information about the MAC to IP mapping. On some medium to high end networking gear MIBs like CISCO-DOT11-ASSOCIATION-MIB can be used. Also I am assuming you are refering to 802.11 here there are other wireless protocols like BlueTooth etc. in play as well which use different scanning techniques so the answer depends on what kind of network you are referring to here.
Ok, I figured out a way without snifing the local network. It makes more sense the following way in my case...
Case:
I want to connect to an device which provide an own wlan network. Then I connect to it and pass the home wlan network to it. The device restarts and is now configured as a client in the local home network. And now I need the local ip for service communications.
My Solution:
I do a register call to the device. The device gives me the mac address. Now I register (with phone device) the mac address from the access point device in a online service. After the device has been restarted and get the new local ip address it sends to the online service.
And the I can retrieve the local ip address through polling or through getting push notification from the server...
Like what the title says, is there a way to get the mac addresses and/or ip of all routers connected to a switch in asp.net c#?
If so, is there a way to determine if the connected router is on and off?
The setup of my routers and switch is in the image link.
Managed switches uses SNMP (Simple Network Management Protocol) where you can get information such as the port connection for every port, see here.
Some more information you can get via LLDP (Link Layer Discovery Protocol), see here
I think this should be enough hints. For some work you can use pcap via c#.
Scenario:
Developing an application targeting a Win CE 5.0 powered device. The application is being developed using .NET Compact Framework 3.5 (C#).
Question:
Just like when you connect a computer to a LAN and you can see all the other active computers and their respective IP Address, we would like to implement something similar in our application where only the IP Addresses will be listed.
Considering that it is an existent Windows feature, I guessed it would be possible to easily implement in out application. What's the best approach considering my scenario?
First you will have to define what you mean by active IP Addresses.
A host may block ICMP_ECHO (commonly reffered to as ping) but allow TCP, UDP etc. connections on specific ports.
Depending on what type of service you require of your hosts you could
send ICMP_ECHO requests to all the IP Addresses in your subnet.
- To calculate the IP range of your subnet you may want to use the address space bitmask
- For ICMP request you could also use a broadcast address. But most hosts do not respond to broadcast requests in order to prevend flooding.
attempt to open connections on specific TCP ports (again on the whole ip range)
ask your router to provide you with a list of hosts using it via dns query
send arp requests to an ethernet broad cast address
Essentially there are too many ways to get a list of hosts in a specific ip range or subnet to give a general answer.
First you need to define what you need that host list for.
you can write a function that ping on about 20 or more IPAdresses going from 192.168.1.1 to 192.168.1.20 and call it in different threads with defferent ofssets exmple :
one going from 192.168.1.1
other one going from 192.168.1.21
.......
call them on defferent threads but dont call them a lot it would slow down the CPU and maybe freez the computer
In my company we use small application called IPMsg, a messenger kind of tool to pass messages and file to other fellows in company, even it allows to multicast the message.
And also it lists the user name, host name and IP addresses of users.
How can it do that? There is no server present for message routing and when checked through netstat command in CMD it does not show any details like what protocol and port it is using to communicate.
There is source code also available on the same site which is in VC++. I didn't understand a line of code... (I'm a C# guy)
Can anyone explain me how it can do that?
One simple way would be to let the application listen on a certain network port, and when you start your instance of it, it tries to connect to that port on each computer on the same network. If that other computer has that port open, and answers correctly, then you have found another instance of the application.
IPMsg probably multicasts a request for all clients to report their user and host details.
A similar mechanism is used when Windows Explorer attempts to find other machines on a network. A good description of this type of multicasting discovery is described here.
IPMsg is a daemon which listens to incoming connections on a specific port which is the connection port. You can find out which port it used by using Wireshark.
Start wireshark, start listening on the interface where you have connected to LAN and then start sending any message, wireshark will show you the message on the screen with the port number also.
The application is a peer-to-peer software and doesn't require a central server software to route messages. it only has a small daemon which accepts incoming connections. This is the way Jabber Instant messaging protocol also works.
As you said it lists username, hostname and ip address of users, do you mean it pings the network and finds it? If yes, then it is actually possible to find the IP addresses of computers on the Local Network which requires you to know the subnet on which you are connected.
You can use ARP/ICMP Ping to know the hosts present on your network provided you enter the correct subnet information
Multicasting a message is also nothing special. It is a feature provided with all Networking Stacks.
If you want mutlicasting in .NET, it is allowed. Check this page on Code Project which gives a nice example