From: Eliezer Croitoru Date: 2012-07-10T11:39:39+09:00 Subject: Re: sock.setsockopt how? Thanks Eric, you helped me a lot. It took me a while to understand how to use socket and specifically in ruby. for now i just added to the first line in socket.rb the constant as: Socket::IP_TRANSPARENT = 19 but i want tproxy to be a more native to the user so i intend to extend the class somehow. and i will try later on to extend some exist http proxy with the tproxy feature. example code for TPROXY client: #!/usr/bin/env ruby require 'socket' Socket::IP_TRANSPARENT = 19 #for this to work you must load the kernel tproxy modules: # nf_tproxy_core # xt_TPROXY # xt_socket sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) socksrcaddr = Socket.pack_sockaddr_in(3232, '127.0.0.7') sockdstaddr = Socket.pack_sockaddr_in(3232, '127.0.0.1') sock.setsockopt(Socket::SOL_IP, Socket::IP_TRANSPARENT, 1) sock.bind(socksrcaddr) sock.connect(sockdstaddr) sock.write("4\r\n") start_time = Time.now begin result = sock.recvfrom(8192) rescue Errno::EAGAIN puts "got timeout -- current state of the code this won't get hit because recvfrom isn't looking for a newline, just reads the first bit of data sent" end time = Time.now - start_time puts "read: #{result.inspect} after: #{time} seconds" #timeout occured, this read returns after 4 seconds #note -- this is testing against a server running, sample code for server here: http://pastie.org/314915 On 7/9/2012 11:24 PM, Eric Wong wrote: > You should also submit a patch to http://bugs.ruby-lang.org to > get IP_TRANSPARENT added to the list of constants Ruby may exposed. > There's already a lot of platform-dependent constants that are > conditionally defined in ext/socket/mkconstants.rb > if you dont mind me asking, how a constant defined in this c source? i couldn't understand it myself and i am not familiar with c programing so a pointer will help me. Thanks, Eliezer -- Eliezer Croitoru https://www1.ngtech.co.il IT consulting for Nonprofit organizations eliezer ngtech.co.il