简体   繁体   中英

Neighbor discovery C

I need to discover all network neighbors in Linux(they are running Linux too) and I need to get theirs IP addresses(3rd layer). Any ideas how to do that?

Btw, I need to do that in C , not in shell

Many thanks in advance!

What you should do is, have the neighbours run a daemon which responds (with a unicast response to the sender) to UDP multicasts.

Then send a UDP multicast with a TTL of 1 (so it will not be routed) and listen to see who responds. You will only receive responses from the neighbours which are running the agent.

Another possibility is to use an existing protocol which already does this, for example, mDNS.

There is no guaranteed way to do this if the machines in question aren't co-operating.

The best you can do is to scan likely addresses and probe each one to see if you can get a response - that probe could be anything from a simple ICMP echo request (a ping) up to a sophisticated malformed packet that attempts to elicit a response from the remote host.

The level of sophistication required, and whether it will work at all, depends entirely on how heavily firewalled etc the host in question is.

As a commenter has already observed, there are entire programs like nmap dedicated to attempting to discover this information, which gives some idea of how non-trivial this can be.

At the other extreme, if the hosts are co-operating, then a simple broadcast ICMP echo request might be enough.

If your segment uses reasonably decent switch, you can discover the link-layer neighbours by inspecting the forwarding database of one of the switches. You should be able to obtain this fairly automatically via SNMP, check your switch's documentation.

Once you have a list of link neighbours, you can try and find out their IP addresses, but remember that they may have many or none at all. For this you'd need some sort of reverse-ARP. Perhaps your router maintains a list of MAC-to-IP associations and you can query it (again SNMP would be the most convenient solution).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM