Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use configured timeout in socket connection as well as reads #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 50 additions & 43 deletions src/main/java/fi/solita/clamav/ClamAVClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -50,19 +51,22 @@ public ClamAVClient(String hostName, int port) {
* @return true if the server responded with proper ping reply.
*/
public boolean ping() throws IOException {
try (Socket s = new Socket(hostName,port); OutputStream outs = s.getOutputStream()) {
s.setSoTimeout(timeout);
outs.write(asBytes("zPING\0"));
outs.flush();
byte[] b = new byte[PONG_REPLY_LEN];
InputStream inputStream = s.getInputStream();
int copyIndex = 0;
int readResult;
do {
readResult = inputStream.read(b, copyIndex, Math.max(b.length - copyIndex, 0));
copyIndex += readResult;
} while (readResult > 0);
return Arrays.equals(b, asBytes("PONG"));
try (Socket s = new Socket()) {
s.connect(new InetSocketAddress(hostName, port), timeout);
try (OutputStream outs = s.getOutputStream()) {
s.setSoTimeout(timeout);
outs.write(asBytes("zPING\0"));
outs.flush();
byte[] b = new byte[PONG_REPLY_LEN];
InputStream inputStream = s.getInputStream();
int copyIndex = 0;
int readResult;
do {
readResult = inputStream.read(b, copyIndex, Math.max(b.length - copyIndex, 0));
copyIndex += readResult;
} while (readResult > 0);
return Arrays.equals(b, asBytes("PONG"));
}
}
}

Expand All @@ -78,39 +82,42 @@ public boolean ping() throws IOException {
* @return server reply
*/
public byte[] scan(InputStream is) throws IOException {
try (Socket s = new Socket(hostName,port); OutputStream outs = new BufferedOutputStream(s.getOutputStream())) {
s.setSoTimeout(timeout);

// handshake
outs.write(asBytes("zINSTREAM\0"));
outs.flush();
byte[] chunk = new byte[CHUNK_SIZE];

try (InputStream clamIs = s.getInputStream()) {
// send data
int read = is.read(chunk);
while (read >= 0) {
// The format of the chunk is: '<length><data>' where <length> is the size of the following data in bytes expressed as a 4 byte unsigned
// integer in network byte order and <data> is the actual chunk. Streaming is terminated by sending a zero-length chunk.
byte[] chunkSize = ByteBuffer.allocate(4).putInt(read).array();

outs.write(chunkSize);
outs.write(chunk, 0, read);
if (clamIs.available() > 0) {
// reply from server before scan command has been terminated.
byte[] reply = assertSizeLimit(readAll(clamIs));
throw new IOException("Scan aborted. Reply from server: " + new String(reply, StandardCharsets.US_ASCII));
}
read = is.read(chunk);
}
try (Socket s = new Socket()) {
s.connect(new InetSocketAddress(hostName, port), timeout);
try (OutputStream outs = new BufferedOutputStream(s.getOutputStream())) {
s.setSoTimeout(timeout);

// terminate scan
outs.write(new byte[]{0,0,0,0});
// handshake
outs.write(asBytes("zINSTREAM\0"));
outs.flush();
// read reply
return assertSizeLimit(readAll(clamIs));
byte[] chunk = new byte[CHUNK_SIZE];

try (InputStream clamIs = s.getInputStream()) {
// send data
int read = is.read(chunk);
while (read >= 0) {
// The format of the chunk is: '<length><data>' where <length> is the size of the following data in bytes expressed as a 4 byte unsigned
// integer in network byte order and <data> is the actual chunk. Streaming is terminated by sending a zero-length chunk.
byte[] chunkSize = ByteBuffer.allocate(4).putInt(read).array();

outs.write(chunkSize);
outs.write(chunk, 0, read);
if (clamIs.available() > 0) {
// reply from server before scan command has been terminated.
byte[] reply = assertSizeLimit(readAll(clamIs));
throw new IOException("Scan aborted. Reply from server: " + new String(reply, StandardCharsets.US_ASCII));
}
read = is.read(chunk);
}

// terminate scan
outs.write(new byte[]{0,0,0,0});
outs.flush();
// read reply
return assertSizeLimit(readAll(clamIs));
}
}
}
}
}

/**
Expand Down