Skip to content
Advertisement

Raw Sockets in C

1.

socket(AF_INET, SOCK_RAW, IPPROTO_RAW);

The linux manual page says about this code.

In socket option, if IP_HDRINCL is set, I can make IP header. Am I right? If it’s right, above socket also let me make TCP header, too?

Then, if IP_HDRINCL is not set, what means above socket?

2.

 socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
 socket(AF_INET, SOCK_RAW, IPPROTO_UDP);

what means above code comparing to number 1 question’s code?

I know IPPROTO_RAW can’t receive any IP packets. And here, these sockets only can receive TCP packets, and UDP pakcets each.(Can I see IP Header, Ethernet Header also?) But how about sending?? I don’know exactly about this.

Advertisement

Answer

  1. IP_HDRINCL means: I want my data (for send and recv) to include the ip hdr. And if your data include the ip hdr, it means that the tcp hdr follows (just after the ip hdr), and finally the app’s message too (the message your normally give to send …). Without IP_HDRINCL, you have access to apps data only.

  2. Yes, IPPROTO_TCP and IPPROTO_UDP whith SOCK_RAW are just filters as you say, for sending and receiving. Use IPPROTO_RAW to be able to send any TCP/IP packet (no filter). But to also receive packets, you need also to change AF_INET into AF_PACKET.

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