Class: DNS::Server::Dispatcher::ConnectionDispatcher::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/faildns/server/dispatcher/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher, what) ⇒ Socket

Returns a new instance of Socket.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/faildns/server/dispatcher/socket.rb', line 31

def initialize (dispatcher, what)
  @dispatcher = dispatcher

  if what.is_a? TCPSocket
    @type = :TCP
    @ip   = what.peeraddr[3]
    @port = what.addr[1]

    @socket = what
  else
    @type = :UDP
    @ip   = what[3]
    @port = what[1]

    @socket = dispatcher.connection.listening[:UDP]
  end
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



29
30
31
# File 'lib/faildns/server/dispatcher/socket.rb', line 29

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



29
30
31
# File 'lib/faildns/server/dispatcher/socket.rb', line 29

def port
  @port
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/faildns/server/dispatcher/socket.rb', line 29

def type
  @type
end

Instance Method Details

#closeObject



80
81
82
83
84
# File 'lib/faildns/server/dispatcher/socket.rb', line 80

def close
  if @socket.is_a? TCPSocket
    @socket.close
  end
end

#send(message, close = true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/faildns/server/dispatcher/socket.rb', line 49

def send (message, close=true)
  @dispatcher.dispatch :output, self, message

  if @type == :UDP && message.pack.length > 512
    [message.additionals, message.authorities, message.answers, message.questions].each {|rr|
      while (tmp = message.pack).length > 512 && r.pop; end

      if tmp.length <= 512
        break
      end
    }

    message.header.questions   = message.questions.length
    message.header.answers     = message.answers.length
    message.header.authorities = message.authorities.length
    message.header.additionals = message.additionals.length

    message.header.truncated!
  end

  if @socket.is_a? TCPSocket
    @socket.send_nonblock(message.pack)

    if close
      @socket.close
    end
  else
    @socket.send(message.pack, 0, ::Socket.pack_sockaddr_in(@port, @ip))
  end
end