List count gives null after adding elements

282 views Asked by At

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

2

There are 2 answers

3
MiMo On

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.

2
Rik On

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.