Module: MockDnsServer::MessageHelper
- Defined in:
- lib/mock_dns_server/message_helper.rb
Constant Summary collapse
- MESSAGE_LENGTH_PACK_UNPACK_FORMAT =
'n'
Class Method Summary collapse
- .convertible_to_dnsruby_message?(object) ⇒ Boolean
-
.read_tcp_message(socket) ⇒ Object
Reads a message from a TCP connection.
-
.send_udp_and_get_response(message, host, port) ⇒ Object
Sends a UDP message and returns the response, using a temporary socket.
-
.tcp_message_package_for_write(message) ⇒ Object
Builds a string for a TCP client to send to a DNS server.
-
.to_dns_message(object) ⇒ Object
If the string can convert to a Dnsruby::Message without throwing an exception, return the Dnsruby::Message instance; else, return the original string.
- .udp_message_package_for_write(object) ⇒ Object
Class Method Details
.convertible_to_dnsruby_message?(object) ⇒ Boolean
24 25 26 |
# File 'lib/mock_dns_server/message_helper.rb', line 24 def self.(object) (object).is_a?(Dnsruby::Message) end |
.read_tcp_message(socket) ⇒ Object
Reads a message from a TCP connection. First gets the 2 byte length, then reads the payload. Attempts to convert the payload into a Dnsruby::Message.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mock_dns_server/message_helper.rb', line 54 def self.(socket) = socket.read(2) raise "Unable to read from socket; read returned nil" if .nil? = .unpack(MESSAGE_LENGTH_PACK_UNPACK_FORMAT).first bytes_not_yet_read = = '' while bytes_not_yet_read > 0 str = socket.read(bytes_not_yet_read) bytes_not_yet_read -= str.size << str end = MessageHelper.() end |
.send_udp_and_get_response(message, host, port) ⇒ Object
Sends a UDP message and returns the response, using a temporary socket.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mock_dns_server/message_helper.rb', line 75 def self.send_udp_and_get_response(, host, port) socket = UDPSocket.new = .encode if .is_a?(Dnsruby::Message) socket.send(, 0, host, port) _, _, _ = IO.select([socket], nil, nil) response_data, _ = socket.recvfrom(10_000) response = (response_data) socket.close response end |
.tcp_message_package_for_write(message) ⇒ Object
Builds a string for a TCP client to send to a DNS server
35 36 37 38 39 |
# File 'lib/mock_dns_server/message_helper.rb', line 35 def self.() = .encode if .is_a?(Dnsruby::Message) size_field = [.size].pack(MESSAGE_LENGTH_PACK_UNPACK_FORMAT) size_field + end |
.to_dns_message(object) ⇒ Object
If the string can convert to a Dnsruby::Message without throwing an exception, return the Dnsruby::Message instance; else, return the original string.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mock_dns_server/message_helper.rb', line 10 def self.(object) case object when String begin Dnsruby::Message.decode(object) rescue object end when Dnsruby::Message object end end |