Reputation: 67
So I created a socket with socket(AF_INET, SOCK_RAW, IPPROTO_UDP). And then I have it recv() with a infinite loop. I know it can captures all datagrams. But will it stop datagrams going to the right places? I made a little experiment. I send very simple UDP message from one program to another. And they both received the message. I assume the raw socket won't block any UDP messages. I then read about it in the second paragraph of this question's best answer. It says an unbound udp socket will mess up the system. So I am not sure that is the same with raw socket. Or is it different on linux and windows?
And another quick question: binding socket(AF_INET, SOCK_RAW, IPPROTO_UDP) with a port won't do anything, right? I did it and the socket still receive messages going to all kinds of other ports.
Upvotes: 2
Views: 1573
Reputation: 12708
Not. Raw sockets deliver all packets to all registered socket users, so you'll get a copy of the packet as soon as it is got into the system, but a UDP socket will receive it also.
To the second question... you don't bind(2)
a raw socket, so you cannot associate it with a port number.
Upvotes: 1