private static void OnRecvCompleted(SocketAsyncEventArgs e) { if (RawSocket.RecvCompleted != null) { RawSocket.RecvCompleted(null, e); } }
public SYNScanner(int nMaxTask, ProbeConfiger probes, EndPoint bindEndPoint) { if (nMaxTask > 60000 || nMaxTask < 1) { throw new ArgumentOutOfRangeException("the MaxTask must be between 1 and 30000"); } m_probes = probes; if (bindEndPoint == null) { foreach (var v in Dns.GetHostAddresses(Dns.GetHostName())) { if (v.IsIPv6LinkLocal || v.IsIPv6Multicast || v.IsIPv6SiteLocal) { continue; } bindEndPoint = new IPEndPoint(v, 0); } } m_rnd = new Random(); m_dic_uid = new Dictionary <uint, SYNScanTaskInfo>();// new Dictionary<uint, uint>(); m_dic_task_running = new Dictionary <uint, SYNScanTaskInfo>(); m_tcp_scanner = new TCPScanner(nMaxTask, probes); m_tcp_scanner.Completed += new ScanEventHandler(m_tcp_Completed); //m_sock_bind = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //m_sock_bind.Bind(bindEndPoint); //bindEndPoint = m_sock_bind.LocalEndPoint; m_strLocalIP = bindEndPoint.ToString().Split(':')[0]; m_uLocalIP = RAWDefine.IPToINT(m_strLocalIP); m_nLocalPort = ushort.Parse(bindEndPoint.ToString().Split(':')[1]); m_se = new Semaphore(nMaxTask, nMaxTask); //m_sock_raw = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); //m_sock_raw.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true); //m_sock_raw.Bind(bindEndPoint); //m_sock_raw.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, null); RawSocket.InitRawSocket(bindEndPoint); RawSocket.RecvCompleted += this.ProcessRecv; m_que_task = new Queue <SYNScanTaskInfo>(); m_que_sae = new Queue <SocketAsyncEventArgs>(); for (int i = 0; i < nMaxTask; i++) { SYNScanTaskInfo ti = new SYNScanTaskInfo(); ti.TaskID = (uint)((i + 1) << 8); ti.SYNPacket = new byte[40]; m_que_task.Enqueue(ti); } //SocketAsyncEventArgs sae = new SocketAsyncEventArgs(); //sae.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed); //sae.SetBuffer(new byte[65535], 0, 65535); //sae.UserToken = m_sock_raw; //if (!m_sock_raw.ReceiveAsync(sae)) IOProcessPool.QueueWork(this.ProcessRecv, sae); m_thread_timeout = new Thread(this.CheckTimeout); m_thread_timeout.IsBackground = true; m_thread_timeout.Start(); }
private static void IO_Completed(object sender, SocketAsyncEventArgs e) { switch (e.LastOperation) { case SocketAsyncOperation.SendTo: RawSocket.ProcessSend(e); break; case SocketAsyncOperation.ReceiveFrom: RawSocket.ProcessRecv(e); break; } }
private static void ProcessRecv(SocketAsyncEventArgs e) { Socket sock = e.UserToken as Socket; if (e.SocketError == SocketError.Success && e.BytesTransferred > 0) { RawSocket.OnRecvCompleted(e); } try { if (!m_sock_raw.ReceiveFromAsync(e)) { IOProcessPool.QueueWork(RawSocket.ProcessRecv, e); } } catch { } }
private void CheckTimeout() { DateTime dt = DateTime.Now; List <SYNScanTaskInfo> lst_remove = new List <SYNScanTaskInfo>(); while (true) { Thread.Sleep(1000); lst_remove.Clear(); dt = DateTime.Now; bool bDisposed = base._IsDisposed; lock (m_dic_task_running) { foreach (var v in m_dic_task_running) { if (!v.Value.IsStarted) { continue; } if (dt.Subtract(v.Value.StartTime).TotalMilliseconds > v.Value.TotalTimeout || bDisposed) { lst_remove.Add(v.Value); continue; } if (dt.Subtract(v.Value.LastTime).TotalMilliseconds > v.Value.Timeout) { if (v.Value.RunedRetry++ < v.Value.Retry) { RawSocket.SendData(v.Value.SYNPacket, 0, v.Value.SYNPacket.Length);// this.SendData(v.Value); v.Value.LastTime = DateTime.Now; } else { lst_remove.Add(v.Value); } } } foreach (var v in lst_remove) { this.EndTask(v); } } if (bDisposed) { break; } } }
public static void SendData(byte[] byData, int nIndex, int nLen) { SocketAsyncEventArgs sae = RawSocket.PopSAE(); if (sae.Buffer == null || sae.Buffer.Length < nLen) { sae.SetBuffer(byData, nIndex, nLen); } else { Array.Copy(byData, nIndex, sae.Buffer, 0, nLen); } //sae.remo if (!m_sock_raw.SendToAsync(sae)) { IOProcessPool.QueueWork(RawSocket.ProcessRecv, sae); } }
//private SocketAsyncEventArgs PopSAE() { // lock (m_obj_sync) { // if (m_que_sae.Count != 0) return m_que_sae.Dequeue(); // } // SocketAsyncEventArgs sae = new SocketAsyncEventArgs(); // sae.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed); // sae.SetBuffer(new byte[40], 0, 40); // return sae; //} //private void PushSAE(SocketAsyncEventArgs sae) { // lock (m_obj_sync) { // if (base._IsDisposed) return; // m_que_sae.Enqueue(sae); // } //} 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("SYNScanner", "The scanner was disposed"); } } m_se.WaitOne(); SYNScanTaskInfo ti = this.CreateTaskInfo(nPort, endPoint, nProbes, nTimeout, nRetry, nTotalTimeout, bUseNullProbe); lock (m_dic_task_running) { m_dic_task_running.Add(ti.TaskID, ti); } //this.SendData(ti); RawSocket.SendData(ti.SYNPacket, 0, ti.SYNPacket.Length); ti.StartTime = ti.LastTime = DateTime.Now; ti.IsStarted = true; return(ti.TaskID); }
private static void ProcessSend(SocketAsyncEventArgs e) { RawSocket.PushSAE(e); }