• 0

[C++/JAVA] DNS Response


Question

Okay, this is probably going to sound crazy.

Does anybody know how to generate DNS response packets (type AA)?

Basically I am writing a small DNS server application to do URL filtering on the network.

I want the DNS Server to accept DNS requests, and if the address is banned, send a response to the client with the IP of our web server.

This is easy, I can query other DNS Servers for information, however I can't seem to pass information to the client.

Does anybody know how to do this? I looked into several open source projects, but they are huge!

I also looked into the DNS manual sheets, and they are quite long and confusing.

I just need the code to generate this one packet type.

Thanks.

(This can be done in C/C++ or JAVA).

Link to comment
https://www.neowin.net/forum/topic/638557-cjava-dns-response/
Share on other sites

3 answers to this question

Recommended Posts

  • 0
If you want to generate your own DNS response packets from scratch then I suggest looking in to UDP socket programming for C/C++ and then read the DNS specs: http://www.ietf.org/rfc/rfc1035.txt

I was kind of hoping there was a library for this sort of thing, but thanks for the reply.

Well, can somebody tell me if this is the right direction (in java):

								//.......New connection, read request, etc.
								String strNAME = "localhost";
				String strTYPE = "A";
				String strCLASS = "IN";
				String strRDATA = "77.77.77.77";

				byte[] NAME;
				byte[] TYPE;
				byte[] CLASS;
				byte[] TTL;
				byte[] RDATA = strRDATA.getBytes();
				byte[] RDLENGTH;

				NAME = strNAME.getBytes();
				TYPE = strTYPE.getBytes();
				CLASS = strCLASS.getBytes();

				System.out.println("Int->byteArray");
				try {
					RDLENGTH = intToByteArray(RDATA.length);
					TTL = intToByteArray(224);
				}
				catch (Exception e) {
					System.out.println(e);
				}

				int size =  144 + RDLENGTH.length;

				System.out.println("Size: " + size);
				buf = new byte[size];

				for (int i = 0; i < 64; i++) {
					if (i >= NAME.length)
						buf[i] = 0;
					else
						buf[i] = NAME[i];
				}
				for (int i = 64; i < 80; i++) {
					if (i >= TYPE.length - 64)
						buf[i] = 0;
					else
						buf[i] = TYPE[i - 64];
				}
				for (int i = 80; i < 96; i++) {
					if (i >= CLASS.length - 80)
						buf[i] = 0;
					else
						buf[i] = CLASS[i - 80];
				}
								//Do the same thing for the next categories.

							   	InetAddress address = packet.getAddress();
				int port = packet.getPort();
				packet = new DatagramPacket(buf, buf.length, address, port);
				socket.send(packet);

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Too soon, I'm still not over this death!
    • Normally, I admit when a title is clickbait (unfortunately, it's become somewhat necessary in AI-dominated news sections today), but in this case, all supported versions is implied and doesn't need to be spelled out in the title. Of course, I'm covering a Patch Tuesday update but that is only available to supported Windows SKUs. All our coverage relates to supported Windows software and SKUs only unless we expressly state that it's "unsupported", "unofficial", or "third-party". I'm sorry, but supported/official SKUs don't need to be spelled out as such in every Neowin headline.
    • ALL versions or ALL SUPPORTED versions? Neowin does it again.
    • But the reality is it will work for people's needs, and they don't care about the technology that makes it. Clearly not everyone's needs, but that low end space where personal laptops were only used to type emails, watch content and browse websites, but they didn't want to do that on a small screen device. Heck, writing that out I can now see the connection and reason it'll do so well. Apple is about experience. If the experience is bad, they don't release it. Low end Windows laptop manufacturers up until this point have not taken that into consideration ever before, so slow laggy usage with brittle slimey plastic shells were common. I hope that the low end space at least creates better physical products that last a bit longer, and if Microsoft get their act together, they could also have a solid OS on such low end hardware that would actually make the experience work for what the hardware was intended for. The fact that the CPU is a "cellphone", sorry mobile phone processor is irrelevant. It's about the experience, and so far, that sounds quite solid.
  • Recent Achievements

    • Week One Done
      Jordan Smith earned a badge
      Week One Done
    • Reacting Well
      BizSAR earned a badge
      Reacting Well
    • First Post
      AndreaB earned a badge
      First Post
    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      595
    2. 2
      +Edouard
      186
    3. 3
      PsYcHoKiLLa
      77
    4. 4
      Michael Scrip
      73
    5. 5
      Steven P.
      65
  • Tell a friend

    Love Neowin? Tell a friend!