From: Jann Horn Date: Thu, 12 Mar 2015 18:58:32 +0000 (+0100) Subject: initial commit X-Git-Url: http://git.thejh.net/?a=commitdiff_plain;h=4e90c4221388c79896c379e0d5c995c56a281813;p=netboost.git initial commit --- 4e90c4221388c79896c379e0d5c995c56a281813 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d9d4d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +netboost.so diff --git a/README b/README new file mode 100644 index 0000000..8918f27 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +This is a little helper for setting TCP_NODELAY on all sockets that a program +creates, thereby reducing round-trip times. +Especially for loopback sockets, this gets rid of nasty 40ms delays created +by the nagle algorithm. diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..0d1d725 --- /dev/null +++ b/compile.sh @@ -0,0 +1,2 @@ +#!/bin/sh +gcc -shared -o netboost.so netboost.c -Wall -ldl -fPIC diff --git a/netboost.c b/netboost.c new file mode 100644 index 0000000..14d92a8 --- /dev/null +++ b/netboost.c @@ -0,0 +1,33 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { + static int (*real_connect)(int, const struct sockaddr *, socklen_t); + if (!real_connect) { + real_connect = dlsym(RTLD_NEXT, "connect"); + } + int res = real_connect(sockfd, addr, addrlen); + if (res != 0) return res; + + static const int one = 1; + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); + return 0; +} + +int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { + static int (*real_accept)(int, struct sockaddr *, socklen_t *); + if (!real_accept) { + real_accept = dlsym(RTLD_NEXT, "accept"); + } + int res = real_accept(sockfd, addr, addrlen); + if (res == -1) return res; + + static const int one = 1; + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); + + return res; +}