我在Perl搜索了计算民主力量头盔所需的大量职能。
I find this function in C: http://www.netfor2.com/udpsum.htm But i don t know how to use it and i try to convert it to Perl but not managed
这样做是为了:
sub udp_checksum(my $len_udp, my @src_addr, my @dest_addr, my $padding, my @buff)
{
my $proto_udp = 17;
my $padd = 0;
my $word16;
my $sum;
# Find out if the length of data is even or odd number. If odd,
# add a padding byte = 0 at the end of packet
if ($padding&1==1){
$padd = 1;
$buff[$len_udp] = 0;
}
# initialize sum to zero
$sum = 0;
# make 16 bit words out of every two adjacent 8 bit words and
# calculate the sum of all 16 bit words
for (my $i = 0; $i < $len_udp + $padd; $i = $i + 2){
$word16 =(($buff[i] << 8) & 0xFF00) + ($buff[i+1] & 0xFF);
$sum = $sum + $word16;
}
# add the UDP pseudo header which contains the IP source and destinationn addresses
for (my $i = 0; $i < 4; $i = $i + 2){
$word16 =(($src_addr[i] << 8) & 0xFF00) + ($src_addr[i+1] & 0xFF);
$sum = $sum + $word16;
}
for (my $i = 0; $i < 4; $i = $i + 2){
$word16 =(($dest_addr[i] << 8) & 0xFF00) + ($dest_addr[i+1] & 0xFF);
$sum = $sum + $word16;
}
# the protocol number and the length of the UDP packet
$sum = $sum + $prot_udp + $len_udp;
# keep only the last 16 bits of the 32 bit calculated sum and add the carries
while ($sum >> 16){
$sum = ($sum & 0xFFFF) + ($sum >> 16);
}
# Take the one s complement of sum
$sum = ~$sum;
return $sum;
}
请帮助我。