• 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.