private Byte[] packetize(ICMP icmp) { Byte[] buff=new Byte[icmp.body_size+8]; //Type buff[0]=icmp.type; //SubCode buff[1]=icmp.subcode; //CheckSum buff[2]=(Byte)(icmp.checkSum>>8); buff[3]=(Byte)(icmp.checkSum&0xFF); //ID buff[4]=(Byte)(icmp.id>>8); buff[5]=(Byte)(icmp.id&0xFF); //Sequence buff[6]=(Byte)(icmp.sq>>8); buff[7]=(Byte)(icmp.sq&0xFF); //Data Array.Copy(icmp.body,0,buff,8,icmp.body_size); //OID buff[8]=(Byte)(icmp.oid>>24); buff[9]=(Byte)(icmp.oid>>16); buff[10]=(Byte)(icmp.oid>>8); buff[11]=(Byte)(icmp.oid&0xFF); //TimeTick buff[12]=(Byte)(icmp.send_time>>24); buff[13]=(Byte)(icmp.send_time>>16); buff[14]=(Byte)(icmp.send_time>>8); buff[15]=(Byte)(icmp.send_time&0xFF); return buff; }
public static ICMP ToICMP(Byte[] rdata){ if(rdata.Length<36) return null; ICMP icmp=new ICMP(); //deCode icmp.type=rdata[20]; icmp.subcode=rdata[21]; icmp.checkSum=(UInt16)((rdata[22]<<8)+rdata[23]); icmp.id=(UInt16)((rdata[24]<<8)+rdata[25]); icmp.sq=(UInt16)((rdata[26]<<8)+rdata[27]); icmp.body_size=0; icmp.body=null; icmp.oid=(rdata[28]<<24)+(rdata[29]<<16)+(rdata[30]<<8)+rdata[31]; icmp.send_time=(uint)((rdata[32]<<24)+(rdata[33]<<16) +(rdata[34]<<8)+rdata[35]); return icmp; }
public int Send(int lsize){ long send_ticks=0; Byte[] sbuff; if(lsize<=8) return -1004; if(target==null) return -1002; ICMP icmp=new ICMP(lsize); icmp.id=pid; icmp.sq=sq++; if(icmp.sq>=1000) sq=1; QueryPerformanceCounter(ref send_ticks); icmp.send_time=(uint)(send_ticks*1000000.0/freq);//milisec //Console.Write("SendTime:{0} ",icmp.send_time); icmp.oid=oid; sbuff=icmp.ToBuffer(); // /* Console.WriteLine("Send:id={0},sq={1},oid={2},send_time={3}", icmp.id,icmp.sq,icmp.oid,icmp.send_time); */ int nBytes=socket.SendTo(sbuff,sbuff.Length,0,target); if(nBytes!=sbuff.Length){ return -1003; } return 0; }