This project is read-only.

Reading Text File into sendBuffer

Jun 2, 2010 at 3:13 AM

I am trying to read a text file in and apply it to the sendBuffer method. The Send Buffer example uses a captured file from the inputCommunicator and the ReceivePacket method so it is abstracted out so I don't really understand what I need to do to make this work.

My input text file has the following format:

0000; -- MAC_DST[47:32]

0013; -- MAC_DST[31:16]

1200; -- MAC_DST[15:0]

and so on until the end of the payload field.

What would be the proper code to apply this text file to the sendBuffer method?

Jun 2, 2010 at 5:08 PM
Edited Jun 2, 2010 at 5:10 PM

The PacketSendBuffer should be Enqueued with Packets.

Packets can be built in one of three ways:

  1. Received from a device (like in the example).
  2. Using PacketBuilder and layers. Here you need to know all the fields you want the different layers of the packet to hold (like the source and destination MAC addresses, IP addresses and so on). You can extract layers from existing packets.
  3. From an array of bytes ( byte[] ). You should build a byte[] in the length of the packet you want to build and set its bytes to the byte values you want.

It seems that method 3 will be the most suitable for you. You simply need to convert the HEX strings to bytes and build a byte[] that will contain all the packet bytes.


You can also use Packet.FromHexadecimalString() static method which will parse a hexadecimal string and give you a packet from the bytes the hexadecimal string represents.


I hope this answers your question.




Jun 3, 2010 at 4:29 PM

Thanks for your response on my previous question.

I have written the following code which reads the text file into a string[] and then converts it to byte[]. it maybe a little rough but it works for now.

My problem is I don't know what to enter for the IDataLink variable in the declaration line for Packet, see BOLD/Italics text below.

            // Open the output device
            using (PacketCommunicator outputCommunicator = selectedDevice.Open(100, // name of the device
                                                                         PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                                                         1000)) // read timeout
                // Allocate a send buffer based on the number of lines in the listBox
                using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)(lbCtlGPPInputFile.Items.Count)*2))
                    // assign the name of the tbCtlInputFile to a variable
                    string s_inputfilename = tbCtlInputFile.Text.ToString();

                    // Read in each line of the file into a string array
                    string[] s_file = File.ReadAllLines(s_inputfilename);

                    // dynamically create a byte array based on the number of array elements
                    // file contains two bytes per line
                    byte[] b_file = new byte[(s_file.Length)*2];

                    for (int i = 0; i < s_file.Length; i++)
                        // Check for packet seperator or end of file
                        if (s_file[i] != "********")
                            // file contains two bytes per line
                            b_file[2 * i] = Convert.ToByte(s_file[i].Substring(0, 2), 16);
                            b_file[2 * i + 1] = Convert.ToByte(s_file[i].Substring(2, 2), 16);

                    // Packet eth_packet = new Packet(byte[] data, DateTime timestamp, IDataLink dataLink);
                    Packet eth_packet = new Packet(b_file, DateTime.Now, ????);


                    outputCommunicator.Transmit(sendBuffer, false);

What are the IDataLink valid types? I am trying to create a MAC/IP/UDP/Payload formatted Ethernet packet.

Intellisense is only so helpful when you don't know what you are looking for. Is there any other form of documentation other than the Intellisense info?



Jun 3, 2010 at 5:34 PM

Since your packet is an Ethernet packet, you need to use the Ethernet data link.

There are multiple ways to do that:

// Straight Forward
Packet p = new Packet(bytes, timestamp, new DataLink(DataLinkKind.Ethernet);

// Use static property (simpler, more efficient). Recommended.
Packet p = new Packet(bytes, timestamp, DataLink.Ethernet);

// Use the enum directly (this is an overload of the Packet constructor)
Packet p = new Packet(bytes, timestamp, DataLinkKind.Ethernet);

Remember you can also use Packet.FromHexadecimalString() static method to create a hexadecimal string to a Packet.