Hello……I am working on voice transmission control through linux as my project in high school……I need to know if the comments I have made on the following program are correct….
Object of the perl program is to build a network analyzer that captures local area network (LAN) traffic destined for the local domain name system (DNS) server and logs information on which IP addresses request which IP name resolutions.
Object of the perl program is to build a network analyzer that captures local area network (LAN) traffic destined for the local domain name system (DNS) server and logs information on which IP addresses request which IP name resolutions.
Code:
<1 #!/usr/bin/perl -w 2 use 5.6.0; # Change to 5.006_000 if using Perl 5.8.0. 3 use strict; 4 5 use constant DNS_PORT => 53;# The DNS PORT is the standard protocol port number for DNS 6 use constant HOWMANY => 100;# default number of packets to capture. 7 8 use Net::DNS::Packet;# A Net::DNS::Packet object represents a DNS packet. 9 use Net::PcapUtils;# Net::PcapUtils is a module to sit in front of Net::Pcap in order to hide some of the pcap(3) initialisation by providing sensible defaults. This enables a programmer to easily write small, specific scripts for a particular purpose without having to worry about too many details. 10 use NetPacket::Ethernet qw( :strip ); 11 use NetPacket::IP;# NetPacket::IP provides a set of routines for assembling and disassembling packets using IP (Internet Protocol) 12 use NetPacket::UDP; # NetPacket::UDP provides a set of routines for assembling and disassembling packets using UDP (User Datagram Protocol). 13 14 our $num_processed = 0; # declare and define a global variable, $num processed, used to record the number of packets processed. 15 16 sub got_a_packet { 17 my $handle = shift ;# assigning the two parameters to $handle and $packet to append the results to the log file using $handle and use the contents of $packet to create a NetPacket::IP object. 18 my $packet = shift; 19 20 my $ip_datagram = NetPacket::IP->decode( 21 NetPacket::Ethernet::eth_strip( $packet ) ); # this subroutine removes the Ethernet frame from $packet and returns the IP datagram, and NetPacket::IP::decode extracts the IP data portion. 22 23 my $udp_datagram = NetPacket::UDP->decode( $ip_datagram->{data} );# extracting UDP datagram 24 25 if ( $udp_datagram->{dest_port} == DNS_PORT ) # ensuring that the destination port matches DNS PORT 26 { 27 my $dns_packet = $udp_datagram->{data};# assign the data portion of the UDP datagram to $dns packet. 28 my $dns_decode = Net::DNS::Packet->new( \$dns_packet );#decode the data portion from the IP packet 29 my @questions = $dns_decode->question; # extract the DNS queries from that and store them in @questions. 30 31 foreach my $q ( @questions ) # process each DNS query. 32 { 33 my $question = $q->string; # get the current query in stringified form, 34 35 unless ( $question =~ /in-addr\.arpa/ ) # skip queries that match in-addr.arpa since those relate to IP addresses, not names. 36 { 37 $question =~ /^(.+)\tIN/; 38 39 print "$ip_datagram->{src_ip} -> "; # 39-44 outputs the source and destination IP addresses together with the IP name to the screen and log-file. 40 print "$ip_datagram->{dest_ip}: "; 41 print "$1\n"; 42 print $handle "$ip_datagram->{src_ip} -> "; 43 print $handle "$ip_datagram->{dest_ip}: "; 44 print $handle "$1\n"; 45 46 $num_processed++;# increment $num processed 47 } 48 }48 } 49 } 50 } 51 52 sub display_results { #calling a ssubroutine 53 my $outof = shift; #no. of packets actually processed in $outof 54 55 print "\nProcessed $num_processed (out of $outof) "; #prints the results 56 print "UDP datagrams carrying DNS.\n\n"; 57 } 58 59 my $count = shift || HOWMANY; # sets the number of packets to process. If a command line argument isn’t provided, it uses the value of HOWMANY 60 my $rem_count = $count; 61 my $pkt_descriptor = Net::PcapUtils::open( # it places the Ethernet card into promiscuous mode for packet capturing 62 FILTER => ’udp’, #DNS uses UDP as a protocol filter 63 SNAPLEN => 1500 ); # 1500 bytes is the maximum payload size on Ethernet networks 64 65 if ( !ref( $pkt_descriptor ) ) # $pkt descriptor is reference to a valid packet capture descriptor 66 { 67 warn "Net::PcapUtils::open returned: $pkt_descriptor\n"; # If it fails it returns an error message and then exit 68 exit; 69 } 70 71 open WDW_FILE, ">>wdw_log.txt" 72 or die "Could not append to wdw_log.txt: $!\n"; # open the log file in append mode 73 74 print WDW_FILE "\n", scalar localtime, " - wdw BEGIN run.\n\n"; #timestamp it at the beginning of the run 75 76 while ( $count) #This subroutine waits for a UDP packet then returns two values—a scalar which represents the raw Ethernet packet, which I store in $packet, and a hash, which is of no nee 77 { 78 my ( $packet, %header ) = Net::PcapUtils::next( $pkt_descriptor ); #call the Net::PcapUtils::next subroutine with packet capture descriptor in $pkt descriptor, saves the UDP packet in $packet 79 got_a_packet( *WDW_FILE, $packet ); #sending the output from got a packet to this file 80 $count--; 81 } 82 83 print WDW_FILE "\n", scalar localtime, " - wdw END run.\n"; #timestamp it at the beginning of the run 84 close WDW_FILE; #closes the file 85 86 display_results( $rem_count ); #prints the initial value of $count before the while loop started at line 76>
Comment