package leigh.stack.dnsd;

import org.xbill.DNS.*;

import java.io.IOException;
import java.net.InetAddress;

public class DNSUtil {

    public static Message buildReply(Message request, InetAddress add) {
        Message reply = new Message(request.getHeader().getID());
        reply.getHeader().setFlag(Flags.RD);
        reply.getHeader().setFlag(Flags.QR);
        reply.getHeader().setFlag(Flags.RA);
        reply.addRecord(request.getQuestion(), Section.QUESTION);

        reply.addRecord(new ARecord(request.getQuestion().getName(), DClass.IN, 60, add), Section.ANSWER);
        return reply;
    }

    public static Message buildReply(Message request, String hostname) throws IOException {
        Message reply = new Message(request.getHeader().getID());
        reply.getHeader().setFlag(Flags.RD);
        reply.getHeader().setFlag(Flags.QR);
        reply.getHeader().setFlag(Flags.RA);
        reply.addRecord(request.getQuestion(), Section.QUESTION);

        reply.addRecord(new PTRRecord(request.getQuestion().getName(),
                DClass.IN, 60, new Name(hostname)), Section.ANSWER);
        return reply;
    }

    public static Message buildEmpty(Message request) throws IOException {
        Message reply = new Message(request.getHeader().getID());
        reply.getHeader().setFlag(Flags.RD);
        reply.getHeader().setFlag(Flags.QR);
        reply.getHeader().setFlag(Flags.RA);
        reply.addRecord(request.getQuestion(), Section.QUESTION);
        return reply;
    }
}
