75
75
import java .net .Socket ;
76
76
import java .net .SocketAddress ;
77
77
import java .net .SocketException ;
78
- import java .net .UnknownHostException ;
79
78
import java .nio .BufferUnderflowException ;
80
79
import java .nio .ByteBuffer ;
81
80
import java .nio .channels .DatagramChannel ;
87
86
import java .security .spec .InvalidKeySpecException ;
88
87
import java .security .spec .RSAPrivateKeySpec ;
89
88
import java .text .SimpleDateFormat ;
89
+ import java .time .Duration ;
90
+ import java .time .Instant ;
91
+ import java .time .temporal .ChronoUnit ;
90
92
import java .util .ArrayList ;
91
- import java .util .Calendar ;
92
93
import java .util .Collection ;
93
94
import java .util .Date ;
94
95
import java .util .Enumeration ;
@@ -232,13 +233,7 @@ private void updateDNSServer(Configuration conf) {
232
233
} catch (SocketException e ) {
233
234
}
234
235
ResolverConfig .refresh ();
235
- ExtendedResolver resolver ;
236
- try {
237
- resolver = new ExtendedResolver ();
238
- } catch (UnknownHostException e ) {
239
- LOG .error ("Can not resolve DNS servers: " , e );
240
- return ;
241
- }
236
+ ExtendedResolver resolver = new ExtendedResolver ();
242
237
for (Resolver check : resolver .getResolvers ()) {
243
238
if (check instanceof SimpleResolver ) {
244
239
InetAddress address = ((SimpleResolver ) check ).getAddress ()
@@ -247,7 +242,7 @@ private void updateDNSServer(Configuration conf) {
247
242
resolver .deleteResolver (check );
248
243
continue ;
249
244
} else {
250
- check .setTimeout (30 );
245
+ check .setTimeout (Duration . ofSeconds ( 30 ) );
251
246
}
252
247
} else {
253
248
LOG .error ("Not simple resolver!!!?" + check );
@@ -260,12 +255,10 @@ private void updateDNSServer(Configuration conf) {
260
255
}
261
256
StringBuilder message = new StringBuilder ();
262
257
message .append ("DNS servers: " );
263
- if (ResolverConfig .getCurrentConfig ().servers () != null ) {
264
- for (String server : ResolverConfig .getCurrentConfig ()
265
- .servers ()) {
266
- message .append (server );
267
- message .append (" " );
268
- }
258
+ for (InetSocketAddress address :
259
+ ResolverConfig .getCurrentConfig ().servers ()) {
260
+ message .append (address );
261
+ message .append (" " );
269
262
}
270
263
LOG .info (message .toString ());
271
264
}
@@ -331,11 +324,10 @@ private void signZones() throws IOException {
331
324
if (isDNSSECEnabled ()) {
332
325
Collection <Zone > zoneCollection = zones .values ();
333
326
for (Zone zone : zoneCollection ) {
334
- Iterator itor = zone .iterator ();
327
+ Iterator < RRset > itor = zone .iterator ();
335
328
while (itor .hasNext ()) {
336
- RRset rRset = (RRset ) itor .next ();
337
- Iterator sigs = rRset .sigs ();
338
- if (!sigs .hasNext ()) {
329
+ RRset rRset = itor .next ();
330
+ if (!rRset .sigs ().isEmpty ()) {
339
331
try {
340
332
signSiteRecord (zone , rRset .first ());
341
333
} catch (DNSSEC .DNSSECException e ) {
@@ -692,10 +684,8 @@ private void signSiteRecord(Zone zone, Record record)
692
684
throws DNSSEC .DNSSECException {
693
685
RRset rrset = zone .findExactMatch (record .getName (),
694
686
record .getType ());
695
- Calendar cal = Calendar .getInstance ();
696
- Date inception = cal .getTime ();
697
- cal .add (Calendar .YEAR , 1 );
698
- Date expiration = cal .getTime ();
687
+ Instant inception = Instant .now ();
688
+ Instant expiration = inception .plus (365 , ChronoUnit .DAYS );
699
689
RRSIGRecord rrsigRecord =
700
690
DNSSEC .sign (rrset , dnsKeyRecs .get (zone .getOrigin ()),
701
691
privateKey , inception , expiration );
@@ -1159,7 +1149,7 @@ private byte remoteLookup(Message response, Name name, int type,
1159
1149
}
1160
1150
}
1161
1151
if (r .getType () == Type .CNAME ) {
1162
- Name cname = (( CNAMERecord ) r ). getAlias ();
1152
+ Name cname = r . getName ();
1163
1153
if (iterations < 6 ) {
1164
1154
remoteLookup (response , cname , type , iterations + 1 );
1165
1155
}
@@ -1255,9 +1245,7 @@ private int getMaxLength(Socket s, OPTRecord queryOPT) {
1255
1245
* @param flags the flags.
1256
1246
*/
1257
1247
private void addAdditional2 (Message response , int section , int flags ) {
1258
- Record [] records = response .getSectionArray (section );
1259
- for (int i = 0 ; i < records .length ; i ++) {
1260
- Record r = records [i ];
1248
+ for (Record r : response .getSection (section )) {
1261
1249
Name glueName = r .getAdditionalName ();
1262
1250
if (glueName != null ) {
1263
1251
addGlue (response , glueName , flags );
@@ -1403,11 +1391,10 @@ byte addAnswer(Message response, Name name, int type, int dclass,
1403
1391
response .getHeader ().setFlag (Flags .AA );
1404
1392
}
1405
1393
} else if (sr .isSuccessful ()) {
1406
- RRset [] rrsets = sr .answers ();
1394
+ List < RRset > rrsets = sr .answers ();
1407
1395
LOG .info ("found answers {}" , rrsets );
1408
- for (int i = 0 ; i < rrsets .length ; i ++) {
1409
- addRRset (name , response , rrsets [i ],
1410
- Section .ANSWER , flags );
1396
+ for (RRset rrset : rrsets ) {
1397
+ addRRset (name , response , rrset , Section .ANSWER , flags );
1411
1398
}
1412
1399
addNS (response , zone , flags );
1413
1400
if (iterations == 0 ) {
@@ -1456,7 +1443,7 @@ private void addSOA(Message response, Zone zone, int flags) {
1456
1443
private void addNXT (Message response , int flags )
1457
1444
throws DNSSEC .DNSSECException , IOException {
1458
1445
Record nxtRecord = getNXTRecord (
1459
- response .getSectionArray (Section .QUESTION )[ 0 ] );
1446
+ response .getSection (Section .QUESTION ). get ( 0 ) );
1460
1447
Zone zone = findBestZone (nxtRecord .getName ());
1461
1448
addRecordCommand .exec (zone , nxtRecord );
1462
1449
RRset nxtRR = zone .findExactMatch (nxtRecord .getName (), Type .NXT );
@@ -1515,19 +1502,15 @@ private void addRRset(Name name, Message response, RRset rrset, int section,
1515
1502
}
1516
1503
}
1517
1504
if ((flags & FLAG_SIGONLY ) == 0 ) {
1518
- Iterator it = rrset .rrs ();
1519
- while (it .hasNext ()) {
1520
- Record r = (Record ) it .next ();
1505
+ for (Record r : rrset .rrs ()) {
1521
1506
if (r .getName ().isWild () && !name .isWild ()) {
1522
1507
r = r .withName (name );
1523
1508
}
1524
1509
response .addRecord (r , section );
1525
1510
}
1526
1511
}
1527
1512
if ((flags & (FLAG_SIGONLY | FLAG_DNSSECOK )) != 0 ) {
1528
- Iterator it = rrset .sigs ();
1529
- while (it .hasNext ()) {
1530
- Record r = (Record ) it .next ();
1513
+ for (Record r : rrset .sigs ()) {
1531
1514
if (r .getName ().isWild () && !name .isWild ()) {
1532
1515
r = r .withName (name );
1533
1516
}
@@ -1554,21 +1537,21 @@ byte[] doAXFR(Name name, Message query, TSIG tsig, TSIGRecord qtsig,
1554
1537
if (zone == null ) {
1555
1538
return errorMessage (query , Rcode .REFUSED );
1556
1539
}
1557
- Iterator it = zone .AXFR ();
1540
+ Iterator < RRset > it = zone .AXFR ();
1558
1541
try {
1559
1542
DataOutputStream dataOut ;
1560
1543
dataOut = new DataOutputStream (s .getOutputStream ());
1561
1544
int id = query .getHeader ().getID ();
1562
1545
while (it .hasNext ()) {
1563
- RRset rrset = ( RRset ) it .next ();
1546
+ RRset rrset = it .next ();
1564
1547
Message response = new Message (id );
1565
1548
Header header = response .getHeader ();
1566
1549
header .setFlag (Flags .QR );
1567
1550
header .setFlag (Flags .AA );
1568
1551
addRRset (rrset .getName (), response , rrset ,
1569
1552
Section .ANSWER , FLAG_DNSSECOK );
1570
1553
if (tsig != null ) {
1571
- tsig .applyStream (response , qtsig , first );
1554
+ tsig .apply (response , qtsig , first );
1572
1555
qtsig = response .getTSIG ();
1573
1556
}
1574
1557
first = false ;
@@ -1688,10 +1671,8 @@ public void exec(Zone zone, Record record) throws IOException {
1688
1671
zone .addRecord (record );
1689
1672
LOG .info ("Registered {}" , record );
1690
1673
if (isDNSSECEnabled ()) {
1691
- Calendar cal = Calendar .getInstance ();
1692
- Date inception = cal .getTime ();
1693
- cal .add (Calendar .YEAR , 1 );
1694
- Date expiration = cal .getTime ();
1674
+ Instant inception = Instant .now ();
1675
+ Instant expiration = inception .plus (365 , ChronoUnit .DAYS );
1695
1676
RRset rRset =
1696
1677
zone .findExactMatch (record .getName (), record .getType ());
1697
1678
try {
@@ -1727,8 +1708,8 @@ public void exec(Zone zone, Record record) throws IOException {
1727
1708
*/
1728
1709
private void addDSRecord (Zone zone ,
1729
1710
Name name , int dClass , long dsTtl ,
1730
- Date inception ,
1731
- Date expiration ) throws DNSSEC .DNSSECException {
1711
+ Instant inception ,
1712
+ Instant expiration ) throws DNSSEC .DNSSECException {
1732
1713
RRset rRset ;
1733
1714
RRSIGRecord rrsigRecord ;
1734
1715
0 commit comments