11package com .mageddo .dns .utils ;
22
3+ import java .io .IOException ;
4+ import java .io .UncheckedIOException ;
35import java .time .Duration ;
6+ import java .util .List ;
47import java .util .Optional ;
58
69import com .mageddo .commons .lang .Objects ;
@@ -41,6 +44,11 @@ public static Message authoritative(Message m) {
4144 return m ;
4245 }
4346
47+ private static Name findQuestionName (Message m ) {
48+ return m .getQuestion ()
49+ .getName ();
50+ }
51+
4452 public static String simplePrint (Response res ) {
4553 return simplePrint (res .getMessage ());
4654 }
@@ -115,7 +123,7 @@ public static Message aAnswer(Message query, String ip) {
115123 }
116124
117125 public static Message aAnswer (Message query , String ip , long ttl ) {
118- final var res = withNoErrorResponse (query . clone ( ));
126+ final var res = withNoErrorResponse (copy ( query ));
119127 if (StringUtils .isBlank (ip )) {
120128 return res ;
121129 }
@@ -182,7 +190,7 @@ public static Entry.Type findQuestionType(Message msg) {
182190 * @return a clone with the combination.
183191 */
184192 public static Message combine (Message source , Message target ) {
185- final var clone = clone (target );
193+ final var clone = copy (target );
186194 for (int i = 1 ; i < 4 ; i ++) {
187195 final var section = source .getSection (i );
188196 for (final var record : section ) {
@@ -298,7 +306,7 @@ public static Message withDefaultResponseHeaders(Message res) {
298306 return res ;
299307 }
300308
301- static Message clone (Message msg ) {
309+ public static Message copy (Message msg ) {
302310 if (msg == null ) {
303311 return null ;
304312 }
@@ -311,6 +319,12 @@ public static Message setFlag(Message m, int flag) {
311319 return m ;
312320 }
313321
322+ public static Message unsetFlag (Message m , int flag ) {
323+ m .getHeader ()
324+ .unsetFlag (flag );
325+ return m ;
326+ }
327+
314328 public static boolean hasFlag (Message msg , int flag ) {
315329 return msg .getHeader ()
316330 .getFlag (flag );
@@ -323,7 +337,6 @@ public static HostnameQuery toHostnameQuery(Message query) {
323337 return HostnameQuery .of (host , version );
324338 }
325339
326-
327340 public static boolean isSuccess (Message res ) {
328341 return res .getRcode () == Rcode .NOERROR ;
329342 }
@@ -340,11 +353,25 @@ public static Message noData(Message query) {
340353 return withResponseCode (query .clone (), Rcode .NOERROR );
341354 }
342355
356+ public static Message of (byte [] m ) {
357+ try {
358+ return new Message (m );
359+ } catch (IOException e ) {
360+ throw new UncheckedIOException (e );
361+ }
362+ }
363+
364+ public static Message unsetAuthoritative (Message m ) {
365+ return unsetFlag (m , Flags .AA );
366+ }
367+
343368 public static Message authoritativeAnswer (Message query , String ip , IP .Version version ) {
344369 return authoritative (answer (query , ip , version ));
345370 }
346371
347- public static Message authoritativeAnswer (Message query , String ip , IP .Version version , long ttl ) {
372+ public static Message authoritativeAnswer (
373+ Message query , String ip , IP .Version version , long ttl
374+ ) {
348375 return authoritative (answer (query , ip , version , ttl ));
349376 }
350377
@@ -355,4 +382,17 @@ public static boolean isAuthoritative(Message m) {
355382 public static boolean isRecursionAvailable (Message m ) {
356383 return hasFlag (m , Flags .RA );
357384 }
385+
386+ public static int getId (Message m ) {
387+ return m .getHeader ()
388+ .getID ();
389+ }
390+
391+ public static Message notSupportedHttps (Message m ) {
392+ return authoritative (withNoErrorResponse (copy (m )));
393+ }
394+
395+ public static List <Record > getAnswers (Message m ) {
396+ return m .getSection (Section .ANSWER );
397+ }
358398}
0 commit comments