Module: Ronin::Support::Network::UNIX::Mixin

Included in:
Mixin
Defined in:
lib/ronin/support/network/unix/mixin.rb

Overview

Provides helper methods for communicating with UNIX sockets.

Instance Method Summary collapse

Instance Method Details

#unix_accept(path) {|client| ... } ⇒ Object

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.



261
262
263
264
265
266
267
268
# 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| ... } ⇒ UNIXSocket?

Connects to a UNIX socket.

Examples:

unix_connect('/tmp/haproxy.stats.socket')
unix_connect('/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. Once the block has returned, the UNIX socket will be closed.

Yield Parameters:

  • socket (UNIXSocket)

    The UNIX socket.

Returns:

  • (UNIXSocket, nil)

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

See Also:



91
92
93
94
95
96
97
98
99
100
# 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| ... } ⇒ UNIXSocket

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.



122
123
124
125
126
127
128
# 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) ⇒ Boolean?

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ true

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.



150
151
152
153
154
155
156
# 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) {|server| ... } ⇒ UNIXServer

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:



180
181
182
183
184
185
# 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| ... } ⇒ Object

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.



231
232
233
234
235
236
237
238
239
240
# 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) {|server| ... } ⇒ Object

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.



206
207
208
209
210
# 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