Skip to content
Advertisement

C socket programming: Set ip header’s id to 0?

I want to send raw IP packets, which works perfectly fine, except for one field in the IP header.

I need to set the IP id to 0. I tried it the following way:

JavaScript

I then create a RAW_SOCKET with the IP_HDRINCL option set:

JavaScript

I then create a buffer, containing my IP header, UDP header and the payload and send it out.

However, the network stack keeps changing my id in the IP header. I need to include the IP header, but I also need to disable fragmentation and / or set the IP id to 0. How do I achieve this?

EDIT: I am on Linux

Advertisement

Answer

On linux, you can use AF_PACKET with SOCK_DGRAM sockets instead of AF_INET with SOCK_RAW and fill an sockaddr_ll.

Something like that:

JavaScript

However, to make you program work with Windows too, you should use libpcap.

Keep in mind that you should resolve the destination’s physical address by yourself (On linux, you can try using the rtnetlink interface for that).

See more on packet(7) and rtnetlink(7). If your project is Open Source, you can use my old project that doing some of these things (for more information, contact me).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement