//这个使用来计算校验码的我照抄c#实现ping那文章的方法,反正ip协议计算校验码方法都一样 public syn(uint _ip, ushort _port, EndPoint _ep, Random _rand) { ip = _ip; port = _port; ep = _ep; rand = _rand; ipHeader iph = new ipHeader(); psh = new psdHeader(); tch = new tcpHeader(); sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1); //这2个挺重要,必须这样才可以自己提供ip头 }
//这个是计算校验,把那些类型不一样的全转为16位字节数组用的 public Int32 ipto(ipHeader iph, byte[] Buffer, int size) { Int32 rtn = 0; int index = 0; byte[] b_verlen = new byte[1]; b_verlen[0] = iph.ip_verlen; byte[] b_tos = new byte[1]; b_tos[0] = iph.ip_tos; byte[] b_totallen = BitConverter.GetBytes(iph.ip_totallength); byte[] b_id = BitConverter.GetBytes(iph.ip_id); byte[] b_offset = BitConverter.GetBytes(iph.ip_offset); byte[] b_ttl = new byte[1]; b_ttl[0] = iph.ip_ttl; byte[] b_protol = new byte[1]; b_protol[0] = iph.ip_protocol; byte[] b_checksum = BitConverter.GetBytes(iph.ip_checksum); byte[] b_srcaddr = BitConverter.GetBytes(iph.ip_srcaddr); byte[] b_destaddr = BitConverter.GetBytes(iph.ip_destaddr); Array.Copy(b_verlen, 0, Buffer, index, b_verlen.Length); index += b_verlen.Length; Array.Copy(b_tos, 0, Buffer, index, b_tos.Length); index += b_tos.Length; Array.Copy(b_totallen, 0, Buffer, index, b_totallen.Length); index += b_totallen.Length; Array.Copy(b_id, 0, Buffer, index, b_id.Length); index += b_id.Length; Array.Copy(b_offset, 0, Buffer, index, b_offset.Length); index += b_offset.Length; Array.Copy(b_ttl, 0, Buffer, index, b_ttl.Length); index += b_ttl.Length; Array.Copy(b_protol, 0, Buffer, index, b_protol.Length); index += b_protol.Length; Array.Copy(b_checksum, 0, Buffer, index, b_checksum.Length); index += b_checksum.Length; Array.Copy(b_srcaddr, 0, Buffer, index, b_srcaddr.Length); index += b_srcaddr.Length; Array.Copy(b_destaddr, 0, Buffer, index, b_destaddr.Length); index += b_destaddr.Length; if (index != size /* sizeof(IcmpPacket) */) { rtn = -1; return(rtn); } rtn = index; return(rtn); }
//这个是计算校验,把那些类型不一样的全转为16位字节数组用的 public Int32 ipto(ipHeader iph, byte[] Buffer, int size) { Int32 rtn = 0; int index = 0; byte[] b_verlen = new byte[1]; b_verlen[0] = iph.ip_verlen; byte[] b_tos = new byte[1]; b_tos[0] = iph.ip_tos; byte[] b_totallen = BitConverter.GetBytes(iph.ip_totallength); byte[] b_id = BitConverter.GetBytes(iph.ip_id); byte[] b_offset = BitConverter.GetBytes(iph.ip_offset); byte[] b_ttl = new byte[1]; b_ttl[0] = iph.ip_ttl; byte[] b_protol = new byte[1]; b_protol[0] = iph.ip_protocol; byte[] b_checksum = BitConverter.GetBytes(iph.ip_checksum); byte[] b_srcaddr = BitConverter.GetBytes(iph.ip_srcaddr); byte[] b_destaddr = BitConverter.GetBytes(iph.ip_destaddr); Array.Copy(b_verlen, 0, Buffer, index, b_verlen.Length); index += b_verlen.Length; Array.Copy(b_tos, 0, Buffer, index, b_tos.Length); index += b_tos.Length; Array.Copy(b_totallen, 0, Buffer, index, b_totallen.Length); index += b_totallen.Length; Array.Copy(b_id, 0, Buffer, index, b_id.Length); index += b_id.Length; Array.Copy(b_offset, 0, Buffer, index, b_offset.Length); index += b_offset.Length; Array.Copy(b_ttl, 0, Buffer, index, b_ttl.Length); index += b_ttl.Length; Array.Copy(b_protol, 0, Buffer, index, b_protol.Length); index += b_protol.Length; Array.Copy(b_checksum, 0, Buffer, index, b_checksum.Length); index += b_checksum.Length; Array.Copy(b_srcaddr, 0, Buffer, index, b_srcaddr.Length); index += b_srcaddr.Length; Array.Copy(b_destaddr, 0, Buffer, index, b_destaddr.Length); index += b_destaddr.Length; if (index != size/* sizeof(IcmpPacket) */) { rtn = -1; return rtn; } rtn = index; return rtn; }