Example #1
0
 private Guid Add(Guid guid, Winsock client)
 {
     AttachEvents(client);
     _values.Add(guid, client);
     List.Add(guid);
     return(guid);
 }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WinsockCollection"/> with the specified parent.
 /// </summary>
 /// <param name="parent">The <see cref="Winsock"/> object that should act as the parent item.</param>
 public WinsockCollection(Winsock parent)
 {
     if (parent == null)
     {
         throw new ArgumentNullException("parent");
     }
     _parent = parent;
 }
Example #3
0
        internal void Start()
        {
            if (-1 == _port)
            {
                return;
            }
            try
            {
                IPAddress addr = IPAddress.Loopback;
                _listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
#if (DEBUG)      // Check an option that provides for listening on all the network interfaces, not only loopback (DEBUG builds only)
                if (!Core.SettingStore.ReadBool("RemoteControl", "LoopbackInterfaceOnly", true))
                {
                    addr = IPAddress.Any;
                }
#endif
                if (_port != 0)
                {
                    _listenSocket.Bind(new IPEndPoint(addr, _port));
                }
                else
                {
                    _storePort = true;
                    while (true)
                    {
                        _port = generateRandomPort();
                        try
                        {
                            _listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, _port));
                            break;
                        }
                        catch (SocketException ex)
                        {
                            if (ex.ErrorCode != 10048) // WSAEADDRINUSE
                            {
                                return;
                            }
                        }
                    }
                }
                _listenSocket.Listen((int)SocketOptionName.MaxConnections);

                _stop = Winsock.WSACreateEvent();
                Winsock.WSAResetEvent(_stop);

                _mainThread      = new Thread(new ThreadStart(Listener));
                _mainThread.Name = _threadName;
                _mainThread.Start();
                _mainThread.Priority = ThreadPriority.BelowNormal;
            }
            catch
            {
                Winsock.WSASetEvent(_stop);
                _port = -1;
                throw;
            }
            return;
        }
Example #4
0
 private Guid FindKey(Winsock client)
 {
     foreach (Guid key in _values.Keys)
     {
         if (ReferenceEquals(_values[key], client))
         {
             return(key);
         }
     }
     return(Guid.Empty);
 }
Example #5
0
        private void Client_Disconnected(object sender, EventArgs e)
        {
            // Close or Disconnected detected - check for AutoRemoval
            Winsock client = (Winsock)sender;
            Guid    guid   = FindKey(client);

            if (guid != Guid.Empty && (AutoRemove || _forceAutoRemove.Contains(guid)))
            {
                lock (SyncRootRemoveList)
                    _autoRemoveList.Enqueue(guid);
                (new Thread(ClientRemovalThread)).Start();
            }
        }
Example #6
0
 private void DetachEvents(Winsock client)
 {
     //client.StateChanged -= Client_StateChanged;
     client.StateChanged        -= Parent.OnStateChanged;
     client.ErrorReceived       -= Parent.OnErrorReceived;
     client.DataArrival         -= Parent.OnDataArrival;
     client.ReceiveProgress     -= Parent.OnReceiveProgress;
     client.SendProgress        -= Parent.OnSendProgress;
     client.Connected           -= Parent.OnConnected;
     client.Disconnected        -= Client_Disconnected;
     client.Disconnected        -= Parent.OnDisconnected;
     client.ValidateCertificate -= Parent.OnValidateCollectionCertificate;
 }
Example #7
0
        private void wskServer_Connected(object sender, Treorisoft.Net.ConnectedEventArgs e)
        {
            string  source = (sender == wskServer) ? "Parent" : "Client";
            Winsock wsk    = (sender as Winsock);

            if (ServerLog)
            {
                LogServer(string.Format("{0}: Connected ({1} on port {2})", source, e.RemoteIP, e.RemotePort));
            }
            if (ServerLog)
            {
                LogServer(string.Format("{0}: Connected ({1} on port {2})", "sender", wsk.RemoteHost, wsk.RemotePort));
            }
        }
Example #8
0
        private void ConfigFormEth_Load(object sender, EventArgs e)
        {
            //Done here to load settings
            cbIntercept.Enabled = false;

            int curIndex = 1;

            cbAPI.Items.Clear();
            //Detect which API's we have
            //Winsock
            winsockAdapters = Winsock.GetAdapters();
            apiIndex.Add(ConfigFile.EthAPI.Winsock, curIndex);
            cbAPI.Items.Add("Sockets (Winsock)");
            curIndex++;
            //Windows Only
            if (PSE.CLR_PSE_Utils.IsWindows())
            {
                //Tap
                tapAdapters = TAPAdapter.GetAdapters();
                if (tapAdapters != null)
                {
                    cbAPI.Items.Add("Tap");
                    apiIndex.Add(ConfigFile.EthAPI.Tap, curIndex);
                    curIndex++;
                }
            }

            //WinPcap
            winPcapAdapters = WinPcapAdapter.GetAdapters();
            if (winPcapAdapters != null)
            {
                cbAPI.Items.Add("WinPcap Bridged");
                apiIndex.Add(ConfigFile.EthAPI.WinPcapBridged, curIndex);
                curIndex++;
                cbAPI.Items.Add("WinPcap Switched (Promiscuous)");
                apiIndex.Add(ConfigFile.EthAPI.WinPcapSwitched, curIndex);
                curIndex++;
            }

            if (apiIndex.ContainsKey(DEV9Header.config.EthType))
            {
                cbAPI.SelectedIndex = apiIndex[DEV9Header.config.EthType] - 1;
            }
            else
            {
                cbAPI.SelectedIndex = (int)ConfigFile.EthAPI.Null;
            }
        }
Example #9
0
        public CommPortProxy(Winsock sck)
        {
            if (sck == null)
            {
                throw new NullReferenceException("CommPortProxy ( Winsock sck ), sck == null");
            }

            if ((StateConstants)sck.State != StateConstants.sckConnected)
            {
                throw new ArgumentException("sck state != connected");
            }

            _winsock              = sck;
            _winsock.DataArrival += new DMSWinsockControlEvents_DataArrivalEventHandler(_winsock_DataArrival);
            _winsock.Error       += new DMSWinsockControlEvents_ErrorEventHandler(_winsock_Error);

            DMSWinsockControlEvents_Event winsockEvent = (DMSWinsockControlEvents_Event)_winsock;

            winsockEvent.Close += new DMSWinsockControlEvents_CloseEventHandler(_winsock_Close);

            _createTime = DateTime.Now;

            m_Timer.Tick += new EventHandler(m_Timer_Tick);
        }
Example #10
0
        internal void Stop()
        {
            if (Winsock.WSAWaitForMultipleEvents(1, new IntPtr[] { _stop }, 1 /*true*/, 0, 0 /*false*/) == Winsock.WSA_WAIT_EVENT_0)
            {
                Trace.WriteLine("[RCS] Second call to Stop().");
                return;
            }
            Winsock.WSASetEvent(_stop);
            Trace.WriteLine("[RCS] Stop event has been fired.");

            try
            {
                Trace.WriteLine("[RCS] Waiting for server thread...");
                if (!Application.MessageLoop)
                {
                    _mainThread.Join();
                }
                else
                {
                    while (_mainThread.IsAlive && !_mainThread.Join(100))
                    {
                        Application.DoEvents();
                    }
                }
                Trace.WriteLine("[RCS] Server thread has finished.");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("[RCS] Server thread waiting exception: " + ex.Message);
            }
            finally
            {
                _mainThread = null;
            }
            Winsock.WSACloseEvent(_stop);
        }
Example #11
0
 private void SendKeyUp(Keys key, Winsock wsk)
 {
     string keyStr = "u:" + ((int)key).ToString();
     wsk.Send(keyStr);
 }
Example #12
0
 public SendKeyTask(Keys key, Winsock wsk, bool down)
 {
     _key = key;
     _wsk = wsk;
     _down = down;
 }
Example #13
0
        /// <summary>
        /// Removes the specified client from the collection.
        /// </summary>
        /// <param name="client">The client to remove from the collection.</param>
        public void Remove(Winsock client)
        {
            Guid guid = FindKey(client);

            Remove(guid);
        }
Example #14
0
 /// <summary>
 /// Adds a client to the collection.
 /// </summary>
 /// <param name="client">The client to add to the collection.</param>
 /// <returns>A <see cref="Guid"/> key to help identify this particular client.</returns>
 public Guid Add(Winsock client)
 {
     return(Add(Guid.NewGuid(), client));
 }
Example #15
0
        private void Listener()
        {
            IntPtr socketEvent = Winsock.WSACreateEvent();

            IntPtr[] allEvents = { _stop, socketEvent };
            IntPtr   handle    = _listenSocket.Handle;

            Winsock.WSAEventSelect(handle, socketEvent, Winsock.FD_ACCEPT);
            while (true)
            {
                try
                {
                    uint res = Winsock.WSAWaitForMultipleEvents(2, allEvents, 0 /*false*/, Winsock.WSA_INFINITE, 1 /*true*/);
                    if (res == Winsock.WSA_WAIT_EVENT_0)
                    {
                        Trace.WriteLine("[RCS] Event 0 (STOP) signalled");
                        break;
                    }
                    else if (res == Winsock.WSA_WAIT_EVENT_0 + 1)
                    {
                        Winsock.WSAResetEvent(socketEvent);
                        Trace.WriteLine("[RCS] Event 1 (ACCEPT) signalled");

                        Socket cs = _listenSocket.Accept();
                        Winsock.WSAEventSelect(cs.Handle, socketEvent, 0);
                        cs.Blocking = true;

                        Trace.WriteLine("[RCS] Client accepted at socket " + cs.Handle.ToString());
                        ThreadPool.QueueUserWorkItem(_onAccept, cs);
                    }
                    else
                    {
                        Trace.WriteLine("[RCS] Event " + res.ToString() + " (?) signalled");
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("[RCS] Accept exception: " + ex.Message);
                    // If not sopped: sleep and try again
                    if (Winsock.WSAWaitForMultipleEvents(1, new IntPtr[] { _stop }, 1 /*true*/, 0, 0 /*false*/) != Winsock.WSA_WAIT_EVENT_0)
                    {
                        Trace.WriteLine("[RCS] It is not stop!");
                        Thread.Sleep(100);
                    }
                    else
                    {
                        Trace.WriteLine("[RCS] It is stop, exiting.");
                        // Stopped: exit
                        break;
                    }
                }
            }
            try
            {
                Trace.WriteLine("[RCS] Closing main socket " + handle.ToString() + " ...");
                _listenSocket.Close();
                Trace.WriteLine("[RCS] Main socket " + handle.ToString() + " closed.");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("[RCS] Main socket closing exception: " + ex.Message);
            }
            finally
            {
                _listenSocket = null;
            }
            Winsock.WSACloseEvent(socketEvent);
        }
Example #16
0
 private void InitVars()
 {
     _clientSocket = new Winsock();
     _serverSockets = new WinsockCollection(true);
     _listenSocket = new Winsock();
     _clients = new ClientCollection();
     _tQueue = new TaskQueue();
     _tQThread = new Thread(new ThreadStart(_tQueue.QueueStart));
     _tQThread.IsBackground = true;
     _tQThread.Start();
 }
Example #17
0
 public WinSocket(Winsock parent)
 {
     Parent = parent;
 }
Example #18
0
 public WinSocket(Winsock parent)
 {
     Parent = parent;
 }