org.xbill.DNS.Rcode Java Examples

The following examples show how to use org.xbill.DNS.Rcode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DnsUpdateWriterTest.java    From nomulus with Apache License 2.0 7 votes vote down vote up
@Test
@SuppressWarnings("AssertThrowsMultipleStatements")
public void testPublishDomainFails_whenDnsUpdateReturnsError() throws Exception {
  DomainBase domain =
      persistActiveDomain("example.tld")
          .asBuilder()
          .setNameservers(ImmutableSet.of(persistActiveHost("ns1.example.tld").createVKey()))
          .build();
  persistResource(domain);
  when(mockResolver.send(any(Message.class))).thenReturn(messageWithResponseCode(Rcode.SERVFAIL));
  VerifyException thrown =
      assertThrows(
          VerifyException.class,
          () -> {
            writer.publishDomain("example.tld");
            writer.commit();
          });
  assertThat(thrown).hasMessageThat().contains("SERVFAIL");
}
 
Example #2
Source File: DnsUpdateWriterTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("AssertThrowsMultipleStatements")
public void testPublishHostFails_whenDnsUpdateReturnsError() throws Exception {
  HostResource host =
      persistActiveSubordinateHost("ns1.example.tld", persistActiveDomain("example.tld"))
          .asBuilder()
          .setInetAddresses(ImmutableSet.of(InetAddresses.forString("10.0.0.1")))
          .build();
  persistResource(host);
  when(mockResolver.send(any(Message.class))).thenReturn(messageWithResponseCode(Rcode.SERVFAIL));
  VerifyException thrown =
      assertThrows(
          VerifyException.class,
          () -> {
            writer.publishHost("ns1.example.tld");
            writer.commit();
          });
  assertThat(thrown).hasMessageThat().contains("SERVFAIL");
}
 
Example #3
Source File: dig.java    From dnsjava with BSD 2-Clause "Simplified" License 6 votes vote down vote up
static void doAXFR(Message response) {
  System.out.println("; java dig 0.0 <> " + name + " axfr");
  if (response.isSigned()) {
    System.out.print(";; TSIG ");
    if (response.isVerified()) {
      System.out.println("ok");
    } else {
      System.out.println("failed");
    }
  }

  if (response.getRcode() != Rcode.NOERROR) {
    System.out.println(response);
    return;
  }

  for (Record record : response.getSection(Section.ANSWER)) {
    System.out.println(record);
  }

  System.out.print(";; done (");
  System.out.print(response.getHeader().getCount(Section.ANSWER));
  System.out.print(" records, ");
  System.out.print(response.getHeader().getCount(Section.ADDITIONAL));
  System.out.println(" additional)");
}
 
Example #4
Source File: Dig.java    From open-rmbt with Apache License 2.0 6 votes vote down vote up
static void	doAXFR(Message response) throws IOException {
	System.out.println("; java dig 0.0 <> " + name + " axfr");
	if (response.isSigned()) {
		System.out.print(";; TSIG ");
		if (response.isVerified())
			System.out.println("ok");
		else
			System.out.println("failed");
	}

	if (response.getRcode() != Rcode.NOERROR) {
		System.out.println(response);
		return;
	}

	Record [] records = response.getSectionArray(Section.ANSWER);
	for (int i = 0; i < records.length; i++)
		System.out.println(records[i]);

	System.out.print(";; done (");
	System.out.print(response.getHeader().getCount(Section.ANSWER));
	System.out.print(" records, ");
	System.out.print(response.getHeader().getCount(Section.ADDITIONAL));
	System.out.println(" additional)");
}
 
Example #5
Source File: OpenAliasHelper.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
@Override
protected Boolean doInBackground(String... args) {
    //main();
    if (args.length != 1) return false;
    String name = args[0];
    if ((name == null) || (name.isEmpty()))
        return false; //pointless trying to lookup nothing
    Timber.d("Resolving %s", name);
    try {
        SimpleResolver sr = new SimpleResolver(DNSSEC_SERVERS[new Random().nextInt(DNSSEC_SERVERS.length)]);
        ValidatingResolver vr = new ValidatingResolver(sr);
        vr.setTimeout(0, DNS_LOOKUP_TIMEOUT);
        vr.loadTrustAnchors(new ByteArrayInputStream(ROOT.getBytes("ASCII")));
        Record qr = Record.newRecord(Name.fromConstantString(name + "."), Type.TXT, DClass.IN);
        Message response = vr.send(Message.newQuery(qr));
        final int rcode = response.getRcode();
        if (rcode != Rcode.NOERROR) {
            Timber.i("Rcode: %s", Rcode.string(rcode));
            for (RRset set : response.getSectionRRsets(Section.ADDITIONAL)) {
                if (set.getName().equals(Name.root) && set.getType() == Type.TXT
                        && set.getDClass() == ValidatingResolver.VALIDATION_REASON_QCLASS) {
                    Timber.i("Reason:  %s", ((TXTRecord) set.first()).getStrings().get(0));
                }
            }
            return false;
        } else {
            dnssec = response.getHeader().getFlag(Flags.AD);
            for (Record record : response.getSectionArray(Section.ANSWER)) {
                if (record.getType() == Type.TXT) {
                    txts.addAll(((TXTRecord) record).getStrings());
                }
            }
        }
    } catch (IOException | IllegalArgumentException ex) {
        return false;
    }
    return true;
}
 
Example #6
Source File: SimpleDoTResolver.java    From androdns with Apache License 2.0 5 votes vote down vote up
private void
verifyTSIG(Message query, Message response, byte [] b, TSIG tsig) {
    if (tsig == null)
        return;
    int error = tsig.verify(response, b, query.getTSIG());
    if (Options.check("verbose"))
        System.err.println("TSIG verify: " + Rcode.TSIGstring(error));
}
 
Example #7
Source File: DNSFormActivity.java    From androdns with Apache License 2.0 5 votes vote down vote up
/**
 * update the the lower gui section with the Values from an AnswerScreenState
 *
 * @param state history: if true, do not update the title
 */
public void updateScreenState(final AnswerScreenState state, final boolean history) {
    Runnable guiUpdate = new Runnable() {
        @Override
        public void run() {
            if (!history) {
                setTitle("");
            }

            ((TextView) findViewById(R.id.txtStatusText)).setText(state.status);
            ((TextView) findViewById(R.id.txtServerIP)).setText(state.server);
            ((TextView) findViewById(R.id.txtQbytes)).setText("" + state.qsize);
            ((TextView) findViewById(R.id.txtAbytes)).setText("" + state.asize);
            ((EditText) findViewById(R.id.txtResult)).setText(state.answerText);
            if (state.rcode > -1) {
                ((TextView) findViewById(R.id.txtRcode)).setText(Rcode.string(state.rcode));
            } else {
                ((TextView) findViewById(R.id.txtRcode)).setText("");

            }


            ((CheckBox) findViewById(R.id.cbaAA)).setChecked(state.flag_AA);
            ((CheckBox) findViewById(R.id.cbaTC)).setChecked(state.flag_TC);
            ((CheckBox) findViewById(R.id.cbaRD)).setChecked(state.flag_RD);
            ((CheckBox) findViewById(R.id.cbaRA)).setChecked(state.flag_RA);
            ((CheckBox) findViewById(R.id.cbaAD)).setChecked(state.flag_AD);
            ((CheckBox) findViewById(R.id.cbaCD)).setChecked(state.flag_CD);
        }
    };

    runOnUiThread(guiUpdate);
}
 
Example #8
Source File: DnsUpdateWriter.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Override
protected void commitUnchecked() {
  try {
    Message response = transport.send(update);
    verify(
        response.getRcode() == Rcode.NOERROR,
        "DNS server failed domain update for '%s' rcode: %s",
        zoneName,
        Rcode.string(response.getRcode()));
  } catch (IOException e) {
    throw new RuntimeException("publishDomain failed for zone: " + zoneName, e);
  }
}
 
Example #9
Source File: DnsUpdateWriterTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  inject.setStaticField(Ofy.class, "clock", clock);

  createTld("tld");
  when(mockResolver.send(any(Update.class))).thenReturn(messageWithResponseCode(Rcode.NOERROR));

  writer = new DnsUpdateWriter(
      "tld", Duration.ZERO, Duration.ZERO, Duration.ZERO, mockResolver, clock);
}
 
Example #10
Source File: DnsMessageTransportTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  simpleQuery =
      Message.newQuery(Record.newRecord(Name.fromString("example.com."), Type.A, DClass.IN));
  expectedResponse = responseMessageWithCode(simpleQuery, Rcode.NOERROR);
  when(mockFactory.createSocket(InetAddress.getByName(UPDATE_HOST), DnsMessageTransport.DNS_PORT))
      .thenReturn(mockSocket);
  resolver = new DnsMessageTransport(mockFactory, UPDATE_HOST, Duration.ZERO);
}
 
Example #11
Source File: XBillDnsSrvResolverTest.java    From dns-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnEmptyForHostNotFound() throws Exception {
  String fqdn = "thefqdn.";

  when(lookupFactory.forName(fqdn)).thenReturn(testLookup(fqdn));
  when(xbillResolver.send(any(Message.class))).thenReturn(messageWithRCode(fqdn, Rcode.NXDOMAIN));

  assertThat(resolver.resolve(fqdn).isEmpty(), is(true));
}
 
Example #12
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 5 votes vote down vote up
byte[] buildErrorMessage(Header header, int rcode, Record question) {
  Message response = new Message();
  response.setHeader(header);
  for (int i = 0; i < 4; i++) {
    response.removeAllRecords(i);
  }
  if (rcode == Rcode.SERVFAIL) {
    response.addRecord(question, Section.QUESTION);
  }
  header.setRcode(rcode);
  return response.toWire();
}
 
Example #13
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public byte[] formerrMessage(byte[] in) {
  Header header;
  try {
    header = new Header(in);
  } catch (IOException e) {
    return null;
  }
  return buildErrorMessage(header, Rcode.FORMERR, null);
}
 
Example #14
Source File: update.java    From dnsjava with BSD 2-Clause "Simplified" License 4 votes vote down vote up
boolean doAssert(Tokenizer st) throws IOException {
  String field = st.getString();
  String expected = st.getString();
  String value = null;
  boolean flag = true;
  int section;

  if (response == null) {
    print("No response has been received");
    return true;
  }
  if (field.equalsIgnoreCase("rcode")) {
    int rcode = response.getHeader().getRcode();
    if (rcode != Rcode.value(expected)) {
      value = Rcode.string(rcode);
      flag = false;
    }
  } else if (field.equalsIgnoreCase("serial")) {
    List<Record> answers = response.getSection(Section.ANSWER);
    if (answers.isEmpty() || !(answers.get(0) instanceof SOARecord)) {
      print("Invalid response (no SOA)");
    } else {
      SOARecord soa = (SOARecord) answers.get(0);
      long serial = soa.getSerial();
      if (serial != Long.parseLong(expected)) {
        value = Long.toString(serial);
        flag = false;
      }
    }
  } else if (field.equalsIgnoreCase("tsig")) {
    if (response.isSigned()) {
      if (response.isVerified()) {
        value = "ok";
      } else {
        value = "failed";
      }
    } else {
      value = "unsigned";
    }
    if (!value.equalsIgnoreCase(expected)) {
      flag = false;
    }
  } else if ((section = Section.value(field)) >= 0) {
    int count = response.getHeader().getCount(section);
    if (count != Integer.parseInt(expected)) {
      value = Integer.toString(count);
      flag = false;
    }
  } else {
    print("Invalid assertion keyword: " + field);
  }

  if (!flag) {
    print("Expected " + field + " " + expected + ", received " + value);
    while (true) {
      Tokenizer.Token token = st.get();
      if (!token.isString()) {
        break;
      }
      print(token.value);
    }
    st.unget();
  }
  return flag;
}
 
Example #15
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 4 votes vote down vote up
byte[] generateReply(Message query, byte[] in, Socket s) {
  Header header;
  boolean badversion;
  int maxLength;
  int flags = 0;

  header = query.getHeader();
  if (header.getFlag(Flags.QR)) {
    return null;
  }
  if (header.getRcode() != Rcode.NOERROR) {
    return errorMessage(query, Rcode.FORMERR);
  }
  if (header.getOpcode() != Opcode.QUERY) {
    return errorMessage(query, Rcode.NOTIMP);
  }

  Record queryRecord = query.getQuestion();

  TSIGRecord queryTSIG = query.getTSIG();
  TSIG tsig = null;
  if (queryTSIG != null) {
    tsig = TSIGs.get(queryTSIG.getName());
    if (tsig == null || tsig.verify(query, in, null) != Rcode.NOERROR) {
      return formerrMessage(in);
    }
  }

  OPTRecord queryOPT = query.getOPT();
  if (s != null) {
    maxLength = 65535;
  } else if (queryOPT != null) {
    maxLength = Math.max(queryOPT.getPayloadSize(), 512);
  } else {
    maxLength = 512;
  }

  if (queryOPT != null && (queryOPT.getFlags() & ExtendedFlags.DO) != 0) {
    flags = FLAG_DNSSECOK;
  }

  Message response = new Message(query.getHeader().getID());
  response.getHeader().setFlag(Flags.QR);
  if (query.getHeader().getFlag(Flags.RD)) {
    response.getHeader().setFlag(Flags.RD);
  }
  response.addRecord(queryRecord, Section.QUESTION);

  Name name = queryRecord.getName();
  int type = queryRecord.getType();
  int dclass = queryRecord.getDClass();
  if (type == Type.AXFR && s != null) {
    return doAXFR(name, query, tsig, queryTSIG, s);
  }
  if (!Type.isRR(type) && type != Type.ANY) {
    return errorMessage(query, Rcode.NOTIMP);
  }

  byte rcode = addAnswer(response, name, type, dclass, 0, flags);
  if (rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN) {
    return errorMessage(query, rcode);
  }

  addAdditional(response, flags);

  if (queryOPT != null) {
    int optflags = (flags == FLAG_DNSSECOK) ? ExtendedFlags.DO : 0;
    OPTRecord opt = new OPTRecord((short) 4096, rcode, (byte) 0, optflags);
    response.addRecord(opt, Section.ADDITIONAL);
  }

  response.setTSIG(tsig, Rcode.NOERROR, queryTSIG);
  return response.toWire(maxLength);
}