<commit_before>import java.lang.reflect.*;
import java.io.*;
import java.net.*;
import java.util.*;
import DNS.*;

public class jnamed {

Zone [] zones;
int zcount;
Hashtable znames;

public
jnamed() {
	zones = new Zone[20];
	zcount = 0;
	znames = new Hashtable();
};

public void
addZone(String zonefile, int type) throws IOException {
	Zone newzone = new Zone(zonefile, type, zones[0]);
	znames.put(newzone.getOrigin(), newzone);
/*System.out.println("Adding zone named <" + newzone.getOrigin() + ">");*/
	zones[zcount++] = newzone;
};

public Zone
findBestZone(Name name) {
	Zone foundzone = null;
	Name tname = name;
	do {
/*System.out.println("Looking for zone named <" + tname + ">");*/
		foundzone = (Zone) znames.get(tname);
		if (foundzone != null)
			return foundzone;
		tname = new Name(tname, 1);
	} while (!tname.equals(Name.root));
	return null;
}

public RRset
findExactMatch(Name name, short type, short dclass) {
	Zone zone = findBestZone(name);
	Hashtable sets = zone.findName(name);
	if (sets == null)
		return null;
	Short Type = new Short(type);
	RRset rrset = (RRset) sets.get(Type);
	return rrset;
	
}

void
addRRset(Message response, RRset rrset) {
	Enumeration e = rrset.rrs();
	while (e.hasMoreElements()) {
		Record r = (Record) e.nextElement();
		response.addRecord(Section.ANSWER, r);
	}
}


void
addAuthority(Message response, Zone zone) {
	if (response.getHeader().getCount(Section.ANSWER) > 0) {
		RRset nsRecords = (RRset) zone.getNS();
		Enumeration e = nsRecords.rrs();
		while (e.hasMoreElements()) {
			Record r = (Record) e.nextElement();
			if (response.findRecord(Section.ANSWER, r) == false)
				response.addRecord(Section.AUTHORITY, r);
		}
	}
	else {
		SOARecord soa = (SOARecord) zone.getSOA();
		response.addRecord(Section.AUTHORITY, soa);
	}
}

private void
addGlue(Message response, Name name) {
	RRset a = findExactMatch(name, Type.A, DClass.IN);
	Enumeration e = a.rrs();
	while (e.hasMoreElements()) {
			Record r = (Record) e.nextElement();
			if (response.findRecord(r) == false)
				response.addRecord(Section.ADDITIONAL, r);
	}
}

private void
addAdditional2(Message response, int section) {
	Enumeration e = response.getSection(section);
	while (e.hasMoreElements()) {
		Record r = (Record) e.nextElement();
		try {
			Method m = r.getClass().getMethod("getTarget", null);
			Name glueName = (Name) m.invoke(r, null);
			addGlue(response, glueName);
		}
		catch (Exception ex) {
		}
	}
	
}

void
addAdditional(Message response) {
	addAdditional2(response, Section.ANSWER);
	addAdditional2(response, Section.AUTHORITY);
}

Message
generateReply(Message query) {
	Enumeration qds = query.getSection(Section.QUESTION);
	Record queryRecord = (Record) qds.nextElement();

	Message response = new Message();
	response.getHeader().setID(query.getHeader().getID());
	response.getHeader().setFlag(Flags.AA);
	response.getHeader().setFlag(Flags.QR);
	response.addRecord(Section.QUESTION, queryRecord);

	Name name = queryRecord.getName();
	short type = queryRecord.getType();
	Zone zone = findBestZone(name);
	if (zone == null) {
		response.getHeader().setRcode(Rcode.SERVFAIL);
	}
	else {
/*System.out.println("Looking up name <" + name + "> in [" + zone.getOrigin() + "]");*/
		Hashtable nameRecords = (Hashtable) zone.findName(name);
		if (nameRecords == null) {
			response.getHeader().setRcode(Rcode.NXDOMAIN);
		}
		else {
			if (type == Type.ANY) {
				Enumeration e = nameRecords.elements();
				while (e.hasMoreElements()) {
					RRset rrset = (RRset) e.nextElement();
					addRRset(response, rrset);
				}
			}
			else {
				Short Type = new Short(type);
				RRset rrset = (RRset) nameRecords.get(Type);
				if (rrset != null)
					addRRset(response, rrset);
			}
		}
		addAuthority(response, zone);
		addAdditional(response);
	}
	return response;
}

public int
truncateSection(Message in, int maxLength, int length, int section) {
	int removed = 0;
	Record [] records = in.getSectionArray(section);<commit_msg>add authority based on non-zone NS if necessary<commit_after>import java.lang.reflect.*;
import java.io.*;
import java.net.*;
import java.util.*;
import DNS.*;

public class jnamed {

Zone [] zones;
int zcount;
Hashtable znames;

public
jnamed() {
	zones = new Zone[20];
	zcount = 0;
	znames = new Hashtable();
};

public void
addZone(String zonefile, int type) throws IOException {
	Zone newzone = new Zone(zonefile, type, zones[0]);
	znames.put(newzone.getOrigin(), newzone);
/*System.out.println("Adding zone named <" + newzone.getOrigin() + ">");*/
	zones[zcount++] = newzone;
};

public Zone
findBestZone(Name name) {
	Zone foundzone = null;
	Name tname = name;
	do {
/*System.out.println("Looking for zone named <" + tname + ">");*/
		foundzone = (Zone) znames.get(tname);
		if (foundzone != null)
			return foundzone;
		tname = new Name(tname, 1);
	} while (!tname.equals(Name.root));
	return null;
}

public RRset
findExactMatch(Name name, short type, short dclass) {
	Zone zone = findBestZone(name);
	Hashtable sets = zone.findName(name);
	if (sets == null)
		return null;
	Short Type = new Short(type);
	RRset rrset = (RRset) sets.get(Type);
	return rrset;
	
}

void
addRRset(Message response, RRset rrset) {
	Enumeration e = rrset.rrs();
	while (e.hasMoreElements()) {
		Record r = (Record) e.nextElement();
		response.addRecord(Section.ANSWER, r);
	}
}


void
addAuthority(Message response, Name name, Zone zone) {
	if (response.getHeader().getCount(Section.ANSWER) > 0) {
		RRset nsRecords = findExactMatch(name, Type.NS, DClass.IN);
		if (nsRecords == null)
			nsRecords = (RRset) zone.getNS();
		Enumeration e = nsRecords.rrs();
		while (e.hasMoreElements()) {
			Record r = (Record) e.nextElement();
			if (response.findRecord(Section.ANSWER, r) == false)
				response.addRecord(Section.AUTHORITY, r);
		}
	}
	else {
		SOARecord soa = (SOARecord) zone.getSOA();
		response.addRecord(Section.AUTHORITY, soa);
	}
}

private void
addGlue(Message response, Name name) {
	RRset a = findExactMatch(name, Type.A, DClass.IN);
	Enumeration e = a.rrs();
	while (e.hasMoreElements()) {
			Record r = (Record) e.nextElement();
			if (response.findRecord(r) == false)
				response.addRecord(Section.ADDITIONAL, r);
	}
}

private void
addAdditional2(Message response, int section) {
	Enumeration e = response.getSection(section);
	while (e.hasMoreElements()) {
		Record r = (Record) e.nextElement();
		try {
			Method m = r.getClass().getMethod("getTarget", null);
			Name glueName = (Name) m.invoke(r, null);
			addGlue(response, glueName);
		}
		catch (Exception ex) {
		}
	}
	
}

void
addAdditional(Message response) {
	addAdditional2(response, Section.ANSWER);
	addAdditional2(response, Section.AUTHORITY);
}

Message
generateReply(Message query) {
	Enumeration qds = query.getSection(Section.QUESTION);
	Record queryRecord = (Record) qds.nextElement();

	Message response = new Message();
	response.getHeader().setID(query.getHeader().getID());
	response.getHeader().setFlag(Flags.AA);
	response.getHeader().setFlag(Flags.QR);
	response.addRecord(Section.QUESTION, queryRecord);

	Name name = queryRecord.getName();
	short type = queryRecord.getType();
	Zone zone = findBestZone(name);
	if (zone == null) {
		response.getHeader().setRcode(Rcode.SERVFAIL);
	}
	else {
/*System.out.println("Looking up name <" + name + "> in [" + zone.getOrigin() + "]");*/
		Hashtable nameRecords = (Hashtable) zone.findName(name);
		if (nameRecords == null) {
			response.getHeader().setRcode(Rcode.NXDOMAIN);
		}
		else {
			if (type == Type.ANY) {
				Enumeration e = nameRecords.elements();
				while (e.hasMoreElements()) {
					RRset rrset = (RRset) e.nextElement();
					addRRset(response, rrset);
				}
			}
			else {
				Short Type = new Short(type);
				RRset rrset = (RRset) nameRecords.get(Type);
				if (rrset != null)
					addRRset(response, rrset);
			}
		}
		addAuthority(response, name, zone);
		addAdditional(response);
	}
	return response;
}

public int
truncateSection(Message in, int maxLength, int length, int section) {
	int removed = 0;
	Record [] records = in.getSectionArray(section);
{'fraction_non_alphanumeric': 0.1508989460632362, 'fraction_numerical': 0.0024798512089274642, 'mean_word_length': 4.334656084656085, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1814273', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2824, 'n_words': 658}