Pergunta

I'd like to send a udp packet to a mumble server using ruby to get status information about how many users are currently connected.

The documentation states there is a way using a UDP packet: http://mumble.sourceforge.net/Protocol#UDP_Ping_packet

However I don't know how to formulate that with ruby and thus get no reply from the server.

require 'socket'
sock = UDPSocket.new
sock.connect("99.99.99.99", 66666)
sock.send("00", 0)
p sock.recvfrom(1) # this does not return
sock.close

How do I format data of the udp packet?

Foi útil?

Solução

This should work to generate your ping packet:

def ping(identifier)
  v = identifier
  a = []

  while v > 256 # extract bytes from the identifier
    a << v % 256
    v = v / 256
  end
  a << v % 256  
  prefix = [0] * (8-a.length) # pad the identifier
  ([0,0,0,0] + prefix + a).pack("C*") # pack the message as bytes
end

usage:

# random 8 byte number as a message identifier - compare this to any packet 
# received to ensure you're receiving the correct response.

identifier = rand(256**8)
sock.send ping(identifier), 0

# you should get a response here if the mumble server is 
# accessible and responding to pings.
sock.recvfrom(1) 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top