Class: Warts::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/wartslib/wl-trace.rb,
ext/sctrace.c

Constant Summary collapse

STOP_REASON_TEXT =

from scamper_trace.h:SCAMPER_TRACE_STOP_*

Hash.new { |h, k| "<<UNKNOWN STOP CODE #{k}>>" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'ext/sctrace.c', line 375

static VALUE sctrace_init(VALUE self)
{
  /*
  ** Workaround for bug in scamper-cvs-20070523-p8: scamper wrote a trace
  ** with hop_count = 5 and hops == NULL, which triggers a segfault.
  *
  *  Program received signal SIGSEGV, Segmentation fault.
  *  0x283f06cc in find_trace_hop (trace=0x8635e40, hop=4, response=0)
  *     at sctrace.c:109
  *  109       trace_hop = trace->hops[hop];
  *  (gdb) p trace
  *  $1 = (scamper_trace_t *) 0x8635e40
  *  (gdb) p *trace
  * $2 = {list = 0x829f2c0, cycle = 0x829f2e0, src = 0x82a7d50, dst = 0x86c39e0,
  * start = {tv_sec = 1189778005, tv_usec = 809855}, hops = 0x0, hop_count = 5,
  * stop_reason = 5 '\005', stop_data = 0 '\0', type = 2 '\002', flags = 0 '\0',
  * responses = 3 '\003', hoplimit = 0 '\0', gaplimit = 0 '\0',
  * firsthop = 1 '\001', tos = 0 '\0', wait = 5 '\005', loops = 1 '\001',
  * probe_size = 44, sport = 46235, dport = 33545, tlvs = 0x0, pmtud = 0x0}
  */
  scamper_trace_t *trace = NULL;
  Data_Get_Struct(self, scamper_trace_t, trace);
  if (trace && !trace->hops) {
    trace->hop_count = 0;
  }

  rb_ivar_set(self, iv_element_type, INT2FIX(SCAMPER_FILE_OBJ_TRACE));
  rb_ivar_set(self, iv_dest_response, Qnil);
  rb_ivar_set(self, iv_list, Qnil);
  rb_ivar_set(self, iv_cycle, Qnil);
  return self;
}

Instance Attribute Details

#element_typeObject (readonly)

Class Method Details

.decompose_flags(flags) ⇒ Object

Just a convenience function. This returns the same strings as warts-dump, except for “gapcont” which arts-dump doesn’t return. See scamper_trace.h:SCAMPER_TRACE_FLAG_*



71
72
73
74
75
76
77
# File 'lib/wartslib/wl-trace.rb', line 71

def self.decompose_flags(flags)
  retval = []
  retval << "all_attempts" if (flags & Warts::Trace::FLAG_ALLATTEMPTS) != 0
  retval << "pmtud" if (flags & Warts::Trace::FLAG_PMTUD) != 0
  retval << "dltxts" if (flags & Warts::Trace::FLAG_DL) != 0
  retval
end

.decompose_hop_flags(flags) ⇒ Object

Just a convenience function. This returns the same strings as warts-dump, except for “tcpresp” which arts-dump doesn’t return. See scamper_trace.h:SCAMPER_TRACE_HOP_FLAG_*. I commented out printing of REPLY_TTL and TCP because they’re mostly of internal interest, and they can be got through accessor methods anyway.



85
86
87
88
89
90
91
92
93
94
# File 'lib/wartslib/wl-trace.rb', line 85

def self.decompose_hop_flags(flags)
  retval = []
  retval << "sockrxts" if (flags & Warts::Trace::HOP_FLAG_TS_SOCK_RX) != 0
  retval << "dltxts" if (flags & Warts::Trace::HOP_FLAG_TS_DL_TX) != 0
  retval << "dlrxts" if (flags & Warts::Trace::HOP_FLAG_TS_DL_RX) != 0
  retval << "tscrtt" if (flags & Warts::Trace::HOP_FLAG_TS_TSC) != 0
  #retval << "replyttl" if (flags & Warts::Trace::HOP_FLAG_REPLY_TTL) != 0
  #retval << "tcpresp" if (flags & Warts::Trace::HOP_FLAG_TCP) != 0
  retval
end

.decompose_tcp_flags(flags) ⇒ Object

Just a convenience function. This returns the same strings as warts-dump.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wartslib/wl-trace.rb', line 54

def self.decompose_tcp_flags(flags)
  retval = []
  retval << "fin" if (flags & 0x01) != 0
  retval << "syn" if (flags & 0x02) != 0
  retval << "rst" if (flags & 0x04) != 0
  retval << "psh" if (flags & 0x08) != 0
  retval << "ack" if (flags & 0x10) != 0
  retval << "urg" if (flags & 0x20) != 0
  retval << "ece" if (flags & 0x40) != 0
  retval << "cwr" if (flags & 0x80) != 0
  retval
end

Instance Method Details

#complete?Boolean

NOTE: This uses the same algorithm as sc_analysis_dump for compatibility.

Returns:

  • (Boolean)


887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'ext/sctrace.c', line 887

static VALUE sctrace_complete(VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;
  VALUE dest_response;
  int dest_hop;
  int hop, response;

  dest_response = sctrace_find_dest_response(self);
  if (NIL_P(dest_response)) {
    return Qfalse;
  }
  dest_hop = FIX2INT(rb_ary_entry(dest_response, 0));

  Data_Get_Struct(self, scamper_trace_t, trace);

  for (hop = 0; hop < trace->hop_count && hop < dest_hop; hop++) {
    if (!find_trace_hop(trace, hop, 0)) {
      return Qfalse;
    }
  }
  return hop == dest_hop ? Qtrue : Qfalse;
}

#cycleObject



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'ext/sctrace.c', line 517

static VALUE sctrace_cycle(VALUE self)
{
  VALUE retval = rb_ivar_get(self, iv_cycle);
  if (NIL_P(retval)) {
    scamper_trace_t *trace;
    Data_Get_Struct(self, scamper_trace_t, trace);
    if (!trace->cycle) { /* may be NULL if no cycle specified at scamper run */
      return Qnil;
    }

    retval = sccycle_create(scamper_cycle_use(trace->cycle),
			    SCAMPER_FILE_OBJ_CYCLE_DEF);
    rb_ivar_set(self, iv_cycle, retval);
  }
  return retval;
}

#cycle=(cycle_v) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'ext/sctrace.c', line 535

static VALUE sctrace_set_cycle(VALUE self, VALUE cycle_v)
{
  scamper_trace_t *trace;
  scamper_cycle_t *cycle;

  Data_Get_Struct(self, scamper_trace_t, trace);

  /* See Pragmatic Programmer's book. */
  if (TYPE(cycle_v) != T_DATA || RDATA(cycle_v)->dfree != sccycle_free) {
    rb_raise(rb_eTypeError, "wrong argument type");
  }

  Data_Get_Struct(cycle_v, scamper_cycle_t, cycle);

  scamper_list_free(trace->list);
  scamper_cycle_free(trace->cycle);

  trace->list = cycle->list;
  trace->cycle = cycle;

  scamper_list_use(trace->list);
  scamper_cycle_use(trace->cycle);
}

#destObject Also known as: dst



425
426
427
428
429
430
431
432
# File 'ext/sctrace.c', line 425

static VALUE sctrace_dest(VALUE self)
{
  scamper_trace_t *trace;
  char buf[128];

  Data_Get_Struct(self, scamper_trace_t, trace);
  return rb_str_new2(scamper_addr_tostr(trace->dst, buf, 128));
}

#dest_cmp(other) ⇒ Object Also known as: dst_cmp



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'ext/sctrace.c', line 473

static VALUE sctrace_dest_cmp(VALUE self, VALUE other)
{
  scamper_trace_t *self_trace;
  scamper_trace_t *other_trace;

  if (other == self) {
    return INT2FIX(0);
  }

  /* See Pragmatic Programmer's book. */
  if (TYPE(other) != T_DATA || RDATA(other)->dfree != sctrace_free) {
    rb_raise(rb_eTypeError, "wrong argument type");
  }

  Data_Get_Struct(self, scamper_trace_t, self_trace);
  Data_Get_Struct(other, scamper_trace_t, other_trace);
  return INT2FIX(scamper_addr_cmp(self_trace->dst, other_trace->dst));
}

#dest_rttObject



837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'ext/sctrace.c', line 837

static VALUE sctrace_dest_rtt(VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;
  VALUE dest_response;
  int hop, response;

  dest_response = sctrace_find_dest_response(self);
  if (NIL_P(dest_response)) {
    return Qnil;
  }

  hop = FIX2INT(rb_ary_entry(dest_response, 0));
  response = FIX2INT(rb_ary_entry(dest_response, 1));

  Data_Get_Struct(self, scamper_trace_t, trace);
  trace_hop = find_trace_hop(trace, hop, response);
  return rb_float_new(1000.0 * trace_hop->hop_rtt.tv_sec
		      + trace_hop->hop_rtt.tv_usec / 1000.0);
}

#dest_rtt_strObject

This is faster than calling sprintf(“%0.3f”).



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'ext/sctrace.c', line 860

static VALUE sctrace_dest_rtt_str(VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;
  VALUE dest_response;
  char buf[128];
  int hop, response;

  dest_response = sctrace_find_dest_response(self);
  if (NIL_P(dest_response)) {
    return Qnil;
  }

  hop = FIX2INT(rb_ary_entry(dest_response, 0));
  response = FIX2INT(rb_ary_entry(dest_response, 1));

  Data_Get_Struct(self, scamper_trace_t, trace);
  trace_hop = find_trace_hop(trace, hop, response);

  sprintf(buf, "%d.%03d",
	  1000 * trace_hop->hop_rtt.tv_sec + trace_hop->hop_rtt.tv_usec / 1000,
	  trace_hop->hop_rtt.tv_usec % 1000);
  return rb_str_new2(buf);
}

#dumpObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/wartslib/wl-trace.rb', line 127

def dump
  fields = ["T"]

  fields.push src, dst, list_id, cycle_id, start

  dest_response = find_dest_response
  if dest_response
    fields.push "R", hop_rtt_str(*dest_response),
	hop_probe_ttl(*dest_response),
	hop_reply_ttl(*dest_response)
  else
    fields.push "N", 0, 0, 0
  end

  fields.push(HALT_REASON[stop_reason] || "?")
  fields.push stop_data

  fields.push(complete? ? "C" : "I")

  # For compatibility with sc_analysis_dump, we don't print missing hops
  # at the end of the trace.
  unresponsive = 0
  each_hop do |hop, exists|
    if exists
	hop_str = ""
	each_attempt(hop) do |attempt|
 # sc_analysis_dump compatibility
 next if dest_response &&
   hop == dest_response[0] && attempt == dest_response[1]

 hop_str << ";" unless hop_str.empty?
 hop_str << hop_addr(hop, attempt)
 hop_str << "," << hop_rtt_str(hop, attempt)
 hop_str << "," << hop_probe_id(hop, attempt).to_s
	end

	unless hop_str.empty?
 unresponsive.times { fields.push "q" }
 unresponsive = 0

 fields.push hop_str
	end
    else
	unresponsive += 1
    end
  end

  fields.join("\t")
end

#each_hopObject



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'ext/sctrace.c', line 655

static VALUE sctrace_each_hop(VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;
  VALUE yield_args;
  int hop;

  Data_Get_Struct(self, scamper_trace_t, trace);

  if (trace->hop_count == 0) {
    return self;
  }

  yield_args = rb_ary_new();
  for (hop = 0; hop < trace->hop_count; hop++) {
    trace_hop = find_trace_hop(trace, hop, 0);

    rb_ary_store(yield_args, 0, INT2FIX(hop));
    rb_ary_store(yield_args, 1, (trace_hop ? Qtrue : Qfalse));
    rb_yield(yield_args);
  }

  return self;
}

#each_hop_and_responseObject Also known as: each, each_hop_and_attempt



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'ext/sctrace.c', line 701

static VALUE sctrace_each_hop_and_response(VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;
  VALUE yield_args;
  int hop;

  Data_Get_Struct(self, scamper_trace_t, trace);

  yield_args = rb_ary_new();
  for (hop = 0; hop < trace->hop_count; hop++) {
    int response = 0;
    trace_hop = find_trace_hop(trace, hop, 0);

    rb_ary_store(yield_args, 0, INT2FIX(hop));
    rb_ary_store(yield_args, 1, INT2FIX(0));
    rb_ary_store(yield_args, 2, (trace_hop ? Qtrue : Qfalse));
    rb_yield(yield_args);

    if (trace_hop) {
      rb_ary_store(yield_args, 2, Qtrue);
      while (trace_hop = trace_hop->hop_next) {
	rb_ary_store(yield_args, 1, INT2FIX(++response));
	rb_yield(yield_args);
      }
    }
  }

  return self;
}

#each_response(hop) ⇒ Object Also known as: each_attempt



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'ext/sctrace.c', line 681

static VALUE sctrace_each_response(VALUE self, VALUE hop)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;

  Data_Get_Struct(self, scamper_trace_t, trace);

  trace_hop = find_trace_hop(trace, NUM2INT(hop), 0);
  if (trace_hop) {
    int response = 0;
    rb_yield(INT2FIX(0));
    while (trace_hop = trace_hop->hop_next) {
      rb_yield(INT2FIX(++response));
    }
  }

  return self;
}

#find_dest_responseObject Also known as: dest_responded?

  • NOTE: This uses the same algorithm as sc_analysis_dump for compatibility.

  • Specifically, it examines the hops in reverse order by hop number

  • and thus picks out the last response from the destination.

  • (Yes, there are traces with multiple responses from the destination

  • at different hop distances.)



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'ext/sctrace.c', line 795

static VALUE sctrace_find_dest_response(VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;
  int hop, response;
  VALUE retval;

  retval = rb_ivar_get(self, iv_dest_response);
  if (!NIL_P(retval)) {
    return (RARRAY(retval)->len == 0 ? Qnil : retval);
  }

  retval = rb_ary_new();
  retval = rb_ivar_set(self, iv_dest_response, retval);

  Data_Get_Struct(self, scamper_trace_t, trace);

  /* sc_analysis_dump checks stop_reason like this, though I don't know why. */
  if (trace->hop_count == 0
      || trace->stop_reason == SCAMPER_TRACE_STOP_ERROR) {
    return Qnil;
  }

  for (hop = trace->hop_count - 1; hop >= 0; hop--) {
    response = 0;
    trace_hop = find_trace_hop(trace, hop, 0);
    while (trace_hop) {
      if (hop_is_dest_response(trace, trace_hop)) {
	rb_ary_push(retval, INT2FIX(hop));
	rb_ary_push(retval, INT2FIX(response));
	return retval;
      }

      ++response;
      trace_hop = trace_hop->hop_next;
    }
  }

  return Qnil;
}

#flags_breakdownObject



97
98
99
# File 'lib/wartslib/wl-trace.rb', line 97

def flags_breakdown
  Warts::Trace.decompose_flags flags
end

#hop_dest_response?(*args) ⇒ Boolean

Returns:

  • (Boolean)


773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'ext/sctrace.c', line 773

static VALUE sctrace_hop_dest_response(int argc, VALUE *argv, VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;

  PROCESS_HOP_PARAMS;
  Data_Get_Struct(self, scamper_trace_t, trace);
  trace_hop = find_trace_hop(trace, NUM2INT(hop), NUM2INT(response));
  if (!trace_hop) {
    return Qnil;
  }
  return hop_is_dest_response(trace, trace_hop) ? Qtrue : Qfalse;
}

#hop_exists?(*args) ⇒ Boolean

Returns:

  • (Boolean)


643
644
645
646
647
648
649
650
651
652
# File 'ext/sctrace.c', line 643

static VALUE sctrace_hop_exists(int argc, VALUE *argv, VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *trace_hop;

  PROCESS_HOP_PARAMS;
  Data_Get_Struct(self, scamper_trace_t, trace);
  trace_hop = find_trace_hop(trace, NUM2INT(hop), NUM2INT(response));
  return (trace_hop ? Qtrue : Qfalse);
}

#hop_flags_breakdown(hop, response = 0) ⇒ Object



102
103
104
# File 'lib/wartslib/wl-trace.rb', line 102

def hop_flags_breakdown(hop, response=0)
  Warts::Trace.decompose_hop_flags hop_flags(hop, response)
end

#hop_has_icmp_reply?Boolean

Returns:

  • (Boolean)

#hop_has_reply_ttl?Boolean

Returns:

  • (Boolean)

#hop_has_tcp_reply?Boolean

Returns:

  • (Boolean)

#hop_tcp_flags_breakdown(*args) ⇒ Object




562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'ext/sctrace.c', line 562

static VALUE sctrace_hop_tcp_flags_breakdown(int argc, VALUE *argv, VALUE self)
{
  scamper_trace_t *trace;
  scamper_trace_hop_t *th;
  VALUE retval;

  PROCESS_HOP_PARAMS;
  Data_Get_Struct(self, scamper_trace_t, trace);
  th = find_trace_hop(trace, NUM2INT(hop), NUM2INT(response));
  if (!th || (th->hop_flags & SCAMPER_TRACE_HOP_FLAG_TCP) == 0) {
    return Qnil;
  }

  retval = rb_ary_new();
  if (th->hop_tcp_flags & 0x01) { rb_ary_push(retval, rb_str_new2("fin")); }
  if (th->hop_tcp_flags & 0x02) { rb_ary_push(retval, rb_str_new2("syn")); }
  if (th->hop_tcp_flags & 0x04) { rb_ary_push(retval, rb_str_new2("rst")); }
  if (th->hop_tcp_flags & 0x08) { rb_ary_push(retval, rb_str_new2("psh")); }
  if (th->hop_tcp_flags & 0x10) { rb_ary_push(retval, rb_str_new2("ack")); }
  if (th->hop_tcp_flags & 0x20) { rb_ary_push(retval, rb_str_new2("urg")); }
  if (th->hop_tcp_flags & 0x40) { rb_ary_push(retval, rb_str_new2("ece")); }
  if (th->hop_tcp_flags & 0x80) { rb_ary_push(retval, rb_str_new2("cwr")); }
  /* there are only 8 flag bits, so no more possible */
  return retval;
}

#listObject



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/sctrace.c', line 500

static VALUE sctrace_list(VALUE self)
{
  VALUE retval = rb_ivar_get(self, iv_list);
  if (NIL_P(retval)) {
    scamper_trace_t *trace;
    Data_Get_Struct(self, scamper_trace_t, trace);
    if (!trace->list) {  /* not sure this can ever be NULL; better safe than */
      return Qnil;
    }

    retval = sclist_create(scamper_list_use(trace->list));
    rb_ivar_set(self, iv_list, retval);
  }
  return retval;
}

#path_lengthObject

The length of the path (in hops), taking any responding destination into account.

The hop_count attribute doesn’t provide this value because hop_count gives the number of hops probed (that is, attempted) and not the number of hops with responses (or, alternatively, the highest TTL of responding hops).



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wartslib/wl-trace.rb', line 114

def path_length
  dest_response = find_dest_response
  if dest_response
    dest_response[0] + 1
  else
    return 0 if hop_count == 0
    (hop_count-1).downto(0) do |hop|
      return hop + 1 if hop_exists? hop
    end
    return 0
  end
end

#srcObject

  • NOTE: scamper can sometimes write out traces with a NULL source address.

  • This seems to happen when a serious error occurs (stop reason == 6,

  • ERROR). We tolerate this by return nil for the src address.



414
415
416
417
418
419
420
421
422
# File 'ext/sctrace.c', line 414

static VALUE sctrace_src(VALUE self)
{
  scamper_trace_t *trace;
  char buf[128];

  Data_Get_Struct(self, scamper_trace_t, trace);
  return (trace->src ? rb_str_new2(scamper_addr_tostr(trace->src, buf, 128))
	  : Qnil);
}

#src_cmp(other) ⇒ Object

  • NOTE: scamper can sometimes write out traces with a NULL source address.

  • See sctrace_src(). For comparison purposes, a NULL source address

  • is considered less than any other address except for another NULL

  • source address. Two NULL addresses are equal.



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
# File 'ext/sctrace.c', line 441

static VALUE sctrace_src_cmp(VALUE self, VALUE other)
{
  scamper_trace_t *self_trace;
  scamper_trace_t *other_trace;

  if (other == self) {
    return INT2FIX(0);
  }

  /* See Pragmatic Programmer's book. */
  if (TYPE(other) != T_DATA || RDATA(other)->dfree != sctrace_free) {
    rb_raise(rb_eTypeError, "wrong argument type");
  }

  Data_Get_Struct(self, scamper_trace_t, self_trace);
  Data_Get_Struct(other, scamper_trace_t, other_trace);
 
  if (self_trace->src && other_trace->src) {
    return INT2FIX(scamper_addr_cmp(self_trace->src, other_trace->src));
  }
  else if (self_trace->src) {
    return INT2FIX(1);
  }
  else if (other_trace->src) {
    return INT2FIX(-1);
  }
  else {
    return INT2FIX(0);
  }
}

#write_to(file) ⇒ Object



493
494
495
496
497
# File 'ext/sctrace.c', line 493

static VALUE sctrace_write_to(VALUE self, VALUE file)
{
  rb_funcall(file, meth_write_trace, 1, self);
  return self;
}