Module: Clamd::SocketManager
- Included in:
- Client
- Defined in:
- lib/clamd/socket_manager.rb
Instance Method Summary collapse
-
#clamd_response_size(command) ⇒ Object
Determines the number of bytes to be read for the command.
-
#close_socket(socket) ⇒ Object
Closes the given socket.
-
#open_socket(host, port) ⇒ Object
Opens socket for the given
hostandport. -
#read_socket(socket, command) ⇒ Object
Reads the response for the given command from the socket.
-
#stop_streaming(socket) ⇒ Object
Stops streaming to ClamAV daemon.
-
#stream_to_clamd(socket, path) ⇒ Object
Streams file content to the ClamAV daemon.
-
#write_chunk(socket, chunk) ⇒ Object
Writes the size of chunk(bytes), chunk(bytes) to the socket.
-
#write_socket(socket, command, path) ⇒ Object
Writes the command to the given sicket.
Instance Method Details
#clamd_response_size(command) ⇒ Object
Determines the number of bytes to be read for the command
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/clamd/socket_manager.rb', line 41 def clamd_response_size(command) case command when 'PING' 4 when 'RELOAD' 9 when 'SHUTDOWN' 1 else 1024 end end |
#close_socket(socket) ⇒ Object
Closes the given socket
15 16 17 |
# File 'lib/clamd/socket_manager.rb', line 15 def close_socket(socket) socket.close end |
#open_socket(host, port) ⇒ Object
Opens socket for the given host and port
8 9 10 |
# File 'lib/clamd/socket_manager.rb', line 8 def open_socket(host, port) TCPSocket.open(host, port) end |
#read_socket(socket, command) ⇒ Object
Reads the response for the given command from the socket
22 23 24 |
# File 'lib/clamd/socket_manager.rb', line 22 def read_socket(socket, command) socket.recv(clamd_response_size(command)).gsub(/(\u0000)|(\n)/, "").strip end |
#stop_streaming(socket) ⇒ Object
Stops streaming to ClamAV daemon
83 84 85 |
# File 'lib/clamd/socket_manager.rb', line 83 def stop_streaming(socket) socket.write([0].pack("N")) end |
#stream_to_clamd(socket, path) ⇒ Object
Streams file content to the ClamAV daemon
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/clamd/socket_manager.rb', line 57 def stream_to_clamd(socket, path) begin file = File.open(path, "rb") bytes = file.read(chunk_size) while bytes write_chunk(socket, bytes) bytes = file.read(chunk_size) end stop_streaming(socket) ensure file.close if file end end |
#write_chunk(socket, chunk) ⇒ Object
Writes the size of chunk(bytes), chunk(bytes) to the socket
75 76 77 78 |
# File 'lib/clamd/socket_manager.rb', line 75 def write_chunk(socket, chunk) socket.write([chunk.size].pack("N")) socket.write(chunk) end |
#write_socket(socket, command, path) ⇒ Object
Writes the command to the given sicket
29 30 31 32 33 34 35 36 |
# File 'lib/clamd/socket_manager.rb', line 29 def write_socket(socket, command, path) if path && command != "zINSTREAM\0" socket.write("#{command} #{path}") else socket.write(command) end stream_to_clamd(socket, path) if command == "zINSTREAM\0" end |