check if TcpLayer or udpLayer is not null in pcap.net

Mar 3, 2015 at 7:00 PM
Edited Mar 3, 2015 at 7:05 PM
Hi
i am using pcap.net to read packets from a pcap file and save them to new pcap file after anonymization(change some fields). i have a problem.
some pcap files have incorrect or incomplete packets e.g they have not tcp layer or payload. i want to know how check if tcplayer exist?

it is part of my code:
TcpLayer tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();                                 
UdpLayer udpLayer = (UdpLayer)packet.Ethernet.IpV4.Udp.ExtractLayer();
the pcap files that have no tcp or udp layer hav error in these lines. how can i check if tcpLayer is not null before this line?
and i have error for some layers that are not in some packets when i want to create new packets.
 if (ipV4Datagram.Protocol == IpV4Protocol.Tcp)
                                {
                                    //error dare
                                    PayloadLayer Payload= (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer(); //extract the data                                 
                                    if (Payload != null)
                                        tmpPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, Payload);
                                    else
                                        tmpPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer);

                                }
                                else if (ipV4Datagram.Protocol == IpV4Protocol.Udp)
                                {

                                    PayloadLayer Payload = (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer(); //extract the data                                 
                                    if (Payload != null)
                                        tmpPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, Payload);
                                    else
                                        tmpPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, udpLayer);

                                }
                                else if ((ipV4Datagram.Protocol != IpV4Protocol.Tcp) && (ipV4Datagram.Protocol != IpV4Protocol.Udp) && (ipV4Layer != null))
                                {                                                                        
                                        PayloadLayer Payload = (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer(); //extract the data                                 
                                        if (Payload != null)
                                            tmpPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, Payload);                                    
                                }
                                else if ((ipV4Datagram.Protocol != IpV4Protocol.Tcp) && (ipV4Datagram.Protocol != IpV4Protocol.Udp) && (ipV4Layer == null))
                                {
                                    PayloadLayer Payload = (PayloadLayer)packet.Ethernet.Payload.ExtractLayer(); //extract the data                                 
                                    if (Payload != null)
                                        tmpPacket = PacketBuilder.Build(packetTimestamp, ethernet, Payload);
                                }
how could i check a layer data exist?

my error is :

Object reference not set to an instance of an object.


thanks
Coordinator
Mar 13, 2015 at 8:55 AM
Hi fatima2007,

You can do this check:
if (packet.Ethernet.IpV4.Transport == null)
  ...
I hope this helps,

Boaz.