Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is raw socket datagram socket or not?

for non-blocking datagram socket, like UDP, when I call write()/send() on the socket, each call of write()/send() or read()/recv() deal with exactly 1 packet.

I'm wondering if raw socket, like the below, is a datagram socket or not?

int on = 1;
rawfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
setsockopt(IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));
like image 245
user1944267 Avatar asked Oct 04 '22 19:10

user1944267


2 Answers

That depends on the kind of IP header you will include in your packets (TCP or UDP). Actually it's more easier to include the UDP header since the kernel will manage some TCP mechanism.

So you have to add the UDP header in your packets, then it will be a datagram socket.

like image 115
nouney Avatar answered Oct 12 '22 11:10

nouney


When you send data out, TCP/IP stack will add TCP/UDP header, IP header and then Ethernet header, and network work card transmit the whole packet out. For raw socket, you prepare all the headers(TCP/UDP, IP and MAC), and network work card transmit the whole packet out. So whether it is datagram depends on the header you add is TCP or UDP.

like image 36
Hardy Feng Avatar answered Oct 12 '22 10:10

Hardy Feng