/* * MakeDNSQueries.java * * Created on April 28, 2004, 9:49 AM */ /** * * Creates queryperf compatible test files from djbdns data files */ import java.io.*; import java.util.Random; import java.util.Vector; import java.lang.Integer; import java.lang.NumberFormatException; public class MakeDNSQueries { /** Creates a new instance of MakeDNSQueries */ public MakeDNSQueries() { } //functions as an allowedType function as well as a getType function public static String getType(char t){ switch(t){ case '.': return "NS"; case '+': return "A"; case '=': return "A"; case '@': return "MX"; case '&': return "NS"; default: return null; } } public static void getQueryListFromDatafile(String filename, Vector list){ String line; String type; String fqdn; char buf[]; try{ BufferedReader in = new BufferedReader(new FileReader(filename)); while((line = in.readLine()) != null){ fqdn = new String(); buf = line.toCharArray(); if((type = getType(buf[0])) != null){ //get the info int i = 1; while((buf[i] != ':') && (i < buf.length)){ fqdn = fqdn + buf[i]; i++; } if(fqdn.length() > 3){ list.addElement(new li(fqdn, type)); } } } in.close(); }catch(Exception e){ e.printStackTrace(); } } public static void main(String args[]){ String usage = "Usage: java MakeDNSQueries -count 10000 -file /var/dns/data"; Vector list = new Vector(); String filename = null; int i = 0; String arg; int count = 0; while (i < args.length && args[i].startsWith("-")) { arg = args[i++]; if(arg.equals("-count")){ if(i < args.length){ arg = args[i++]; try{ count = Integer.parseInt(arg); }catch(NumberFormatException nfe){ System.err.println("-count !!"); return; } }else{ System.err.println("-count needs a number!"); return; } }else if(arg.equals("-file")){ if(i < args.length){ filename = args[i++]; //lets check it if(new File(filename).exists() != true){ System.err.println(filename + " does not exist!"); return; } }else{ System.err.println("-file needs a filename!"); return; } } } if(filename == null){ System.out.println(usage); return; } if(count == 0){ System.out.println(usage); return; } getQueryListFromDatafile(filename, list); if(list.size() < 1){ System.out.println("Didn't get any information from the config file!"); return; } try{ try{ new File("test.dat").delete(); }catch(Exception e){ System.out.println("Old file probably did not exist.. continuing.."); } BufferedWriter out = new BufferedWriter(new FileWriter("test.dat")); Random rand = new Random(38980094); int ind; for(i=0; i < count; i++){ ind = rand.nextInt(list.size()); li item = (li)(list.elementAt(ind)); String line = item.fqdn + " " + item.type + "\n"; out.write(line); } try{out.flush();}catch(Exception e){e.printStackTrace();} out.close(); }catch(Exception e){ e.printStackTrace(); } } static class li { String fqdn; String type; public li(String fqdn, String type){ this.fqdn = fqdn; this.type = type; } } }