Perl 中的 UDP 校验和函数 [英] UDP Checksum Function in Perl
本文介绍了Perl 中的 UDP 校验和函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我搜索了很多我需要的函数来计算 Perl 中的 UDP 标头校验和.
I have searched much i need function to calculate UDP header checksum in Perl.
我在 C 中找到了这个函数:http://www.netfor2.com/udpsum.htm但我不知道如何使用它,我尝试将其转换为 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;
}
请帮帮我.
推荐答案
use List::Util qw( sum );
use Socket qw( IPPROTO_UDP );
sub udp_checksum {
my $packed_src_addr = shift; # As 4-char string, e.g. as returned by inet_aton.
my $packed_dst_addr = shift; # As 4-char string, e.g. as returned by inet_aton.
my $udp_packet = shift; # UDP header and data as a string.
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;
}
要构建一个 UDP 数据包,
To build a UDP packet,
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));
要验证 UDP 数据包的校验和,
To verify a UDP packet's checksum,
udp_checksum($packed_src_addr, $packed_dst_addr, $udp_packet)
and die("Invalid checksum\n");
示例:
#!/usr/bin/perl
use strict;
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 string, e.g. as returned by inet_aton.
my $packed_dst_addr = shift; # As 4-char string, e.g. as returned by inet_aton.
my $udp_packet = shift; # UDP header and data as a string.
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;
}
这篇关于Perl 中的 UDP 校验和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文