Per RFC792 (http://www.faqs.org/rfcs/rfc792.html):
"The checksum is the 16-bit ones's complement of the one's complement sum of the ICMP message starting with the ICMP Type. For computing the checksum , the checksum field should be zero. This checksum may be replaced in the future."
Therefore, simply replace your '$chk += $short;' with '$chk ^= $short;' and it should work.
BTW: Thanks for the code! :-)
Happy Hacking! | [reply] |
thanks before,
actually this script's working fine for localhost, and i got a reports from some ppl that this script is working fine too in their LAN, just to make sure i remade this script (well..actually it's only copied and pasted from ping.pm)..
##Update-2##
Well, i just found out that if we send single icmp packet to LOCALHOST we dont need a 'right value' for checksum, i mean we can put any numbers into $checksum, so for Localhost we dont need sub checksum
##EndOfUpdate-2##
use strict;
use Socket;
ping_icmp();
use constant ICMP_ECHOREPLY => 0; # ICMP packet types
use constant ICMP_ECHO => 8;
use constant ICMP_STRUCT => "C2 n3 A"; # Structure of a minimal ICMP p
+acket
use constant SUBCODE => 0; # No ICMP subcodefor ECHO and ECHOREPLY
use constant ICMP_FLAGS => 0; # No special flags for send or recv
use constant ICMP_PORT => 0; # No port with ICMP
sub ping_icmp
{
my ($ip) = inet_aton('192.168.0.2');
my ($saddr, $checksum, $msg, $len_msg);
my $seq = 1;
my $pid = $$ & 0xffff;
$checksum = 0; # No checksum for starters
my $data= "";
$msg = pack(ICMP_STRUCT . 0,ICMP_ECHO,SUBCODE,$checksum,$pid,$seq,$d
+ata);
$checksum =checksum($msg);
$msg = pack(ICMP_STRUCT . 0,ICMP_ECHO,SUBCODE,$checksum,$pid,$seq,$d
+ata);
$len_msg = length($msg);
$saddr = sockaddr_in(ICMP_PORT, $ip);
socket(SOCK,PF_INET,SOCK_RAW,getprotobyname('icmp'));
send(SOCK,$msg,ICMP_FLAGS,$saddr);
}
sub checksum{
my ($msg) = @_;
my ($len_msg,$num_short,$short,$chk);
$len_msg = length($msg);
$num_short = int($len_msg / 2);
$chk = 0;
foreach $short (unpack("n$num_short", $msg)) {
$chk += $short;
}
$chk += (unpack("C", substr($msg, $len_msg - 1, 1)) << 8) if $len_ms
+g % 2;
$chk = ($chk >> 16) + ($chk & 0xffff); # Foldhigh into low
return(~(($chk >> 16) + $chk) & 0xffff); # Again and complement
}
would you mind to test this script in your LAN and send me a report?
nb : maybe u can add this lil code (thanks to satuspam aka edwin pramono from id-perl yahoogroup)..
***UPDATE***
Because i still got weird result from this code (below), i'll strike this code
my $buff;
defined(recv(SOCK,$buff,256,0)) or die "error: $! \n";
print "received: ", length($buff), " bytes\n";
my($type,$subcode,$cksum,$recv_pid,$recv_seq)=unpack(ICMP_STRUCT,sub
+str($buff,20));
if($type == ICMP_ECHOREPLY) {
if($recv_pid != $pid) {
print "icmp reply ignored (different pid) \n";
}
elsif($recv_seq != $seq) {
print "icmp reply ignored (different seq) \n";
}
else {print "got icmp reply \n"};
}
else {
print "not an icmp reply\n";
}
}
thank you | [reply] [d/l] [select] |