开发者

Accessing data link layer packets

开发者 https://www.devze.com 2022-12-12 05:23 出处:网络
I want to create a socket for accessing IPv4 packets from data link layer. From unix network programming V1,

I want to create a socket for accessing IPv4 packets from data link layer. From unix network programming V1,

socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))

1)I am implementing a dhcp client, is this the correct way of doing that? (means without accessing data link layer, i cannot receive reply from dhcp server) or is there any other easier way? also, since this socket will receive all IPv4 packets destined for my system, how should I distinguish dhcp reply packet from ot开发者_如何学Cher packets?

2)please suggest me a good link/tuorial for network programming with data link layer access. In the above book, it is not detailed description.

This is my code


Did you tried looking at PCAP libraries? It provides nice filtering functions on IP, port and other things.


Do you need the link layer headers too? If so, You need to use SOCK_RAWSOCK_DGRAM will remove the link layer header before feeding it to your application.

You can identify DHCP requests by the source and destination ports, since DHCP generates traffic on UDP ports 67 and 68.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号