Last active
June 21, 2022 11:57
-
-
Save straight-shoota/3f235b0cc046111806cba1d0d5195915 to your computer and use it in GitHub Desktop.
Test support of SO_REUSEPORT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple program to test support of SO_REUSEPORT | |
// Adapted from https://blog.dubbelboer.com/2016/04/23/so-reuseport.html | |
#include <stdio.h> | |
#include <errno.h> | |
#include <sys/socket.h> | |
#include <unistd.h> | |
int main() { | |
#ifdef SO_REUSEPORT | |
int sock = socket(AF_INET, SOCK_STREAM, 0); | |
int reuse = 1; | |
int size = sizeof(reuse); | |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void*)&reuse, size) < 0) { | |
if (errno == EINVAL || errno == ENOPROTOOPT) { | |
printf("SO_REUSEPORT is not supported by your kernel\n"); | |
} else { | |
printf("unknown error\n"); | |
} | |
} else { | |
printf("SO_REUSEPORT is supported\n"); | |
if (getsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void*)&reuse, &size) < 0) { | |
if(errno == ENOPROTOOPT) { | |
printf("failed getsockopt: protocol not supported\n"); | |
} else { | |
printf("unknown error\n"); | |
} | |
} else { | |
printf("getsockopt works: %d", reuse); | |
} | |
} | |
close(sock); | |
#else | |
printf("SO_REUSEPORT is not supported by your include files\n"); | |
#endif | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SO_REUSEPORT is supported | |
failed getsockopt: protocol not supported |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment