1 # faithd, ruby version. requires v6-enabled ruby.
3 # highly experimental (not working right at all) and very limited
6 # $Id: faithd.rb,v 1.1.2.4 1999/05/10 17:06:30 itojun Exp $
12 # XXX should be derived from system headers
18 # TODO: OOB data handling
19 def tcpcopy(s1, s2, m)
20 STDERR.print "tcpcopy #{s1} #{s2}\n" if DEBUG
32 STDERR.print "tcpcopy #{s1} #{s2} finished\n" if DEBUG
37 def relay_ftp_passiveconn(s6, s4, dport6, dport4)
39 d6 = TCPserver.open("::", dport6).accept
40 d4 = TCPsocket.open(s4.getpeer[3], dport4)
42 t[0] = Thread.start do
45 t[1] = Thread.start do
56 def ftp_parse_2428(line)
57 if (line[0] != line[line.length - 1])
60 t = line.split(line[0 .. 0]) # as string
61 if (t.size != 4 || t[1] !~ /^[12]$/ || t[3] !~ /^\d+$/)
67 def relay_ftp_command(s6, s4, state)
68 STDERR.print "relay_ftp_command start\n" if DEBUG
71 STDERR.print "s6.gets\n" if DEBUG
73 STDERR.print "line is #{line}\n" if DEBUG
79 STDERR.print "line is #{line}\n" if DEBUG
80 if (line =~ /^EPSV\r\n/i)
81 STDERR.print "EPSV -> PASV\n" if DEBUG
84 elsif (line =~ /^EPRT\s+(.+)\r\n/i)
85 t = ftp_parse_2428($1)
87 s6.puts "501 illegal parameter to EPRT\r\n"
91 # some tricks should be here
92 s6.puts "501 illegal parameter to EPRT\r\n"
95 STDERR.print "fail: send #{line} as is\n" if DEBUG
104 STDERR.print "relay_ftp_command finish\n" if DEBUG
108 def relay_ftp_status(s4, s6, state)
109 STDERR.print "relay_ftp_status start\n" if DEBUG
117 # translate then copy
120 next if line =~ /^\d\d\d-/
121 next if line !~ /^\d/
123 # special post-processing
125 when /^221 / # result to QUIT
130 break if (line =~ /^\d\d\d /)
137 STDERR.print "relay_ftp_status finish\n" if DEBUG
141 def relay_ftp(sock, name)
142 STDERR.print "relay_ftp(#{sock}, #{name})\n" if DEBUG
144 STDERR.print "relay_ftp(#{sock}, #{name}) accepting\n" if DEBUG
146 STDERR.print "relay_ftp(#{sock}, #{name}) accepted #{s}\n" if DEBUG
149 STDERR.print "accepted #{s} -> #{Thread.current}\n" if DEBUG
153 t = s.getsockname.unpack("x8 x12 C4")
154 dest4 = "#{t[0]}.#{t[1]}.#{t[2]}.#{t[3]}"
161 STDERR.print "IPv6 dest: #{dest6} IPv4 dest: #{dest4}\n" if DEBUG
163 STDERR.print "connect to #{dest4} #{port4}\n" if DEBUG
164 s4 = TCPsocket.open(dest4, port4)
165 STDERR.print "connected to #{dest4} #{port4}, #{s4.addr[1]}\n" if DEBUG
168 # translate status line
169 state = relay_ftp_status(s4, s6, state)
170 break if state == nil
171 # translate command line
172 state = relay_ftp_command(s6, s4, state)
173 break if state == nil
175 STDERR.print "relay_ftp(#{sock}, #{name}) closing s4\n" if DEBUG
177 STDERR.print "relay_ftp(#{sock}, #{name}) closing s6\n" if DEBUG
179 STDERR.print "relay_ftp(#{sock}, #{name}) done\n" if DEBUG
182 STDERR.print "relay_ftp(#{sock}, #{name}) finished\n" if DEBUG
185 def relay_tcp(sock, name)
186 STDERR.print "relay_tcp(#{sock}, #{name})\n" if DEBUG
188 STDERR.print "relay_tcp(#{sock}, #{name}) accepting\n" if DEBUG
190 STDERR.print "relay_tcp(#{sock}, #{name}) accepted #{s}\n" if DEBUG
193 STDERR.print "accepted #{s} -> #{Thread.current}\n" if DEBUG
197 t = s.getsockname.unpack("x8 x12 C4")
198 dest4 = "#{t[0]}.#{t[1]}.#{t[2]}.#{t[3]}"
205 STDERR.print "IPv6 dest: #{dest6} IPv4 dest: #{dest4}\n" if DEBUG
207 STDERR.print "connect to #{dest4} #{port4}\n" if DEBUG
208 s4 = TCPsocket.open(dest4, port4)
209 STDERR.print "connected to #{dest4} #{port4}, #{s4.addr[1]}\n" if DEBUG
211 threads[i] = Thread.start do
219 STDERR.print "relay_tcp(#{sock}, #{name}) wait\n" if DEBUG
221 STDERR.print "relay_tcp(#{sock}, #{name}) wait #{i}\n" if DEBUG
223 STDERR.print "relay_tcp(#{sock}, #{name}) wait #{i} done\n" if DEBUG
225 STDERR.print "relay_tcp(#{sock}, #{name}) closing s4\n" if DEBUG
227 STDERR.print "relay_tcp(#{sock}, #{name}) closing s6\n" if DEBUG
229 STDERR.print "relay_tcp(#{sock}, #{name}) done\n" if DEBUG
232 STDERR.print "relay_tcp(#{sock}, #{name}) finished\n" if DEBUG
236 STDERR.print "usage: #{$0} [-f] port...\n"
239 #------------------------------------------------------------
243 while ARGV[0] =~ /^-/ do
259 ftpport = Socket.getservbyname("ftp")
263 t = Socket.getaddrinfo(nil, port, Socket::PF_INET6, Socket::SOCK_STREAM,
264 nil, Socket::AI_PASSIVE)
266 STDERR.print "FATAL: getaddrinfo failed (port=#{port})\n"
277 s = TCPserver.new(i[3], i[1])
278 n = Socket.getnameinfo(s.getsockname, Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV).join(" port ")
279 if i[6] == IPPROTO_IPV6
280 s.setsockopt(i[6], IPV6_FAITH, 1)
282 s.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
288 (0 .. sockpool.size - 1).each do |i|
289 STDERR.print "listen[#{i}]: #{sockpool[i]} #{names[i]}\n" if DEBUG
293 (0 .. sockpool.size - 1).each do |i|
294 listenthreads[i] = Thread.start do
296 STDERR.print "listen[#{i}]: thread #{Thread.current}\n" if DEBUG
298 STDERR.print "listen[#{i}]: thread #{Thread.current}\n" if DEBUG
301 relay_tcp(sockpool[i], names[i])
303 relay_ftp(sockpool[i], names[i])
308 for i in listenthreads