Sock.setsockopt how?

i want to use specific tcp\dup socketopts.
in my case i want to use IP_TRANSPARENT SOCKET.
this is the page of tproxy documentation:

the option in the kernel ic constant number 19.

i have seen some examples on with setsockopt like this
http://pastie.org/314914 or the code in bottom.
but i am unable to understand how am i suppose to set the constant?
i have looked at the ruby source code and didnt found anywhere the word
IP_TRANSPARENT so i suppose it wasnt integrated into the source.
any suggestions?

Thanks,
Eliezer

require ‘socket’

opt = [1, 500_000].pack(“I_2”)

sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(3232, ‘192.168.1.10’)

sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, opt)
puts “sockopt = #{sock.getsockopt(Socket::SOL_SOCKET,
Socket::SO_RCVTIMEO).inspect}”

sock.connect(sockaddr)

#this tells the server at 192.168.1.10:3232 to wait 10 seconds before
responding, so we should get a timeout on our socket
sock.write(“10\r\n”)

start_time = Time.now
result = sock.gets
time = Time.now - start_time

puts “read: #{sock.gets} after: #{time} seconds”
#no timeout occured, this read returns after 10 seconds

Eliezer C. [email protected] wrote:

i want to use specific tcp\dup socketopts.
in my case i want to use IP_TRANSPARENT SOCKET.
this is the page of tproxy documentation:
Linux Kernel Documentation / networking / tproxy.txt

the option in the kernel ic constant number 19.

Until Ruby exposes the IP_TRANSPARENT constant, you can just use 19
in your code (or define the constant yourself):

    IP_TRANSPARENT = 19

socket.setsockopt(Socket::SOL_IP, IP_TRANSPARENT, 1)

i have seen some examples on with setsockopt like this
http://pastie.org/314914 or the code in bottom.
but i am unable to understand how am i suppose to set the constant?
i have looked at the ruby source code and didnt found anywhere the
word IP_TRANSPARENT so i suppose it wasnt integrated into the
source.
any suggestions?

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

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 W. 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