Example #1
0
        private void UpdateSockets()
        {
            for (int i = 0; i < _connections.Count; i++)
            {
                bool found = false;
                for (int j = 0; j < _sockets.Count; j++)
                {
                    if (_connections[i].LocalIP == _sockets[j].LocalIP &&
                        (!UseSocketFilter || (_connections[i].RemoteIP == _sockets[j].RemoteIP)))
                    {
                        found = true;
                    }
                }

                if (!found)
                {
                    Trace.WriteLine("TCPNetworkMonitor: Starting " + MonitorType.ToString() + " listener on [" +
                                    new IPAddress(_connections[i].LocalIP).ToString() + "]" +
                                    (UseSocketFilter ? "=> [" + new IPAddress(_connections[i].RemoteIP).ToString() + "]." : ""));

                    if (MonitorType == NetworkMonitorType.WinPCap)
                    {
                        _sockets.Add(new RawPCap());
                    }
                    else
                    {
                        _sockets.Add(new RawSocket());
                    }
                    _sockets.Last().Create(_connections[i].LocalIP, UseSocketFilter ? _connections[i].RemoteIP : 0);
                }
            }

            for (int i = _sockets.Count - 1; i >= 0; i--)
            {
                bool found = false;
                for (int j = 0; j < _connections.Count; j++)
                {
                    if (_connections[j].LocalIP == _sockets[i].LocalIP &&
                        (!UseSocketFilter || (_connections[j].RemoteIP == _sockets[i].RemoteIP)))
                    {
                        found = true;
                    }
                }

                if (!found)
                {
                    Trace.WriteLine("TCPNetworkMonitor: Stopping " + MonitorType.ToString() + " listener on [" +
                                    new IPAddress(_sockets[i].LocalIP).ToString() + "]" +
                                    (UseSocketFilter ? "=> [" + new IPAddress(_sockets[i].RemoteIP).ToString() + "]." : ""));
                    _sockets[i].Destroy();
                    _sockets.RemoveAt(i);
                }
            }
        }
Example #2
0
        private void Cleanup()
        {
            for (int i = 0; i < _sockets.Count; i++)
            {
                _sockets[i].Destroy();
                Trace.WriteLine("TCPNetworkMonitor: Stopping " + MonitorType.ToString() + " listener between [" +
                                new IPAddress(_sockets[i].LocalIP).ToString() + "] => [" +
                                new IPAddress(_sockets[i].RemoteIP).ToString() + "].");
            }

            _sockets.Clear();
            _connections.Clear();
        }