unsafe private void Unpack(byte[] buf, int len)//信号解码 { byte protocol = 0; uint version = 0; uint ipSourceAddress = 0; uint ipDestinationAddress = 0; int sourcePort = 0; int destinationPort = 0; IPAddress ip; Array.Copy(buf, CBTCPacketProperties.Buf, len); CBTCPacketProperties.BufLength = len; fixed(byte *FixedBuf = buf) { try { IPHeader *head = (IPHeader *)FixedBuf; CBTCPacketProperties.IPHeaderLength = (uint)((head->versionAndLength & 0x0f) << 2); protocol = head->protocol; version = (uint)((head->versionAndLength & 0xf0) >> 4); CBTCPacketProperties.Version = version.ToString(); ipSourceAddress = head->sourceAddress; ipDestinationAddress = head->destinationAdress; ip = new IPAddress(ipSourceAddress); CBTCPacketProperties.OriginationAddress = ip.ToString(); ip = new IPAddress(ipDestinationAddress); CBTCPacketProperties.DestinationAddress = ip.ToString(); sourcePort = buf[CBTCPacketProperties.IPHeaderLength] * 256 + buf[CBTCPacketProperties.IPHeaderLength + 1]; destinationPort = buf[CBTCPacketProperties.IPHeaderLength + 2] * 256 + buf[CBTCPacketProperties.IPHeaderLength + 3]; CBTCPacketProperties.OriginationPort = sourcePort.ToString(); CBTCPacketProperties.DestinationPort = destinationPort.ToString(); CBTCPacketProperties.PacketLength = (uint)len; CBTCPacketProperties.MessageLength = CBTCPacketProperties.PacketLength - CBTCPacketProperties.IPHeaderLength; CBTCPacketProperties.PacketBuffer = buf; Array.Copy(buf, (int)CBTCPacketProperties.IPHeaderLength, CBTCPacketProperties.MessageBuffer, 0, (int)CBTCPacketProperties.MessageLength); if (protocol == 17) { if (CBTCPacketProperties.DestinationPort == "2152") { int UDP_HEADER_LENGTH = 8; //int GTP_MUST_CHOICE_LENGTH = 8; int GTP_LENGTH_FLAG = 2; int gtpMessegePayloadLength = buf[CBTCPacketProperties.IPHeaderLength + UDP_HEADER_LENGTH + GTP_LENGTH_FLAG] * 256 + buf[CBTCPacketProperties.IPHeaderLength + UDP_HEADER_LENGTH + GTP_LENGTH_FLAG + 1]; //byte[] ActualBuf = new byte[len - PacketProperties.IPHeaderLength - UDP_HEADER_LENGTH - GTP_MUST_CHOICE_LENGTH - gtpMessegePayloadLength]; //Array.Copy(buf, PacketProperties.IPHeaderLength + UDP_HEADER_LENGTH + GTP_MUST_CHOICE_LENGTH + gtpMessegePayloadLength, ActualBuf, 0, len - PacketProperties.IPHeaderLength - UDP_HEADER_LENGTH - GTP_MUST_CHOICE_LENGTH - gtpMessegePayloadLength); byte[] ActualBuf = new byte[len - CBTCPacketProperties.IPHeaderLength - UDP_HEADER_LENGTH - 12]; Array.Copy(buf, CBTCPacketProperties.IPHeaderLength + UDP_HEADER_LENGTH + 12, ActualBuf, 0, len - CBTCPacketProperties.IPHeaderLength - UDP_HEADER_LENGTH - 12); Unpack(ActualBuf, ActualBuf.Length); } else { DataFilterByIPAndPort(CBTCPacketProperties); } } } catch (Exception e) { } } }
//解析接收的数据包,形成PacketArrivedEventArgs事件数据类对象,并引发OnPacketArrival事件 unsafe private void Receive(byte[] buf, int len) { byte temp_protocol = 0; uint temp_version = 0; uint temp_sourceAddress = 0; uint temp_destinationAddress = 0; short temp_srcport = 0; short temp_dstport = 0; IPAddress temp_ip; PacketArrivedEventArgs e = new PacketArrivedEventArgs();//新网络数据包信息事件 fixed(byte *fixed_buf = buf) { IPHeader *head = (IPHeader *)fixed_buf; //把数据流整和为IPHeader结构 e.HeaderLength = (uint)(head->versionAndLength & 0x0F) << 2; e.IPHeaderBuffer = new byte[e.HeaderLength]; temp_protocol = head->protocol; switch (temp_protocol) //提取协议类型 { case 1: e.Protocol = "ICMP"; break; case 2: e.Protocol = "IGMP"; break; case 6: e.Protocol = "TCP"; break; case 17: e.Protocol = "UDP"; break; default: e.Protocol = "UNKNOWN"; break; } temp_version = (uint)(head->versionAndLength & 0xF0) >> 4;//提取IP协议版本 e.IPVersion = temp_version.ToString(); //以下语句提取出了PacketArrivedEventArgs对象中的其他参数 temp_sourceAddress = head->sourceAddress; temp_destinationAddress = head->destinationAddress; temp_ip = new IPAddress(temp_sourceAddress); e.OriginationAddress = temp_ip.ToString(); temp_ip = new IPAddress(temp_destinationAddress); e.DestinationAddress = temp_ip.ToString(); temp_srcport = *(short *)&fixed_buf[e.HeaderLength]; temp_dstport = *(short *)&fixed_buf[e.HeaderLength + 2]; e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString(); e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString(); e.PacketLength = (uint)len; e.MessageLength = (uint)len - e.HeaderLength; e.MessageBuffer = new byte[e.MessageLength]; e.ReceiveBuffer = buf; //把buf中的IP头赋给PacketArrivedEventArgs中的IPHeaderBuffer Array.Copy(buf, 0, e.IPHeaderBuffer, 0, (int)e.HeaderLength); //把buf中的包中内容赋给PacketArrivedEventArgs中的MessageBuffer Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength); } //引发PacketArrival事件 OnPacketArrival(e); }
unsafe public List <PacapData> Parse() { List <PacapData> pacapDataList = new List <PacapData>(); while (this.Position != this.Length) { byte[] PktHeaderBuffer = new byte[sizeof(PacapPacketHeader)]; this.Read(PktHeaderBuffer, 0, sizeof(PacapPacketHeader)); fixed(byte *pktPtr = PktHeaderBuffer) { PacapPacketHeader *pktHeader = (PacapPacketHeader *)pktPtr; byte[] data = new byte[pktHeader->Len]; int nReadCount = 0; while (nReadCount < pktHeader->Len) { nReadCount += this.Read(data, 0, (int)(pktHeader->Len - nReadCount)); } fixed(byte *enterPtr = data) { EthernetHeader *etherHeader = (EthernetHeader *)enterPtr; byte * ipPtr = enterPtr + sizeof(EthernetHeader); IPHeader * header = (IPHeader *)ipPtr; int protocol = header->ProtocolType; PacapData pData = null; switch ((ProtocolType)header->ProtocolType) { case ProtocolType.TCP: pData = new TcpData(); break; case ProtocolType.UDP: pData = new UdpData(); break; } if (pData != null) { int offset = sizeof(EthernetHeader) + pData.HeaderLen; pData.IPHeader = *header; pData.EthernetHeader = *etherHeader; pData.PacketHeader = *pktHeader; pData.Fill(data, offset, (int)(pktHeader->Len - offset)); pacapDataList.Add(pData); } } } } return(pacapDataList); }
unsafe private void Unpack(byte[] buf, int len) { byte protocol = 0; uint version = 0; uint ipSourceAddress = 0; uint ipDestinationAddress = 0; int sourcePort = 0; int destinationPort = 0; IPAddress ip; Array.Copy(buf, PacketProperties.ReceiveBuf, len); PacketProperties.BufLength = len; fixed(byte *FixedBuf = buf) { IPHeader *head = (IPHeader *)FixedBuf; PacketProperties.IPHeaderLength = (uint)((head->versionAndLength & 0x0f) << 2); protocol = head->protocol; version = (uint)((head->versionAndLength & 0xf0) >> 4); if (protocol == 17 && version == 4) { ipSourceAddress = head->sourceAddress; ipDestinationAddress = head->destinationAdress; ip = new IPAddress(ipSourceAddress); PacketProperties.OriginationAddress = ip.ToString(); ip = new IPAddress(ipDestinationAddress); PacketProperties.DestinationAddress = ip.ToString(); sourcePort = buf[PacketProperties.IPHeaderLength] * 256 + buf[PacketProperties.IPHeaderLength + 1]; destinationPort = buf[PacketProperties.IPHeaderLength + 2] * 256 + buf[PacketProperties.IPHeaderLength + 3]; PacketProperties.OriginationPort = sourcePort.ToString(); PacketProperties.DestinationPort = destinationPort.ToString(); PacketProperties.PacketLength = Convert.ToUInt32(len); if (PacketProperties.PacketLength > PacketProperties.IPHeaderLength + 40) { PacketProperties.MessageLength = PacketProperties.PacketLength - PacketProperties.IPHeaderLength; Array.Copy(buf, (int)PacketProperties.IPHeaderLength, PacketProperties.MessageBuffer, 0, (int)PacketProperties.MessageLength); DataFilterByIPAndPort(PacketProperties); } } else if (protocol == 41 && version == 4) { ipSourceAddress = head->sourceAddress; ipDestinationAddress = head->destinationAdress; ip = new IPAddress(ipSourceAddress); PacketProperties.OriginationAddress = ip.ToString(); ip = new IPAddress(ipDestinationAddress); PacketProperties.DestinationAddress = ip.ToString(); sourcePort = buf[PacketProperties.IPHeaderLength + 40] * 256 + buf[PacketProperties.IPHeaderLength + 41]; destinationPort = buf[PacketProperties.IPHeaderLength + 42] * 256 + buf[PacketProperties.IPHeaderLength + 43]; PacketProperties.OriginationPort = sourcePort.ToString(); PacketProperties.DestinationPort = destinationPort.ToString(); PacketProperties.PacketLength = Convert.ToUInt32(len); if (PacketProperties.PacketLength > PacketProperties.IPHeaderLength + 40) { PacketProperties.MessageLength = PacketProperties.PacketLength - PacketProperties.IPHeaderLength - 40; Array.Copy(buf, (int)PacketProperties.IPHeaderLength + 40, PacketProperties.MessageBuffer, 0, (int)PacketProperties.MessageLength); DataFilterByIPAndPort(PacketProperties); } } } }
//解析接收的数据包,形成PacketArrivedEventArgs时间数据类对象,并引发PacketArrival事件 unsafe private void Receive(byte[] buf, int len) { byte temp_protocol = 0; uint temp_version = 0; uint temp_ip_srcaddr = 0; uint temp_ip_destaddr = 0; short temp_srcport = 0; short temp_dstport = 0; IPAddress temp_ip; //return; PacketArrivedEventArgs e = new PacketArrivedEventArgs(); fixed(byte *fixed_buf = buf) { IPHeader *head = (IPHeader *)fixed_buf; e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2; temp_protocol = head->ip_protocol; switch (temp_protocol) { case 1: e.Protocol = "ICMP:"; break; case 2: e.Protocol = "IGMP:"; break; case 6: e.Protocol = "TCP:"; break; case 17: e.Protocol = "UDP:"; break; default: e.Protocol = "UNKNOWN"; break; } temp_version = (uint)(head->ip_verlen & 0xF0) >> 4; e.IPVersion = temp_version.ToString(); temp_ip_srcaddr = head->ip_srcaddr; temp_ip_destaddr = head->ip_destaddr; temp_ip = new IPAddress(temp_ip_srcaddr); e.OriginationAddress = temp_ip.ToString(); temp_ip = new IPAddress(temp_ip_destaddr); e.DestinationAddress = temp_ip.ToString(); temp_srcport = *(short *)&fixed_buf[e.HeaderLength]; temp_dstport = *(short *)&fixed_buf[e.HeaderLength + 2]; e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString(); e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString(); // if(e.DestinationAddress!="211.87.212.116"||int.Parse(e.DestinationPort)>1000) // { // return; // } e.PacketLength = (uint)len; e.MessageLength = (uint)len - e.HeaderLength; e.ReceiveBuffer = buf; //把buf中的IP头赋给PacketArrivedEventArgs中的IPHeaderBuffer Array.Copy(buf, 0, e.IPHeaderBuffer, 0, (int)e.HeaderLength); //把buf中的包中内容赋给PacketArrivedEventArgs中的MessageBuffer Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength); } //引发PacketArrival事件 OnPacketArrival(e); }
unsafe private void Receive(byte[] buf, int len) { byte temp_protocol = 0; uint temp_version = 0; uint temp_ip_srcaddr = 0; uint temp_ip_destaddr = 0; short temp_srcport = 0; short temp_dstport = 0; IPAddress temp_ip; PacketArrivedEventArgs e = new PacketArrivedEventArgs(); fixed(byte *fixed_buf = buf) { IPHeader *head = (IPHeader *)fixed_buf; e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2; //Extract Network Protocal Type temp_protocol = head->ip_protocol; switch (temp_protocol) { case 1: e.Protocol = "ICMP"; break; case 2: e.Protocol = "IGMP"; break; case 6: e.Protocol = "TCP"; break; case 17: e.Protocol = "UDP"; break; default: e.Protocol = "UNKNOWN"; break; } //Extract Network Protocal Version temp_version = (uint)(head->ip_verlen & 0xF0) >> 4; e.IPVersion = temp_version.ToString(); temp_ip_srcaddr = head->ip_srcaddr; temp_ip_destaddr = head->ip_destaddr; temp_ip = new IPAddress(temp_ip_srcaddr); e.OriginationAddress = temp_ip.ToString(); temp_ip = new IPAddress(temp_ip_destaddr); e.DestinationAddress = temp_ip.ToString(); temp_srcport = *(short *)&fixed_buf[e.HeaderLength]; temp_dstport = *(short *)&fixed_buf[e.HeaderLength + 2]; e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString(); e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString(); e.PacketLength = (uint)len; e.MessageLength = (uint)len - e.HeaderLength; e.ReceiveBuffer = buf; Array.Copy(buf, 0, e.IPHeaderBuffer, 0, (int)e.HeaderLength); Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength); } OnPacketArrival(e); }
unsafe private void Receive(byte[] buf, int len) { byte protocol = 0; uint version = 0; uint ipSourceAddress = 0; uint ipDestinationAddress = 0; int sourcePort = 0; int destinationPort = 0; IPAddress ip; PacketArrivedEventArgs e = new PacketArrivedEventArgs(); e.ReceiveBuf = buf; e.BufLength = len; fixed(byte *FixedBuf = buf) { IPHeader *head = (IPHeader *)FixedBuf; e.IPHeaderLength = (uint)((head->versionAndLength & 0x0f) << 2); //一个指向结构体或对象的指针访问其内成员(->指向结构体成员运算符) //并将变量versionAndLength的值高四位清0,保留低四位 // protocol = head->protocol; switch (protocol) { case 1: e.Protocol = "ICMP"; break; case 2: e.Protocol = "IGMP"; break; case 6: e.Protocol = "TCP"; break; case 17: e.Protocol = "UDP"; break; case 132: e.Protocol = "SCTP"; break; default: e.Protocol = "UNKNOWN"; break; } version = (uint)((head->versionAndLength & 0xf0) >> 4); e.Version = version.ToString(); ipSourceAddress = head->sourceAddress; ipDestinationAddress = head->destinationAdress; ip = new IPAddress(ipSourceAddress); e.OriginationAddress = ip.ToString(); ip = new IPAddress(ipDestinationAddress); e.DestinationAddress = ip.ToString(); sourcePort = buf[e.IPHeaderLength] * 256 + buf[e.IPHeaderLength + 1]; destinationPort = buf[e.IPHeaderLength + 2] * 256 + buf[e.IPHeaderLength + 3]; e.OriginationPort = sourcePort.ToString(); e.DestinationPort = destinationPort.ToString(); e.PacketLength = (uint)len; e.MessageLength = e.PacketLength - e.IPHeaderLength; e.PacketBuffer = buf; Array.Copy(buf, (int)e.IPHeaderLength, e.MessageBuffer, 0, (int)e.MessageLength); OnPacketArrival(e);//引发PacketArrival事件 } }