从一个网卡获取所有网络数据包并将其发送到第二个网卡? [英] Get all network packets from one network card and send it to the second network card?

查看:79
本文介绍了从一个网卡获取所有网络数据包并将其发送到第二个网卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的系统上有两张网卡。

我想写C#中的一个程序。

第一个网卡接收数据包并将它们发送到第二个网卡。

我写了一个接收第一个数据包网卡的程序。我写了一个程序代码:

Hi
I have two network cards on your system.
I want to write a program in C#.
The first network card receive the packets and sends them to a second network card.
I have written a program that receives the first packet network card.I have written a program code:

private Socket mainSocket;
        private byte[] byteData = new byte[4096];
        private bool bContinueCapturing = false;
 
        private delegate void AddTreeNode(TreeNode node);
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string strIP = null;
 
            IPHostEntry HosyEntry = Dns.GetHostEntry((Dns.GetHostName()));
            if (HosyEntry.AddressList.Length > 0)
            {
                foreach (IPAddress ip in HosyEntry.AddressList)
                {
                    strIP = ip.ToString();
                    cmbInterfaces.Items.Add(strIP);
                }
            }
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bContinueCapturing)
            {
                mainSocket.Close();
            }
        }
 
        private void btnStart_Click(object sender, EventArgs e)
        {
             
            if (cmbInterfaces.Text == "")
            {
                MessageBox.Show("Select an Interface to capture the packets.", "sniffer",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (!bContinueCapturing)
                {
 
                    btnStart.Text = "&Stop";
 
                    bContinueCapturing = true;
 
                    mainSocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Raw, ProtocolType.IP);
 
                    mainSocket.Bind(new IPEndPoint(IPAddress.Parse(cmbInterfaces.Text), 0));
 
                    mainSocket.SetSocketOption(SocketOptionLevel.IP,
                                               SocketOptionName.HeaderIncluded,
                                               true);
 
                    byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
                    byte[] byOut = new byte[4] { 1, 0, 0, 0 };
 
                    mainSocket.IOControl(IOControlCode.ReceiveAll,
                                         byTrue,
                                         byOut);
                     
                    mainSocket.Receive(byteData);
                     
                    mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }
                else
                {
                    btnStart.Text = "&Start";
                    bContinueCapturing = false;
                    mainSocket.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "sniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void OnReceive(IAsyncResult ar)
        {
             
            try
            {
                 
                int nReceived = mainSocket.EndReceive(ar);
 
                if (bContinueCapturing)
                {
                    byteData = new byte[4096];
                    mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                        new AsyncCallback(OnReceive), null);
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "sniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
    }



我想编写一个程序来发送收到第二张网卡的数据包。

请帮帮我。


I want to write a program that sends packets received a second network card.
Please help me.

推荐答案

根据SA的评论,你真的必须有充分的理由手动尝试这样的事情



我可能错了,但是,它只有在你有两个不同的网络范围时才能工作,每个网络绑定一个地址到每个网卡 - 监听网卡很容易



- 所以假设你有10.0.0.250绑定到NIC 1,你将收听来自端口的传入流量



NIC 2例如可能在网络192.168.2.x中,假设您的机器是192.168.2.49 - 所以,如何将发送路由到192.168.2.100端口12345是强制使用正确的本地端点: -



As per SA's comments, you'd really have to have a good reason to attempt something like this manually

I may be wrong, but, it can only work if you have two different 'network ranges', with one address from each network bound to each NIC - the listen NIC is easy

- so lets say you have 10.0.0.250 bound to NIC 1, which you will listen to incoming traffic from on a port

NIC 2 for example may be in network 192.168.2.x, lets say your machine is 192.168.2.49 - so, how you route the send to 192.168.2.100 port 12345 is by forcing use of the correct local endpoint :-

var localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.49"), port: 0);
var tcpSendClient = new TcpClient(localEndPoint);
tcpSendClient.Connect("192.168.2.100", 12345);





在端点上使用'端口0'表示192.168.2.49表示输出/出站端口是动态的 - 通过指定端点192.168.2.49绕过自动路由



by using 'port 0' in the Endpoint for the 192.168.2.49 means the output/outbound port is dynamic - by specifying the endpoint 192.168.2.49 you circumvent automatic routing


这篇关于从一个网卡获取所有网络数据包并将其发送到第二个网卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆