3 #include <sys/socket.h>
4 #include <netinet/in.h>
9 #define TCPI_ATTR_READER(x) \
10 static VALUE tcp_info_##x(VALUE self) \
12 struct tcp_info *info = DATA_PTR(self); \
13 return UINT2NUM((uint32_t)info->tcpi_##x); \
16 TCPI_ATTR_READER(state
)
17 TCPI_ATTR_READER(ca_state
)
18 TCPI_ATTR_READER(retransmits
)
19 TCPI_ATTR_READER(probes
)
20 TCPI_ATTR_READER(backoff
)
21 TCPI_ATTR_READER(options
)
22 TCPI_ATTR_READER(snd_wscale
)
23 TCPI_ATTR_READER(rcv_wscale
)
26 TCPI_ATTR_READER(snd_mss
)
27 TCPI_ATTR_READER(rcv_mss
)
28 TCPI_ATTR_READER(unacked
)
29 TCPI_ATTR_READER(sacked
)
30 TCPI_ATTR_READER(lost
)
31 TCPI_ATTR_READER(retrans
)
32 TCPI_ATTR_READER(fackets
)
33 TCPI_ATTR_READER(last_data_sent
)
34 TCPI_ATTR_READER(last_ack_sent
)
35 TCPI_ATTR_READER(last_data_recv
)
36 TCPI_ATTR_READER(last_ack_recv
)
37 TCPI_ATTR_READER(pmtu
)
38 TCPI_ATTR_READER(rcv_ssthresh
)
40 TCPI_ATTR_READER(rttvar
)
41 TCPI_ATTR_READER(snd_ssthresh
)
42 TCPI_ATTR_READER(snd_cwnd
)
43 TCPI_ATTR_READER(advmss
)
44 TCPI_ATTR_READER(reordering
)
45 TCPI_ATTR_READER(rcv_rtt
)
46 TCPI_ATTR_READER(rcv_space
)
47 TCPI_ATTR_READER(total_retrans
)
49 static VALUE
alloc(VALUE klass
)
51 struct tcp_info
*info
= xmalloc(sizeof(struct tcp_info
));
53 /* Data_Make_Struct has an extra memset 0 which is so wasteful */
54 return Data_Wrap_Struct(klass
, NULL
, -1, info
);
60 * Raindrops::TCP_Info.new(tcp_socket) -> TCP_Info object
62 * Reads a TCP_Info object from any given +tcp_socket+. See the tcp(7)
63 * manpage and /usr/include/linux/tcp.h for more details.
65 static VALUE
init(VALUE self
, VALUE io
)
67 int fd
= my_fileno(io
);
68 struct tcp_info
*info
= DATA_PTR(self
);
69 socklen_t len
= (socklen_t
)sizeof(struct tcp_info
);
70 int rc
= getsockopt(fd
, IPPROTO_TCP
, TCP_INFO
, info
, &len
);
73 rb_sys_fail("getsockopt");
78 void Init_raindrops_linux_tcp_info(void)
80 VALUE cRaindrops
= rb_const_get(rb_cObject
, rb_intern("Raindrops"));
84 * Document-class: Raindrops::TCP_Info
86 * This is used to wrap "struct tcp_info" as described in tcp(7)
87 * and /usr/include/linux/tcp.h. The following readers methods
88 * are defined corresponding to the "tcpi_" fields in the
91 * In particular, the +last_data_recv+ field is useful for measuring
92 * the amount of time a client spent in the listen queue before
93 * +accept()+, but only if +TCP_DEFER_ACCEPT+ is used with the
94 * listen socket (it is on by default in Unicorn).
129 * http://kernel.org/doc/man-pages/online/pages/man7/tcp.7.html
131 cTCP_Info
= rb_define_class_under(cRaindrops
, "TCP_Info", rb_cObject
);
132 rb_define_alloc_func(cTCP_Info
, alloc
);
133 rb_define_private_method(cTCP_Info
, "initialize", init
, 1);
135 #define TCPI_DEFINE_METHOD(x) \
136 rb_define_method(cTCP_Info, #x, tcp_info_##x, 0)
138 TCPI_DEFINE_METHOD(state
);
139 TCPI_DEFINE_METHOD(ca_state
);
140 TCPI_DEFINE_METHOD(retransmits
);
141 TCPI_DEFINE_METHOD(probes
);
142 TCPI_DEFINE_METHOD(backoff
);
143 TCPI_DEFINE_METHOD(options
);
144 TCPI_DEFINE_METHOD(snd_wscale
);
145 TCPI_DEFINE_METHOD(rcv_wscale
);
146 TCPI_DEFINE_METHOD(rto
);
147 TCPI_DEFINE_METHOD(ato
);
148 TCPI_DEFINE_METHOD(snd_mss
);
149 TCPI_DEFINE_METHOD(rcv_mss
);
150 TCPI_DEFINE_METHOD(unacked
);
151 TCPI_DEFINE_METHOD(sacked
);
152 TCPI_DEFINE_METHOD(lost
);
153 TCPI_DEFINE_METHOD(retrans
);
154 TCPI_DEFINE_METHOD(fackets
);
155 TCPI_DEFINE_METHOD(last_data_sent
);
156 TCPI_DEFINE_METHOD(last_ack_sent
);
157 TCPI_DEFINE_METHOD(last_data_recv
);
158 TCPI_DEFINE_METHOD(last_ack_recv
);
159 TCPI_DEFINE_METHOD(pmtu
);
160 TCPI_DEFINE_METHOD(rcv_ssthresh
);
161 TCPI_DEFINE_METHOD(rtt
);
162 TCPI_DEFINE_METHOD(rttvar
);
163 TCPI_DEFINE_METHOD(snd_ssthresh
);
164 TCPI_DEFINE_METHOD(snd_cwnd
);
165 TCPI_DEFINE_METHOD(advmss
);
166 TCPI_DEFINE_METHOD(reordering
);
167 TCPI_DEFINE_METHOD(rcv_rtt
);
168 TCPI_DEFINE_METHOD(rcv_space
);
169 TCPI_DEFINE_METHOD(total_retrans
);
171 #endif /* TCP_INFO */
172 #endif /* __linux__ */