Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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:
C#
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.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Dec-14 22:52pm    
This kind of bridging between two networks is quite possible, but why? Such things can be easily done by simple Windows administration. It's called "hosted network" and network sharing (not to be confused with file sharing; all network functionality is shared). Your code could be useful only if you do some specialized processing in between.
—SA
2097 28-Dec-14 22:31pm    
I want to do some processing on the packet filters.
Sergey Alexandrovich Kryukov 29-Dec-14 2:07am    
Understood...
—SA

1 solution

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 :-

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


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
 
Share this answer
 
Comments
2097 28-Dec-14 23:16pm    
Each package includes: source address, destination address, port and .....

I'm processing the received packets and each packet to the address and port, and I would send the package to the address and port.

The problem with this is that the source address changes.
2097 28-Dec-14 23:23pm    
If the operating system can do it, so can be said for the work program

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900