This project is read-only.

Chance packet port

Nov 24, 2012 at 9:58 AM
Edited Nov 24, 2012 at 9:58 AM

i write a function that change the packet IP but in my function i need to check before if my packet is TCP or UDP.

is it possible to build new packet with the new port if i don't know if my packet is TCP or UDP ?

this is my code:

        public static Packet changePacketPort(Packet packet, ushort oldPort, ushort newPort)
        {
            try
            {
                EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
                IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
                DateTime packetTimestamp = packet.Timestamp;
                ILayer payload = null;
                Packet newPacket = null;
                TcpLayer tcpLayer = null;
                UdpLayer udpLayer = null;

                payload = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer();
                tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
                tcpLayer.Checksum = null;

                if (packet.Ethernet.IpV4.Protocol.ToString() == "Tcp")
                {
                    payload = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer();
                    tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
                    tcpLayer.Checksum = null;

                    if (packet.Ethernet.IpV4.Tcp.SourcePort == oldPort)
                    {
                        tcpLayer.SourcePort = newPort;
                    }
                    else if (packet.Ethernet.IpV4.Tcp.DestinationPort == oldPort)
                    {
                        tcpLayer.DestinationPort = newPort;
                    }

                    newPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer, payload);
                }
                else if (packet.Ethernet.IpV4.Protocol.ToString() == "Udp")
                {
                    payload = packet.Ethernet.IpV4.Udp.Payload.ExtractLayer();
                    udpLayer = (UdpLayer)packet.Ethernet.IpV4.Udp.ExtractLayer();
                    udpLayer.Checksum = null;

                    if (packet.Ethernet.IpV4.Udp.SourcePort == oldPort)
                    {
                        udpLayer.SourcePort = newPort;
                    }
                    else if (packet.Ethernet.IpV4.Udp.DestinationPort == oldPort)
                    {
                        udpLayer.DestinationPort = newPort;
                    }

                    newPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, udpLayer, payload);
                }

                return newPacket;
            }
            catch (Exception)
            {
                return null;
            }
        }
Dec 1, 2012 at 8:09 PM

Hi gshhar,

 

Try and use Transport instead of Udp or Tcp and see if this work out for you.

 

I hope this helps,

 

Boaz.