static IEnumerable <DnsTransaction.ResourceRecord> readCsv(string file) { string line; using (var reader = File.OpenText(file)) { while ((line = reader.ReadLine()) != null) { // Skip comment if (line[0] == '#') { continue; } string[] substr = line.Split(','); if (substr.Length != 4) { continue; } DnsTransaction.QTYPE qtype; if (Enum.TryParse <DnsTransaction.QTYPE>(substr[2], out qtype)) { byte[] data = null; switch (qtype) { case DnsTransaction.QTYPE.NS: data = DnsTransaction.getLabelBytes(substr[3]); break; case DnsTransaction.QTYPE.AAAA: case DnsTransaction.QTYPE.A: IPAddress ip; ip = IPAddress.Parse(substr[3]); data = ip.GetAddressBytes(); break; case DnsTransaction.QTYPE.MX: MemoryStream stream = new MemoryStream(); string[] subsubstr = substr[3].Split(' '); stream.Write(DnsTransaction.getLEBytes(ushort.Parse(subsubstr[0])), 0, 2); stream.Write(DnsTransaction.getLabelBytes(subsubstr[1]), 0, subsubstr[1].Length); data = stream.ToArray(); break; } yield return(new DnsTransaction.ResourceRecord(substr[0], qtype, DnsTransaction.RCLASS.IN, uint.Parse(substr[1]), data)); } } } }
public void cache(object obj) { uint source = 0; // source nameserver id? if (obj is DnsTransaction) { DnsTransaction t = obj as DnsTransaction; // cache question too? they're in the resource records.... // there should be a table that indicates the question (request made, the time, the flags, the response code, etc) foreach (DnsTransaction.ResourceRecord rr in t.getRecords()) { cache(rr); } } if (obj is DnsTransaction.ResourceRecord) { DnsTransaction.ResourceRecord record = obj as DnsTransaction.ResourceRecord; uint qid = getDnsQueryId(record.question); MySqlCommand sql = new MySqlCommand("SELECT id FROM records WHERE query=@query AND ttl > (now()-cached) ORDER BY cached DESC LIMIT 1", db); sql.Parameters.AddWithValue("@query", qid); sql.Prepare(); object r = sql.ExecuteScalar(); if (r == null) { // Not in cache, or expired sql.CommandText = "INSERT INTO records (query,ttl,data,source) VALUES (@query,@ttl,@data,@source)"; sql.Parameters.AddWithValue("@ttl", record.ttl); sql.Parameters.AddWithValue("@data", record.data); if (source != 0) { sql.Parameters.AddWithValue("@source", source); } else { sql.Parameters.AddWithValue("@source", null); } sql.ExecuteNonQuery(); } } }
protected void onRecieve(IAsyncResult result) { int nBytesRec = tor.proxy.TcpClient.Client.EndReceive(result); if (nBytesRec <= 0) { // tcp connection closing this.tor.cleanup(); } else { buffer.Write(chunk, 0, nBytesRec); foreach (DnsTransaction answer in DnsTransaction.parseBuffer(buffer)) // cache all resourcerecords (answers/authoratative/additional) { callback(answer); } this.tor.proxy.TcpClient.Client.BeginReceive(chunk, 0, chunk.Length, SocketFlags.None, new AsyncCallback(onRecieve), this); } }
public DnsTask(DnsTransaction.QuestionRecord question) { this.resolver = new DnsTransaction(1, new DnsTransaction.DnsFlags(0x0100), question); }