implement TCP Fast Open support (client + server)
[kgio.git] / ext / kgio / kgio_ext.c
blob03b30e5e14b5efe3226b6ef55932d33906496606
1 #include "kgio.h"
2 #include <sys/utsname.h>
3 #include <stdio.h>
4 /* true if TCP Fast Open is usable */
5 unsigned kgio_tfo;
7 static void tfo_maybe(void)
9 VALUE mKgio = rb_define_module("Kgio");
11 /* Deal with the case where system headers have not caught up */
12 if (KGIO_TFO_MAYBE) {
13 /* Ensure Linux 3.7 or later for TCP_FASTOPEN */
14 struct utsname buf;
15 unsigned maj, min;
17 if (uname(&buf) != 0)
18 rb_sys_fail("uname");
19 if (sscanf(buf.release, "%u.%u", &maj, &min) != 2)
20 return;
21 if (maj < 3 || (maj == 3 && min < 7))
22 return;
26 * KGIO_TFO_MAYBE will be false if a distro backports TFO
27 * to a pre-3.7 kernel, but includes the necessary constants
28 * in system headers
30 #if defined(MSG_FASTOPEN) && defined(TCP_FASTOPEN)
31 rb_define_const(mKgio, "TCP_FASTOPEN", INT2NUM(TCP_FASTOPEN));
32 rb_define_const(mKgio, "MSG_FASTOPEN", INT2NUM(MSG_FASTOPEN));
33 kgio_tfo = 1;
34 #endif
37 void Init_kgio_ext(void)
39 tfo_maybe();
40 init_kgio_wait();
41 init_kgio_read_write();
42 init_kgio_connect();
43 init_kgio_accept();
44 init_kgio_autopush();
45 init_kgio_poll();
46 init_kgio_tryopen();