unsafe public override void Receive(byte[] buf, int len) { long ip_srcaddr = 10, ip_destaddr = 10, header_len = 0; string out_string = ""; int protocol; short src_port = 0, dst_port = 0; IPAddress tmp_ip; string from_ip, to_ip; fixed(byte *fixed_buf = buf) { IpHeader *header = (IpHeader *)fixed_buf; header_len = (header->ip_verlen & 0x0F) << 2; protocol = header->ip_protocol; ip_srcaddr = header->ip_srcaddr; ip_destaddr = header->ip_destaddr; tmp_ip = new IPAddress(ip_srcaddr); from_ip = tmp_ip.ToString(); tmp_ip = new IPAddress(ip_destaddr); to_ip = tmp_ip.ToString(); tmp_ip = null; switch (protocol) { case 1: out_string = "ICMP:"; break; case 2: out_string = "IGMP:"; break; case 6: out_string = "TCP:"; src_port = *(short *)&fixed_buf[header_len]; dst_port = *(short *)&fixed_buf[header_len + 2]; from_ip += ":" + IPAddress.NetworkToHostOrder(src_port).ToString(); to_ip += ":" + IPAddress.NetworkToHostOrder(dst_port).ToString(); break; case 17: out_string = "UDP:"; src_port = *(short *)&fixed_buf[header_len]; dst_port = *(short *)&fixed_buf[header_len + 2]; from_ip += ":" + IPAddress.NetworkToHostOrder(src_port).ToString(); to_ip += ":" + IPAddress.NetworkToHostOrder(dst_port).ToString(); break; default: out_string = "UNKNOWN"; break; } } out_string += from_ip + "--->" + to_ip.ToString(); list.Items.Add(out_string); out_string = "total length: " + len.ToString() + " bytes"; list.Items.Add(out_string); out_string = "data length: " + (len - header_len).ToString() + " bytes"; list.Items.Add(out_string); }
//parse packet unsafe public override void Receive(byte[] buf, int len) { try { long ip_srcaddr = 10, ip_destaddr = 10, header_len = 0; string out_string = ""; int protocol; short src_port = 0, dst_port = 0; IPAddress tmp_ip; string from_ip, to_ip; fixed(byte *fixed_buf = buf) { IpHeader *header = (IpHeader *)fixed_buf; header_len = (header->ip_verlen & 0x0F) << 2; protocol = header->ip_protocol; ip_srcaddr = header->ip_srcaddr; ip_destaddr = header->ip_destaddr; try { tmp_ip = new IPAddress(ip_srcaddr); from_ip = tmp_ip.ToString(); } catch { from_ip = ""; } try { tmp_ip = new IPAddress(ip_destaddr); to_ip = tmp_ip.ToString(); } catch { to_ip = ""; } tmp_ip = null; switch (protocol) { case 1: out_string = "ICMP:"; break; case 2: out_string = "IGMP:"; break; case 6: out_string = "TCP:"; src_port = *(short *)&fixed_buf[header_len]; dst_port = *(short *)&fixed_buf[header_len + 2]; from_ip += ":" + IPAddress.NetworkToHostOrder(src_port).ToString(); to_ip += ":" + IPAddress.NetworkToHostOrder(dst_port).ToString(); break; case 17: out_string = "UDP:"; src_port = *(short *)&fixed_buf[header_len]; dst_port = *(short *)&fixed_buf[header_len + 2]; from_ip += ":" + IPAddress.NetworkToHostOrder(src_port).ToString(); to_ip += ":" + IPAddress.NetworkToHostOrder(dst_port).ToString(); break; default: out_string = "UNKNOWN:"; break; } } if (protocol == 6) { out_string += from_ip + "--->" + to_ip.ToString(); if (IPAddress.NetworkToHostOrder(src_port) < 0 || IPAddress.NetworkToHostOrder(dst_port) < 0) { out_string += "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"; } //list.Items.Add(out_string); out_string = "total length: " + len.ToString() + " bytes!" + "data length: " + (len - header_len).ToString() + " bytes!"; //list.Items.Add(out_string); int ridhead = 71; if (from_ip == ipSource + ":" + portSource.ToString() && len > ridhead && IPAddress.NetworkToHostOrder(dst_port) == portDest) { byte[] tmpbyte = new byte[len - ridhead]; for (int i = 0; i < tmpbyte.Length - 2; i++) { tmpbyte[i] = buf[ridhead + i]; } string s = System.Text.Encoding.ASCII.GetString(tmpbyte); Thread.Sleep(2000); if (ePlus.GlobalVar.str_Listener == "") { ePlus.GlobalVar.str_Listener_Set = s; } //ePlus.GlobalVar.stdRichTB.AppendText("listenStart"); //MessageBox.Show(s.Substring(19)); } } } catch (Exception ex) { //list.Items.Add(ex.Source + ex.Message); } }