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

Includes:
Mixin
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

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 |sock|
  puts sock.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.

Returns:

  • (UDPSocket)

    The newly created UDPSocket object.



100
101
102
103
104
# File 'lib/ronin/network/mixins/udp.rb', line 100

def udp_connect(&block)
  print_info "Connecting to #{self.host}:#{self.port} ..."

  return ::Net.udp_connect(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.

Parameters:

  • data (String)

    The data to send through the 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.

Returns:

  • (UDPSocket)

    The newly created UDPSocket object.



126
127
128
129
130
131
# File 'lib/ronin/network/mixins/udp.rb', line 126

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

  return ::Net.udp_connect_and_send(data,self.host,self.port,self.local_host,self.local_port,&block)
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.

Returns:

  • (UDPServer)

    The newly created server.



177
178
179
180
181
182
183
184
185
# File 'lib/ronin/network/mixins/udp.rb', line 177

def udp_server(&block)
  if self.server_host
    print_info "Listening on #{self.server_host}:#{self.server_port} ..."
  else
    print_info "Listening on #{self.server_port} ..."
  end

  return ::Net.udp_server(self.server_port,self.server_host,&block)
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.

Returns:

  • (nil)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ronin/network/mixins/udp.rb', line 207

def udp_server_session(&block)
  if self.server_host
    print_info "Listening on #{self.server_host}:#{self.server_port} ..."
  else
    print_info "Listening on #{self.server_port} ..."
  end

  ::Net.udp_server_session(&block)

  if self.server_host
    print_info "Closed #{self.server_host}:#{self.server_port}"
  else
    print_info "Closed #{self.server_port}"
  end

  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.

Returns:

  • (nil)


150
151
152
153
154
155
156
157
# File 'lib/ronin/network/mixins/udp.rb', line 150

def udp_session(&block)
  print_info "Connecting to #{self.host}:#{self.port} ..."

  ::Net.udp_session(self.host,self.port,self.local_host,self.local_port,&block)

  print_info "Disconnected from #{self.host}:#{self.port}"
  return nil
end