Class: Scriptroute::Tulip::Train2

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptroute/tulip/helper.rb

Overview

train2 ##############

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsts, numpackets, intratrain, types, ttls, psizes, numtrains, intertrain) ⇒ Train2

Returns a new instance of Train2.



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/scriptroute/tulip/helper.rb', line 475

def initialize(dsts, numpackets, intratrain, types, ttls, psizes, numtrains, intertrain) 
  @dsts, @numpackets, @intratrain, @types, @ttls, @psizes, @numtrains, @intertrain = 
    dsts, numpackets, intratrain,   types,  ttls,  psizes,  numtrains,  intertrain;
  @reverse_ttls, @rem_srcs, @num_losses, @packets = Hash.new(), Hash.new(), Array.new(), Array.new();

  ##forget the MAX_TRAIN complication for this part;

  delayedPackets = Array.new();
  (0..@numtrains-1).each { |i|
    delay = (i==0)? 0 : intertrain;
    (0..@numpackets-1).each { |j|
	probe = getProbe(i,j);
	#puts "#{i} #{delay}\n";
	delayedPackets.push(Struct::DelayedPacket.new(delay, probe));
	delay = intratrain;
	delay = (j==@numpackets-2)? 0.003 : 0.0005;
    }
  }
  @responsesPackets = Scriptroute::send_train(delayedPackets);

  ##check sanity of the train and insert it into @packets and @num_losses
  (0..@numtrains-1).each { |i|
    @packets[i] = Array.new();
    @num_losses[i] = 0;
    (0..@numpackets-1).each  { |j|
	index = i*@numpackets + j;
	pkt = @responsesPackets[index];
	@packets[i].push(pkt);

	dst = (@dsts[j])? @dsts[j] : @dsts[0];
	ttl = (@ttls[j])? @ttls[j] : @ttls[0];
	key = "#{dst}::#{ttl}";
	if (!@reverse_ttls[key]) 
 @reverse_ttls[key] = Hash.new(0);
 @rem_srcs[key] = Hash.new(0);
	end

	if (pkt) 
 if (pkt.probe and pkt.probe.time)
   if (pkt.response)
     rttl = pkt.response.packet.ip_ttl;
     src = pkt.response.packet.ip_src;
     @reverse_ttls[key][rttl] += 1;
     @rem_srcs[key][src] += 1;
   else
     @num_losses[i]+=1;
   end
   ##check the probe sort order
   npkt = @responsesPackets[index+1];
   if (npkt and npkt.probe and npkt.probe.time and pkt.probe.time > npkt.probe.time) 
     STDERR.puts "ERROR: packets not in probe sorted order. \n%s %d %.3f\n%s %d %.3f\n" % 
[pkt.probe.packet.ip_dst, pkt.probe.packet.ip_ttl, pkt.probe.time.to_f*1000, 
npkt.probe.packet.ip_dst, npkt.probe.packet.ip_ttl, npkt.probe.time.to_f*1000];
   end
 end
	else
 raise "ERROR: there are null responses\n";
	end
    }
  }
  
  @reverse_ttls.each_key { |key| 
    if (@reverse_ttls[key].keys.length > 1) 
	puts "WARNING: too many reverse TTLs for #{key}. #{@reverse_ttls[key].keys.join(" ")} #{@reverse_ttls[key].values.join(" ")} ";
    end
    
    if (@rem_srcs[key].keys.length > 1) 
	puts "WARNING: too many sources. #{@rem_srcs[key].keys.join(" ")} #{@rem_srcs[key].values.join(" ")} ";
    end
  }
	
end

Instance Attribute Details

#dstsObject (readonly)

Returns the value of attribute dsts.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def dsts
  @dsts
end

#intertrainObject (readonly)

Returns the value of attribute intertrain.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def intertrain
  @intertrain
end

#intratrainObject (readonly)

Returns the value of attribute intratrain.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def intratrain
  @intratrain
end

#num_lossesObject (readonly)

Returns the value of attribute num_losses.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def num_losses
  @num_losses
end

#numpacketsObject (readonly)

Returns the value of attribute numpackets.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def numpackets
  @numpackets
end

#numtrainsObject (readonly)

Returns the value of attribute numtrains.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def numtrains
  @numtrains
end

#packetsObject (readonly)

Returns the value of attribute packets.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def packets
  @packets
end

#psizesObject (readonly)

Returns the value of attribute psizes.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def psizes
  @psizes
end

#reverse_ttlsObject (readonly)

Returns the value of attribute reverse_ttls.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def reverse_ttls
  @reverse_ttls
end

#ttlsObject (readonly)

Returns the value of attribute ttls.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def ttls
  @ttls
end

#typesObject (readonly)

Returns the value of attribute types.



436
437
438
# File 'lib/scriptroute/tulip/helper.rb', line 436

def types
  @types
end

Instance Method Details

#getProbe(i, j) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/scriptroute/tulip/helper.rb', line 438

def getProbe (i,j)
  dst = (@dsts[j])? @dsts[j] : @dsts[0];
  ttl = (@ttls[j])? @ttls[j] : @ttls[0];
  type = (@types[j])? @types[j] : @types[0];
  psize = (@psizes[j])? @psizes[j] : @psizes[0];
  
  probe = nil;
  case type
  when "udp"
    psize = (psize==-1)? 12 : psize;
    probe = Scriptroute::UDP.new(psize);
    probe.uh_dport = 33444;
  when "tcp"
    psize = (psize==-1)? 0 : psize;
    probe = Scriptroute::TCP.new(psize);
    probe.th_dport=80;
    probe.th_win=1024;
  else # "echo" or "tstamp"
    psize = (psize==-1)? 0 : psize;
    probe = Scriptroute::ICMP.new(psize);
    if (type == "echo")
	probe.icmp_type = Scriptroute::ICMP::ICMP_ECHO;
    elsif (type == "tstamp")
	probe.icmp_type = Scriptroute::ICMP::ICMP_TSTAMP;
    else 
	raise "ERROR: unsupported packettype #{i} #{j} #{type}\n";
    end
    probe.icmp_code = 0;
    probe.icmp_seq = i*numpackets + j;
  end	
  probe.ip_ttl = ttl;
  probe.ip_dst = dst;
  
  return probe;
end

#simple_to_sObject



570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/scriptroute/tulip/helper.rb', line 570

def simple_to_s
  str = "";
  (0..@numtrains-1).each { |i|
    (0..@numpackets-1).each { |j| 
	pr = @packets[i][j];
	src = (pr.response)? pr.response.packet.ip_src : -1;
	stime = pr.probe.time.to_f * 1000;
	ipid = (pr.response)? pr.response.packet.ip_id : -1;
	str += "%.3f %d " %[stime, ipid];
    }
    str += "\n";
  }
  return str;
end

#to_sObject



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/scriptroute/tulip/helper.rb', line 548

def to_s
  str = "train: #{@dsts} #{@numpackets} #{@intratrain} (#{@types}) (#{@ttls})\n";
  (0..@numtrains-1).each { |i|
    str += "train #{i}\n";
    (0..@numpackets-1).each { |j| 
	pr = @packets[i][j];
	src = (pr.response)? pr.response.packet.ip_src : -1;
	srcid = pr.probe.packet.ip_id;
	stime = pr.probe.time.to_f * 1000;
	rtt = (pr.probe and pr.response) ? (pr.response.time - pr.probe.time) * 1000 : -1;
	ipid = (pr.response)? pr.response.packet.ip_id : -1;
	str += "#{src} %d %.3f %d " %[rtt, stime, ipid];
	if (@types[j] == "tstamp") then 
 rem_time =  (pr.response)? pr.response.packet.icmp_ttime : 0;
 str += "#{rem_time} ";
	end
    }
    str += "\n";
  }
  return str;
end