This project is read-only.

How can I use HttpDatagram class??

Sep 15, 2013 at 5:03 AM
I want to use C # to collect network packets.
Now I have to receive IP and TCP packet content.
But how can I use HttpDatagram class to get http content, such as the domain name (www.xxx.com), Response (200 OK) ...

The code is as follows :

using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Http;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace PackageGet
{
public partial class Form1 : Form
{
    private IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; 
    private int chosen_device_send_Index = 0; 

    private PacketDevice l_selectedDevice;
    private PacketCommunicator l_communicator;

    private BerkeleyPacketFilter filter;
    private Thread listen_thread;

    private TcpDatagram tcp;
    private String port;
    private Packet packet;

    public Form1()
    {
        InitializeComponent(); 
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (allDevices.Count == 0)
        {
            MessageBox.Show("No description available!!");
        }
        for (int i = 0; i != allDevices.Count; ++i)
        {
            LivePacketDevice device = allDevices[i];
            String device_name = (i + 1) + ". " + device.Name;
            if (device.Description != null)
            {
                device_name += "(" + device.Description + ")";
            }
            else
            {
                device_name += " (No description available)";
            }
            this.comboBox1.Items.Add(device_name);
        }
    }

    private void comboBox1_TextChanged(object sender, EventArgs e)
    {
        chosen_device_send_Index = this.comboBox1.SelectedIndex;
        l_selectedDevice = allDevices[chosen_device_send_Index];

        l_communicator = l_selectedDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous,1000);
        //check
        if (l_communicator.DataLink.Kind != DataLinkKind.Ethernet)
        {
            textBox1.Text = "only support Ethernet\r\n";
        }
        //compile filter
        filter = l_communicator.CreateFilter("ip and tcp");
        //setting filter
        l_communicator.SetFilter(filter);
        //start capture
        listen_thread = new Thread(new ThreadStart(() =>
        {
            l_communicator.ReceivePackets(0, PacketHandler);
        }));
        listen_thread.IsBackground = true;
        listen_thread.Start();

        textBox1.Text += "Listening on " + l_selectedDevice.Description + "...\r\n";
    }

    private void PacketHandler(Packet local_packet) {
        try {
            //get Destination ip
            IpV4Datagram ip = local_packet.Ethernet.IpV4;
            String ipaddress= ip.Destination.ToString();

            if (ip.Protocol == IpV4Protocol.Tcp) {
                tcp = ip.Tcp;
                port = tcp.DestinationPort.ToString();
                textBox1.Text += ipaddress + " : " + port + "\r\n";
            }

            /*
             __get http payload ??__
             */
        }
        catch (Exception e)
        {
            textBox1.Text = e.ToString();
        }
    }
}
}

I really do not understand how
Thanks to help
Thanks~~~
Coordinator
Sep 20, 2013 at 6:39 AM
Try using the Http property of TcpDatagram.
packet.Ethernet.IpV4.Tcp.Http.