Module: Ronin::Network::Mixins::UDP

Includes:
Mixin, UDP
Defined in:
lib/ronin/network/mixins/udp.rb

Overview

Adds UDP convenience methods and connection parameters to a class.

Defines the following parameters:

  • host (String) - UDP host.
  • port (Integer) - UDP port.
  • local_host (String) - UDP local host.
  • local_port (Integer) - UDP local port.
  • server_host (String) - UDP server host.
  • server_port (Integer) - UDP server port.

Instance Method Summary collapse

Methods included from Mixin

included

Methods included from UDP

#udp_banner

Instance Method Details

#udp_connect {|socket| ... } ⇒ UDPSocket (protected)

Opens a UDP connection to the host and port specified by the host and port parameters. If the local_host and local_port parameters are set, they will be used for the local host and port of the UDP connection.

Examples:

udp_connect
# => UDPSocket
udp_connect do |socket|
  puts socket.readlines
end

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

See Also:


116
117
118
119
120
# File 'lib/ronin/network/mixins/udp.rb', line 116

def udp_connect(&block)
  print_info "Connecting to #{host_port} ..."

  return super(self.host,self.port,self.local_host,self.local_port,&block)
end

#udp_connect_and_send(data) {|socket| ... } ⇒ UDPSocket (protected)

Connects to the host and port specified by the host and port parameters, then sends the given data. If the local_host and local_port instance methods are set, they will be used for the local host and port of the UDP connection.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

See Also:


144
145
146
147
148
149
# File 'lib/ronin/network/mixins/udp.rb', line 144

def udp_connect_and_send(data,&block)
  print_info "Connecting to #{host_port} ..."
  print_debug "Sending data: #{data.inspect}"

  return super(data,self.host,self.port,self.local_host,self.local_port,&block)
end

#udp_open?(timeout = nil) ⇒ Boolean? (protected)

Tests whether the UDP port, specified by the host and port parameters, is open.

Since:

  • 0.5.0


82
83
84
85
86
# File 'lib/ronin/network/mixins/udp.rb', line 82

def udp_open?(timeout=nil)
  print_info "Testing if #{host_port} is open ..."

  super(self.host,self.port,self.local_host,self.local_port,timeout)
end

#udp_recv {|server, (client_host, client_port), mesg| ... } ⇒ nil (protected)

Creates a new UDPServer listening on the server_host and server_port parameters, accepts only one message from a client.

Examples:

udp_recv do |server,(host,port),mesg|
  server.send('hello',host,port)
end

Yields:

  • (server, (client_host, client_port), mesg)

    The given block will be passed the client host/port and the received message.

Yield Parameters:

  • server (UDPServer)

    The UDPServer.

  • client_host (String)

    The source host of the mesg.

  • client_port (Integer)

    The source port of the mesg.

  • mesg (String)

    The received message.

See Also:

Since:

  • 0.5.0


339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/ronin/network/mixins/udp.rb', line 339

def udp_recv(&block)
  print_info "Listening on #{self.server_host_port} ..."

  super(self.server_port,self.server_host) do |server,(host,port),mesg|
    print_info "Received message from #{host}:#{port}"
    print_debug mesg

    yield server, [host, port], mesg if block_given?
  end

  print_info "Closed #{self.server_host_port}"
  return nil
end

#udp_send(data) ⇒ true (protected)

Connects to the host and port specified by the host and port parameters, sends the given data and then disconnects.

Examples:

buffer = "GET /" + ('A' * 4096) + "\n\r"
udp_send(buffer)
# => true

See Also:

Since:

  • 0.4.0


197
198
199
200
201
202
203
204
205
# File 'lib/ronin/network/mixins/udp.rb', line 197

def udp_send(data)
  print_info "Connecting to #{host_port} ..."
  print_debug "Sending data: #{data.inspect}"

  super(data,self.host,self.port,self.local_host,self.local_port)

  print_info "Disconnected from #{host_port}"
  return true
end

#udp_server {|server| ... } ⇒ UDPServer (protected)

Creates a new UDPServer object listening on server_host and server_port parameters.

Examples:

udp_server

Yields:

  • (server)

    The given block will be passed the newly created server.

Yield Parameters:

  • server (UDPServer)

    The newly created server.

See Also:


227
228
229
230
231
# File 'lib/ronin/network/mixins/udp.rb', line 227

def udp_server(&block)
  print_info "Listening on #{server_host_port} ..."

  return super(self.server_port,self.server_host,&block)
end

#udp_server_loop {|server, (client_host, client_port), mesg| ... } ⇒ nil (protected)

Creates a new UDPServer listening on the server_host and server_port parameters, accepting messages from clients in a loop.

Examples:

udp_server_loop do |server,(host,port),mesg|
  server.send('hello',host,port)
end

Yields:

  • (server, (client_host, client_port), mesg)

    The given block will be passed the client host/port and the received message.

Yield Parameters:

  • server (UDPServer)

    The UDPServer.

  • client_host (String)

    The source host of the mesg.

  • client_port (Integer)

    The source port of the mesg.

  • mesg (String)

    The received message.

See Also:

Since:

  • 0.5.0


297
298
299
300
301
302
303
304
# File 'lib/ronin/network/mixins/udp.rb', line 297

def udp_server_loop(&block)
  print_info "Listening on #{self.server_host_port} ..."

  super(self.server_port,self.server_host,&block)

  print_info "Closed #{self.server_host_port}"
  return nil
end

#udp_server_session {|server| ... } ⇒ nil (protected)

Creates a new temporary UDPServer object listening on the server_host and server_port parameters.

Examples:

udp_server_session do |server|
  data, sender = server.recvfrom(1024)
end

Yields:

  • (server)

    The given block will be passed the newly created server. When the block has finished, the server will be closed.

Yield Parameters:

  • server (UDPServer)

    The newly created server.

See Also:


255
256
257
258
259
260
261
262
# File 'lib/ronin/network/mixins/udp.rb', line 255

def udp_server_session(&block)
  print_info "Listening on #{self.server_host_port} ..."

  super(self.server_port,self.server_host,&block)

  print_info "Closed #{self.server_host_port}"
  return nil
end

#udp_session {|socket| ... } ⇒ nil (protected)

Creates a UDP session to the host and port specified by the host and port parameters. If the local_host and local_port parameters are set, they will be used for the local host and port of the UDP connection.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. After the block has returned, the socket will then be closed.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

See Also:


170
171
172
173
174
175
176
177
# File 'lib/ronin/network/mixins/udp.rb', line 170

def udp_session(&block)
  print_info "Connecting to #{host_port} ..."

  super(self.host,self.port,self.local_host,self.local_port,&block)

  print_info "Disconnected from #{host_port}"
  return nil
end

#udp_single_server(&block) ⇒ Object (protected)

Deprecated.

Deprecated as of 0.5.0. Use #udp_recv instead.

Since:

  • 0.5.0


359
360
361
# File 'lib/ronin/network/mixins/udp.rb', line 359

def udp_single_server(&block)
  udp_recv(&block)
end