lib/ronin/network/udp.rb in ronin-support-0.4.0.rc1 vs lib/ronin/network/udp.rb in ronin-support-0.4.0.rc2
- old
+ new
@@ -1,7 +1,7 @@
#
-# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
+# Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This file is part of Ronin Support.
#
# Ronin Support is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
@@ -52,21 +52,19 @@
# @example
# udp_connect('www.hackety.org',80)
# # => UDPSocket
#
# @example
- # udp_connect('www.wired.com',80) do |sock|
- # puts sock.readlines
+ # udp_connect('www.wired.com',80) do |socket|
+ # puts socket.readlines
# end
#
# @api public
#
def udp_connect(host,port,local_host=nil,local_port=nil)
- host = host.to_s
- local_host = if local_host
- local_host.to_s
- end
+ host = host.to_s
+ local_host = (local_host || '0.0.0.0').to_s
socket = UDPSocket.new
socket.bind(local_host,local_port) if (local_host && local_port)
socket.connect(host,port)
@@ -103,15 +101,15 @@
# The newly created UDPSocket object.
#
# @api public
#
def udp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
- sock = udp_connect(host,port,local_host,local_port)
- sock.write(data)
+ socket = udp_connect(host,port,local_host,local_port)
+ socket.write(data)
- yield sock if block_given?
- return sock
+ yield socket if block_given?
+ return socket
end
#
# Creates a new temporary UDPSocket object, connected to the given host
# and port.
@@ -138,19 +136,58 @@
# @return [nil]
#
# @api public
#
def udp_session(host,port,local_host=nil,local_port=nil)
- sock = udp_connect(host,port,local_host,local_port)
+ socket = udp_connect(host,port,local_host,local_port)
- yield sock if block_given?
+ yield socket if block_given?
- sock.close
+ socket.close
return nil
end
#
+ # Connects to a specified host and port, sends the given data and then
+ # closes the connection.
+ #
+ # @param [String] data
+ # The data to send through the connection.
+ #
+ # @param [String] host
+ # The host to connect to.
+ #
+ # @param [Integer] port
+ # The port to connect to.
+ #
+ # @param [String] local_host (nil)
+ # The local host to bind to.
+ #
+ # @param [Integer] local_port (nil)
+ # The local port to bind to.
+ #
+ # @return [true]
+ # The data was successfully sent.
+ #
+ # @example
+ # buffer = "GET /" + ('A' * 4096) + "\n\r"
+ # udp_send(buffer,'victim.com',80)
+ # # => true
+ #
+ # @api public
+ #
+ # @since 0.4.0
+ #
+ def udp_send(data,host,port,local_host=nil,local_port=nil)
+ udp_session(host,port,local_host,local_port) do |socket|
+ socket.write(data)
+ end
+
+ return true
+ end
+
+ #
# Reads the banner from the service running on the given host and port.
#
# @param [String] host
# The host to connect to.
#
@@ -175,12 +212,12 @@
# @api public
#
def udp_banner(host,port,local_host=nil,local_port=nil)
banner = nil
- udp_session(host,port,local_host,local_port) do |sock|
- banner = sock.readline
+ udp_session(host,port,local_host,local_port) do |socket|
+ banner = socket.readline
end
yield banner if block_given?
return banner
end
@@ -200,12 +237,12 @@
# @example
# udp_server(1337)
#
# @api public
#
- def udp_server(port,host='0.0.0.0')
- host = host.to_s
+ def udp_server(port=nil,host=nil)
+ host = (host || '0.0.0.0').to_s
server = UDPSocket.new
server.bind(host,port)
yield server if block_given?
@@ -235,10 +272,10 @@
# data, sender = server.recvfrom(1024)
# end
#
# @api public
#
- def udp_server_session(port,host='0.0.0.0',&block)
+ def udp_server_session(port=nil,host=nil,&block)
server = udp_server(port,host,&block)
server.close()
return nil
end