Tcp网络协议计算校验和,以及包生成工具

  • Post author:
  • Post category:其他


这是大学计算机网络的一个课程设计:

//((想要源代码或者有问题的小伙伴可以私聊我,我是拿js嵌html实现的,不用配置环境))

TCP校验和是一个端到端的校验和,由发送端计算,然后由接收端验证。其目的是为了发现TCP首部和数据在发送端到

接收端之间发生的任何改动。如果接收方检测到校验和有差错,则TCP段会被直接丢弃。

TCP校验和覆盖TCP首部和TCP数据,而IP首部中的校验和只覆盖IP的首部,不覆盖IP数据报中的任何数据。

TCP的校验和是必需的,而UDP的校验和是可选的。TCP和UDP计算校验和时,都要加上一个12字节的伪首部。



伪首部

伪首部共有12字节,包含如下信息:源IP地址、目的IP地址、保留字节(置0)、传输层协议号(TCP是6)、TCP报文长度(报头+数据)。

伪首部是为了增加TCP校验和的检错能力:如检查TCP报文是否收错了(目的IP地址)、传输层协议是否选对了(传输层协议号)等。

运行截图:

简单的算法实现如下:


<


script




type


=


“text/javascript”


>




// $(document).ready(function () {




// var ip = $(“#yuanip”).val();




//     alert(ip);




// });




$


(


document


).


ready


(


function


() {




$


(


“#jiaoyan”


).


click


(


function


() {




//32网络地址数据部分取值转换




function




bitchange


(


number_1


,


number_2


) {




var




change


;




number_1


=


parseInt


(


number_1


,


10


);




number_2


=


parseInt


(


number_2


,


10


);




number_2


= (


“000000000”


+


number_2


.


toString


(


2


)).


substr


(-


8


);




change


=


number_1


.


toString


(


2


) +


number_2


;




change


=


parseInt


(


change


,


2


);




return




change


;


}




//tcp Data




function




addzero


(


tcp


) {




var




tcpdatasum


=


0


;




var




tcpsum


=


0


;




for


(


var




i


=


0


;


i


<


tcp


.


length


+


1


;


i


++) {




if


(


i


%


4


==


0


) {




tcpdatasum


=


parseInt


(


tcpdatasum


,


16


);




tcpsum


=


tcpsum


+


tcpdatasum


;




tcpdatasum


=


0


;




tcpdatasum


+=


tcp


.


charAt


(


i


);


}




else


{




tcpdatasum


+=


tcp


.


charAt


(


i


);


}




// tcpdatasum = (tcpdatasum + “0000”).substr(-4);




// tcpdatasum = parseInt(tcpdatasum,16);




// alert(tcpdatasum);


}




tcpdatasum


=


parseInt


(


tcpdatasum


,


16


);




tcpdatasum


=


tcpdatasum


.


toString


(


16


);




tcpdatasum


= (


tcpdatasum


+


“0000”


).


substr


(


0


,


4


);




tcpdatasum


=


parseInt


(


tcpdatasum


,


16


);




alert


(


tcpsum


+


tcpdatasum


);




return




tcpsum


+


tcpdatasum


;


}




//校验和计算并




function




checksum


(


sum


) {




sum


= (


sum


>>


16


) + (


sum


&


0xffff


);




sum


+= (


sum


>>


16


);




return




sum


;


}




//数组循环相加




function




getSum


() {




var




sum


=


0


;




for


(


var




i


=


0


;


i


<


data


.


length


;


i


++) {




sum


+=


parseInt


(


data


[


i


],


10


);


}




return




sum


;


}




//32bit change 16bit




function




bitchange_2


(


data


) {




var




n


=


0


;




var




n_1


=


“”


;




var




n_2


=


“”


;




data


=


parseInt


(


data


,


10


);




data


=


data


.


toString


(


2


);




data


= (


“00000000000000000000000000000000”


+


data


).


substr


(-


32


);




for


(


var




i


=


0


;


i


<


data


.


length


;


i


++) {




if


(


i


<


16


) {




n_1


+=


data


.


charAt


(


i


);


}




else


{




n_2


+=


data


.


charAt


(


i


);


}


}




n_1


=


parseInt


(


n_1


,


2


);




return




n_1


;


}




function




bitchange_3


(


data


) {




var




n


=


0


;




var




n_1


=


“”


;




var




n_2


=


“”


;




data


=


parseInt


(


data


,


10


);




data


=


data


.


toString


(


2


);




data


= (


“00000000000000000000000000000000”


+


data


).


substr


(-


32


);




for


(


var




i


=


0


;


i


<


data


.


length


;


i


++) {




if


(


i


<


16


) {




n_1


+=


data


.


charAt


(


i


);


}




else


{




n_2


+=


data


.


charAt


(


i


);


}


}




n_2


=


parseInt


(


n_2


,


2


);




return




n_2


;


}




//生成报文




function




baowen


() {




var




bw


=


“”


;




var




bw_1


=


“”


;




for


(


var




i


=


0


;


i


<


data


.


length





1


;


i


++) {




// sum += parseInt(data[i], 10);




// sum = checksum(sum);




data


[


i


] =


parseInt


(


data


[


i


],


10


);




data


[


i


] = (


“0000”


+


data


[


i


].


toString


(


16


)).


substr


(-


4


);




bw


=


bw


+


data


[


i


];


}




return




bw


+


sum


+


tcp


;


}




//报文字符串格式化




function




baowenzl


() {




var




bw_2


=


“”


;




for


(


var




i


=


0


;


i


<


a


.


length


;


i


++) {




if


(


i


%


2


!=


0


) {




bw_2


=


bw_2


+


a


.


charAt


(


i


) +


” ”


;


}




else




bw_2


=


bw_2


+


a


.


charAt


(


i


);


}




return




bw_2


;


}




var




data


=


new




Array


();




var




yuanip1


=


$


(


“#yuanip1”


).


val


();




var




yuanip2


=


$


(


“#yuanip2”


).


val


();




var




yuanip3


=


$


(


“#yuanip3”


).


val


();




var




yuanip4


=


$


(


“#yuanip4”


).


val


();




var




mudiip1


=


$


(


“#mudiip1”


).


val


();




var




mudiip2


=


$


(


“#mudiip2”


).


val


();




var




mudiip3


=


$


(


“#mudiip3”


).


val


();




var




mudiip4


=


$


(


“#mudiip4”


).


val


();




data


[


4


] =


$


(


“#xieyitype”


).


val


();




data


[


5


] =


$


(


“#tcplength”


).


val


();




data


[


6


] =


$


(


“#yuanport”


).


val


();




data


[


7


] =


$


(


“#mudiport”


).


val


();




var




bit32_1


=


$


(


“#number”


).


val


();




var




bit32_2


=


$


(


“#acknumber”


).


val


();




one


=


$


(


“#shift”


).


val


();




two


=


$


(


“#urg”


).


val


();




three


=


$


(


“#ack”


).


val


();




four


=


$


(


“#psh”


).


val


();




five


=


$


(


“#rst”


).


val


();




six


=


$


(


“#syn”


).


val


();




seven


=


$


(


“#fin”


).


val


();




data


[


13


] =


$


(


“#window”


).


val


();




// data[12] = $(“#finger”).val();




var




tcp


=


$


(


“#tcp”


).


val


();




data


[


0


] =


bitchange


(


yuanip1


,


yuanip2


);




data


[


1


] =


bitchange


(


yuanip3


,


yuanip4


);




data


[


2


] =


bitchange


(


mudiip1


,


mudiip2


);




data


[


3


] =


bitchange


(


mudiip3


,


mudiip4


);





//一堆乱七八糟的数据移位




data


[


5


] =


parseInt


(


data


[


5


],


10


) + (


tcp


.


length


/


2


) ;




$


(


“#tcplength”


).


val


(


data


[


5


]);




data


[


8


] =


bitchange_2


(


bit32_1


);




data


[


9


] =


bitchange_3


(


bit32_1


);




data


[


10


] =


bitchange_2


(


bit32_2


);




data


[


11


] =


bitchange_3


(


bit32_2


);




one


=


parseInt


(


one


,


10


);




one


=


one


.


toString


(


2


);




var




sum_5


=


one


+


“000000”


+


two


+


three


+


four


+


five


+


six


+


seven


;




sum_5


=


parseInt


(


sum_5


,


2


);




data


[


12


] =


sum_5


;




data


[


14


] =


addzero


(


tcp


);




// var wsum = parseInt(data[0],10) + parseInt(data[1],10) + parseInt(data[2],10) + parseInt(data[3],10) + parseInt(data[4],10)  + parseInt(data[6],10) + parseInt(data[5],10);




// var sum =   parseInt(data[5],10) + parseInt(data[7],10) + parseInt(data[8],10) + parseInt(data[9],10) + parseInt(data[10],10) + parseInt(data[11],10) + parseInt(data[12],10) + parseInt(data[13],10) + parseInt(data[14],10) + parseInt(data[15],10) + parseInt(data[16],10) + parseInt(data[17],10) + parseInt(data[18],10) + parseInt(data[19],10);




// var zsum = wsum +sum +parseInt(data[20],10);




var




sum


=


getSum


(


data


);




sum


=


checksum


(


sum


);




sum


=


parseInt


(


sum


,


10


);




sum


=


0xffff





sum


;




// var sum_2 = (“0000000000000000” + sum.toString(2)).substr(-16);




// var wsum = getSum();




// alert(wsum);




// alert(sum);




// alert(zsum);




// sum = checksum(zsum);




sum


=


sum


.


toString


(


16


);




$


(


“#checksum_1”


).


val


(


sum


);




// sum = parseInt(sum,10);




// sum = 0xffff-sum;




// sum = sum.toString(2);




// sum = (‘000000000000000’+sum).slice(-16);




// alert(sum);





var




a


=


baowen


(


data


);




a


=


baowenzl


(


a


);




$


(


“#jyprint”


).


val


(


a


);


});


});


<


/


script


>



版权声明:本文为NightChenRight原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。