//这个使用来计算校验码的我照抄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头 }
//这个是把tcp伪首部转为字节数组用的 public Int32 tchto(tcpHeader tch, byte[] buffer, int size) { Int32 rtn; int index = 0; byte[] b_tch_sport = BitConverter.GetBytes(tch.th_sport); byte[] b_tch_dport = BitConverter.GetBytes(tch.th_dport); byte[] b_tch_seq = BitConverter.GetBytes(tch.th_seq); byte[] b_tch_ack = BitConverter.GetBytes(tch.th_ack); byte[] b_tch_lenres = new byte[1]; b_tch_lenres[0] = tch.th_lenres; byte[] b_tch_flag = new byte[1]; b_tch_flag[0] = tch.th_flag; byte[] b_tch_win = BitConverter.GetBytes(tch.th_win); byte[] b_tch_sum = BitConverter.GetBytes(tch.th_sum); byte[] b_tch_urp = BitConverter.GetBytes(tch.th_urp); Array.Copy(b_tch_sport, 0, buffer, index, b_tch_sport.Length); index += b_tch_sport.Length; Array.Copy(b_tch_dport, 0, buffer, index, b_tch_dport.Length); index += b_tch_dport.Length; Array.Copy(b_tch_seq, 0, buffer, index, b_tch_seq.Length); index += b_tch_seq.Length; Array.Copy(b_tch_ack, 0, buffer, index, b_tch_ack.Length); index += b_tch_ack.Length; Array.Copy(b_tch_lenres, 0, buffer, index, b_tch_lenres.Length); index += b_tch_lenres.Length; Array.Copy(b_tch_flag, 0, buffer, index, b_tch_flag.Length); index += b_tch_flag.Length; Array.Copy(b_tch_win, 0, buffer, index, b_tch_win.Length); index += b_tch_win.Length; Array.Copy(b_tch_sum, 0, buffer, index, b_tch_sum.Length); index += b_tch_sum.Length; Array.Copy(b_tch_urp, 0, buffer, index, b_tch_urp.Length); index += b_tch_urp.Length; if (index != size) { rtn = -1; return(rtn); } else { rtn = index; return(rtn); } }
//这个是把tcp伪首部转为字节数组用的 public Int32 tchto(tcpHeader tch, byte[] buffer, int size) { Int32 rtn; int index = 0; byte[] b_tch_sport = BitConverter.GetBytes(tch.th_sport); byte[] b_tch_dport = BitConverter.GetBytes(tch.th_dport); byte[] b_tch_seq = BitConverter.GetBytes(tch.th_seq); byte[] b_tch_ack = BitConverter.GetBytes(tch.th_ack); byte[] b_tch_lenres = new byte[1]; b_tch_lenres[0] = tch.th_lenres; byte[] b_tch_flag = new byte[1]; b_tch_flag[0] = tch.th_flag; byte[] b_tch_win = BitConverter.GetBytes(tch.th_win); byte[] b_tch_sum = BitConverter.GetBytes(tch.th_sum); byte[] b_tch_urp = BitConverter.GetBytes(tch.th_urp); Array.Copy(b_tch_sport, 0, buffer, index, b_tch_sport.Length); index += b_tch_sport.Length; Array.Copy(b_tch_dport, 0, buffer, index, b_tch_dport.Length); index += b_tch_dport.Length; Array.Copy(b_tch_seq, 0, buffer, index, b_tch_seq.Length); index += b_tch_seq.Length; Array.Copy(b_tch_ack, 0, buffer, index, b_tch_ack.Length); index += b_tch_ack.Length; Array.Copy(b_tch_lenres, 0, buffer, index, b_tch_lenres.Length); index += b_tch_lenres.Length; Array.Copy(b_tch_flag, 0, buffer, index, b_tch_flag.Length); index += b_tch_flag.Length; Array.Copy(b_tch_win, 0, buffer, index, b_tch_win.Length); index += b_tch_win.Length; Array.Copy(b_tch_sum, 0, buffer, index, b_tch_sum.Length); index += b_tch_sum.Length; Array.Copy(b_tch_urp, 0, buffer, index, b_tch_urp.Length); index += b_tch_urp.Length; if (index != size) { rtn = -1; return rtn; } else { rtn = index; return rtn; } }