Skip to content

Instantly share code, notes, and snippets.

@azat
Created March 15, 2019 12:51
Show Gist options
  • Save azat/aba9e905605b2d77aa5afc4a89cbc8c0 to your computer and use it in GitHub Desktop.
Save azat/aba9e905605b2d77aa5afc4a89cbc8c0 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
static size_t tcp_nodelay_count;
static size_t tcp_nodelay_errors;
int (*socket_orig)(int domain, int type, int protocol) = NULL;
__attribute__((constructor)) static void socket_init(void)
{
socket_orig = dlsym(RTLD_NEXT, "socket");
}
int socket(int domain, int type, int protocol)
{
int fd = socket_orig(domain, type, protocol);
if (fd < 0) {
return fd;
}
if (type == SOCK_STREAM) {
int one = 1;
++tcp_nodelay_count;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one))) {
++tcp_nodelay_errors;
}
}
return fd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment