Sending data on AF_PACKET socket

2019-06-14 02:50发布

问题:

How do I send data on a SOCK_PACKET socket without specifying which host it's bound for? I've constructed the IP header to show where it should go, but write() won't work.

回答1:

Don't. use write(), use sendto().

If you use PF_PACKET,SOCK_DGRAM, then it builds the link-layer headers for you, which is normally what you want. You still need to build whatever higher protocol you are using on top though.

You specify a sockaddr_ll in the destination parameter. You can specify a link-layer unicast address, multicast address or broadcast address.