Skip to content

Instantly share code, notes, and snippets.

@straight-shoota
Last active June 21, 2022 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save straight-shoota/3f235b0cc046111806cba1d0d5195915 to your computer and use it in GitHub Desktop.
Save straight-shoota/3f235b0cc046111806cba1d0d5195915 to your computer and use it in GitHub Desktop.
Test support of SO_REUSEPORT
// 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;
}
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