0

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 other 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

avd
  • 13,993
  • 32
  • 78
  • 99
  • I got where ETH_IP is . SO I have changed the question. I apologise for that. Please answer the above new question – avd Nov 25 '09 at 03:39

2 Answers2

2

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

Jack
  • 1,398
  • 3
  • 16
  • 26
  • pcap does not parse the IP headers or the DHCP packets. It just allows you, as mentioned by Jack, to *filter* in the kernel, so your application is not overwhelmed by packets you do not want. – bortzmeyer Nov 25 '09 at 20:28
1

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.

Left For Archive
  • 2,626
  • 1
  • 18
  • 19
  • I think I wont need link layer header bcoz I am implementing DHCP client so I need only IPv4 header. – avd Nov 25 '09 at 04:58