0

I am having trouble retrieving values from the list Packet.net and SharpPcap are the two libraries used here ! I have 2 function which does the main work 1st one This function is called from ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessPacket), ph); which is inside a background worker This is the function which calls the ProcessTcpPacket()

private void ProcessPacket(object p)
    {
        PacketHolder pHolder = (PacketHolder) p;
        RawCapture raw = pHolder.RawPacket;

        Packet packet = null;
        if (raw.LinkLayerType == LinkLayers.Ethernet)
        {
            packet = Packet.ParsePacket(PacketDotNet.LinkLayers.Ethernet, raw.Data);
        }
        else if (raw.LinkLayerType == LinkLayers.LinuxSLL)
        {
            packet = Packet.ParsePacket(PacketDotNet.LinkLayers.LinuxSLL, raw.Data);
        }

        while (packet.PayloadPacket != null)
        {
            packet = packet.PayloadPacket;
        }

        if (packet is TcpPacket && packet.PayloadData != null)
        {
            if (packet.PayloadData.Length > 0)
            {
                TcpPacket tcp = (TcpPacket) packet;
                ProcessTcpPacket(tcp, raw.Timeval, pHolder.Index); //called here!
            }
        }
        else
        {
            if (packet.PayloadData.Length > 0)
            {
                UdpPacket udp = (UdpPacket) packet;
                ProcessUdpPacket(udp, raw.Timeval, pHolder.Index);
            }
        }

    }

private void ProcessTcpPacket(TcpPacket tcp, PosixTimeval posixTimeval, ulong Index)
    {
        IpPacket parentPacket = (IpPacket) tcp.ParentPacket;

        IPAddress tcpSrcAddress = SourceIPList.Find(srcIP => srcIP.Equals(parentPacket.SourceAddress));
        IPAddress tcpDstAddress = DestinationIPList.Find(dstIP => dstIP.Equals(parentPacket.DestinationAddress));

        if (tcpSrcAddress.Equals(parentPacket.SourceAddress) || tcpDstAddress.Equals(parentPacket.DestinationAddress))
        {
            PacketDetails pd = new PacketDetails(); //class with simple getters and setters
            pd.Index = Index;
            pd.Time = posixTimeval;
            pd.Buffer = tcp.PayloadData;
            //TcpPackets.Add(pd);
            socketHelper.tcpPackets.Add(pd); //properly adds all the packets to list 

        }
    }

Now The below function is inside SocketHelper Class where I want to retrieve the data inside the tcpPackets list

public void ProcessTCPMessage(TcpClient tcpClient, NetworkStream stream, byte[] clientByte)
    {
        tcpPackets = new List<PacketDetails>(128);
        strRequest = Encoding.ASCII.GetString(clientByte, 0, clientByte.Length);
        myClient = tcpClient;
        strRequest = strRequest.Substring(0, 5);
        _form1 = new Form1();
        if (strRequest.Equals("Hello"))
        {
            for (int i = 0; i < 100; i++)
            {
                strResponse = tcpPackets[i].Buffer.ToString(); // here I get the list count as null and throws an exception..
            }
        }
        else
        {
            strResponse = "What?";
        }
        bytesSent = Encoding.ASCII.GetBytes(strResponse);
        stream.Write(bytesSent, 0, bytesSent.Length);
    }

I don't know what I am doing wrong ! Plz help ! And Try not to be rude if I am doing some thing silly! Just reply thinking I am noob ! :P :D

Questioner
  • 233
  • 5
  • 13
  • 2
    You are assigning a List of PacketDetails (of 128 entries) to tcpPackets, You never fill it with anything, but do start reading from it (where your error is). – Marvin Smit Oct 22 '13 at 09:44
  • Index was out of range. Must be non-negative and less than the size of the collection. This is the exception I get and Marvin if I do tcpPackets = new List(); then also it wont work ! And that is just a default initial capacity that shouldn't cause any problem – Questioner Oct 22 '13 at 09:46

2 Answers2

0

tcpPackets is empty: it is initialized as a list with an initial capacity of 128 items, but it does not contain any items (there is no code that add items to it), hence trying to access its first 100 items will result in an error.

MiMo
  • 11,793
  • 1
  • 33
  • 48
  • When print to console while adding I get the list count of more than 8000, Forgot to say The ProcessPacket(object p) function is in the Form1.cs, only ProcessTCPMessage() is in another class ! – Questioner Oct 22 '13 at 10:03
  • My friend I am adding to `tcpPackets` [link](http://s15.postimg.org/mf281x117/untitled.jpg) Check the debug image in the link – Questioner Oct 22 '13 at 10:11
  • 1
    Yes, you are adding items to the `tcpPackets` property of the `socketHelper` object, that has nothing to do with the `tcpPackets` local variable that is causing the exception - that is declared and never added to, nor passed to another function before being accessed – MiMo Oct 22 '13 at 10:22
0

You are instantiating the tcpPackets list at the beginning of the ProcessTCPMessage method, but you don't put in any elements before using it in the for loop. So when you're trying to reed the Buffer property of the list element, the element is empty (or more accurately, null), resulting in a NullReferenceException.

Rik
  • 28,507
  • 14
  • 48
  • 67