简体   繁体   中英

How to get Mac address of ipv6 address?

Hi I have written one TCP server which runs on IPv6 address. What server does is accepting the client connection from various machines in the same subnet and categorize the clients based on their mac address.

For Ex: Client A and B are from the same machine and Client C is from another machine. When client A, B and C connected to server then server shows client A and B are from same machine and Client C is from another machine.

To achieve this I have to get the mac address from client ipv6 address.

In ipv4 addresses I can achieve this using ioctl(mac_arp_sock, SIOCGARP, &areq);

as ARP doest not work in IPv6 what is the best way to retrieve the mac address of peer machine just by knowing peer IPv6 address ?

Well like the name says, SIOCG ARP uses the Address Resolution Protocol which is IPv4 only. And like the wiki page mentions: the Neighbor Discovery Protocol is its successor.

So, previously you could use ioctl with SIOCGARP or "arp -a" which used the same backend. And now you have to start a subprocess / task "ndp -na" and parse its stdout which looks like:

Neighbor                        Linklayer Address  Netif Expire    St Flgs Prbs
fd00::xxxx:b1ff:xxxx:fc25       b8:xx:b1:xx:fc:xx    en0 permanent R  R   
xxxx::ca2a:xxxx:fe56:xxxx%en0   xx:2a:xx:56:xx:cc    en0 23h17m41s S      
fe80::xxxx:14ff:xxxx:ca52%en0   c8:xx:14:xx:ca:xx    en0 23h17m40s S    

See reference:http://www.freebsd.org/cgi/man.cgi?format=html&query=ndp(8)

Advanced: Google for the implementation of ndp eg BSD/OpenBSD/src/usr.sbin/ndp/ndp.c if you want to implement it your self without using the ndp-command.

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