10 Replies - 8883 Views - Last Post: 19 April 2012 - 10:36 AM

#1 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

SnojNS: Hosts file killer/replacement

Posted 20 May 2011 - 11:23 PM

See this post for latest version release.

Haven't been active for a couple years, but I could use your guys' (and gals'!) commentary on my newest personal project!

I started SnojNS because I needed DNS records that the hosts file cannot do and I didn't want to setup a DNS server and kill access to a domain just to test those records. Since I knew of no other DNS servers that let you override only certain portions of the target domain, I had to start writing my own.

Right now, you can define a, cname, srv, and ptr records and if it doesn't know the requested domain or record type, it'll forward the request on to someone who does. This way, we can override say www.dreamincode.net to point to 127.0.0.1, but dreamincode.net would still point to where it's suppose to.

Current version can crash when presented with a ton of requests, so you'll want to run it just for your own box and not your entire network.

You can follow the dev on my personal site by watching the snojns tag.

downloads:


screenshots:

Posted Image
Posted Image
Posted Image



Thanks all!

This post has been edited by snoj: 01 June 2011 - 08:10 AM


Is This A Good Question/Topic? 1
  • +

Replies To: SnojNS: Hosts file killer/replacement

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: SnojNS: Hosts file killer/replacement

Posted 30 May 2011 - 09:43 AM

Snojns version 0.2 is released. (link)

After many an hour pouring over the code and running tests galore, I think I’ve resolved some of the major issues from version 0.1. Like how it would just randomly crash when the forward lookup hosts decided they had had enough. Now it’ll recover from it and chug along.

There is now some simple compression. So now instead of having 6 “google.com” entires, we now have 1 with references to that first occurrence. Down the road, I plan on adding better compression so that parts of domains can be compressed. For instance “someSubdomain.josherickson.org” becomes “someSubdomain.{reference to first josherickson.org}”.

  • fixed: non-coded types returned empty from outside sources.
  • fixed: crash when socket was interupted.
  • added: m attribute to tags. <a m="somehost" ip="1.1.1.1" />
  • added: beginning dns compression...I think. at least it appears to work.
  • notes: improved xml lookups. now can easily add xpath based lookups.
  • notes+: might need to improve this further to allow other filter types.
  • notes: rm attributes will likely wait until xpath fn:matches function is available.

Was This Post Helpful? 0
  • +
  • -

#3 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: SnojNS: Hosts file killer/replacement

Posted 01 June 2011 - 07:22 AM

Very interesting, I'm having a shot at the network code, trying to see if I can't give the performance a boost. You should look at putting it up on github/bitbucket etc, much easier than publishing zipped sources.
Was This Post Helpful? 1
  • +
  • -

#4 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: SnojNS: Hosts file killer/replacement

Posted 01 June 2011 - 08:09 AM

View PostT3hC13h, on 01 June 2011 - 09:22 AM, said:

Very interesting, I'm having a shot at the network code, trying to see if I can't give the performance a boost. You should look at putting it up on github/bitbucket etc, much easier than publishing zipped sources.


Thanks man! I'd do that, but I'd rather solidify the name first. I'm not sold on snojns quite yet. :/ Dunnot want to start a repo at one of those places if I plan on changing the name soonish. :/ There's also that I'm still learning to use Git (I already use it for source control with this project) and that some of the stuff I'm starting to work on requires passwords, so I'm not sure how to approach that with a repository.

I'd greatly appreciate any comments on the socket code! It's more or less lifted from the MSDN with only some slight modification.
Was This Post Helpful? 0
  • +
  • -

#5 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: SnojNS: Hosts file killer/replacement

Posted 01 June 2011 - 07:03 PM

Alright man, do your thing.

I've dropped the async code for now, the msdn samples for the Socket.BeginX methods use the async calls and then block the thread waiting for the async call to finish...completely pointless IMO. With single threading I can see where we'd run into an issue where a lookup gets stalled waiting for a query from a forward lookup leaving all the other requests to sit but we can tackle that a bit later.

I did mess around with an async pattern and I can toss out some mockup code but I'm a little worried about the parsing code not being threadsafe and acting wonky.

I added a new class for organisational purposes ConnectionInfo.cs

using System.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace snojns
{
    class ConnectionInfo
    {
        public const int BufferSize = 1024;
        public System.Net.EndPoint EndPoint { get; set; }
        public int ByteCount { get; set; }
        public byte[] buffer { get; set; }
        //public IList<System.ArraySegment<byte>> buffer { get; set; }
        public string Index { get; set; }
        public ConnectionInfo()
        {
            buffer = new byte[BufferSize - 1];
            //buffer = new List<ArraySegment<byte>>(BufferSize);
        }
    }
}


Heres the updated Watchtowers.cs working from .2

using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace snojns
{
    class Watchtowers
    {
        //System.Threading.ThreadPool tp = 
        System.Threading.Thread t;
        System.Net.Sockets.Socket s;
        System.Net.EndPoint rep = (System.Net.EndPoint)new System.Net.IPEndPoint(System.Net.IPAddress.Any, 53);
        IList<ConnectionInfo> _connections = new List<ConnectionInfo>();
        private xmldns.XmlDnsDocument hosts = new xmldns.XmlDnsDocument();
        public bool listening = false;
        public Watchtowers()
        {

            //this.c = new System.Net.Sockets.UdpClient(53);
            this.t = new Thread(new ThreadStart(readLoop));
        }

        private static Socket getListenSocket()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress hostIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0];
            IPEndPoint ep = new IPEndPoint(hostIP, 53);
            sock.Bind(ep);
            return sock;
        }

        private void readLoop()
        {
            ConnectionInfo conn;
            if (_connections.Count > 0)
                conn = _connections[0];
            else
                conn = new ConnectionInfo();
            while (listening)
            {
                try
                {
                    EndPoint tempEP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 53);
                    conn.ByteCount = s.ReceiveFrom(conn.buffer, ref tempEP);
                    if (conn.ByteCount > 0)
                    {
                        conn.EndPoint = tempEP;
                        Stopwatch sw = new Stopwatch();
                        sw.Start();
                        acceptInc(conn);
                        sw.Stop();
                        Console.WriteLine(sw.ElapsedMilliseconds);
                    }
                }
                catch (SocketException sockEx)
                {
                    Debug.WriteLine(sockEx);
                    s.Close();
                    s = getListenSocket();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }


        public void start()
        {
            this.s = getListenSocket();
            ConnectionInfo conn = new ConnectionInfo();
            _connections.Add(conn);
            this.listening = true;
            this.t.Start();
        }
        public void stop()
        {
            this.listening = false;
            Console.WriteLine("Closing socket");
            s.Close(100);
            this.t.Join(1000);
            Console.WriteLine("Listen thread has exited");
        }

        private void acceptInc(ConnectionInfo conn)
        {
            //UdpState us = (UdpState)ar.AsyncState;
            //Byte[] receiveBytes = us.client.EndReceive(ar, ref us.ep);
            //Console.WriteLine("req id = {0}", dns.Utilities.getUint16(ref receiveBytes, 0));
            byte[] rawmsg;
            rawmsg = new byte[conn.ByteCount];
            Array.Copy(conn.buffer, 0, rawmsg, 0, conn.ByteCount);
            try
            {
                dns.Message msg = dns.Message.Parse(ref rawmsg);
                dns.Message rmsg = msg.CreateResponse();

                //dns.Message rmsg2;
                try
                {
                    rmsg = lookup2(msg, ref rawmsg);

                }
                catch (dns.exceptions.DNSParseException dnse)
                {
                    //debug.SaveError(dnse);
                    debug.WriteLine(debug.PrintError(dnse), 15);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);

                    rmsg = msg.CreateResponse();
                    rmsg.RCode = (UInt16)dns.RCodes.ServerFailure;

                    //debug.SaveError(e, rawmsg);
                }
                if (rmsg != null)
                {
                    rmsg.ID = msg.ID;
                    debug.WriteLine("sending reply...", 7);
                    debug.WriteLine(rmsg.ToString(), 7);
                    byte[] breply = rmsg.GetBytes();
                    //debug.SaveRaw(breply);
                    this.s.SendTo(breply, conn.EndPoint);
                    //sl.ws.Disconnect(true);
                }
            }
            catch (dns.exceptions.DNSParseException dnse)
            {
                //debug.SaveError(dnse);
                debug.WriteLine(debug.PrintError(dnse), 15);
            }
            catch (Exception e)
            {
                //TODO: Error reporting
                debug.WriteLine(e.Message, 15);
                debug.WriteLine(e.StackTrace, 15);

                //debug.SaveError(e, rawmsg);
            }
            /*
            StringBuilder sb = new StringBuilder();

            foreach (byte b in msg)
            {
                sb.AppendLine(string.Format("byte: {0}\thex: {1}\tascii: {2}", b, Convert.ToString(b, 16).PadLeft(2,'0'), Convert.ToChar(B)/>));
            }
            
            Console.WriteLine("packet length: {0}", length);
            Console.WriteLine("dns id: {0}", dns.Utilities.getUint16(ref msg, 0));
            try {
                dns2.Message m = dns2.Message.Parse(ref msg);
                Console.WriteLine("requested domain: {0}", m.Questions[0].Name);
            } catch {
                System.IO.File.WriteAllText(string.Format(@".\req{0}.txt", dns.Utilities.getUint16(ref msg, 0)), sb.ToString(), Encoding.ASCII);
            }*/


            //us.client.Close();
        }

        private dns.Message lookup2(dns.Message msg, ref byte[] ori_msg)
        {
            //List<dns.ResourceRecord> rrs = this.hosts.Lookup(msg.Questions[0]);
            List<dns.ResourceRecord> rrs = this.hosts.Find(msg.Questions[0]);

            if (rrs.Count == 0)
            {
                //forward lookup.
                debug.WriteLine(string.Format("nothing found for '{0}'...looking ahead", msg.Questions[0]), 7);
                foreach (IPAddress fl in this.hosts.GetForwardLookups(msg.Questions[0]))
                {
                    dns.Message resmsg = new dns.Message();
                    byte[] res = new byte[0];
                    try
                    {
                        debug.WriteLine(string.Format("Looking ahead to {0}", fl.ToString()), 7);
                        //Console.WriteLine("trying {0}", fl);
                        System.Net.Sockets.UdpClient u = new System.Net.Sockets.UdpClient();
                        u.Connect(fl, 53);
                        System.Net.IPEndPoint rep = (System.Net.IPEndPoint)u.Client.RemoteEndPoint;
                        byte[] b = msg.GetBytes();
                        u.Send(b, b.Length);
                        res = u.Receive(ref rep);
                        //resmsg = dns.Message.Parse(ref res).CreateResponse();
                        resmsg = dns.Message.Parse(ref res);
                        u.Close();
                        resmsg.ID = msg.ID;
                        //dns.Message flresponse = msg.CreateResponse();
                        //flresponse.Answers = resmsg.Answers;
                        return resmsg;
                    }
                    catch (Exception e)
                    {
                        debug.WriteLine("...Error looking ahead", 7);
                        if (res.Length > 0)
                        {
                            throw new dns.exceptions.DNSParseException(string.Format("error looking forward to {0}", fl.ToString()), 0, res, e);
                        }
                        else
                        {
                            throw new dns.exceptions.DNSParseException(string.Format("error looking forward to {0}", fl.ToString()), 0, resmsg.GetBytes(), e);
                        }
                        //throw new dns.exceptions.DNSParseException(string.Format("error looking forward to {0}", fl.ToString()), e);
                    }
                }
                debug.WriteLine("No records or forward looks found...", 7);
                dns.Message norecord = msg.CreateResponse();
                norecord.RCode = (ushort)dns.RCodes.NameError;
                //norecord.
                return norecord;
            }
            dns.Message response = msg.CreateResponse();
            response.Answers.AddRange(rrs);
            return response;
        }

        /*private dns.Message lookup(dns.Message msg)
        {
            //TODO: These need to move into the XML and be able to overridden on a per domain request basis.
            //IPAddress[] forwardlookups = new IPAddress[] { IPAddress.Parse("8.8.8.8"), IPAddress.Parse("8.8.4.4") };
            List<IPAddress> forwardLookups = new List<IPAddress>();
            dns.Message response = msg.CreateResponse();
            foreach (dns.Question q in msg.Questions)
            {
                //Console.WriteLine("Request for {0} ", q.DomainFlat);
                //TODO: replace this with code to check on local hosts file
                xmldns.DoSomething[] acts = new xmldns.DoSomething[0];
                switch (q.Type)
                {
                    case (UInt16)dns.Types.PTR:
                        acts = this.hosts.GetRecord("ptr", q.Name);
                        break;
                    case (UInt16)dns.Types.A:
                        acts = this.hosts.GetRecord("a", q.Name);
                        break;
                    default:
                        //okay, we don't know what to do here...just go get a forward lookup.
                        acts = this.hosts.GetRecord("ns", q.Name);
                        break;
                }

                foreach (xmldns.DoSomething act in acts)
                {
                    switch (act.rt)
                    {
                        case xmldns.RecordTypes.a:
                            dns.ResourceRecords.A arec = new dns.ResourceRecords.A();
                            arec.IPAddress = act.nextIP;
                            response.Answers.Add(new dns.ResourceRecord(q.Name, arec));
                            break;
                        case xmldns.RecordTypes.ptr:
                            dns.ResourceRecords.PTR data = new dns.ResourceRecords.PTR();
                            data.Name = act.nextHostname;
                            response.Answers.Add(new dns.ResourceRecord(q.Name, data));
                            break;
                        case xmldns.RecordTypes.ns:
                            //TODO: need to do forward lookup here
                            forwardLookups.Add(act.nextIP);
                            break;
                    }
                    /*if (false || act.isFinal)
                    {
                        //Message response = msg.CreateResponse();
                        response.Answers.Add(new dns.ResouceRecords.Answer(q)
                        {
                            RData = new AResouceRecordData()
                            {
                                Data = act.nextIP.GetAddressBytes()
                            },
                            Type = (UInt16)dns.Question.Types.A,
                            Class = (UInt16)dns.Question.Classes.IN
                        });
                        Console.WriteLine("---Responding");
                        return response ;
                    }* /
                }

                if (response.AnCount == 0)
                {
                    foreach (IPAddress fl in forwardLookups)
                    {
                        try
                        {
                            System.Net.Sockets.UdpClient u = new System.Net.Sockets.UdpClient();
                            u.AllowNatTraversal(true);
                            u.Connect(fl, 53);
                            System.Net.IPEndPoint rep = (System.Net.IPEndPoint)u.Client.RemoteEndPoint;
                            byte[] b = msg.GetBytes();
                            u.Send(b, b.Length);
                            byte[] res = u.Receive(ref rep);
                            dns.Message resmsg = dns.Message.Parse(ref res);
                            u.Close();
                            resmsg.ID = msg.ID;
                            return resmsg;
                        }
                        catch (Exception e)
                        {
                            debug.WriteLine(string.Format("Error doing forward lookup with {0}.", fl.ToString()), int.MaxValue);
                            debug.WriteLine(string.Format("Exception: {0}.", e.Message), int.MaxValue);
                            debug.WriteLine(string.Format("{0}.", e.StackTrace), int.MaxValue);
                            debug.WriteLine("-----------------------", int.MaxValue);
                        }
                    }
                }

                return response;

                /*if (false) 
                {
                    //Message nreq = new Message();
                    
                    System.Net.Sockets.UdpClient u = new System.Net.Sockets.UdpClient();
                    u.AllowNatTraversal(true);
                    u.Connect(forwardlookups[0], 53);
                    System.Net.IPEndPoint rep = (System.Net.IPEndPoint)u.Client.RemoteEndPoint;
                    byte[] b = msg.GetBytes();
                    u.Send(b, b.Length);
                    byte[] res = u.Receive(ref rep);
                    dns.Message resmsg = dns.Message.Parse(res);
                    u.Close();
                    return resmsg;
                }* /
            }

            throw new Exception("nothing");
        }*/
    }
}




And Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace snojns
{
    class Program
    {
        private static Watchtowers w;
        static void Main(string[] args)
        {
            Console.TreatControlCAsInput = false;
            Console.CancelKeyPress += new ConsoleCancelEventHandler(handleCancelKeyPress);
            statemachine.debugLvl = 0; //ALL!
            statemachine.debugLvl = 7; //informational
            //statemachine.debugLvl = 16; //NONE!
            try
            {
                w = new Watchtowers();
                w.start();
                System.Windows.Forms.Application.Run();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Write(e.StackTrace);
            }
            Console.WriteLine("All done!");
            Console.In.ReadLine();
            return;

            //This is test code!
            #region testing code
            /*dns.Message m = new dns.Message();
            m.RD = true;
            m.ID = 0xdead;
            m.Questions.Add(new dns.Question() { Domain = new dns.DomainName("www.northeastern.edu"), Class = (UInt16)snojns.dns.Question.Classes.IN, Type = (UInt16)dns.Question.Types.A });
            dns.Message n = m.CreateResponse(); //(dns.Message)m.Clone();
            n.ID = 0xdaed;
            m.Questions[0].Domain = new dns.DomainName("test.com");
            byte[] m_array = m.GetBytes();
            byte[] n_array = n.GetBytes();
            for (int i = 0; i < ((m_array.Length > n_array.Length) ? m_array.Length : n_array.Length); i++)
            {
                string a = "na";
                string b = "na";
                if (i < m_array.Length)
                {
                    a = m_array[i].ToString();
                }
                if (i < n_array.Length)
                {
                    b = n_array[i].ToString();
                }
                Console.WriteLine("{0}|{1}", a, B)/>;
            }
            Console.In.ReadLine();
            return;*/

            /*
            //forwarding requests test
            dns.Message req = new dns.Message();
            req.ID = 1;
            req.RD = true;
            req.Questions.Add(new dns.Question(new dns.DomainName("josherickson.org"), dns.Question.Classes.IN, dns.Question.Types.A));

            System.Net.Sockets.UdpClient u = new System.Net.Sockets.UdpClient();
            u.Connect(System.Net.IPAddress.Parse("8.8.8.8"), 53);
            System.Net.IPEndPoint rep = (System.Net.IPEndPoint)u.Client.RemoteEndPoint;
            u.AllowNatTraversal(true);
            byte[] b= req.GetBytes();
            u.Send(b, b.Length);
            byte[] res = u.Receive(ref rep);
            dns.Message resmsg = dns.Message.Parse(res);
            return;*/
            //string t = snojns.xmldns.StringUtil.And("asd", "bsr", xmldns.StringUtil.Direction.rtl);
            //string t2 = xmldns.StringUtil.Mask("rse", "010", xmldns.StringUtil.Direction.rtl);

            /*
            Test on references.
            byte[] t = new byte[] { 0x03, //three chars
                0x77,0x77,0x77, // www
                0x0c, // 12 chars
                0x6e,0x6f,0x72,0x74,0x68,0x65,0x61,0x73,0x74,0x65,0x72,0x6e, //northeastern
                0x03, // 3 chars
                0x65,0x64,0x75, //edu
                0x00,
                0x03,
                0x65,0x65,0x65, //eee
                0xc0, 0x04,
                0xc0, 0x04,
                0x00
            };
            dns.DomainName d = dns.DomainName.Parse(ref t, 22);
            return;*/

            /*
            Testing new dns2 namespace.
            byte[] query_array = new byte[] {
                0xde, 0xad, //ID
                0x01, 0x00, // 0 0000 0 0 1 0 000 0000 | QR OPCODE AA TC RD RA Z RCODE
                0x00, 0x01, //1 questions
                0x00, 0x00, //No answers
                0x00, 0x00, //No name servers
                0x00, 0x00, //No additional resources
				
                0x03, //three chars
                0x77,0x77,0x77, // www
                0x0c, // 12 chars
                0x6e,0x6f,0x72,0x74,0x68,0x65,0x61,0x73,0x74,0x65,0x72,0x6e, //northeastern
                0x03, // 3 chars
                0x65,0x64,0x75, //edu
                0x00, // end of line
                0x00,0x01, //Query is Type A records
                0x00,0x01 //Query is Class IN.
            };
            byte[] response_array = new byte[] {
                0xde, 0xad, //ID
                0x81, 0x80, // { 1 000|0 0 0 1 }{ 1 000 | 0000 } // QR OPCODE AA TC RD RA Z RCODE
                0x00, 0x01, //1 Question
                0x00, 0x02, //1 answers
                0x00, 0x00, //No name servers
                0x00, 0x00, //No additional resources
                
                0x03, //three chars
                0x77,0x77,0x77, // www
                0x0c, // 12 chars
                0x6e,0x6f,0x72,0x74,0x68,0x65,0x61,0x73,0x74,0x65,0x72,0x6e, //northeastern
                0x03, // 3 chars
                0x65,0x64,0x75, //edu
                0x00, // end of line
                0x00,0x01, //Query is Type A records
                0x00,0x01, //Query is Class IN.

                0xc0, 0x0c, //pointer 0xc (0b1100, 12) points to 0x00c (0b000000001100, 12) //Offset is from the beginning of the packet.
                0x00, 0x01, //answer type A
                0x00, 0x01, //in class
                0x00, 0x00, 0x02, 0x58, //ttl (600 seconds)
                0x00, 0x04, //address is 4 bytes
                0x9b, 0x21, 0x11, 0x44, //ipaddress 155.33.17.68

                0x03,
                0x77,0x77,0x77, // www
                0xc0, 0x10, //
                0x00, 0x01, //answer type A
                0x00, 0x01, //in class
                0x00, 0x00, 0x02, 0x58, //ttl (600 seconds)
                0x00, 0x04, //address is 4 bytes
                0x9b, 0x21, 0x11, 0x45 //ipaddress 155.33.17.69
            };
            dns2.Message m = dns2.Message.Parse(ref query_array);

            dns2.Message r = dns2.Message.Parse(ref response_array);

            return;*/

            /*foreach (ushort a in new ushort[] { 0x0000, 0x1000, 0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0x7000, 0x8000, 0x9000, 0xa000, 0xb000, 0xc000, 0xd000, 0xe000, 0xf000 })
            {
                ushort b = (ushort)(a >> 1);
                ushort c = (ushort)(a >> 12);
                //Console.WriteLine(string.Format("ori: {0}\tushort: {1}\thex: 0x{2}\tbyte: {3}", a, b, Convert.ToString(b, 16).PadLeft(4, '0'), BitConverter.GetBytes(c)[0]));
                Console.WriteLine("Reserved{0} = 0x{1},", BitConverter.GetBytes(c)[0], Convert.ToString(b, 16).PadLeft(4, '0'));
            }
            Console.In.ReadLine();
            return;*/

            /*List<byte> lmsg = new List<byte>(); //.Select(f => true).ToArray();
            foreach (string s in System.IO.File.ReadAllLines(@".\req.yahoo.com.byte"))
            {
                lmsg.Add(byte.Parse(s));
            }
            byte[] bmsg = lmsg.ToArray();
            List<dns.DomainName> dnames = new List<dns.DomainName>();
            dnames.Add(dns.DomainName.Parse(ref bmsg, 12));
            dnames.Add(dns.DomainName.Parse(ref bmsg, 27));
            dnames.Add(dns.DomainName.Parse(ref bmsg, 43));
            dnames.Add(dns.DomainName.Parse(ref bmsg, 59));
            dnames.Add(dns.DomainName.Parse(ref bmsg, 75));
            dnames.Add(dns.DomainName.Parse(ref bmsg, 91));
            List<dns.ResourceRecord> rrs = new List<dns.ResourceRecord>();
            UInt16[] offsets = new UInt16[] { 27, 43, 59, 75, 91 };
            foreach (UInt16 i in offsets)
            {
                UInt16 noffset = 0;
                try
                {
                    dns.ResourceRecord rr = new dns.ResourceRecord();
                    
                    rr.Parse(ref bmsg, i, out noffset);
                    rrs.Add(rr);
                }
                catch (dns.exceptions.DNSParseException dnse)
                {
                    Console.WriteLine("offset: {0}\tnoffset: {1}", i, noffset);
                    Console.WriteLine(dnse.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine("offset: {0}\tnoffset: {1}", i, noffset);
                    Console.WriteLine(e.Message);
                }
            }
            Console.In.ReadLine();
            return;*/
            //dns.Message dmsg = dns.Message.Parse(ref bmsg);
            //return;
            /*try
            {
                bool run = true;
                while (run)
                {
                    try
                    {
                        WatchTower wt = new WatchTower();
                        wt.watching = true;
                        wt.start();
                        while (wt.watching)
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    }
                    catch (Exception e)
                    {
                        //TODO: Better error messages and the like.
                        debug.WriteLine("error: " + e.Message, int.MaxValue);
                    }
                    //TODO: Stop hooks.
                }

            }
            catch (Exception e)
            {
                debug.WriteLine("error: " + e.Message, int.MaxValue);
                Console.In.ReadLine();
            }*/
            /*
            
            dns.Message m = new dns.Message();
            m.RD = true;
            m.ID = 0xdead;
            m.Questions.Add(new dns.Question() { Domain = new dns.DomainName("www.northeastern.edu"), Class = (UInt16)snojns.dns.Question.Classes.IN, Type = (UInt16)dns.Question.Types.A });
            dns.Message n = (dns.Message)m.Clone();
            n.ID = 0xdaed;
            byte[] m_array = m.GetBytes();
            byte[] n_array = n.GetBytes();
            for (int i = 0; i < ((m_array.Length > n_array.Length) ? m_array.Length : n_array.Length); i++)
            {
                string a = "na";
                string b = "na";
                if (i < m_array.Length)
                {
                    a = m_array[i].ToString();
                }
                if (i < n_array.Length)
                {
                    b = n_array[i].ToString();
                }
                Console.WriteLine("{0}|{1}", a, B)/>;
            }
            Console.In.ReadLine();
            return;
            
            byte[] query_array = new byte[] {
                0xde, 0xad, //ID
                0x01, 0x00, // 0 0000 0 0 1 0 000 0000 | QR OPCODE AA TC RD RA Z RCODE
                0x00, 0x01, //1 questions
                0x00, 0x00, //No answers
                0x00, 0x00, //No name servers
                0x00, 0x00, //No additional resources
				
                0x03, //three chars
                0x77,0x77,0x77, // www
                0x0c, // 12 chars
                0x6e,0x6f,0x72,0x74,0x68,0x65,0x61,0x73,0x74,0x65,0x72,0x6e, //northeastern
                0x03, // 3 chars
                0x65,0x64,0x75, //edu
                0x00, // end of line
                0x00,0x01, //Query is Type A records
                0x00,0x01 //Query is Class IN.
            };

            dns.Message m = new dns.Message();
            m.RD = true;
            m.ID = 0xdead;
            m.Questions.Add(new dns.Question() { Domain = new dns.DomainName("www.northeastern.edu"), Class = (UInt16)snojns.dns.Question.Classes.IN, Type = (UInt16)dns.Question.Types.A });
            byte[] m_array = m.GetBytes();

            for (int i = 0; i < ((m_array.Length > query_array.Length) ? m_array.Length : query_array.Length); i++)
            {
                string a = "na";
                string b = "na";
                if (i < m_array.Length)
                {
                    a = m_array[i].ToString();
                }
                if (i < query_array.Length)
                {
                    b = query_array[i].ToString();
                }
                Console.WriteLine("{0}|{1}", a, B)/>;
            }
            return;
             
             */
            #endregion
        }
        static void handleCancelKeyPress(object sender,ConsoleCancelEventArgs e)
        {
            if (w != null)
                w.stop();
            System.Windows.Forms.Application.Exit();
        }
    }

    public class statemachine
    {
        public static int debugLvl
        {
            get { return debug.level; }
            set { debug.level = value; }
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#6 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: SnojNS: Hosts file killer/replacement

Posted 02 June 2011 - 09:00 AM

Thanks T3hC13h!

I'm looking forward to integrating some of that into the code!

Though I can't confirm without the code in front of me, but I can't think of an instance where multiple threads could be an issue. I haven't done a whole lot of work with threads before, so any pointers at my code would be super helpful! Which would be great, as in the next version I'm hoping to do some things with caching and round robins.

Anywho, I'm off to read up on lock()s and Monitors!
Was This Post Helpful? 0
  • +
  • -

#7 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: SnojNS: Hosts file killer/replacement

Posted 03 June 2011 - 08:27 AM

I saw some potential issues in the utilities methods but a simple lock object should take care of those.
Was This Post Helpful? 0
  • +
  • -

#8 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: SnojNS: Hosts file killer/replacement

Posted 03 June 2011 - 08:51 AM

Had a conversation with Shane Hudson on IRC earlier today and we talked a bit about using SnojNS instead of the hosts file. He brought up that it could be used for ad blocking, but wanted to know why he would use it over the hosts file.


  • 1 line of XML (3 if formatted) to block an entire domain
  • Since all subdomains are blocked, no matter how many new ones are created by the ad company, they will always be blocked.


Example snojns config:
<domain d="."> <!-- root -->
	<!-- formatted version -->
	<domain d="adcompany.com.">
		<a m="." ip="0.0.0.0" /> <!-- '.' is not a wild card, it just appears in all subdomains. -->
	</domain>
	<!-- one liner -->
	<domain d="adcompany.com."><a m="." ip="0.0.0.0" /></domain>
</domain>



I'd rather not update the hosts file for each new subdomain for adcompany.com, but instead just one line to take care of it forever. ....At least until adcompany.com gets a new domain.

View PostT3hC13h, on 03 June 2011 - 10:27 AM, said:

I saw some potential issues in the utilities methods but a simple lock object should take care of those.

OOOhhhh.

I figured they were fairly safe since I was passing references to them and they only work on a specific object. :/ I got a lot to learn.

Well, I think I know what I'll be doing this weekend! Thanks again man!....or woman?
Was This Post Helpful? 1
  • +
  • -

#9 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: SnojNS: Hosts file killer/replacement

Posted 04 June 2011 - 12:50 PM

The passwords bit is simple. Just make sure you never, ever commit a password. The easiest way to do that is to store the passwords and other credentials separate from the actual code. Use a configuration file that you can put in ~/ or something.
Was This Post Helpful? 0
  • +
  • -

#10 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: SnojNS: Hosts file killer/replacement

Posted 11 July 2011 - 02:54 PM

Well, what do you know, 5 weekends later and no movement from me! Babies....amiright fella's? Unfortunatly, no real code or binaries this time. Mostly out loud thoughts.

T3hC13h, I ended up not using your modifications! :S I'd like to stay with the async/threaded model as I feel it makes the most sense for the goals of the project.

With that in mind, I've added a "queue", an array of waiting IAsyncResult objects (5 currently) and sleeps until at least one finishes....that is if I understand what I did correctly. I still need to do some testing to see if it actually works like I imagine.

Basic code:
list<iasyncresult> queue;
while(listening) {
	
	for(i=0;i<queue.count;i++) {
		if(queue[i].iscompleted) {
			queue.removeat(i);
		}
	}
	//reindex?
	queue = new list<iasynresult>(queue);
	
	if(queue.where(f => !f.iscompleted).count > 4) {
		sleep(5);
		continue;
	}
	
	result = socket.receive()
	
	queue.add(result);
}


In the works is a ssh lookup! Using an SSH connection and dig, I'm able to get CNAME and A records. Code cleanup and deleting things no longer needed are also getting a serious work-over in prep for launching on SF or github. There's also lock()ing down to make things thread safe.

*crossing fingers* If the little one and wife lets me, I'm hoping there will be a release by month's end since winter is coming.
Was This Post Helpful? 0
  • +
  • -

#11 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: SnojNS: Hosts file killer/replacement

Posted 19 April 2012 - 10:36 AM

This is what happens when work sucks, you buy a house and have yet another baby. Your hobbies suffer and your ADHD brain decides it wants to do other things.

But if you follow my blog, you may have seen I haven't been entirely neglectful of SnojNS. It's been renamed DNShifter and I started experimenting with a nodejs variant (code).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1