//Thread listener; public void listen() { try { //��ȡ������ַ string hostName = Dns.GetHostName(); IPAddress hostAddr = (IPAddress)Dns.GetHostByName(hostName).AddressList[0]; string myAddr = hostAddr.ToString(); socket = new RawSocket(); socket.CreateAndBindSocket(myAddr); if (socket.ErrorOccurred) { //MessageBox.Show("�������ִ���"); UpdateTBResult(ui, "�������ִ���\r\n"); return; } socket.KeepRunning = true; socket.PacketArrival += socket_PacketArrival; socket.Run(); } catch// (Exception ex) { //Console.WriteLine(ex); //throw; } finally { //Console.Read(); //socket.Shutdown(); } }
/// <summary> /// ��ȡ�� /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void TcpSyn_PacketArrival(object sender, RawSocket.PacketArrivedEventArgs args) { if (args.Protocol == "TCP" && args.OriginationAddress == aimAddr) { //�������ݰ� byte[] revMsg = args.ReceiveBuffer; byte flag = revMsg[33]; ushort port = BitConverter.ToUInt16(revMsg, 20); byte[] rport = BitConverter.GetBytes(port); Array.Reverse(rport); port = BitConverter.ToUInt16(rport, 0); if (flag == 0x0012) { Updateportlist1(ui,port); success = true; #region ������յ�SYN|ACK������һ��RST�źŹر����� IPAddress ipAddr = IPAddress.Parse(aimAddr); IPEndPoint iep = new IPEndPoint(ipAddr, Convert.ToInt32(port)); Random rand = new Random(); uint ip = BitConverter.ToUInt32(ipAddr.GetAddressBytes(), 0); syn rstSock = new syn(ip, port, iep, rand, Convert.ToByte(0x04)); rstSock.synFS(); #endregion } if (Scanner.finishPort == System.Math.Abs(endPort - startPort + 1) || rSocket.ErrorOccurred || rSocket.KeepRunning == false ) { UpdateTBResult(ui, "Finish!\r\n"); rSocket.Shutdown(); UpdateBtnStart(ui, true); } } }
public void socket_PacketArrival(object sender, RawSocket.PacketArrivedEventArgs args) { if (args.Protocol == "TCP" && args.OriginationAddress == aimAddr) { //�������ݰ� byte[] revMsg = args.ReceiveBuffer; byte flag = revMsg[33]; ushort port = BitConverter.ToUInt16(revMsg, 20); byte[] rport = BitConverter.GetBytes(port); Array.Reverse(rport); port = BitConverter.ToUInt16(rport, 0); if (flag == 0x0004 || flag == 0x0014) { Updateportlist(ui, port); } if (Scanner.finishPort == System.Math.Abs(endPort - startPort + 1)) { UpdateTBResult(ui, "Finish!\r\n"); socket.Shutdown(); UpdateBtnStart(ui, true); } } }
/// <summary> /// ���չ���Tcp�� /// </summary> public void Receive() { try { //��ȡ������ַ string hostName = Dns.GetHostName(); IPAddress hostAddr = (IPAddress)Dns.GetHostByName(hostName).AddressList[0]; string myAddr = hostAddr.ToString(); rSocket = new RawSocket(); rSocket.CreateAndBindSocket(myAddr); if (rSocket.ErrorOccurred) { UpdateTBResult(ui, "�����˿ڴ���"); } rSocket.PacketArrival += TcpSyn_PacketArrival; rSocket.KeepRunning = true; rSocket.Run(); } catch//(Exception ex) { //UpdateTBResult(ui, ex.Message.ToString()); } finally { //rSocket.Shutdown(); } }