计算UDP校验和的代码如下:
def do_checksum(self, source_string): """ Verify the packet interitity """ sum = 0 max_count = (len(source_string) / 2) * 2 count = 0 while count < max_count: val = ord(source_string[count + 1]) * 256 + ord(source_string[count]) sum = sum + val sum = sum & 0xffffffff count = count + 2 if max_count < len(source_string): sum = sum + ord(source_string[len(source_string) - 1]) sum = sum & 0xffffffff sum = (sum >> 16) + (sum & 0xffff) sum = sum + (sum >> 16) answer = ~sum answer = answer & 0xffff answer = answer >> 8 | (answer << 8 & 0xff00) return answer
UDP的校验和在计算时遇到的任何溢出都会被回卷,那么问题来了:sum = sum & 0xffffffff这条语句应该是直接把溢出舍去了?
所以这段代码对么。。。
付费偷看金额在0.1-10元之间
一周热门 更多>