C – setsockopt exception error, “Protocol not available”

setsockopt exception error, “Protocol not available”… here is a solution to the problem.

setsockopt exception error, “Protocol not available”

Here’s the applicable code, along with the exact error.

sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if ( setsockopt( sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on) ) < 0 )
{
    printf("%s\n", strerror(errno));
}
if ( setsockopt( sd, IPPROTO_IP, IP_DF, &on, sizeof(on) ) < 0 )
{
    printf("%s\n", strerror(errno));
    printf("DF\n");
}

Error:

root@PC:~# gcc main.c
main.c: In function ‘main’:
main.c:71: warning: format not a string literal and no format arguments
root@PC:~# ./a.out localhost
Protocol not available
DF

Oddly enough, the second setsockopt goes wrong, while the first one doesn’t.

Solution

IP_DF is a packet flag, not a socket option. Valid socket options are available in the ip(7) man page.

Related Problems and Solutions