8000 Fix zone transfer timeout handling · dnsjava/dnsjava@28567db · GitHub
[go: up one dir, main page]

Skip to content

Commit 28567db

Browse files
committed
Fix zone transfer timeout handling
Closes #218 Closes #219
1 parent 89034e4 commit 28567db

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/main/java/org/xbill/DNS/TCPClient.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
import java.nio.channels.SelectionKey;
1212
import java.nio.channels.Selector;
1313
import java.nio.channels.SocketChannel;
14-
import java.util.concurrent.TimeUnit;
14+
import java.time.Duration;
15+
import java.time.temporal.ChronoUnit;
1516

1617
final class TCPClient {
17-
private final long endTime;
18+
private final long startTime;
19+
private final Duration timeout;
1820
private final SelectionKey key;
1921

20-
TCPClient(long timeout) throws IOException {
21-
endTime = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeout);
22+
TCPClient(Duration timeout) throws IOException {
23+
this.timeout = timeout;
24+
startTime = System.nanoTime();
2225
boolean done = false;
2326
Selector selector = null;
2427
SocketChannel channel = SocketChannel.open();
@@ -51,7 +54,7 @@ void connect(SocketAddress addr) throws IOException {
5154
try {
5255
while (!channel.finishConnect()) {
5356
if (!key.isConnectable()) {
54-
blockUntil(key, endTime);
57+
blockUntil(key);
5558
}
5659
}
5760
} finally {
@@ -84,11 +87,11 @@ void send(byte[] data) throws IOException {
8487
throw new EOFException();
8588
}
8689
nsent += (int) n;
87-
if (nsent < data.length + 2 && endTime - System.nanoTime() < 0) {
90+
if (nsent < data.length + 2 && System.nanoTime() - startTime >= timeout.toNanos()) {
8891
throw new SocketTimeoutException();
8992
}
9093
} else {
91-
blockUntil(key, endTime);
94+
blockUntil(key);
9295
}
9396
}
9497
} finally {
@@ -112,11 +115,11 @@ private byte[] _recv(int length) throws IOException {
112115
throw new EOFException();
113116
}
114117
nrecvd += (int) n;
115-
if (nrecvd < length && System.currentTimeMillis() > endTime) {
118+
if (nrecvd < length && System.nanoTime() - startTime >= timeout.toNanos()) {
116119
throw new SocketTimeoutException();
117120
}
118121
} else {
119-
blockUntil(key, endTime);
122+
blockUntil(key);
120123
}
121124
}
122125
} finally {
@@ -127,12 +130,13 @@ private byte[] _recv(int length) throws IOException {
127130
return data;
128131
}
129132

130-
private static void blockUntil(SelectionKey key, long endTime) throws IOException {
131-
long timeout = TimeUnit.NANOSECONDS.toMillis(endTime - System.nanoTime());
133+
private void blockUntil(SelectionKey key) throws IOException {
134+
long remainingTimeout =
135+
timeout.minus(System.nanoTime() - startTime, ChronoUnit.NANOS).toMillis();
132136
int nkeys = 0;
133-
if (timeout > 0) {
134-
nkeys = key.selector().select(timeout);
135-
} else if (timeout == 0) {
137+
if (remainingTimeout > 0) {
138+
nkeys = key.selector().select(remainingTimeout);
139+
} else if (remainingTimeout == 0) {
136140
nkeys = key.selector().selectNow();
137141
}
138142
if (nkeys == 0) {

src/main/java/org/xbill/DNS/ZoneTransferIn.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class ZoneTransferIn {
6161
private TCPClient client;
6262
private TSIG tsig;
6363
private TSIG.StreamVerifier verifier;
64-
private long timeout = 900 * 1000L;
64+
private Duration timeout = Duration.ofMinutes(15);
6565

6666
private int state;
6767
private long end_serial;
@@ -298,7 +298,7 @@ public void setTimeout(int secs) {
298298
if (secs < 0) {
299299
throw new IllegalArgumentException("timeout cannot be negative");
300300
}
301-
timeout = 1000L * secs;
301+
timeout = Duration.ofSeconds(secs);
302302
}
303303

304304
/**
@@ -307,7 +307,7 @@ public void setTimeout(int secs) {
307307
* @param t The maximum amount of time that this zone transfer can take.
308308
*/
309309
public void setTimeout(Duration t) {
310-
timeout = (int) t.toMillis();
310+
timeout = t;
311311
}
312312

313313
/**

0 commit comments

Comments
 (0)
0