Permalink
Browse files

snapshot

  • Loading branch information...
1 parent 435b676 commit 5aaff5a4243ed63f1156828baf45fd8b284e13a6 @dlitz committed Jun 24, 2009
Showing with 75 additions and 16 deletions.
  1. +12 −2 bin/maglevdns
  2. +2 −1 lib/maglevdns.rb
  3. +43 −0 lib/maglevdns/tcpconnectionthread.rb
  4. +13 −10 lib/maglevdns/tcplistener.rb
  5. +5 −3 lib/maglevdns/udplistener.rb
View
14 bin/maglevdns
@@ -35,11 +35,21 @@ opts = OptionParser.new do |opts|
end
opts.on("--listen HOST,PORT", "Listen on the specified host/port combination") do |optarg|
host, port = optarg.split(",")
+ ipaddr = IPAddr.new(host)
options[:listener_factory_lambdas] << lambda { |request_queue|
MaglevDNS::UDPListenerThread.new(
:request_queue => request_queue,
- :address_family => Socket::AF_INET6,
- :bind_address => [host, port]
+ :address_family => ipaddr.family,
+ :host => ipaddr.to_s,
+ :port => port
+ )
+ }
+ options[:listener_factory_lambdas] << lambda { |request_queue|
+ MaglevDNS::TCPListenerThread.new(
+ :request_queue => request_queue,
+ :address_family => ipaddr.family,
+ :host => ipaddr.to_s,
+ :port => port
)
}
end
View
3 lib/maglevdns.rb
@@ -34,6 +34,7 @@
require 'maglevdns/server'
# Listeners
-require 'maglevdns/udplistener'
+require 'maglevdns/tcpconnectionthread'
require 'maglevdns/tcplistener'
+require 'maglevdns/udplistener'
View
43 lib/maglevdns/tcpconnectionthread.rb
@@ -0,0 +1,43 @@
+#--
+# MaglevDNS
+# Copyright (c) 2009 Dwayne C. Litzenberger <dlitz@dlitz.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#++
+
+module MaglevDNS
+
+ # Thread for handling incoming TCP connections
+ class TCPConnectionThread < StoppableThread
+
+ def initialize(client_addr_family, client_host, client_port, sock, request_queue)
+ @client_addr_family = client_addr_family
+ @client_host = client_host
+ @client_port = client_port
+ @sock = sock
+ @request_queue = request_queue
+ super()
+ end
+
+ private
+ def thread_main
+ puts "TCP connection received" # TODO XXX FIXME
+ ensure
+ # Make sure we always close the socket
+ @sock.close
+ end
+
+ end
+
+end
View
23 lib/maglevdns/tcplistener.rb
@@ -23,36 +23,39 @@ module MaglevDNS
class TCPListenerThread < StoppableThread
def initialize(options={})
- @listen_address = {:family => options[:address_family], :bind_address => options[:bind_address]}
+ @listen_address = {
+ :family => options[:address_family],
+ :bind_args => [Socket::pack_sockaddr_in(options[:port], options[:host])],
+ }
@request_queue = options[:request_queue]
- super
+ super()
end
private
def thread_main
Socket.open(@listen_address[:family], Socket::Constants::SOCK_STREAM, 0) do |sock|
- sock.bind(*@listen_address[:bind_address])
+ sock.bind(*@listen_address[:bind_args])
sock.listen(10) # XXX - hard-coded backlog
loop do
rr = IO::select([@stop_pipe_r, sock], [], [])[0]
check_stop
raise "BUG: socket not returned by select()" unless rr.include?(sock)
- client = sock.accept()
- puts "client: #{client.inspect}"
+ client_sock, client_addr = sock.accept()
+ port, host = Socket::unpack_sockaddr_in(client_addr)
+ ipaddr = IPAddr.new(host)
+ # TODO FIXME: add thread to ThreadContainer
+ TCPConnectionThread.new(ipaddr.family, ipaddr.to_s, port, client_sock, @request_queue)
# @request_queue << {
# :listener => self,
# :raw_message => msg,
# :tcp => true,
-# :address => addr,
-# :sock => sock,
+# :address => client_addr,
+# :sock => client_sock,
# :respond_lambda => lambda {|*args| respond(*args) },
# }
end
end
end
-# def respond(request, raw_response)
-# request[:sock].send(raw_response, 0, request[:address][3], request[:address][1])
-# end
end
end
View
8 lib/maglevdns/udplistener.rb
@@ -23,15 +23,18 @@ module MaglevDNS
class UDPListenerThread < StoppableThread
def initialize(options={})
- @listen_address = {:family => options[:address_family], :bind_address => options[:bind_address]}
+ @listen_address = {
+ :family => options[:address_family],
+ :bind_args => [options[:host], options[:port]],
+ }
@request_queue = options[:request_queue]
super()
end
private
def thread_main
UDPSocket.open(@listen_address[:family]) do |sock|
- sock.bind(*@listen_address[:bind_address])
+ sock.bind(*@listen_address[:bind_args])
loop do
rr = IO::select([@stop_pipe_r, sock], [], [])[0]
check_stop
@@ -54,6 +57,5 @@ def thread_main
end
end
-
end
end

0 comments on commit 5aaff5a

Please sign in to comment.