[ This is a repost of the following article: ]
[ Subject: raw sockets, IP header ]
[ Newsgroups: comp.unix.programmer ]
Sorry for the multipost, but I posted this on CUP recently, and have
gotten no responses as of yet, and thought some of you guys might
have some information...
So, for learning experience and a future project I'm experimenting
with raw sockets. I'm trying to send a raw packet with the 'don't
fragment' bit set in the IP header to try to determine the path MTU.
I will embed an ICMP packet as my IP payload, and do an echo request,
and thus hopefully get either:
1) an echo reply
2) a "message too long" error reply
So, I have a buffer that contains my IP header and ICMP header. I
set the appropriate fields of the IP header and the ICMP header,
and I send the packet out with sendto(). However, recvfrom() blocks
and I never get a reply. A few of questions (link to code at bottom):
1) Do I call socket() with a protocol of IPPROTO_RAW or IPPROTO_ICMP?
(note that I'm setting the IP_HDRINCL socket option so the kernel knows
that the first byte of data is the first byte of the IP header itself).
2) Do I need any payload for the ICMP packet?
3) Will the kernel do IP and ICMP checksum for me? My intuition says yes,
but in UNPV1 Stevens manually computes an ICMP checksum (chapter 25).
4) Assuming I can get some response from my remote host at some point,
to actually get the path MTU discovery to work, do I actually have to
send large IP packets or can I just set the ip_len field in the IP header
to a large number to simulate a large packet?
5) Is there a way to get the kernel to fill in the proper source IP
address in the IP header? (supposedly setting ip_id=0 in the header
tells the kernel to set the packet ID, so maybe something for source IP?)
Depending on where this packet goes, it could be one of several IP
addresses for the machine. If the kernel can't do it, am I going to have
to parse the routing table and find which interface the packet should be
sent out on?
Thanks for any help anyone can offer... the code is here:
josh(at)cc.gatech.edu | http://intmain.net:800
298322 local keystrokes since last reboot (26 days ago)