Setsockopt does not work for IPPROTO_TCP IP_TOS in C

My code is not working. I play the root role (same behavior as a regular user)

First I want to install TOS and then get the value.

int tos_local = 0x28; if (setsockopt(sockfd, IPPROTO_TCP, IP_TOS, &tos_local, sizeof(tos_local))) { error("error at socket option"); } else { int tos=0; int toslen=0; if (getsockopt(sockfd, IPPROTO_TCP, IP_TOS, &tos, &toslen) < 0) { error("error to get option"); }else { printf ("changing tos opt = %d\n",tos); } } 

printf prints

change tos opt = 0

I would expect to print 0x28 (40).

What is the problem?

Correct answer:

  if (setsockopt(sockfd, **IPPROTO_IP**, IP_TOS, &tos_local, sizeof(tos_local))) { int tos=0; int toslen=sizeof(tos); //that line here if (getsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, &toslen) < 0) { 
+6
source share
2 answers

IP_TOS has the IPPROTO_IP level, not IPPROTO_TCP .

See the documentation .

This affects both the setup and the receipt of the option.

Also, what Seth said about initializing a length parameter, which only affects getsockopt .

+6
source

When you call the getsockopt method, you pass the size of the memory & tos points to. In other words, initialize toslen to sizeof (tos).

+3
source

Source: https://habr.com/ru/post/889130/


All Articles