Fonction de sum de contrôle UDP en Perl

J’ai cherché beaucoup j’ai besoin de la fonction pour calculer la sum de contrôle d’en-tête UDP en Perl.

Je trouve cette fonction en C: http://www.netfor2.com/udpsum.htm Mais je ne sais pas comment l’utiliser et j’essaie de la convertir en Perl mais pas encore

C’est ce que j’essaye:

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] <> 16){ $sum = ($sum & 0xFFFF) + ($sum >> 16); } # Take the one's complement of sum $sum = ~$sum; return $sum; } 

Aidez-moi, s’il vous plaît.

 use List::Util qw( sum ); use Socket qw( IPPROTO_UDP ); sub udp_checksum { my $packed_src_addr = shift; # As 4-char ssortingng, eg as returned by inet_aton. my $packed_dst_addr = shift; # As 4-char ssortingng, eg as returned by inet_aton. my $udp_packet = shift; # UDP header and data as a ssortingng. my $sum = sum( IPPROTO_UDP, length($udp_packet), map({ unpack('n*', $_) } $packed_src_addr, $packed_dst_addr, $udp_packet."\0", # Extra byte ignored if length($udp_packet) is even. ), ); while (my $hi = $sum >> 16) { $sum = ($sum & 0xFFFF) + $hi; } return ~$sum & 0xFFFF; } 

Pour construire un paquet UDP,

 my $udp_packet = pack('nnnna*', $src_port, $dst_port, 8+length($body), 0, $body); my $checksum = udp_checksum($packed_src_addr, $packed_dst_addr, $udp_packet); substr($udp_packet, 6, 2, pack('n', $checksum)); 

Pour vérifier la sum de contrôle d’un paquet UDP,

 udp_checksum($packed_src_addr, $packed_dst_addr, $udp_packet) and die("Invalid checksum\n"); 

Exemple:

 #!/usr/bin/perl use ssortingct; use Socket; use warnings; use List::Util qw(sum); use Socket qw(IPPROTO_UDP); use Socket qw(inet_aton); main(); sub main { my $src_port = 27005; my $dst_port = 27015; my $body = pack("H*", "92380000621f008063d5179000927df3a2e09bf319a66bf300c239aa9393d8eaa244119a"); my $packed_src_addr = inet_aton("156.215.205.76"); my $packed_dst_addr = inet_aton("31.186.251.163"); my $udp_packet = pack('nnnna*', $src_port, $dst_port, 8+length($body), 0, $body); my $checksum = udp_checksum($packed_src_addr, $packed_dst_addr, $udp_packet); print "\n\nUDP Checksum: $checksum\n\n"; } sub udp_checksum { my $packed_src_addr = shift; # As 4-char ssortingng, eg as returned by inet_aton. my $packed_dst_addr = shift; # As 4-char ssortingng, eg as returned by inet_aton. my $udp_packet = shift; # UDP header and data as a ssortingng. my $sum = sum( IPPROTO_UDP, length($udp_packet), map({ unpack('n*', $_) } $packed_src_addr, $packed_dst_addr, $udp_packet."\0", # Extra byte ignored if length($udp_packet) is even. ), ); while (my $hi = $sum >> 16) { $sum = ($sum & 0xFFFF) + $hi; } return ~$sum & 0xFFFF; }