Beispiel #1
0
        static void SendTextConsole(SockMgr sockMgr)
        {
            Console.WriteLine("Enter message to send");
            Console.Write("> ");
            string msg = Console.ReadLine();

            sockMgr.SendText(msg);
        }
Beispiel #2
0
 public Responser(SockController sockController, ProtocolStack protocolStack, SockMgr sockMgr)
 {
     _sockController = sockController;
     if (sockMgr.GetSockBase().Role == SocketRole.Client)  // listener does not need to send or receive
     {
         LinkProtocolStackEvents(protocolStack);
     }
     _sockMgr = sockMgr;
 }
Beispiel #3
0
        // <https://gist.github.com/louis-e/888d5031190408775ad130dde353e0fd>
        public SockMgr GetUdpListener()
        {
            Socket listener = new Socket(_options.ListenerIpAddress.AddressFamily,
                                         SocketType.Dgram, ProtocolType.Udp);

            listener.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            listener.Bind(new IPEndPoint(_options.ListenerIpAddress, _options.ListenerPort));

            SockBase sockBase = new SockBase(listener, SocketRole.Listener, true);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            return(sockMgr);
        }
Beispiel #4
0
        public SockMgr GetUdpClient()
        {
            Socket sock = new Socket(_options.ListenerIpAddress.AddressFamily,
                                     SocketType.Dgram, ProtocolType.Udp);

            SockBase sockBase = new SockBase(sock, SocketRole.Client, false);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            // TODO: use BeginConnect instead
            sock.Connect(new IPEndPoint(_options.ListenerIpAddress, _options.ListenerPort));

            return(sockMgr);
        }
Beispiel #5
0
        // add sockMgr to sockList
        public void AddSockMgr(SockMgr sockMgr, SocketRole socketRole)
        {
            switch (socketRole)
            {
            case SocketRole.Client:
                _sockList.Clients.Add(sockMgr);
                break;

            case SocketRole.Listener:
                _sockList.Listeners.Add(sockMgr);
                break;
            }
        }
Beispiel #6
0
 public void RemoveSockMgr(SockMgr sockMgr)
 {
     _shutdownLock.WaitOne();
     if (sockMgr.GetSockBase().Role == SocketRole.Listener)
     {
         _sockList.Listeners.Remove(sockMgr);
     }
     else
     {
         _sockList.Clients.Remove(sockMgr);
     }
     _shutdownLock.ReleaseMutex();
 }
Beispiel #7
0
        public void BuildTcpClient(SockMgr.SockMgrConnectEventHandler connectCallback, object callbackState = null)
        {
            Socket sock = new Socket(_options.ListenerIpAddress.AddressFamily,
                                     SocketType.Stream, ProtocolType.Tcp);

            if (_options.ClientPort >= 0)
            {
                sock.Bind(new IPEndPoint(IPAddress.Any, _options.ClientPort));
            }

            SockBase sockBase = new SockBase(sock, SocketRole.Client, false);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            sockMgr.SockMgrConnectEvent += OnSocketConnect;
            sockMgr.StartConnect(new IPEndPoint(_options.ListenerIpAddress, _options.ListenerPort), _options.TimesToTry, connectCallback, callbackState);
        }
Beispiel #8
0
        static void ListenerMenu(SockMgr sockMgr)
        {
            Console.WriteLine("[Interface Menu]");
            Console.WriteLine("1. Close");

            Console.Write("> ");
            string sel = Console.ReadLine();

            switch (sel)
            {
            case "1":
                sockMgr.Shutdown();
                break;

            default:
                break;
            }
        }
Beispiel #9
0
        public SockMgr GetTcpListener()
        {
            IPAddress  ipAddress     = IPAddress.Parse("0.0.0.0");
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, _options.ListenerPort);

            // Create a TCP/IP socket.
            Socket listener = new Socket(ipAddress.AddressFamily,
                                         SocketType.Stream, ProtocolType.Tcp);

            // makes restarting a socket become possible
            // https://blog.csdn.net/limlimlim/article/details/23424855
            listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            SockBase sockBase = new SockBase(listener, SocketRole.Listener, true);
            SockMgr  sockMgr  = new SockMgr(sockBase, _sockController, (IProtocolFactory)_options.ProtocolFactory.Clone());

            listener.Bind(localEndPoint);
            listener.Listen(4);

            return(sockMgr);
        }
Beispiel #10
0
        static void SendSmallFileConsole(SockMgr sockMgr)
        {
            Console.WriteLine("Enter filepath to send");
            Console.Write("> ");
            string filepath = Console.ReadLine();

            Protocol.SmallFileDataObject dataObject = new Protocol.SmallFileDataObject();
            dataObject.Filename = Path.GetFileName(filepath);
            Console.WriteLine("Reading File");
            try
            {
                dataObject.BinData = File.ReadAllBytes(filepath);
            }
            catch (Exception)
            {
                Console.WriteLine("[Error] File Not Found");
                return;
            }
            Console.WriteLine("Sending File");
            sockMgr.SendSmallFile(dataObject, SendSmallFileCallback, filepath);
        }
Beispiel #11
0
        // return listener if possible; set callback to null if no callback is needed
        public SockMgr BeginBuildTcp(SockFactoryOptions options, SocketRole socketRole, SockMgr.SockMgrConnectEventHandler connectCallback = null, SockMgr.SockMgrAcceptEventHandler acceptCallback = null, object callbackState = null)
        {
            SockMgr listenerMgr = null;

            _sockFactory.SetOptions(options);
            switch (socketRole)
            {
            case SocketRole.Client:
                if (options.TimesToTry > 0)
                {
                    _sockFactory.BuildTcpClient(connectCallback, callbackState);
                }
                break;

            case SocketRole.Listener:
                listenerMgr = _sockFactory.GetTcpListener();
                _sockFactory.ServerAccept(listenerMgr, acceptCallback, callbackState);
                AddSockMgr(listenerMgr, SocketRole.Listener);
                break;
            }
            return(listenerMgr);
        }
Beispiel #12
0
 // start accepting
 public void ServerAccept(SockMgr listener, SockMgr.SockMgrAcceptEventHandler acceptCallback = null, object callbackState = null)
 {
     listener.SockMgrAcceptEvent += OnSocketAccept;
     listener.StartAccept(acceptCallback, callbackState);
 }
Beispiel #13
0
        static void InterfaceAesConsole(SockMgr sockMgr)
        {
            byte[] key;
            string input;

            Protocol.AESProtocolState   state;
            List <Protocol.AESProtocol> aesProtocols = new List <Protocol.AESProtocol>();
            int i = 0;

            foreach (var proto in sockMgr.GetProtocolStack().GetState().MiddleProtocols)
            {
                if (proto.GetType() == typeof(Protocol.AESProtocol))
                {
                    aesProtocols.Add((Protocol.AESProtocol)proto);
                    Console.WriteLine($"{i}\t{proto.ToString()}");
                    ++i;
                }
            }
            if (aesProtocols.Count == 0)
            {
                return;
            }

            Console.WriteLine("[Interface-AES] Select one by index");
            Console.Write("> ");
            input = Console.ReadLine();
            int index;

            try
            {
                index = int.Parse(input);
            }
            catch (FormatException)
            {
                Console.WriteLine("[Error] Invalid Input");
                return;
            }

            try
            {
                state = aesProtocols[index].GetState();
            }
            catch (ArgumentOutOfRangeException)
            {
                Console.WriteLine("[Error] Index Out Of Range");
                return;
            }

            Console.WriteLine("[Interface-AES]");
            Console.WriteLine("1. Set Key");
            Console.WriteLine("2. Disable Key");
            Console.WriteLine("3. Exit");
            Console.Write("> ");
            input = Console.ReadLine();
            switch (input)
            {
            case "1":
                key = SetKeyConsole();
                if (key != null)
                {
                    state.Key     = key;
                    state.Enabled = true;
                    aesProtocols[index].SetState(state);
                }
                break;

            case "2":
                state.Enabled = false;
                aesProtocols[index].SetState(state);
                break;

            case "3":
                return;
            }
        }
Beispiel #14
0
        static void InterfaceMenu(SockMgr sockMgr)
        {
            bool isExit = false;

            while (!isExit && !sockMgr.IsShutdown)
            {
                Console.WriteLine(string.Format("[Interface Menu] {0} -> {1}",
                                                sockMgr.GetSockBase().GetSocket().LocalEndPoint.ToString(),
                                                sockMgr.GetSockBase().GetSocket().RemoteEndPoint.ToString()));
                Console.WriteLine("1. Send Text");
                Console.WriteLine("2. Close");
                Console.WriteLine("3. Is Host?");
                Console.WriteLine("4. Exit");
                Console.WriteLine("5. Config AES");
                Console.WriteLine("6. Send Small File");
                foreach (var proto in sockMgr.GetProtocolStack().GetState().MiddleProtocols)
                {
                    if (proto.GetType() == typeof(Protocol.AESProtocol) && ((Protocol.AESProtocol)proto).GetState().Enabled == false)
                    {
                        Console.WriteLine("[Warning] There exists an AES layer that is not enabled");
                    }
                }

                Console.Write("> ");
                string sel = Console.ReadLine();
                if (sockMgr.IsShutdown)
                {
                    break;
                }
                try
                {
                    switch (sel)
                    {
                    case "1":
                        SendTextConsole(sockMgr);
                        break;

                    case "2":
                        sockMgr.Shutdown();
                        break;

                    case "3":
                        Console.WriteLine(sockMgr.GetSockBase().IsHost.ToString());
                        break;

                    case "4":
                        isExit = true;
                        break;

                    case "5":
                        InterfaceAesConsole(sockMgr);
                        break;

                    case "6":
                        SendSmallFileConsole(sockMgr);
                        break;

                    default:
                        break;
                    }
                }
                catch (NullReferenceException) { }  // in case the remote has shutdown
            }
        }
Beispiel #15
0
 // adapt events from new SockMgr
 private void RegisterNewSockMgr(SockMgr sockMgr)
 {
     sockMgr.SockMgrReceiveEvent       += OnSockMgrReceive;
     sockMgr.SockMgrShutdownBeginEvent += OnSockMgrShutdownBegin;
 }