Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

RE: Simple Construction of a RAW TCP/IP Packet.

by JanneVee (Friar)
on Jun 12, 2000 at 17:56 UTC ( [id://17703] : note . print w/replies, xml ) Need Help??

in reply to Simple Construction of a RAW TCP/IP Packet.

This is an interesting insight to a TCP/IP packet... And actually I consider this example simpler than the C examples.
  • Comment on RE: Simple Construction of a RAW TCP/IP Packet.

Replies are listed 'Best First'.
RE: RE: Simple Construction of a RAW TCP/IP Packet.
by cleen (Pilgrim) on Jun 13, 2000 at 04:08 UTC
    The fact is, perl is the bomb, I havnt really been given a good example why C is so much better then perl, Ive heard people say it, but never been given a REASONABLE example, they claim efficiency, less process time, bah, in either case, if the programmer doesnt know what they are doing they are going to be in-efficient. Reality: Perl can be faster, and more efficient then C if it is written properly.

    on a side note, I am in the process of writing more raw socket examples for perl monks, IE constructing ICMP packets (what I am currently working on), to even constructing multicast packets...Its really fun to do, really it is :)

      I'm just waiting to see and excited to see the end result of that. Crazy idea: a traceroute perlscript!
      you really do a great job. i'm looking forward to some work for same thing. can i communicate with you by email? Thanks my name: haijun nation: China
Re^2: Simple Construction of a RAW TCP/IP Packet.
by tahirfattani (Initiate) on Jul 21, 2014 at 12:50 UTC

    I am sending a packets from client machine to server machine. I have 4 dynamic fields i.e. source IP address ,Source Port , Destination IP address and Destination Port All four fields are calling from file. It means every times generating new IP packets which have different IP addresses and Ports. Question is that, For example If I want to send the 100,000 packets per second for the duration of 10 seconds.So my total send packets is 1 Million. But they are sending not more than 26 thousands. I am using one variable DIM...for suppose I change this field like that $dim=1; instead of $dim= scalar(@lines); then it is perfectly work and send 1 Million Packets(but everytime same packets which is not my requirement). Itz means my program can generate 1 Million packets. I want to generate the different packets .Please help me where I am lacking???

    #! /usr/bin/perl -s use Net::RawSock; use NetPacket::IP; use NetPacket::TCP; use Time::HiRes qw(usleep gettimeofday tv_interval); use feature qw/say/; use Benchmark; if(!defined $n or !defined $secs ) { print "Usage: ./ -n=<number of packets/sec> -secs=< +number of seconds> \n"; exit; } print "\nCreating Packets ...."; open (FILE, '/home/tahir/Downloads/packet_generator.txt'); chomp (@lines = (<FILE>)); close(FILE); ## Create IP my $ip_obj = NetPacket::IP->decode(''); ## Init IP $ip_obj->{ver} = 4; $ip_obj->{hlen} = 5; $ip_obj->{tos} = 0; $ip_obj->{id} = 0x1d1d; $ip_obj->{ttl} = 0x5a; $ip_obj->{flags} = 2; $ip_obj->{len} = 84; ## Create TCP my $tcp_obj = NetPacket::TCP->decode(''); ## Init TCP $tcp_obj->{hlen} = 5; $tcp_obj->{winsize} = 0x8e30; $tcp_obj->{seqnum} = 0xFFFF; $tcp_obj->{acknum} = 0xCCCC; $tcp_obj->{flags} = SYN; $tcp_obj->{data} =ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn; ## Assemble $ip_obj->{proto} = 6; $ip_obj->{data} = $tcp_obj->encode($ip_obj); $num =0; for ($num = 0; $num<=@lines; $num++) { $line=$lines[$num]; #for every line in the lines #build packet my($sip, $sport, $dip, $dport) = split(/\s+/, $line); + $ip_obj->{src_ip} = $sip; $tcp_obj->{src_port} = $sport; $ip_obj->{dest_ip} = $dip; $tcp_obj->{dest_port} = $dport; #print ": $sip\t"; #print ": $sport\t"; #print " : $dip\t"; #print ": $dport\n"; ## Create RAW @packets[$num] = $ip_obj->encode; #say $lines[$j]; #add packet to a vector of packets #end for #Now we have an array of packets } #you want n pkt/sec for 10 seconds, so #you want n/1000 pkt per 1/1000 sec $num=0; $count = 0; $pkts_per_ms = $n / 1000; $start = time(); #[Time::HiRes::gettimeofday]; $dim= scalar(@lines); # $TEST=$packets[0]; while ( ((time()-$start)<($secs)) || ($count < ($n*$secs))) { $before = [Time::HiRes::gettimeofday]; for ($i=$pkts_per_ms;$i>0;$i--) { $num=($num+1)% $dim ; #if now packets is the array of packets,you will send Net::RawSock::write_ip($packets[$num]); # Net::RawSock::write_ip($TEST); } while (tv_interval($before)<0.001) { #print tv_interval($before), " "; } $count = $count + $pkts_per_ms; } print "TIME: ", time()-$start, "\n"; #print "You took ", tv_interval($start), " seconds for sending $co +unt packets.\n"; print "You took ", time() - $start, " seconds for sending $count p +ackets.\n";