Module: Ronin::Network::UNIX

Included in:
Mixins::UNIX, Support
Defined in:
lib/ronin/network/unix.rb

Overview

Provides helper methods for communicating with UNIX sockets.

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) unix_accept(path) {|client| ... }

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

Examples:

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

Parameters:

  • path (String)

    The path for the new UNIX socket.

Yields:

  • (client)

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

Yield Parameters:

  • client (UNIXSocket)

    The accepted connection to UNIX socket.

Since:

  • 0.5.0



276
277
278
279
280
281
282
283
# File 'lib/ronin/network/unix.rb', line 276

def unix_accept(path)
  unix_server_session(path) do |server|
    client = server.accept

    yield client if block_given?
    client.close
  end
end

- (UNIXSocket) unix_connect(path) {|socket| ... }

Connects to a UNIX socket.

Examples:

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

Parameters:

  • path (String)

    The path to the UNIX socket.

Yields:

  • (socket)

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

Yield Parameters:

  • socket (UNIXSocket)

    The UNIX socket.

Returns:

  • (UNIXSocket)

    The UNIX socket.

See Also:

Since:

  • 0.5.0



83
84
85
86
87
88
# File 'lib/ronin/network/unix.rb', line 83

def unix_connect(path)
  socket = UNIXSocket.new(path)

  yield socket if block_given?
  return socket
end

- (UNIXSocket) unix_connect_and_send(data, path) {|socket| ... }

Connects to a UNIX Socket and sends the given data.

Parameters:

  • data (String)

    The data to send to the socket.

  • path (String)

    The path to the socket.

Yields:

  • (socket)

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

Yield Parameters:

  • socket (UNIXSocket)

    The newly created UNIXSocket object.

Returns:

  • (UNIXSocket)

    The newly created UNIXSocket object.

Since:

  • 0.5.0



110
111
112
113
114
115
116
# File 'lib/ronin/network/unix.rb', line 110

def unix_connect_and_send(data,path)
  socket = unix_connect(path)
  socket.write(data)

  yield socket if block_given?
  return socket
end

- (Boolean?) unix_open?(path, timeout = nil)

Tests whether a UNIX socket is open.

Parameters:

  • path (String)

    The path to the socket.

  • timeout (Integer) (defaults to: nil)

    (5) The maximum time to attempt connecting.

Returns:

  • (Boolean, nil)

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

Since:

  • 0.5.0



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ronin/network/unix.rb', line 48

def unix_open?(path,timeout=nil)
  timeout ||= 5

  begin
    Timeout.timeout(timeout) { unix_session(path) }
    return true
  rescue Timeout::Error
    return nil
  rescue SocketError, SystemCallError
    return false
  end
end

- (true) unix_send(data, path)

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

Examples:

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

Parameters:

  • data (String)

    The data to send to the UNIX socket.

  • path (String)

    The UNIX socket to connect to.

Returns:

  • (true)

    The data was successfully sent.

Since:

  • 0.5.0



165
166
167
168
169
170
171
# File 'lib/ronin/network/unix.rb', line 165

def unix_send(data,path)
  unix_session(path) do |socket|
    socket.write(data)
  end

  return true
end

- (UNIXServer) unix_server(path) {|server| ... }

Opens a UNIX socket.

Examples:

unix_server('/tmp/test.socket')

Parameters:

  • path (String)

    The path for the new UNIX socket.

Yields:

  • (server)

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

Yield Parameters:

  • server (UNIXServer)

    The new UNIX socket.

Returns:

  • (UNIXServer)

    The new UNIX socket.

See Also:

Since:

  • 0.5.0



195
196
197
198
199
200
# File 'lib/ronin/network/unix.rb', line 195

def unix_server(path)
  socket = UNIXServer.new(path)

  yield socket if block_given?
  return socket
end

- (Object) unix_server_loop(path) {|client| ... }

Opens a UNIX socket, accepts connections in a loop.

Examples:

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

Parameters:

  • path (String)

    The path for the new UNIX socket.

Yields:

  • (client)

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

Yield Parameters:

  • client (UNIXSocket)

    An accepted connection to UNIX socket.

Since:

  • 0.5.0



246
247
248
249
250
251
252
253
254
255
# File 'lib/ronin/network/unix.rb', line 246

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

- (Object) unix_server_session(path) {|server| ... }

Temporarily opens a UNIX socket.

Examples:

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

Parameters:

  • path (String)

    The path for the new UNIX socket.

Yields:

  • (server)

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

Yield Parameters:

  • server (UNIXServer)

    The new UNIX socket.

Since:

  • 0.5.0



221
222
223
224
225
# File 'lib/ronin/network/unix.rb', line 221

def unix_server_session(path,&block)
  socket = unix_server(path,&block)
  socket.close
  return nil
end

- (Object) unix_session(path) {|socket| ... }

Temporarily connects to a UNIX socket.

Examples:

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

Parameters:

  • path (String)

    The path to the UNIX socket.

Yields:

  • (socket)

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

Yield Parameters:

  • socket (UNIXSocket)

    The UNIX socket.

Since:

  • 0.5.0



137
138
139
140
141
142
143
# File 'lib/ronin/network/unix.rb', line 137

def unix_session(path)
  socket = unix_connect(path)

  yield socket if block_given?
  socket.close
  return nil
end