用tcl实现ip头部校验和
上一篇 /
下一篇 2009-05-11 17:57:14
最近测试经常需要组建ip报文,我习惯于使用tcl,但是老愁不知道怎么计算校验和,今天琢磨了半天写出了ip头校验和的tcl实现,供需要的人分享。
//这是需要计算校验和的ip头的16进制表示形式
set Data "45000034921e400080060000c0a80141c0a80140"
proc checksum {Data} { //用要计算校验和的数据的16进制表示形式作为参数,返回校验和
set DataLen [string length $Data]
set sum 0
for {set i 0} {$i < [expr $DataLen - 3] } { incr i 4 } {
set next "0x"
append next [string range $Data $i [expr $i + 3] ]
set sum [expr $sum + $next]
}
if { $i != $DataLen } {
set next "0x"
append next [string range $Data $i end ]
append next [string repeat "0" [expr 4-($DataLen - $i)]]
puts "$next"
set sum [expr $sum + $next]
}
while { [expr $sum >> 16] > 0 } {
set sum [expr ($sum & 0xffff) + ($sum >> 16)]
}
return [expr (~$sum)& 0xffff]
}
puts [checksum $Data]
相关阅读:
- TCPUDP端口测试软件 (GPS软件测试, 2008-7-08)
- 根据IP查询地址 (xfzhang, 2008-7-25)
- IP网络测试技术与方法探讨 (caption, 2008-7-28)
- 脚本语言杂谈 (caption, 2008-7-28)
- TCL集成测试环境搭建步骤 (yanming_huo, 2008-9-20)
- set static ip address (intothestorm, 2008-11-12)
- add the temporary ip address and route (intothestorm, 2008-11-12)
- carol2000的LR学习笔记(2) 虚拟IP的添加- IP spoofer (carol2000, 2008-12-22)
- 已安装Bugzilla的计算机局域网ip更换后,Bugzilla参数的更改 (jlminghui, 2008-12-31)
- TCL脚本语言学习 (gdou_wzy, 2009-3-01)
收藏
举报
TAG:
IP
Tcl
TCP
Tk
UDP
tcl
tk
ip
checksum
udp
tcp