Exemple #1
0
        private void StartConnect(TCPScanTaskInfo ti)
        {
            if (ti.Socket != null)
            {
                base.CloseSocket(ti.Socket);
            }
            ti.Socket    = this.GetNextSocket(ti.Timeout);
            ti.LastTime  = DateTime.Now;
            ti.IsStarted = true;
            //lock (m_hs_task_running) m_hs_task_running.Add(ti);//repeat
            SocketAsyncEventArgs sae = this.PopSAE();

            sae.SetBuffer(0, 0);
            sae.RemoteEndPoint = ti.EndPoint;
            sae.UserToken      = ti;
            try {
                if (!ti.Socket.ConnectAsync(sae))
                {
                    IOProcessPool.QueueWork(this.ProcessConnect, sae);
                }
            } catch (Exception ex) {
                this.PushSAE(sae);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-CONNECT]-" + ex.Message));
            }
        }
Exemple #2
0
        private void ProcessSend(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            this.PushSAE(e);
        }
Exemple #3
0
        private void ProcessRecv(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            if (e.SocketError != SocketError.Success || e.BytesTransferred < 1)
            {
                if (ti.SendProbes != null && ti.SendProbes.Count != 0 && !ti.IsTotalTimeout)
                {
                    ti.CurrentProbe = ti.SendProbes.Dequeue();
                    this.StartConnect(ti);
                }
                else
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                return;
            }
            MatchResult mr = new MatchResult();

            if (ti.CurrentProbe == null)
            {
                mr = m_configer.MatchData(e.Buffer, e.BytesTransferred, 0, ProbeType.Tcp);
            }
            else
            {
                mr = m_configer.MatchData(e.Buffer, e.BytesTransferred, ti.Port, ProbeType.Tcp, ti.CurrentProbe);
            }
            this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, mr.Name, mr.RegexLine, mr.DataString, e.Buffer, e.BytesTransferred));
        }
Exemple #4
0
 public TCPScanner(int nMaxTask, ProbeConfiger probes)
 {
     if (nMaxTask > 60000 || nMaxTask < 1)
     {
         throw new ArgumentOutOfRangeException("the MaxTask must be between 1 and 30000");
     }
     m_configer        = probes;
     m_que_task        = new Queue <TCPScanTaskInfo>();
     m_hs_task_running = new HashSet <TCPScanTaskInfo>();
     m_que_sae         = new Queue <SocketAsyncEventArgs>();
     for (int i = 0; i < nMaxTask; i++)
     {
         TCPScanTaskInfo ti = new TCPScanTaskInfo();
         ti.TaskID = (uint)(i + 1);
         SocketAsyncEventArgs sae = new SocketAsyncEventArgs();
         sae.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
         sae.SetBuffer(new byte[2048], 0, 2048);
         sae.UserToken = ti;
         ti.RecvSAE    = sae;
         m_que_task.Enqueue(ti);
     }
     m_se             = new Semaphore(nMaxTask, nMaxTask);
     m_thread_timeout = new Thread(this.CheckTimeout);
     m_thread_timeout.IsBackground = true;
     m_thread_timeout.Start();
 }
        private void StartConnect(TCPScanTaskInfo ti)
        {
            if (ti.Socket != null)
            {
                base.CloseSocket(ti.Socket);
            }
            ti.Socket    = this.GetNextSocket(ti.Timeout);
            ti.LastTime  = DateTime.Now;
            ti.IsStarted = true;
            //lock (m_hs_task_running) m_hs_task_running.Add(ti);//repeat
            SocketAsyncEventArgs sae = this.PopSAE();

            sae.SetBuffer(0, 0);
            sae.RemoteEndPoint = ti.EndPoint;
            sae.UserToken      = ti;
            try {
                if (!ti.Socket.ConnectAsync(sae))
                {
                    IOProcessPool.QueueWork(this.ProcessConnect, sae);
                }
            } catch (Exception ex) {
                this.PushSAE(sae);
                lock (m_writer) {
                    if (ex.Message.IndexOf("asynchronous") != -1)
                    {
                        int a = 0;
                        a++;
                    }
                    m_writer.WriteLine("[SOCKET-CONNECT]-" + ex.Message + " tt:" + DateTime.Now.Subtract(ti.StartTime) + " rr" + ti.RunedRetry);
                    m_writer.Flush();
                }
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-CONNECT]-" + ex.Message));
            }
        }
        private TCPScanTaskInfo CreateTaskInfo(int nPort, EndPoint endPoint, int nProbes, int nTimeout, int nRetry, int nTotalTimeout, bool bUseNullProbes)
        {
            TCPScanTaskInfo ti = null;

            lock (m_obj_sync) {
                ti = m_que_task.Dequeue();
            }
            ti.Retry      = nRetry;
            ti.RunedRetry = 0;
            ti.Port       = nPort;
            ti.EndPoint   = endPoint;
            ti.IsStarted  = false;
            //ti.Socket = this.GetNextSocket(nTimeout);
            ti.CanConnect   = false;
            ti.Timeout      = nTimeout;
            ti.TotalTimeout = nTotalTimeout;
            ti.CurrentProbe = null;
            if (nProbes < 0)
            {
                ti.SendProbes = m_configer.GetProbesQueue(ProbeType.Tcp, nPort, 0);
            }
            else if (nProbes > 0)
            {
                ti.SendProbes = m_configer.GetProbesQueue(ProbeType.Tcp, nPort, nProbes);
            }
            else
            {
                ti.SendProbes = null;
            }
            if (!bUseNullProbes && ti.SendProbes != null && ti.SendProbes.Count != 0)
            {
                ti.CurrentProbe = ti.SendProbes.Dequeue();
            }
            return(ti);
        }
Exemple #7
0
 private void EndTask(TCPScanTaskInfo ti, ScanEventArgs e)
 {
     base.CloseSocket(ti.Socket);
     ti.Socket    = null;
     ti.IsStarted = false;
     lock (m_hs_task_running) m_hs_task_running.Remove(ti);
     lock (m_obj_sync) {
         if (!base._IsDisposed)
         {
             m_que_task.Enqueue(ti);
         }
     }
     base.OnCompleted(e);
     m_se.Release();
 }
Exemple #8
0
        protected override uint OnScan(int nPort, EndPoint endPoint, int nProbes, int nTimeout, int nRetry, int nTotalTimeout, bool bUseNullProbe)
        {
            lock (m_obj_sync) {
                if (base._IsDisposed)
                {
                    throw new ObjectDisposedException("TCPScanner", "The scanner was disposed");
                }
            }
            m_se.WaitOne();
            TCPScanTaskInfo ti = this.CreateTaskInfo(nPort, endPoint, nProbes, nTimeout, nRetry, nTotalTimeout, bUseNullProbe);

            ti.StartTime = DateTime.Now;
            lock (m_hs_task_running) m_hs_task_running.Add(ti);
            this.StartConnect(ti);
            return(ti.TaskID);
        }
Exemple #9
0
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            Socket    sock = ti.Socket;
            ProbeInfo pi   = ti.CurrentProbe;

            //SocketError.AccessDenied
            switch (e.SocketError)
            {
            case SocketError.Success:
                ti.RunedRetry = 0;
                ti.CanConnect = true;
                break;

            case SocketError.ConnectionReset:           //此连接由远程对等计算机重置
            case SocketError.ConnectionRefused:         //远程主机正在主动拒绝连接
            case SocketError.AddressNotAvailable:       //选定的 IP 地址在此上下文中无效
            case SocketError.Fault:                     //基础套接字提供程序检测到无效的指针地址
            case SocketError.HostDown:                  //由于远程主机被关闭 操作失败
            case SocketError.HostNotFound:              //无法识别这种主机 该名称不是正式的主机名或别名
            case SocketError.HostUnreachable:           //没有到指定主机的网络路由
            case SocketError.NetworkDown:               //网络不可用
            case SocketError.NetworkUnreachable:        //不存在到远程主机的路由
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                return;
            }
            if (SocketError.Success != e.SocketError || !ti.IsStarted)
            {
                this.PushSAE(e);
                if (++ti.RunedRetry > ti.Retry || ti.IsTotalTimeout)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                else
                {
                    this.StartConnect(ti);
                }
                return;
            }
            try {
                if (!sock.ReceiveAsync(ti.RecvSAE))
                {
                    IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
                }
            } catch (Exception ex) {
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-RECV]-" + ex.Message));
                return;
            }
            try {
                if (pi != null)
                {
                    if (e.Buffer.Length < pi.Data.Length)
                    {
                        e.SetBuffer(new byte[pi.Data.Length], 0, pi.Data.Length);
                    }
                    Array.Copy(pi.Data, e.Buffer, pi.Data.Length);
                    e.SetBuffer(0, pi.Data.Length);
                    if (!sock.SendAsync(e))
                    {
                        IOProcessPool.QueueWork(this.ProcessSend, e);
                    }
                }
                else
                {
                    this.PushSAE(e);
                }
            } catch {
                this.PushSAE(e);
            }
        }
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            Socket    sock = ti.Socket;
            ProbeInfo pi   = ti.CurrentProbe;

            //SocketError.AccessDenied
            switch (e.SocketError)
            {
            case SocketError.Success: break;

            case SocketError.ConnectionReset:
            case SocketError.ConnectionRefused:
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                return;

            default:
                if (e.SocketError != SocketError.OperationAborted)
                {
                    DateTime dd = DateTime.Now;
                    lock (m_writer) {
                        if (dd.Subtract(ti.StartTime).TotalSeconds > 70)
                        {
                            int a = 0;
                            a++;
                        }
                        m_writer.WriteLine("ERR:" + e.SocketError + " tt:" + DateTime.Now.Subtract(ti.StartTime) + " rr:" + ti.RunedRetry);
                        m_writer.Flush();
                    }
                }
                this.PushSAE(e);
                if (++ti.RunedRetry > ti.Retry)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                else
                {
                    this.StartConnect(ti);
                }
                return;
            }
            //if (e.SocketError != SocketError.Success) {
            //    m_writer.WriteLine(e.SocketError);
            //    m_writer.Flush();
            //    this.PushSAE(e);
            //    if (++ti.RunedRetry > ti.Retry)
            //        this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
            //    else
            //        this.StartConnect(ti);
            //    return;
            //} else {
            //    //m_writer.WriteLine(e.SocketError);
            //    //m_writer.Flush();
            //}
            ti.RunedRetry = 0;
            ti.CanConnect = true;
            try {
                if (!sock.ReceiveAsync(ti.RecvSAE))
                {
                    IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
                }
            } catch (Exception ex) {
                this.PushSAE(e);
                lock (m_writer) {
                    m_writer.WriteLine("[SOCKET-RECV]-" + ex.Message);
                    m_writer.Flush();
                }
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-RECV]-" + ex.Message));
                return;
            }
            try {
                if (pi != null)
                {
                    if (e.Buffer.Length < pi.Data.Length)
                    {
                        e.SetBuffer(new byte[pi.Data.Length], 0, pi.Data.Length);
                    }
                    Array.Copy(pi.Data, e.Buffer, pi.Data.Length);
                    e.SetBuffer(0, pi.Data.Length);
                    if (!sock.SendAsync(e))
                    {
                        IOProcessPool.QueueWork(this.ProcessSend, e);
                    }
                }
                else
                {
                    this.PushSAE(e);
                }
            } catch {
                this.PushSAE(e);
            }
            //try {
            //    if (!ti.Socket.ReceiveAsync(ti.RecvSAE)) IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
            //    if (ti.CurrentProbe != null) {
            //        if (e.Buffer.Length < ti.CurrentProbe.Data.Length)
            //            e.SetBuffer(new byte[ti.CurrentProbe.Data.Length], 0, ti.CurrentProbe.Data.Length);
            //        Array.Copy(ti.CurrentProbe.Data, e.Buffer, ti.CurrentProbe.Data.Length);
            //        e.SetBuffer(0, ti.CurrentProbe.Data.Length);
            //        if (!ti.Socket.SendAsync(e)) IOProcessPool.QueueWork(this.ProcessSend, e);
            //    } else this.PushSAE(e);
            //} catch (Exception ex) {
            //    this.PushSAE(e);
            //    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET]-" + ex.Message));
            //}
        }