From bf2fb0a16091201a9b2798ebdea54e03c1c3e61b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 16 Mar 2017 03:16:52 +0000 Subject: define Raindrops::TCP hash for TCP states FreeBSD not only uses different values than Linux for TCP states, but different names, too. To ease writing portable code between the OSes, do more CPP metaprogramming via extconf.rb and define a common hash supported on both OSes. Putting all this in a hash allows for easy dumping and mapping in an OS-neutral way, since the actual TCP states are OS-independent. --- ext/raindrops/extconf.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'ext/raindrops/extconf.rb') diff --git a/ext/raindrops/extconf.rb b/ext/raindrops/extconf.rb index 5273b74..86c7d78 100644 --- a/ext/raindrops/extconf.rb +++ b/ext/raindrops/extconf.rb @@ -82,6 +82,33 @@ EOF "#{Shellwords.shellescape( %Q[rb_define_method(cTCP_Info,#{rbmethod},#{cfunc},0)])}" end + tcp_state_map = { + ESTABLISHED: %w(TCP_ESTABLISHED TCPS_ESTABLISHED), + SYN_SENT: %w(TCP_SYN_SENT TCPS_SYN_SENT), + SYN_RECV: %w(TCP_SYN_RECV TCPS_SYN_RECEIVED), + FIN_WAIT1: %w(TCP_FIN_WAIT1 TCPS_FIN_WAIT_1), + FIN_WAIT2: %w(TCP_FIN_WAIT2 TCPS_FIN_WAIT_2), + TIME_WAIT: %w(TCP_TIME_WAIT TCPS_TIME_WAIT), + CLOSE: %w(TCP_CLOSE TCPS_CLOSED), + CLOSE_WAIT: %w(TCP_CLOSE_WAIT TCPS_CLOSE_WAIT), + LAST_ACK: %w(TCP_LAST_ACK TCPS_LAST_ACK), + LISTEN: %w(TCP_LISTEN TCPS_LISTEN), + CLOSING: %w(TCP_CLOSING TCPS_CLOSING), + } + nstate = 0 + tcp_state_map.each do |state, try| + try.each do |os_name| + have_const(os_name, headers) or next + tcp_state_map[state] = os_name + nstate += 1 + end + end + if nstate == tcp_state_map.size + $defs << '-DRAINDROPS_TCP_STATES_ALL_KNOWN=1' + tcp_state_map.each do |state, name| + $defs << "-DRAINDROPS_TCP_#{state}=#{name}" + end + end end have_func("getpagesize", "unistd.h") -- cgit v1.2.3-24-ge0c7