how to use Socket Permission in C# ?!

From: BiGgY (BiGgY_at_discussions.microsoft.com)
Date: 12/23/04

  • Next message: rox.scott: "GetProcessesByName() fails intermittantly with Access Denied"
    Date: Thu, 23 Dec 2004 09:17:07 -0800
    
    

    I want to create an application that can filter packet from incomming network
    traffic (like an ACL of Router) and I find out class to use this way seem
    that it should be SocketPermission Class. I have try to implement it. It can
    run but can't block an specific traffic to another application in my PC. I
    don't know did I understand something wrong about this class, is it can block
    packet !? (and how to implement it if I do it wrong). My source code look
    like below.

    /////////////////////////////////////////////////////
            SocketPermission sp = new SocketPermission(PermissionState.None);
            sp.AddPermission(NetworkAccess.Accept,TransportType.Tcp,"0.0.0.0",6667);
            sp.AddPermission(NetworkAccess.Connect,TransportType.Tcp,"0.0.0.0",6667); sp.PermitOnly();

            int recv;

            Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
    ProtocolType.Tcp);
                            
            TcpListener newsock = null;
            byte[] data = new byte[1024];

            Console.Write("Enter port number to use: ");
            string stringPort = Console.ReadLine();
            int port = Convert.ToInt32(stringPort);

            try
            {
                    newsock = new TcpListener(port);
                    newsock.Start();
            }
            catch(SecurityException)
            {
                    Console.WriteLine("Sorry, that port is unavailable");
                    return;
            }
            Console.WriteLine("Waiting for a Client...");

            TcpClient client = newsock.AcceptTcpClient();
            NetworkStream ns = client.GetStream();

            string welcome = "Welcome to my test server";
            data = Encoding.ASCII.GetBytes(welcome);
            ns.Write(data, 0, data.Length);

            while(true)
            {
                    data = new byte[1024];
                    recv = ns.Read(data, 0, data.Length);
                    if (recv == 0)
                            break;

                    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
                                    ns.Write(data, 0, recv);
            }
            ns.Close();
            client.Close();
            newsock.Stop();
    /////////////////////////////////////////////////////


  • Next message: rox.scott: "GetProcessesByName() fails intermittantly with Access Denied"