receiving the IP header with a UDP datagram

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to receive UDP packets with the IP header
information. I can see how to do this in XP, which is
similar to how it is done in BSD with recvmsg.

But that mechanism is exclusive to XP/2003. How do I do
this in Windows 2000? The documentation seems to imply
raw sockets will always give the IP header when reading
data, but I'm not seeing it.

Fragements of my code:
s = socket (AF_INET, SOCK_RAW, IPPROTO_UDP);

memset (&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (5002);
sin.sin_addr.s_addr = htonl (INADDR_ANY);

if (bind (s, (const struct sockaddr *)&sin, sizeof
(sin))) ...


sinlen = sizeof (sin);
r = recvfrom (s, buf, sizeof (buf), 0, (struct
sockaddr *) &sin, &sinlen);
 
I can confirm that you can use raw sockets to capture/view IP header
information in Windows 2000. The only thing which I think you're missing is
to call setsockopt(...., IPPROTO_IP, IP_HDRINCL, ...);
 
the setsockopt() didn't make a difference, nor should it
have since it only applies to outgoing packets.

Tried my code on both 2000 and XP with the same results.

I would be interested in your sample code.
 
After doing some more reasarch (and finding out a few
others have asked with no answers), it appears I can only
receive ALL packets hitting the network interface via
SIO_RCVALL or only certain types of ICMP messages
directed at me. I can't just open a raw socket using
IPPROTO_UDP, bind it and recieve just messages bound for
the port I am interested in.

Which means I need to have a dummy UDP socket bound to
the port I am interested in to avoid ICMP destination
port unreachable messages from being sent back from my
machine, and have my server's main loop be woken up for
EVERY SINGLE PACKET on the network or hope the server is
on a switching hub. This is a rediculous amount of time
spent in user mode, and going between user mode and
waiting on I/O.

All for what is probably a two line bug in winsuck's
source code keeping me from seeting the damn header.

This also means the Winsock Annex topic "TCP/IP Raw
Sockets" in the Plafrom SDK documentation is dead wrong:

"An application always gets the IP header at the front of
each received datagram regardless of the IP_HDRINCL
option."
 
Ach....you're right...I was taking a look at a some code I'd written
to -=write=- raw packets. Lack of coffee, sorry.
 
Back
Top