473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating A Packet Filter.

19
Ok i have never before tried networking in my programming, so this is the first time i have tried anything like this. But i am trying to make an application which acts as a medium between a server and client, originally the client would communicate directly to the server, but in this case i am trying to filter the packets and remove the ones i don't want. So i created a packet filtering class, which accepts a connection from the client, and connects to the server, i then tried using two separate threads to send the packets received from the server to the client, and receive the packets from the client to send to the server.

So effectively(i have not tried to filter packets yet) the client and server should be communicating exactly the same, except through my application, however this is not the case, and with my lack of experience in sockets i cannot figure out why.

Some good things, the client connects to my application when i click login, however it then freezes waiting for the return packet from the server which i must never get.

Here is the class:
Expand|Select|Wrap|Line Numbers
  1. public static class PacketFilter
  2.     {
  3.         static int BufferSize = 4096;
  4.  
  5.         static Server RedirectToServer;
  6.  
  7.         static bool ConnectingToClient = false;
  8.         static Socket ClientConnection;
  9.         static byte[] RecievedFromClient = new byte[BufferSize];
  10.  
  11.         static bool ConnectingToServer = false;
  12.         static Socket ServerConnection;
  13.         static byte[] RecievedFromServer = new byte[BufferSize];
  14.  
  15.         static Thread ClientConnectionThread = new Thread(new ThreadStart(ClientConnectionThread_Tick));
  16.         static Thread ServerConnectionThread = new Thread(new ThreadStart(ServerConnectionThread_Tick));
  17.  
  18.         public static void Start(Server server)
  19.         {
  20.             RedirectToServer = server;
  21.  
  22.             ClientConnectionThread.Start();
  23.             ServerConnectionThread.Start();
  24.  
  25.             ClientConnect(true);
  26.             ServerConnect(true);
  27.         }
  28.  
  29.         public static void End()
  30.         {
  31.             try
  32.             {
  33.                 ClientConnection.Close();
  34.                 ServerConnection.Close();
  35.             }
  36.             catch (Exception e)
  37.             {
  38.                 MessageBox.Show("Failed to close sockets, " + e.Message);
  39.             }
  40.         }
  41.  
  42.         public static void ClientConnect(bool connect)
  43.         {
  44.             ConnectingToClient = true;
  45.  
  46.             ClientConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  47.             IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("localhost").AddressList[0], (int)Main.UserInterfaceSettings.ClientPacketListenPort);
  48.             try
  49.             {
  50.                 ClientConnection.Bind(ep);
  51.                 ClientConnection.Listen(5);
  52.                 ClientConnection.BeginAccept(ClientConnect_Complete, ClientConnection);
  53.             }
  54.             catch (Exception) { ConnectingToClient = false; }
  55.         }
  56.  
  57.         public static void ClientConnect_Complete(IAsyncResult sock)
  58.         {
  59.             try
  60.             {
  61.                 ClientConnection = ClientConnection.EndAccept(sock);
  62.             }
  63.             catch (Exception) { }
  64.             ConnectingToClient = false;
  65.         }
  66.  
  67.         public static void ServerConnect(bool connect)
  68.         {
  69.             ConnectingToServer = true;
  70.  
  71.             ServerConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  72.             IPEndPoint ep = new IPEndPoint(RedirectToServer.ReturnServerAddress(), (int)RedirectToServer.ReturnPort());
  73.             try
  74.             {
  75.                 ServerConnection.BeginConnect(ep, ServerConnect_Complete, ServerConnection);
  76.             }
  77.             catch (Exception) { ConnectingToServer = false; }
  78.         }
  79.  
  80.         public static void ServerConnect_Complete(IAsyncResult sock)
  81.         {
  82.             try { ServerConnection.EndConnect(sock); }
  83.             catch (Exception) {  }
  84.             ConnectingToServer = false;
  85.         }
  86.  
  87.         public static void ClientConnectionThread_Tick()
  88.         {
  89.             while (!Program.EndProgram)
  90.             {
  91.                 if (ClientConnection != null)
  92.                 {
  93.                     if (ClientConnection.Connected)
  94.                     {
  95.                         try
  96.                         {
  97.                             ClientConnection.BeginSend(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ClientConnectionSend, ClientConnection);
  98.                             ClientConnection.BeginReceive(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ClientConnectionRecieve, ClientConnection);
  99.                         }
  100.                         catch (Exception) { }
  101.                     }
  102.                     else
  103.                         if (!ConnectingToClient)
  104.                             ClientConnect(true);
  105.                 }
  106.                 else
  107.                     if (!ConnectingToClient)
  108.                         ClientConnect(true);
  109.  
  110.                 Thread.Sleep(1);
  111.             }
  112.         }
  113.  
  114.         public static void ClientConnectionRecieve(IAsyncResult sock)
  115.         {
  116.             try
  117.             {
  118.                 ClientConnection.EndReceive(sock);
  119.             }
  120.             catch (Exception) { }
  121.         }
  122.  
  123.         public static void ClientConnectionSend(IAsyncResult sock)
  124.         {
  125.             try
  126.             {
  127.                 ClientConnection.EndSend(sock);
  128.                 RecievedFromServer = new byte[BufferSize];
  129.             }
  130.             catch (Exception) { }
  131.         }
  132.  
  133.         public static void ServerConnectionThread_Tick()
  134.         {
  135.             while (!Program.EndProgram)
  136.             {
  137.                 if (ServerConnection != null)
  138.                 {
  139.                     if (ServerConnection.Connected)
  140.                     {
  141.                         try
  142.                         {
  143.                             ServerConnection.BeginSend(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ServerConnectionSend, ServerConnection);
  144.                             ServerConnection.BeginReceive(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ServerConnectionRecieve, ServerConnection);
  145.                         }
  146.                         catch (Exception) { }
  147.                     }
  148.                     else
  149.                         if (!ConnectingToServer)
  150.                             ServerConnect(true);
  151.                 }
  152.                 else
  153.                     if (!ConnectingToServer)
  154.                         ServerConnect(true);
  155.  
  156.                 Thread.Sleep(10);
  157.             }
  158.         }
  159.  
  160.         public static void ServerConnectionRecieve(IAsyncResult sock)
  161.         {
  162.             try
  163.             {
  164.                 ServerConnection.EndReceive(sock);
  165.             }
  166.             catch (Exception) { }
  167.         }
  168.  
  169.         public static void ServerConnectionSend(IAsyncResult sock)
  170.         {
  171.             try
  172.             {
  173.                 ServerConnection.EndSend(sock);
  174.                 MessageBox.Show(RecievedFromClient.ToString());
  175.                 RecievedFromClient = new byte[BufferSize];
  176.             }
  177.             catch (Exception) { }
  178.         }
  179.     }
  180.  
Thank you very much for any help.
Jul 8 '09 #1
1 5779
alex21
19
I made some changes and now it does a tiny bit of what it is supposed to do.

Expand|Select|Wrap|Line Numbers
  1. public static class PacketFilter
  2.     {
  3.         static int BufferSize = 4096;
  4.  
  5.         static Server RedirectToServer;
  6.  
  7.         static bool ConnectingToClient = false;
  8.         static Socket ClientConnection;
  9.         static byte[] RecievedFromClient = new byte[BufferSize];
  10.  
  11.         static bool ConnectingToServer = false;
  12.         static Socket ServerConnection;
  13.         static byte[] RecievedFromServer = new byte[BufferSize];
  14.  
  15.         static int ThreadSyncA = 0;
  16.         static int ThreadSyncB = 0;
  17.         static Thread ClientConnectionThread = new Thread(new ThreadStart(ClientConnectionThread_Tick));
  18.         static Thread ServerConnectionThread = new Thread(new ThreadStart(ServerConnectionThread_Tick));
  19.  
  20.         public static void Start(Server server)
  21.         {
  22.             RedirectToServer = server;
  23.  
  24.             ClientConnectionThread.Start();
  25.             ServerConnectionThread.Start();
  26.  
  27.             ClientConnect(true);
  28.             ServerConnect(true);
  29.         }
  30.  
  31.         public static void End()
  32.         {
  33.             try
  34.             {
  35.                 ClientConnection.Close();
  36.                 ServerConnection.Close();
  37.             }
  38.             catch (Exception e)
  39.             {
  40.                 MessageBox.Show("Failed to close sockets, " + e.Message);
  41.             }
  42.         }
  43.  
  44.         public static void ClientConnect(bool connect)
  45.         {
  46.             ConnectingToClient = true;
  47.  
  48.             ClientConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  49.             IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("localhost").AddressList[0], (int)Main.UserInterfaceSettings.ClientPacketListenPort);
  50.             try
  51.             {
  52.                 ClientConnection.Bind(ep);
  53.                 ClientConnection.Listen(5);
  54.                 ClientConnection.BeginAccept(ClientConnect_Complete, ClientConnection);
  55.             }
  56.             catch (Exception) { ConnectingToClient = false; }
  57.         }
  58.  
  59.         public static void ClientConnect_Complete(IAsyncResult sock)
  60.         {
  61.             try
  62.             {
  63.                 ClientConnection = ClientConnection.EndAccept(sock);
  64.             }
  65.             catch (Exception) { }
  66.             ConnectingToClient = false;
  67.         }
  68.  
  69.         public static void ServerConnect(bool connect)
  70.         {
  71.             ConnectingToServer = true;
  72.  
  73.             ServerConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  74.             IPEndPoint ep = new IPEndPoint(RedirectToServer.ReturnServerAddress(), (int)RedirectToServer.ReturnPort());
  75.             try
  76.             {
  77.                 ServerConnection.BeginConnect(ep, ServerConnect_Complete, ServerConnection);
  78.             }
  79.             catch (Exception) { ConnectingToServer = false; }
  80.         }
  81.  
  82.         public static void ServerConnect_Complete(IAsyncResult sock)
  83.         {
  84.             try { ServerConnection.EndConnect(sock); }
  85.             catch (Exception) {  }
  86.             ConnectingToServer = false;
  87.         }
  88.  
  89.         public static void ClientConnectionThread_Tick()
  90.         {
  91.             while (!Program.EndProgram)
  92.             {
  93.                 while (ThreadSyncA > ThreadSyncB)
  94.                     Thread.Sleep(1);
  95.                 RecievedFromClient = new byte[BufferSize];
  96.  
  97.                 if (ClientConnection != null)
  98.                 {
  99.                     if (ClientConnection.Connected)
  100.                     {
  101.                         try
  102.                         {
  103.                             ClientConnection.BeginSend(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ClientConnectionSend, ClientConnection);
  104.                             //ClientConnection.BeginReceive(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ClientConnectionRecieve, ClientConnection);
  105.  
  106.                             //ClientConnection.Send(RecievedFromServer);
  107.                             int test = 0;
  108.                             test = ClientConnection.Receive(RecievedFromClient);
  109.                             if (test != 0)
  110.                                 MessageBox.Show(test.ToString() + " client");
  111.                         }
  112.                         catch (Exception) { }
  113.                     }
  114.                     else
  115.                         if (!ConnectingToClient)
  116.                             ClientConnect(true);
  117.                 }
  118.                 else
  119.                     if (!ConnectingToClient)
  120.                         ClientConnect(true);
  121.  
  122.                 ThreadSyncA += 1;
  123.                 Thread.Sleep(10);
  124.             }
  125.         }
  126.  
  127.         public static void ClientConnectionRecieve(IAsyncResult sock)
  128.         {
  129.             try
  130.             {
  131.                 ClientConnection.EndReceive(sock);
  132.             }
  133.             catch (Exception) { }
  134.         }
  135.  
  136.         public static void ClientConnectionSend(IAsyncResult sock)
  137.         {
  138.             try { ClientConnection.EndSend(sock); }
  139.             catch (Exception) { }
  140.         }
  141.  
  142.         public static void ServerConnectionThread_Tick()
  143.         {
  144.             while (!Program.EndProgram)
  145.             {
  146.                 while (ThreadSyncB > ThreadSyncA)
  147.                     Thread.Sleep(1);
  148.                 RecievedFromServer = new byte[BufferSize];
  149.  
  150.                 if (ServerConnection != null)
  151.                 {
  152.                     if (ServerConnection.Connected)
  153.                     {
  154.                         try
  155.                         {
  156.                             ServerConnection.BeginSend(RecievedFromClient, 0, RecievedFromClient.Length, SocketFlags.None, ServerConnectionSend, ServerConnection);
  157.                             //ServerConnection.BeginReceive(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ServerConnectionRecieve, ServerConnection);
  158.  
  159.                             //ServerConnection.Send(RecievedFromClient);
  160.                             int test = 0;
  161.                             test = ServerConnection.Receive(RecievedFromServer);
  162.                             if (test != 0)
  163.                                 MessageBox.Show(test.ToString() + " server");
  164.                         }
  165.                         catch (Exception) { }
  166.                     }
  167.                     else
  168.                         if (!ConnectingToServer)
  169.                             ServerConnect(true);
  170.                 }
  171.                 else
  172.                     if (!ConnectingToServer)
  173.                         ServerConnect(true);
  174.  
  175.                 ThreadSyncB += 1;
  176.                 Thread.Sleep(10);
  177.             }
  178.         }
  179.  
  180.         public static void ServerConnectionRecieve(IAsyncResult sock)
  181.         {
  182.             try
  183.             {
  184.                 ServerConnection.EndReceive(sock);
  185.             }
  186.             catch (Exception) { }
  187.         }
  188.  
  189.         public static void ServerConnectionSend(IAsyncResult sock)
  190.         {
  191.             try { ServerConnection.EndSend(sock); }
  192.             catch (Exception) { }
  193.         }
  194.     }
Now when i click login on the client it pop's up with '83 client' then when i click ok it pops up with '46 server' then the break point i have put on this line

ClientConnection.BeginSend(RecievedFromServer, 0, RecievedFromServer.Length, SocketFlags.None, ClientConnectionSend, ClientConnection);

is never tripped again, in fact any break point in the Client thread is never tripped again, meaning after the reply from the server is received it freezes somewhere and the thread stops ticking.
Jul 8 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: AA | last post by:
If I want to write the code, so... is everything same in both case (udp, tcp) with the only difference of the .net Socket class constructor? socketType for udp: Dgram for tcp: Stream ...
1
by: Steve | last post by:
C# I am developing a basic app to run on a mobile device, to interact with our central server using WebServices. I have created a class called PacketInfo which has a couple of properties suchs...
2
by: bobrics | last post by:
Hi, I would like to create a packet for a RAW socket transfer. Please let me know if this is the right approach. 1. First, I am creating a header structure where I store all the information I...
3
by: nexus024 | last post by:
I am trying to write a program that will continuously sniff eth0 for a specific UDP packet thats being sent to a specific destination IP, alter the data of the packet, and finally transmit it to the...
1
by: sangith | last post by:
Hi, I tried the packet capture module program. I did a file transfer using ftp from this host to another server. But when I ran the program, it was just hanging off and it did not print the...
2
by: T00l | last post by:
I'm a newbie to python and for my first project i'm trying to create a packet filter. Having looked around i think pcapy will be the best way, there is a sniffer script available that incorporates a...
1
by: nitheshsalian | last post by:
I have created a Packet as a structure. i am not able to send the packet through the send() because what i have understood it sends only strings. Is there any way to send the packet as a structure...
0
by: neeru29 | last post by:
I'm using Pcapy and impacket module for packet sniffer. I'm able to capture the whole data in a variable and display it. I want extract the IP addresses , Port no's and Payload data into separate...
1
by: sudarshan1986 | last post by:
Dear Friends' I'm BE final year student and as my project i need to develop a firewall. Can you give me a basic packet filter in C# so that I may redesign as par my project. Thankyou in advance
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.