use Socket; use strict; socket(SOCK,PF_INET,SOCK_RAW,getprotobyname('icmp')) || die $!; my $checksum = 0; use constant ICMP_ECHOREPLY => 0; use constant ICMP_ECHO => 8; use constant ICMP_STRUCT => "C2 n3"; use constant SUBCODE => 0; use constant ICMP_FLAGS => 0; use constant ICMP_PORT => 0; my $seq=1; my $msg = pack(ICMP_STRUCT,ICMP_ECHO,SUBCODE,$checksum,$$,$seq); my $chks = &ceksum($msg); $msg = pack(ICMP_STRUCT,ICMP_ECHO,SUBCODE,$chks,$$,$seq); my $host = sockaddr_in(ICMP_PORT,inet_aton('192.168.0.1')); send(SOCK,$msg,ICMP_FLAGS,$host) || warn "$! \n"; sub ceksum { 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_msg % 2; $chk = ($chk >> 16) + ($chk & 0xffff); return(~(($chk >> 16) + $chk) & 0xffff); } #### use Socket; socket(SOCK,PF_INET,SOCK_RAW,getprotobyname('icmp')) || die "Error: $!\n"; $msg = pack("C2 n3",8,0,32127,0,1); print "the value of checksum is:", unpack('H4', pack('n',32127)), "\n"; print "[And you should see this in your tcpdump -i lo icmp -xxx]\n"; send(SOCK,$msg,0,sockaddr_in(0,inet_aton('localhost'))) || die "Error: $! \n";