rb-wartslib 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #############################################################################
4
+ ## Ruby version of sc_analsysis_dump.
5
+ ##
6
+ ## $Id: scdump,v 1.11 2007/11/29 20:39:13 youngh Exp $
7
+ #############################################################################
8
+
9
+ require 'rubygems'
10
+ require 'wartslib'
11
+
12
+ $halt_reason = []
13
+ $halt_reason[Warts::Trace::STOP_COMPLETED] = "S"
14
+ $halt_reason[Warts::Trace::STOP_NONE] = "S"
15
+ $halt_reason[Warts::Trace::STOP_UNREACH] = "U"
16
+ $halt_reason[Warts::Trace::STOP_LOOP] = "L"
17
+ $halt_reason[Warts::Trace::STOP_DEAD] = "G"
18
+
19
+ # NOTE: This method is included for demonstration purposes only. You can
20
+ # generate the same output line directly from rb-wartslib by invoking
21
+ # Trace#dump. Hence, you could have implemented this method as follows:
22
+ #
23
+ # def show_trace(trace)
24
+ # puts trace.dump
25
+ # end
26
+
27
+ def show_trace(trace)
28
+ fields = ["T"]
29
+
30
+ fields.push trace.src, trace.dst, trace.list_id, trace.cycle_id, trace.start
31
+
32
+ dest_response = trace.find_dest_response
33
+ if dest_response
34
+ fields.push "R", trace.hop_rtt_str(*dest_response),
35
+ trace.hop_probe_ttl(*dest_response),
36
+ trace.hop_reply_ttl(*dest_response)
37
+ else
38
+ fields.push "N", 0, 0, 0
39
+ end
40
+
41
+ fields.push($halt_reason[trace.stop_reason] || "?")
42
+ fields.push trace.stop_data
43
+
44
+ # The following algorithm for determining path_complete is a transcription
45
+ # of the algorithm used by sc_analysis_dump for compatibility reasons.
46
+ =begin
47
+ path_complete = "I"
48
+ if dest_response
49
+ hop = 0
50
+ trace.each do |hop, attempt, exists|
51
+ break if !exists || hop >= dest_response[0]
52
+ end
53
+ path_complete = "C" if hop == dest_response[0]
54
+ end
55
+ fields.push path_complete
56
+ =end
57
+ fields.push(trace.complete? ? "C" : "I")
58
+
59
+ # For compatibility with sc_analysis_dump, we don't print missing hops
60
+ # at the end of the trace.
61
+ unresponsive = 0
62
+ trace.each_hop do |hop, exists|
63
+ if exists
64
+ hop_str = ""
65
+ trace.each_attempt(hop) do |attempt|
66
+ # sc_analysis_dump compatibility
67
+ next if dest_response &&
68
+ hop == dest_response[0] && attempt == dest_response[1]
69
+
70
+ hop_str << ";" unless hop_str.empty?
71
+ hop_str << trace.hop_addr(hop, attempt)
72
+ hop_str << "," << trace.hop_rtt_str(hop, attempt)
73
+ hop_str << "," << trace.hop_probe_id(hop, attempt).to_s
74
+ end
75
+
76
+ unless hop_str.empty?
77
+ unresponsive.times { fields.push "q" }
78
+ unresponsive = 0
79
+
80
+ fields.push hop_str
81
+ end
82
+ else
83
+ unresponsive += 1
84
+ end
85
+ end
86
+
87
+ puts fields.join("\t")
88
+ end
89
+
90
+
91
+ #############################################################################
92
+ # Main
93
+ #############################################################################
94
+
95
+ ARGV.each do |path|
96
+ file = Warts::File::open path
97
+ file.add_filters Warts::File::TRACE
98
+ while (trace = file.read)
99
+ show_trace trace
100
+ end
101
+ end
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #############################################################################
4
+ ## A primitive version of warts-dump that demonstrates use of some of the
5
+ ## accessor methods on traces and trace hops.
6
+ ##
7
+ ## Maybe I'll expand this to be a replacement for warts-dump, like scdump
8
+ ## replaces sc_analysis_dump, as a more complete demonstration of using
9
+ ## rb-wartslib.
10
+ ##
11
+ ## $Id: wdump,v 1.9 2007/11/29 20:35:10 youngh Exp $
12
+ #############################################################################
13
+
14
+ require 'rubygems'
15
+ require 'wartslib'
16
+
17
+ def show_attr(object, name, *args)
18
+ print name.to_s, ": "
19
+ p object.send(name, *args)
20
+ end
21
+
22
+ def show_trace(trace)
23
+ [
24
+ :src,
25
+ :dst,
26
+ :hop_count,
27
+ :stop_reason,
28
+ :type,
29
+ :flags,
30
+ :attempts,
31
+ :hoplimit ,
32
+ :gaplimit,
33
+ :firsthop,
34
+ :tos,
35
+ :wait,
36
+ :loops,
37
+ :probe_size,
38
+ :sport,
39
+ :dport
40
+ ].each do |name|
41
+ show_attr trace, name
42
+ end
43
+
44
+ trace.each do |hop, attempt, exists|
45
+ unless exists
46
+ puts "hop #{hop}, attempt 0: *"
47
+ next
48
+ end
49
+
50
+ puts "hop #{hop}, attempt #{attempt}"
51
+ [
52
+ :hop_addr,
53
+ :hop_flags,
54
+ :hop_probe_id,
55
+ :hop_probe_ttl,
56
+ :hop_reply_ttl,
57
+ :hop_probe_size,
58
+ :hop_reply_size,
59
+ :hop_icmp_type,
60
+ :hop_icmp_code,
61
+ :hop_tcp_flags,
62
+ :hop_rtt_sec,
63
+ :hop_rtt_usec
64
+ ].each do |name|
65
+ print " "
66
+ show_attr trace, name, hop, attempt
67
+ end
68
+ end
69
+ end
70
+
71
+ ARGV.each do |path|
72
+ puts "\n:::: #{path} ::::"
73
+ Warts::File::open(path) do |file|
74
+ file.add_filters Warts::File::TRACE
75
+ file.read do |trace|
76
+ puts "=" * 70
77
+ show_trace trace
78
+ end
79
+ end
80
+ end
81
+
@@ -0,0 +1,32 @@
1
+ ##===========================================================================
2
+ ## Execute:
3
+ ##
4
+ ## SCAMPER=../scamper-cvs-20070523h
5
+ ## ruby extconf.rb --with-scamper-include=$SCAMPER --with-scamper-lib=$SCAMPER
6
+ ##
7
+ ## --------------------------------------------------------------------------
8
+ ## Copyright (C) 2007 The Regents of the University of California.
9
+ ##
10
+ ## This program is free software; you can redistribute it and/or modify
11
+ ## it under the terms of the GNU General Public License as published by
12
+ ## the Free Software Foundation; either version 2 of the License, or
13
+ ## (at your option) any later version.
14
+ ##
15
+ ## This program is distributed in the hope that it will be useful,
16
+ ## but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ ## GNU General Public License for more details.
19
+ ##
20
+ ## You should have received a copy of the GNU General Public License
21
+ ## along with this program; if not, write to the Free Software
22
+ ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
+ ##
24
+ ## $Id: extconf.rb,v 1.6 2007/11/29 01:41:47 youngh Exp $
25
+ ##===========================================================================
26
+
27
+ require 'mkmf'
28
+
29
+ have_header("stdint.h")
30
+ dir_config("scamper")
31
+ have_library("scamperfile", "scamper_trace_alloc")
32
+ create_makefile("wartslibext")
@@ -0,0 +1,223 @@
1
+ /*
2
+ ** Ruby bindings to scamper_addr_t.
3
+ **
4
+ ** --------------------------------------------------------------------------
5
+ ** Copyright (C) 2007 The Regents of the University of California.
6
+ **
7
+ ** This program is free software; you can redistribute it and/or modify
8
+ ** it under the terms of the GNU General Public License as published by
9
+ ** the Free Software Foundation; either version 2 of the License, or
10
+ ** (at your option) any later version.
11
+ **
12
+ ** This program is distributed in the hope that it will be useful,
13
+ ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ ** GNU General Public License for more details.
16
+ **
17
+ ** You should have received a copy of the GNU General Public License
18
+ ** along with this program; if not, write to the Free Software
19
+ ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ **
21
+ ** $Id: scaddr.c,v 1.9 2007/11/29 01:42:07 youngh Exp $
22
+ **/
23
+
24
+ #include "ruby.h"
25
+
26
+ #if defined(__HAVE_STDINT_H__)
27
+ #include <stdint.h>
28
+ #endif
29
+
30
+ #include "scamper_file.h"
31
+ #include "scaddr.h"
32
+
33
+ extern VALUE mWarts;
34
+
35
+ static VALUE cAddr;
36
+
37
+ static ID iv_element_type;
38
+
39
+ /*-------------------------------------------------------------------------*/
40
+
41
+ #define DEF_CONST(name) \
42
+ rb_define_const(cAddr, #name, INT2FIX(SCAMPER_ADDR_TYPE_##name));
43
+
44
+
45
+ /*-------------------------------------------------------------------------*/
46
+
47
+ #define DEF_ATTR(name) \
48
+ rb_define_method(cAddr, #name, scaddr_attr_##name, 0);
49
+
50
+ #define NUM_ATTR_FIELD_FUN(name,field) \
51
+ static VALUE scaddr_attr_##name(VALUE self) \
52
+ { \
53
+ scamper_addr_t *addr; \
54
+ Data_Get_Struct(self, scamper_addr_t, addr); \
55
+ return INT2NUM(addr->field); \
56
+ }
57
+
58
+ #define NUM_ATTR_FUN(name) NUM_ATTR_FIELD_FUN(name,name)
59
+
60
+ NUM_ATTR_FUN(type);
61
+ NUM_ATTR_FUN(refcnt); /* for debugging purposes only */
62
+
63
+
64
+ /*-------------------------------------------------------------------------*/
65
+
66
+ #define DEF_QUERY(name) \
67
+ rb_define_method(cAddr, #name "?", scaddr_query_##name, 0);
68
+
69
+ #define QUERY_FUN(name,macro) \
70
+ static VALUE scaddr_query_##name(VALUE self) \
71
+ { \
72
+ scamper_addr_t *addr; \
73
+ Data_Get_Struct(self, scamper_addr_t, addr); \
74
+ return (addr->type == SCAMPER_ADDR_TYPE_##macro ? Qtrue: Qfalse); \
75
+ }
76
+
77
+ QUERY_FUN(ipv4, IPV4);
78
+ QUERY_FUN(ipv6, IPV6);
79
+ QUERY_FUN(ethernet, ETHERNET);
80
+ QUERY_FUN(firewire, FIREWIRE);
81
+
82
+
83
+ /***************************************************************************/
84
+
85
+ static void scaddr_free(void *data)
86
+ {
87
+ if (data) {
88
+ scamper_addr_free((scamper_addr_t *)data);
89
+ }
90
+ }
91
+
92
+ static VALUE scaddr_alloc(VALUE klass)
93
+ {
94
+ return Data_Wrap_Struct(klass, 0, scaddr_free, 0);
95
+ }
96
+
97
+
98
+ static VALUE scaddr_init(VALUE self)
99
+ {
100
+ rb_ivar_set(self, iv_element_type, INT2FIX(SCAMPER_FILE_OBJ_ADDR));
101
+ return self;
102
+ }
103
+
104
+
105
+ #if 0
106
+ static VALUE scaddr_init_copy(VALUE copy, VALUE orig)
107
+ {
108
+ scamper_addr_t *orig_addr;
109
+
110
+ if (copy == orig) {
111
+ return copy;
112
+ }
113
+
114
+ /* See Pragmatic Programmer's book. */
115
+ if (TYPE(orig) != T_DATA || RDATA(orig)->dfree != scaddr_free) {
116
+ rb_raise(rb_eTypeError, "wrong argument type");
117
+ }
118
+
119
+ Data_Get_Struct(orig, scamper_addr_t, orig_addr);
120
+ DATA_PTR(copy) = scamper_addr_use(orig_addr);
121
+
122
+ return copy;
123
+ }
124
+ #endif
125
+
126
+
127
+ static VALUE scaddr_cmp(VALUE self, VALUE other)
128
+ {
129
+ scamper_addr_t *self_addr;
130
+ scamper_addr_t *other_addr;
131
+
132
+ if (other == self) {
133
+ return INT2FIX(0);
134
+ }
135
+
136
+ /* See Pragmatic Programmer's book. */
137
+ if (TYPE(other) != T_DATA || RDATA(other)->dfree != scaddr_free) {
138
+ rb_raise(rb_eTypeError, "wrong argument type");
139
+ }
140
+
141
+ Data_Get_Struct(self, scamper_addr_t, self_addr);
142
+ Data_Get_Struct(other, scamper_addr_t, other_addr);
143
+ return INT2FIX(scamper_addr_cmp(self_addr, other_addr));
144
+ }
145
+
146
+
147
+ static VALUE scaddr_addr(VALUE self)
148
+ {
149
+ scamper_addr_t *addr;
150
+ char buf[128];
151
+
152
+ Data_Get_Struct(self, scamper_addr_t, addr);
153
+ return rb_str_new2(scamper_addr_tostr(addr, buf, 128));
154
+ }
155
+
156
+
157
+ static VALUE scaddr_write_to(VALUE self, VALUE file)
158
+ {
159
+ return self; /* nothing to do: users shouldn't write addrs themselves */
160
+ }
161
+
162
+
163
+ /*-------------------------------------------------------------------------*/
164
+
165
+ VALUE scaddr_create(scamper_addr_t *addr)
166
+ {
167
+ VALUE obj = Data_Wrap_Struct(cAddr, 0, scaddr_free, addr);
168
+
169
+ if (NIL_P(scaddr_init(obj))) {
170
+ return Qnil;
171
+ }
172
+
173
+ return obj;
174
+ }
175
+
176
+
177
+ /***************************************************************************/
178
+ /***************************************************************************/
179
+
180
+ void Init_scaddr(void)
181
+ {
182
+ ID private_class_method_ID, private_ID;
183
+ ID new_ID, dup_ID, clone_ID;
184
+
185
+ iv_element_type = rb_intern("@element_type");
186
+
187
+ cAddr = rb_define_class_under(mWarts, "Addr", rb_cObject);
188
+
189
+ rb_include_module(cAddr, rb_mComparable);
190
+
191
+ DEF_CONST(IPV4);
192
+ DEF_CONST(IPV6);
193
+ DEF_CONST(ETHERNET);
194
+ DEF_CONST(FIREWIRE);
195
+
196
+ DEF_ATTR(type);
197
+ DEF_ATTR(refcnt);
198
+
199
+ DEF_QUERY(ipv4);
200
+ DEF_QUERY(ipv6);
201
+ DEF_QUERY(ethernet);
202
+ DEF_QUERY(firewire);
203
+
204
+ rb_define_alloc_func(cAddr, scaddr_alloc);
205
+
206
+ rb_define_attr(cAddr, "element_type", 1, 0);
207
+
208
+ rb_define_method(cAddr, "initialize", scaddr_init, 0);
209
+ /* rb_define_method(cAddr, "initialize_copy", scaddr_init_copy, 1); */
210
+ rb_define_method(cAddr, "<=>", scaddr_cmp, 1);
211
+ rb_define_method(cAddr, "addr", scaddr_addr, 0);
212
+ rb_define_method(cAddr, "write_to", scaddr_write_to, 1);
213
+
214
+ private_class_method_ID = rb_intern("private_class_method");
215
+ private_ID = rb_intern("private");
216
+ new_ID = rb_intern("new");
217
+ dup_ID = rb_intern("dup");
218
+ clone_ID = rb_intern("clone");
219
+
220
+ rb_funcall(cAddr, private_class_method_ID, 1, ID2SYM(new_ID));
221
+ rb_funcall(cAddr, private_ID, 1, ID2SYM(dup_ID));
222
+ rb_funcall(cAddr, private_ID, 1, ID2SYM(clone_ID));
223
+ }
@@ -0,0 +1,32 @@
1
+ /*
2
+ ** Ruby bindings to scamper_addr_t.
3
+ **
4
+ ** --------------------------------------------------------------------------
5
+ ** Copyright (C) 2007 The Regents of the University of California.
6
+ **
7
+ ** This program is free software; you can redistribute it and/or modify
8
+ ** it under the terms of the GNU General Public License as published by
9
+ ** the Free Software Foundation; either version 2 of the License, or
10
+ ** (at your option) any later version.
11
+ **
12
+ ** This program is distributed in the hope that it will be useful,
13
+ ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ ** GNU General Public License for more details.
16
+ **
17
+ ** You should have received a copy of the GNU General Public License
18
+ ** along with this program; if not, write to the Free Software
19
+ ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ **
21
+ ** $Id: scaddr.h,v 1.2 2007/11/29 01:29:59 youngh Exp $
22
+ **/
23
+
24
+ #ifndef __SCADDR_H
25
+ #define __SCADDR_H
26
+
27
+ #include "scamper_addr.h"
28
+
29
+ VALUE scaddr_create(scamper_addr_t *addr);
30
+ void Init_scaddr(void);
31
+
32
+ #endif # __SCADDR_H