How can I fix the variable declaration error?

86 Views Asked by At

I am a programming newbie.I am trying to capture a udp ethernet packet,I use the following code:

using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
using PcapDotNet.Packets;
//.. System usings


namespace FirstLesson
{
    class Program
    {
        static void Main(string[] args)
        {
            Packet wpacket;

            // wpacket = received packet

            UdpDatagram udp = null;
            TcpDatagram tcp = null;
            Datagram datagram = null;

            IpV4Datagram ip4 = wpacket.Ethernet.IpV4;
            if (ip4.Protocol == IpV4Protocol.Udp)
            {
                udp = ip4.Udp;
                datagram = udp.Payload;
            }
            if (ip4.Protocol == IpV4Protocol.Tcp)
            {
                tcp = ip4.Tcp;
                datagram = tcp.Payload;
            }
            if (null != datagram)
            {
                int payloadLength = datagram.Length;
                using (MemoryStream ms = datagram.ToMemoryStream())
                {
                    byte[] rx_payload = new byte[payloadLength];
                    ms.Read(rx_payload, 0, payloadLength);
                }
            }
        }
    }

    public sealed class Packet : IList<byte>, 
        ICollection<byte>, 
        IEnumerable<byte>, IEnumerable, 
        IEquatable<Packet> 
    { 
        private readonly byte[] _data; 
        private readonly DateTime _timestamp; 
        private readonly IDataLink _dataLink; 
        private bool? _isValid; 
        private EthernetDatagram _ethernet; 
        private IpV4Datagram _ipV4;
    }

And it gives the following error:

Error CS0165 Use of unassigned local variable 'wpacket' I don't know how to initialize the wpacket variable. I tried =null but it doesn't work properly. Please help me fix the code. Note: I have attached "Class Packet" public sealed class Packet : IList<byte>, ICollection<byte>, IEnumerable<byte>, IEnumerable, IEquatable<Packet>

Hope the code can work!

1

There are 1 best solutions below

0
Itamar Peretz Cohen On

Initializing the packet means actually building something into it. Equaling it to "null" would simply do not effect. What you have to do is to construct a new 'Packet' class.

Check this code:

static void Main(string[] args)
{
    Packet wpacket = new Packet(); 

    UdpDatagram udp = null;
    TcpDatagram tcp = null;
    Datagram datagram = null;

    IpV4Datagram ip4 = wpacket.Ethernet.IpV4;
    if (ip4.Protocol == IpV4Protocol.Udp)
    {
        udp = ip4.Udp;
        datagram = udp.Payload;
    }
    if (ip4.Protocol == IpV4Protocol.Tcp)
    {
        tcp = ip4.Tcp;
        datagram = tcp.Payload;
    }
    if (null != datagram)
    {
        int payloadLength = datagram.Length;
        using (MemoryStream ms = datagram.ToMemoryStream())
        {
            byte[] rx_payload = new byte[payloadLength];
            ms.Read(rx_payload, 0, payloadLength);
        }
    }
}

I also suggest you going over the documentation of the library, to get a better grasp on some other concepts related to it - https://www.codeproject.com/Articles/12458/SharpPcap-A-Packet-Capture-Framework-for-NET

Good luck with the learnings!