module Ronin::Support::Network::UNIX::Mixin

Provides helper methods for communicating with UNIX sockets.

Public Instance Methods

unix_accept(path) { |client| ... } click to toggle source

Opens a UNIX socket, accepts a connection, then closes the socket.

@param [String] path

The path for the new UNIX socket.

@yield [client]

If a block is given, it will be passed the accepted connection.

@yieldparam [UNIXSocket] client

The accepted connection to UNIX socket.

@example

unix_accept('/tmp/test.socket') do |client|
  # ...
end

@api public

# File lib/ronin/support/network/unix/mixin.rb, line 261
def unix_accept(path)
  unix_server_session(path) do |server|
    client = server.accept

    yield client if block_given?
    client.close
  end
end
unix_connect(path) { |socket| ... } click to toggle source

Connects to a UNIX socket.

@param [String] path

The path to the UNIX socket.

@yield [socket]

If a block is given, it will be passed an UNIX socket object.
Once the block has returned, the UNIX socket will be closed.

@yieldparam [UNIXSocket] socket

The UNIX socket.

@return [UNIXSocket, nil]

The UNIX socket. If a block was given, `nil` will be returned.

@example

unix_connect('/tmp/haproxy.stats.socket')

@example

unix_connect('/tmp/haproxy.stats.socket') do |socket|
  # ...
end

@see rubydoc.info/stdlib/socket/UNIXSocket

@api public

# File lib/ronin/support/network/unix/mixin.rb, line 91
def unix_connect(path)
  socket = UNIXSocket.new(path)

  if block_given?
    yield socket
    socket.close
  else
    return socket
  end
end
unix_connect_and_send(data,path) { |socket| ... } click to toggle source

Connects to a UNIX Socket and sends the given data.

@param [String] data

The data to send to the socket.

@param [String] path

The path to the socket.

@yield [socket]

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

@yieldparam [UNIXSocket] socket

The newly created UNIXSocket object.

@return [UNIXSocket]

The newly created UNIXSocket object.

@api public

# File lib/ronin/support/network/unix/mixin.rb, line 122
def unix_connect_and_send(data,path)
  socket = unix_connect(path)
  socket.write(data)

  yield socket if block_given?
  return socket
end
unix_open?(path,timeout=nil) click to toggle source

Tests whether a UNIX socket is open.

@param [String] path

The path to the socket.

@param [Integer] timeout (5)

The maximum time to attempt connecting.

@return [Boolean, nil]

Specifies whether the UNIX socket is open.
If the connection was not accepted, `nil` will be returned.

@api public

@since 0.5.0

# File lib/ronin/support/network/unix/mixin.rb, line 47
def unix_open?(path,timeout=nil)
  timeout ||= 5

  begin
    Timeout.timeout(timeout) do
      socket = unix_connect(path)
      socket.close
    end
    return true
  rescue Timeout::Error
    return nil
  rescue SocketError, SystemCallError
    return false
  end
end
unix_send(data,path) click to toggle source

Connects to a UNIX socket, sends the given data and then closes the socket.

@param [String] data

The data to send to the UNIX socket.

@param [String] path

The UNIX socket to connect to.

@return [true]

The data was successfully sent.

@example

buffer = "GET /" + ('A' * 4096) + "\n\r"
unix_send(buffer,'/tmp/thin.socket')
# => true

@api public

# File lib/ronin/support/network/unix/mixin.rb, line 150
def unix_send(data,path)
  unix_connect(path) do |socket|
    socket.write(data)
  end

  return true
end
unix_server(path) { |socket| ... } click to toggle source

Opens a UNIX socket.

@param [String] path

The path for the new UNIX socket.

@yield [server]

If a block is given, it will be passed an UNIX socket object.

@yieldparam [UNIXServer] server

The new UNIX socket.

@return [UNIXServer]

The new UNIX socket.

@example

unix_server('/tmp/test.socket')

@see rubydoc.info/stdlib/socket/UNIXServer

@api public

# File lib/ronin/support/network/unix/mixin.rb, line 180
def unix_server(path)
  socket = UNIXServer.new(path)

  yield socket if block_given?
  return socket
end
unix_server_loop(path) { |client| ... } click to toggle source

Opens a UNIX socket, accepts connections in a loop.

@param [String] path

The path for the new UNIX socket.

@yield [client]

If a block is given, it will be passed each accepted connection.

@yieldparam [UNIXSocket] client

An accepted connection to UNIX socket.

@example

unix_server_loop('/tmp/test.socket') do |client|
  # ...
end

@api public

# File lib/ronin/support/network/unix/mixin.rb, line 231
def unix_server_loop(path)
  unix_server_session(path) do |server|
    loop do
      client = server.accept

      yield client if block_given?
      client.close
    end
  end
end
unix_server_session(path,&block) click to toggle source

Temporarily opens a UNIX socket.

@param [String] path

The path for the new UNIX socket.

@yield [server]

If a block is given, it will be passed an UNIX socket object.

@yieldparam [UNIXServer] server

The new UNIX socket.

@example

unix_server_session('/tmp/test.socket') do |server|
  # ...
end

@api public

# File lib/ronin/support/network/unix/mixin.rb, line 206
def unix_server_session(path,&block)
  socket = unix_server(path,&block)
  socket.close
  return nil
end