473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating A Packet Filter.

19 New Member
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 5823
alex21
19 New Member
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

ClientConnectio n.BeginSend(Rec ievedFromServer , 0, RecievedFromSer ver.Length, SocketFlags.Non e, ClientConnectio nSend, ClientConnectio n);

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
412
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 protocolType for udp: Udp for tcp: Tcp
1
1025
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 as CreateDate RecieveDate and Message. I created this classs in a new project called MAMI, built this in Release mode to give me a MAMI.dll. I have added a reference to this dll in my WebService code on the central server, and I have a...
2
6161
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 would like to put in the header of a packet. 2. I know the size of data I will send and the header size in bytes, so I allocate the space in memory for both and create two pointers: for data part (dataptr) and header part. Header pointer coinsided...
3
3659
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 destination. My script compiles fine and runs fine until it finds the specific packet and tries to alter the payload of the data. Hopefully someone can give me some insight on why it might be doing this... #!/usr/bin/perl -w # # Custom script:...
1
3396
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 src ip, dst ip, src port, dst port. Should I run this program as a Daemon? If so, how do I do that? I would appreciate your response.
2
9678
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 filter function here .. http://oss.coresecurity.com/impacket/sniff.py I want to filter all SYN packets into a dump file, can anyone tell me how I need to configure the filter string to do this? Thanks
1
2553
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 itself?? Basically i want to know how to send a packet via socket connection?
0
3700
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 variable and display it. code is as follows: import sys import string from threading import Thread
1
2805
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
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.