vendor/bundle/ruby/2.7.0/gems/dnsruby-1.61.5/lib/dnsruby/packet_sender.rb
# Asynchronously send a Message to the server. The send can be done using just
# Dnsruby. Support for EventMachine has been deprecated.
# 
# == Dnsruby pure Ruby event loop :
# 
# A client_queue is supplied by the client,
# along with an optional client_query_id to identify the response. The client_query_id
# is generated, if not supplied, and returned to the client.
# When the response is known, the tuple
# (query_id, response_message, response_exception) is put in the queue for the client to process.
# 
# The query is sent synchronously in the caller's thread. The select thread is then used to
# listen for and process the response (up to pushing it to the client_queue). The client thread
# is then used to retrieve the response and deal with it.
# 
# Takes :
# 
# * msg - the message to send
# * client_queue - a Queue to push the response to, when it arrives
# * client_query_id - an optional ID to identify the query to the client
# * use_tcp - whether to use TCP (defaults to PacketSender.use_tcp)
# 
# Returns :
# 
# * client_query_id - to identify the query response to the client. This ID is
# generated if it is not passed in by the client
# 
# If the native Dsnruby networking layer is being used, then this method returns the client_query_id
# 
#     id = res.send_async(msg, queue)
#     NOT SUPPORTED : id = res.send_async(msg, queue, use_tcp)
#     id = res.send_async(msg, queue, id)
#     id = res.send_async(msg, queue, id, use_tcp)
# 
# Use Message#send_raw to send the packet with an untouched header.
# Use Message#do_caching to tell dnsruby whether to check the cache before
# sending, and update the cache upon receiving a response.
# Use Message#do_validation to tell dnsruby whether or not to do DNSSEC
# validation for this particular packet (assuming SingleResolver#dnssec == true)
# Note that these options should not normally be used!
Asynchronously send a Message to the server. The send can be done using just
Dnsruby. Support for EventMachine has been deprecated.

Dnsruby pure Ruby event loop .

A client_queue is supplied by the client,
along with an optional client_query_id to identify the response. The client_query_id
is generated, if not supplied, and returned to the client.
When the response is known, the tuple
(query_id, response_message, response_exception) is put in the queue for the client to process.

The query is sent synchronously in the caller's thread. The select thread is then used to
listen for and process the response (up to pushing it to the client_queue). The client thread
is then used to retrieve the response and deal with it.

Takes .

the message to send
client_queue - a Queue to push the response to, when it arrives
client_query_id - an optional ID to identify the query to the client
use_tcp - whether to use TCP (defaults to PacketSender.use_tcp)

Returns .

to identify the query response to the client. This ID is
generated if it is not passed in by the client

If the native Dsnruby networking layer is being used, then this method returns the client_query_id



Use Message#send_raw to send the packet with an untouched header.
Use Message#do_caching to tell dnsruby whether to check the cache before
sending, and update the cache upon receiving a response.
Use Message#do_validation to tell dnsruby whether or not to do DNSSEC
validation for this particular packet (assuming SingleResolver#dnssec == true)
Note that these options should not normally be used!
Asynchronously send a Message to the server . The send can be done using just Dnsruby . Support for EventMachine has been deprecated . Dnsruby pure Ruby event loop . A client_queue is supplied by the client along with an optional client_query_id to identify the response . The client_query_id is generated if not supplied and returned to the client . When the response is known the tuple ( query_id response_message response_exception ) is put in the queue for the client to process . The query is sent synchronously in the caller ' s thread . The select thread is then used to listen for and process the response ( up to pushing it to the client_queue ) . The client thread is then used to retrieve the response and deal with it . Takes . the message to send client_queue - a Queue to push the response to when it arrives client_query_id - an optional ID to identify the query to the client use_tcp - whether to use TCP ( defaults to PacketSender . use_tcp ) Returns . to identify the query response to the client . This ID is generated if it is not passed in by the client If the native Dsnruby networking layer is being used then this method returns the client_query_id Use Message#send_raw to send the packet with an untouched header . Use Message#do_caching to tell dnsruby whether to check the cache before sending and update the cache upon receiving a response . Use Message#do_validation to tell dnsruby whether or not to do DNSSEC validation for this particular packet ( assuming SingleResolver#dnssec == true ) Note that these options should not normally be used!
def send_async(*args) 
      msg = args[0]
      client_query_id = nil
      client_queue = nil
      use_tcp = @use_tcp
      if (msg.kind_of? String)
        msg = Message.new(msg)
        if (@dnssec)
          msg.header.cd = @dnssec 
          if (Dnssec.no_keys?)
            msg.header.cd = false
          end
        end
      end
      if (args.length > 1)
        if (args[1].class==Queue)
          client_queue = args[1]
        elsif (args.length == 2)
          use_tcp = args[1]
        end
        if (args.length > 2)
          client_query_id = args[2]
          if (args.length > 3)
            use_tcp = args[3]
          end
        end
      end
      if (!client_query_id)
        client_query_id = Time.now + rand(10000) 
      end
      begin
        query_packet = make_query_packet(msg, use_tcp)
      rescue EncodeError => err
        Dnsruby.log.error { "#{err}" }
        st = SelectThread.instance
        st.push_exception_to_select(client_query_id, client_queue, err, nil)
        return
      end
      if (msg.do_caching && (msg.class != Update))
        cachedanswer = nil
        if (msg.header.rd)
          cachedanswer = @@recursive_cache.find(msg.question()[0].qname, msg.question()[0].type)
        else
          cachedanswer = @@authoritative_cache.find(msg.question()[0].qname, msg.question()[0].type)
        end
        if (cachedanswer)
          TheLog.debug("Sending cached answer to client\n")
          cachedanswer.header.id = msg.header.id
          st = SelectThread.instance
          st.push_response_to_select(client_query_id, client_queue, cachedanswer, msg, self)
          return client_query_id
        end
      end
      if (udp_packet_size < query_packet.length)
        if (@no_tcp)
          err=IOError.new("Can't send message - too big for UDP and no_tcp=true")
          Dnsruby.log.error { "#{err}" }
          st.push_exception_to_select(client_query_id, client_queue, err, nil)
          return
        end
        Dnsruby.log.debug { "Query packet length exceeds max UDP packet size - using TCP" }
        use_tcp = true
      end
      send_dnsruby(query_packet, msg, client_query_id, client_queue, use_tcp)
      return client_query_id
    end
def send_async ( * args ) msg = args [ 0 ] client_query_id = nil client_queue = nil use_tcp = @use_tcp if ( msg . kind_of? String ) msg = Message . new ( msg ) if ( @dnssec ) msg . header . cd = @dnssec if ( Dnssec . no_keys? ) msg . header . cd = false end end end if ( args . length > 1 ) if ( args [ 1 ] . class == Queue ) client_queue = args [ 1 ] elsif ( args . length == 2 ) use_tcp = args [ 1 ] end if ( args . length > 2 ) client_query_id = args [ 2 ] if ( args . length > 3 ) use_tcp = args [ 3 ] end end end if ( ! client_query_id ) client_query_id = Time . now + rand ( 10000 ) end begin query_packet = make_query_packet ( msg , use_tcp ) rescue EncodeError => err Dnsruby . log . error { "#{err}" } st = SelectThread . instance st . push_exception_to_select ( client_query_id , client_queue , err , nil ) return end if ( msg . do_caching && ( msg . class != Update ) ) cachedanswer = nil if ( msg . header . rd ) cachedanswer = @@recursive_cache . find ( msg . question ( ) [ 0 ] . qname , msg . question ( ) [ 0 ] . type ) else cachedanswer = @@authoritative_cache . find ( msg . question ( ) [ 0 ] . qname , msg . question ( ) [ 0 ] . type ) end if ( cachedanswer ) TheLog . debug ( "Sending cached answer to client\n" ) cachedanswer . header . id = msg . header . id st = SelectThread . instance st . push_response_to_select ( client_query_id , client_queue , cachedanswer , msg , self ) return client_query_id end end if ( udp_packet_size < query_packet . length ) if ( @no_tcp ) err = IOError . new ( "Can't send message - too big for UDP and no_tcp=true" ) Dnsruby . log . error { "#{err}" } st . push_exception_to_select ( client_query_id , client_queue , err , nil ) return end Dnsruby . log . debug { "Query packet length exceeds max UDP packet size - using TCP" } use_tcp = true end send_dnsruby ( query_packet , msg , client_query_id , client_queue , use_tcp ) return client_query_id end
Asynchronously send a Message to the server.
Asynchronously send a Message to the server .
# msg, client_queue, client_query_id, use_tcp=@use_tcp) #  @TODO@ Need to select a good Header ID here - see forgery-resilience RFC draft for details # we'll do our own validation by default #  Need to keep track of the request mac (if using tsig) so we can validate the response (RFC2845 4.1) #       #Are we using EventMachine or native Dnsruby? #       if (Resolver.eventmachine?) #         return send_eventmachine(query_packet, msg, client_query_id, client_queue, use_tcp) #       else # is this safe?! #  Check the cache!! #  @TODO@ Fix up the header - ID and flags #  If we can find the answer, send it to the client straight away #  Post the result to the client using SelectThread #  Otherwise, run the query #  Can't send the message - abort! #       end
args
<not_specific>