0

In my C# application I'm using the PacketDotNet lib. So, I'm trying to send a vlan tagged frame, but can't build it correctly. My code:

public static void Main(string[] args)
{
    const ushort udpSourcePort = 123;
    const ushort udpDestinationPort = 321;
    var udpPacket = new UdpPacket(udpSourcePort, udpDestinationPort);

    var ipSourceAddress = System.Net.IPAddress.Parse("192.168.1.1");
    var ipDestinationAddress = System.Net.IPAddress.Parse("192.168.1.2");
    var iPv4Packet = new IPv4Packet(ipSourceAddress, ipDestinationAddress);

    var vlan = new Ieee8021QPacket(iPv4Packet.BytesSegment)
    {
        VlanIdentifier = 14
    };

    const string sourceHwAddress = "90-90-90-90-90-90";
    var ethernetSourceHwAddress = System.Net.NetworkInformation.PhysicalAddress.Parse(sourceHwAddress);
    
    const string destinationHwAddress = "80-80-80-80-80-80";
    var ethernetDestinationHwAddress = System.Net.NetworkInformation.PhysicalAddress.Parse(destinationHwAddress);

    
    var ethernetPacket = new EthernetPacket(ethernetSourceHwAddress,
        ethernetDestinationHwAddress,
        EthernetType.None);

    // Now stitch all of the packets together
    iPv4Packet.PayloadPacket = udpPacket;
    vlan.PayloadPacket = iPv4Packet;
    ethernetPacket.PayloadPacket = vlan;

    // and print out the packet to see that it looks just like we wanted it to
    Console.WriteLine(ethernetPacket.ToString());
}

However, the output created by seems not to be correct.

How to build the vlan tagged frame in PacketDotNet correctly?

Mav17
  • 51
  • 4
  • What protocol are you using? Is it TCP or something else? – jdweng May 25 '22 at 14:03
  • I'm using UDP protocol – Mav17 May 25 '22 at 14:13
  • VLAN is VTP not UDP. See : https://en.wikipedia.org/wiki/VLAN_Trunking_Protocol?force_isolation=true – jdweng May 25 '22 at 14:31
  • Sorry, but I don't get it... Would you please provide an example on simple vlan tagged frame using this lib? – Mav17 May 25 '22 at 14:37
  • You are constructing a UDP package. But if you want to have VLAN information you should use VTP package type - I think this is what @jdweng wanted to clarify – Sascha May 25 '22 at 14:52
  • The ethernet packet type is not UDP. See : https://github.com/dotpcap/packetnet/issues/113?force_isolation=true – jdweng May 25 '22 at 14:58
  • I finally fixed it... Had to add the Type = EtherType.IpV4 within the Ieee8021QPacket – Mav17 May 25 '22 at 15:01

0 Answers0