2 #include <sys/utsname.h>
4 /* true if TCP Fast Open is usable */
6 static VALUE eErrno_EPIPE
, eErrno_ECONNRESET
;
7 static ID id_set_backtrace
;
9 static void tfo_maybe(void)
11 VALUE mKgio
= rb_define_module("Kgio");
13 /* Deal with the case where system headers have not caught up */
15 /* Ensure Linux 3.7 or later for TCP_FASTOPEN */
21 if (sscanf(buf
.release
, "%u.%u", &maj
, &min
) != 2)
23 if (maj
< 3 || (maj
== 3 && min
< 7))
28 * KGIO_TFO_MAYBE will be false if a distro backports TFO
29 * to a pre-3.7 kernel, but includes the necessary constants
32 #if defined(MSG_FASTOPEN) && defined(TCP_FASTOPEN)
33 rb_define_const(mKgio
, "TCP_FASTOPEN", INT2NUM(TCP_FASTOPEN
));
34 rb_define_const(mKgio
, "MSG_FASTOPEN", INT2NUM(MSG_FASTOPEN
));
39 void kgio_raise_empty_bt(VALUE err
, const char *msg
)
41 VALUE exc
= rb_exc_new2(err
, msg
);
42 VALUE bt
= rb_ary_new();
44 rb_funcall(exc
, id_set_backtrace
, 1, bt
);
48 void kgio_wr_sys_fail(const char *msg
)
53 kgio_raise_empty_bt(eErrno_EPIPE
, msg
);
56 kgio_raise_empty_bt(eErrno_ECONNRESET
, msg
);
61 void kgio_rd_sys_fail(const char *msg
)
63 if (errno
== ECONNRESET
) {
65 kgio_raise_empty_bt(eErrno_ECONNRESET
, msg
);
70 void Init_kgio_ext(void)
72 VALUE mKgio
= rb_define_module("Kgio");
73 VALUE mPipeMethods
= rb_define_module_under(mKgio
, "PipeMethods");
74 VALUE mSocketMethods
= rb_define_module_under(mKgio
, "SocketMethods");
75 VALUE mWaiters
= rb_define_module_under(mKgio
, "DefaultWaiters");
77 id_set_backtrace
= rb_intern("set_backtrace");
78 eErrno_EPIPE
= rb_const_get(rb_mErrno
, rb_intern("EPIPE"));
79 eErrno_ECONNRESET
= rb_const_get(rb_mErrno
, rb_intern("ECONNRESET"));
82 * Returns the client IP address of the socket as a string
83 * (e.g. "127.0.0.1" or "::1").
84 * This is always the value of the Kgio::LOCALHOST constant
85 * for UNIX domain sockets.
87 rb_define_attr(mSocketMethods
, "kgio_addr", 1, 1);
88 rb_include_module(mPipeMethods
, mWaiters
);
89 rb_include_module(mSocketMethods
, mWaiters
);