RIP BASED SOFTWARE ROUTER FOR WINDOWS

Jun 7 2010 1:38 PM
Good day everyone,

I am working on a project and I am having a few issues.

Project detail

1. Am trying to simulate a RIP based router in Windows
2. I am developing this in C#
3. Am using Winpcap and SharpCap
4. So far I have figured out what my routing table would look like

My issues are as follows

1. Initially when RIP starts it sends a request packet out all RIP enabled interfaces
on IP address 255.255.255.255
I have tried to simulate this directly but decided to let .NET manage the packet 
header creation, so what i have done is simply to send "Type 1 message" as 
the payload and when the receiving router sees this it knows it should send its routes.
This is not how it should be done, but i guess i can work with this for now, as I can't
seem to be able to create my own packet header in C# easily.

Any suggestions on how best I can create a packet without doing low level stuffs would be
appreciated or any library i can simply reference that just accepts port number, ip addresses, 
payload and returns a packet as response?

2. After sending I should wait for response from other Routers.
These received packets are called Response Packets and contain the responding routers'
routes.

To do this i have decided that after the Request is sent out all interfaces, I would wait for about
25sec for responses. 

Issue: I do not know how best to implement this, I have tried editting the Sample Codes in sharpCap
and also tried socket.listen method
but I can't tell if anything is coming in or not.

3. I installed WireShark to help me detect if I am even sending anything out the interfaces in the first place
and it appears am not, as wireshark does not report anything. And yes I selected the right interface.

So any help please.

Below is a cut out of my code so far

...
ArrayList ripIntefaces;
public void start(int[] deviceIndex)
        {
            //get list of intefaces again
            var devices = LivePcapDeviceList.Instance;

            //send request out all RIP enabled interfaces
            for (int k = 0; k < deviceIndex.Length; k++)
                sendRequestsInitial(devices[deviceIndex[k]]);

            //wait 4 response
            while (true)
            {
                //1st time around dont run infinitively, just capture n close
                for (int k = 0; k < deviceIndex.Length; k++)
                    receiveResponse(ripIntefaces, deviceIndex);     //this should be threaded 4 listening on each interface
            }
        }

....
  private void sendRequestsInitial(LivePcapDevice enabledIntf)
        {
            string nxtHop = enabledIntf.Addresses[1].Addr.ipAddress.ToString(); //should be but I cant bind ip add "255.255.255.255"; 
            int portNo = 520;

            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

            IPAddress sendTo = IPAddress.Parse(nxtHop);
            IPEndPoint sEndPoint = new IPEndPoint(sendTo, portNo);
            EndPoint sendEndPoint = (EndPoint)sEndPoint;
            sock.Bind(sendEndPoint);

            //create packet to send
            byte[] pktContent = packetProcessor.makeRequestPacket();
                        
            sock.SendTo(pktContent,sendEndPoint);
        }

....

 private void receiveResponse(ArrayList ripNetworks, int[] deviceIndex)
        {
            //monitor all ripInterfaces
            //start new thread 4 each - WORK ON
            var devices = LivePcapDeviceList.Instance;
            LivePcapDevice device = devices[deviceIndex[0]];    //testing 4 1 device 1st, others with thread

            device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);

            int readTimeoutMilliseconds = 25000;
            device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);

            device.StartCapture();

            //device.Close();

            
        } 

...

  private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
        //    myPacket tempPacket = new myPacket();

            var packet = PacketDotNet.Packet.ParsePacket(e.Packet);
            var ip = PacketDotNet.IpPacket.GetEncapsulated(packet);
            var udp = PacketDotNet.UdpPacket.GetEncapsulated(packet);
            string result = string.Empty;

                if(ip != null)
                    {          

                        if (udp != null)
                        {
                            result = Encoding.UTF8.GetString(udp.PayloadData);         

                        }
                }
                Console.WriteLine(result);
        }


....................

class packetProcessor
    {
        static byte[] packet;
        public static byte[] makeRequestPacket()
        {
            string content = "Type 1 Message, Request";
            packet = Encoding.ASCII.GetBytes(content);
            return packet;
        }
    }

So any inputs??