initial commit
authorJann Horn <jann@thejh.net>
Thu, 12 Mar 2015 18:58:32 +0000 (19:58 +0100)
committerJann Horn <jann@thejh.net>
Thu, 12 Mar 2015 18:58:32 +0000 (19:58 +0100)
.gitignore [new file with mode: 0644]
README [new file with mode: 0644]
compile.sh [new file with mode: 0755]
netboost.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..4d9d4d8
--- /dev/null
@@ -0,0 +1 @@
+netboost.so
diff --git a/README b/README
new file mode 100644 (file)
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 (executable)
index 0000000..0d1d725
--- /dev/null
@@ -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 (file)
index 0000000..14d92a8
--- /dev/null
@@ -0,0 +1,33 @@
+#define _GNU_SOURCE
+#include <sys/socket.h>
+#include <netinet/ip.h>
+#include <netinet/tcp.h>
+#include <dlfcn.h>
+#include <string.h>
+
+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;
+}